Add differents ComputerPlayer class + refactor calls

This commit is contained in:
Katchan 2021-11-05 15:35:25 +01:00
parent a2ebb5195c
commit 3ccc26592c
9 changed files with 66 additions and 27 deletions

View File

@ -92,7 +92,7 @@ public class App extends Application {
);
classPlayer = null;
}
playerClass = ComputerPlayer.class;
playerClass = RandomComputerPlayer.class;
}
case "default" -> classPlayer = ClassPlayer.DEFAULT;
case "tank" -> classPlayer = ClassPlayer.TANK;

View File

@ -10,6 +10,11 @@ public class Nothing extends AbstractAction {
public Nothing(Game game){
super(game);
}
public Nothing(){
super(null);
}
@Override
public void doAction(){
}

View File

@ -1,10 +0,0 @@
package fr.lnl.game.server.games.player;
import fr.lnl.game.server.utils.Point;
public class ComputerPlayer extends AbstractPlayer{
public ComputerPlayer(Integer id, Point point, ClassPlayer classPlayer) {
super(id,point,false, classPlayer);
}
}

View File

@ -1,6 +1,6 @@
package fr.lnl.game.server.games.player;
import fr.lnl.game.server.games.action.Action;
import fr.lnl.game.server.utils.Point;
public class HumanPlayer extends AbstractPlayer {
@ -8,4 +8,9 @@ public class HumanPlayer extends AbstractPlayer {
public HumanPlayer(Integer id, Point point, ClassPlayer classPlayer) {
super(id, point,false, classPlayer);
}
@Override
public Action choseAction() {
return null;
}
}

View File

@ -1,5 +1,6 @@
package fr.lnl.game.server.games.player;
import fr.lnl.game.server.games.Game;
import fr.lnl.game.server.games.action.Action;
import fr.lnl.game.server.games.weapon.Weapon;
import fr.lnl.game.server.utils.Point;
@ -16,8 +17,10 @@ public interface Player {
void setWeapon(Weapon weapon);
Action[] getActions();
void setActions(Action[] actions);
Action choseAction();
ClassPlayer getClassPlayer();
void setPoint(Point point);
void decrementEnergy(int energy);
void incrementEnergy(int energy);
}

View File

@ -0,0 +1,31 @@
package fr.lnl.game.server.games.player;
import fr.lnl.game.server.games.action.Action;
import fr.lnl.game.server.games.action.Nothing;
import fr.lnl.game.server.utils.Point;
import java.util.Random;
public class RandomComputerPlayer extends AbstractPlayer{
public RandomComputerPlayer(Integer id, Point point, ClassPlayer classPlayer) {
super(id,point,false, classPlayer);
}
@Override
public Action choseAction() {
Action action = null;
switch (getActions().length){
case 0 -> action = new Nothing();
case 1 -> action = getActions()[0];
default -> {
Random random = new Random();
while (action == null || !action.isPossible()) {
action = getActions()
[random.nextInt(0,getActions().length)];
}
}
}
return action;
}
}

View File

@ -0,0 +1,16 @@
package fr.lnl.game.server.games.player;
import fr.lnl.game.server.games.action.Action;
import fr.lnl.game.server.utils.Point;
public class StrategyComputerPlayer extends AbstractPlayer{
public StrategyComputerPlayer(Integer id, Point point, ClassPlayer classPlayer) {
super(id,point,false, classPlayer);
}
@Override
public Action choseAction() {
return null;
}
}

View File

@ -53,18 +53,7 @@ public class GridTest {
System.out.println(" Tour du joueur " + game.getCurrentPlayer().getId() + " : " +
game.getCurrentPlayer().getEnergy() + " points de vies restants");
System.out.println(game.getGrid().toString());
Action action = null;
switch (game.getCurrentPlayer().getActions().length){
case 0 -> action = new Nothing(game);
case 1 -> action = game.getCurrentPlayer().getActions()[0];
default -> {
Random random = new Random();
while (action == null || !action.isPossible()) {
action = game.getCurrentPlayer().getActions()
[random.nextInt(0,game.getCurrentPlayer().getActions().length)];
}
}
}
Action action = game.getCurrentPlayer().choseAction();
action.doAction();
System.out.println("Action " + action + " : " + game.getCurrentPlayer().getEnergy() +
" points de vies restants");

View File

@ -5,8 +5,8 @@ import fr.lnl.game.server.games.grid.EnergyBall;
import fr.lnl.game.server.games.grid.Grid;
import fr.lnl.game.server.games.grid.Wall;
import fr.lnl.game.server.games.player.ClassPlayer;
import fr.lnl.game.server.games.player.ComputerPlayer;
import fr.lnl.game.server.games.player.Player;
import fr.lnl.game.server.games.player.RandomComputerPlayer;
import fr.lnl.game.server.utils.Cardinal;
import fr.lnl.game.server.utils.Point;
@ -19,8 +19,8 @@ public class Mock {
Game game;
public Mock() {
List<Player> players = Arrays.asList(new ComputerPlayer(1,null, ClassPlayer.DEFAULT),
new ComputerPlayer(2,null, ClassPlayer.DEFAULT));
List<Player> players = Arrays.asList(new RandomComputerPlayer(1,null, ClassPlayer.DEFAULT),
new RandomComputerPlayer(2,null, ClassPlayer.DEFAULT));
this.grid = new Grid(16,16, players);
placePlayersBRUT();
placeEnergyBallBRUT();