diff --git a/client/src/main/java/fr/lnl/game/client/App.java b/client/src/main/java/fr/lnl/game/client/App.java index f70b898..8ac55ad 100644 --- a/client/src/main/java/fr/lnl/game/client/App.java +++ b/client/src/main/java/fr/lnl/game/client/App.java @@ -106,6 +106,15 @@ public class App extends Application { } playerClass = RandomComputerPlayer.class; } + case "computerS" -> { + if(playerClass != null) { + playerList.add(createNewPlayer(playerClass, + classPlayer != null ? classPlayer : ClassPlayer.DEFAULT, playerList.size()) + ); + classPlayer = null; + } + playerClass = StrategyComputerPlayer.class; + } case "default" -> classPlayer = ClassPlayer.DEFAULT; case "tank" -> classPlayer = ClassPlayer.TANK; case "dps" -> classPlayer = ClassPlayer.DPS; @@ -113,6 +122,7 @@ public class App extends Application { default -> throw new IllegalArgumentException("Unknown argument: " + str); } } + System.out.println("oui"); if(playerClass != null) playerList.add(createNewPlayer(playerClass, classPlayer != null ? classPlayer : ClassPlayer.DEFAULT, playerList.size()) diff --git a/client/src/main/java/fr/lnl/game/client/view/AbstractView.java b/client/src/main/java/fr/lnl/game/client/view/AbstractView.java index 482ea7c..11b328a 100644 --- a/client/src/main/java/fr/lnl/game/client/view/AbstractView.java +++ b/client/src/main/java/fr/lnl/game/client/view/AbstractView.java @@ -1,12 +1,9 @@ package fr.lnl.game.client.view; import fr.lnl.game.server.games.Game; -import fr.lnl.game.server.games.action.Action; import fr.lnl.game.server.games.action.ReunionSameAction; import fr.lnl.game.server.games.player.Player; import fr.lnl.game.server.utils.Maths; - -import java.util.ArrayList; import java.util.List; import java.util.Scanner; @@ -20,23 +17,6 @@ public abstract class AbstractView implements View { this.player = player; } - protected List generateAvailableActions() { - List actions = new ArrayList<>(); - for (Action a : player.getActions()) { - ReunionSameAction reunionFilter = actions.stream() - .filter(r -> r.getActionName().equals(a.getClass().getSimpleName())) - .findFirst() - .orElse(null); - if(reunionFilter != null){ - reunionFilter.addAction(a); - } - else{ - actions.add(new ReunionSameAction(a.getClass().getSimpleName(),a)); - } - } - return actions; - } - protected ReunionSameAction choseReunionSameAction(List actions) { ReunionSameAction reunion = null; String error = "Veuillez renseigner une valeur numérique comprise entre 1 et " + actions.size(); diff --git a/server/src/main/java/fr/lnl/game/server/games/Game.java b/server/src/main/java/fr/lnl/game/server/games/Game.java index a96cedf..cfaba42 100644 --- a/server/src/main/java/fr/lnl/game/server/games/Game.java +++ b/server/src/main/java/fr/lnl/game/server/games/Game.java @@ -47,7 +47,7 @@ public class Game { public boolean play() { if(currentPlayer instanceof ComputerPlayer computer) // si le joueur est humain alors le choix se fait avant l'appel de play() - selectedAction = computer.choseAction(); + selectedAction = computer.choseAction(this); selectedAction.doAction(); countdownGridElementsUpdate(); gridPlayersUpdate(); diff --git a/server/src/main/java/fr/lnl/game/server/games/player/AbstractPlayer.java b/server/src/main/java/fr/lnl/game/server/games/player/AbstractPlayer.java index 41dcfdd..48a1bda 100644 --- a/server/src/main/java/fr/lnl/game/server/games/player/AbstractPlayer.java +++ b/server/src/main/java/fr/lnl/game/server/games/player/AbstractPlayer.java @@ -1,9 +1,11 @@ package fr.lnl.game.server.games.player; import fr.lnl.game.server.games.action.Action; +import fr.lnl.game.server.games.action.ReunionSameAction; import fr.lnl.game.server.games.weapon.Weapon; import fr.lnl.game.server.utils.Point; +import java.util.ArrayList; import java.util.List; public abstract class AbstractPlayer implements Player { @@ -25,6 +27,24 @@ public abstract class AbstractPlayer implements Player { this.position = position; } + @Override + public List generateAvailableActions() { + List actions = new ArrayList<>(); + for (Action a : getActions()) { + ReunionSameAction reunionFilter = actions.stream() + .filter(r -> r.getActionName().equals(a.getClass().getSimpleName())) + .findFirst() + .orElse(null); + if(reunionFilter != null){ + reunionFilter.addAction(a); + } + else{ + actions.add(new ReunionSameAction(a.getClass().getSimpleName(),a)); + } + } + return actions; + } + @Override public boolean isAlive(){ return energy > 0; @@ -85,6 +105,7 @@ public abstract class AbstractPlayer implements Player { return position; } + @Override public void setPosition(Point position){ if(position == null){ @@ -95,7 +116,10 @@ public abstract class AbstractPlayer implements Player { @Override public void decrementEnergy(int energy){ - this.energy -= energy; + if(!isShieldDeploy()){ + this.energy -= energy; + } + shieldDeploy = false; } @Override diff --git a/server/src/main/java/fr/lnl/game/server/games/player/ComputerPlayer.java b/server/src/main/java/fr/lnl/game/server/games/player/ComputerPlayer.java index a6fd6c5..0337dcc 100644 --- a/server/src/main/java/fr/lnl/game/server/games/player/ComputerPlayer.java +++ b/server/src/main/java/fr/lnl/game/server/games/player/ComputerPlayer.java @@ -1,6 +1,8 @@ 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.action.Nothing; import fr.lnl.game.server.utils.Point; public abstract class ComputerPlayer extends AbstractPlayer { @@ -9,5 +11,17 @@ public abstract class ComputerPlayer extends AbstractPlayer { super(id, point, false, classPlayer); } - public abstract Action choseAction(); + public Action choseAction(Game game){ + Action action; + switch (getActions().size()){ + case 0 -> action = new Nothing(); + case 1 -> action = getActions().get(0); + default -> { + return strategy(game); + } + } + return action; + } + + public abstract Action strategy(Game game); } diff --git a/server/src/main/java/fr/lnl/game/server/games/player/Player.java b/server/src/main/java/fr/lnl/game/server/games/player/Player.java index 4eef35e..b772c52 100644 --- a/server/src/main/java/fr/lnl/game/server/games/player/Player.java +++ b/server/src/main/java/fr/lnl/game/server/games/player/Player.java @@ -1,6 +1,7 @@ package fr.lnl.game.server.games.player; import fr.lnl.game.server.games.action.Action; +import fr.lnl.game.server.games.action.ReunionSameAction; import fr.lnl.game.server.games.weapon.Weapon; import fr.lnl.game.server.utils.Point; @@ -8,6 +9,8 @@ import java.util.List; public interface Player { + List generateAvailableActions(); + Point getPosition(); boolean isAlive(); diff --git a/server/src/main/java/fr/lnl/game/server/games/player/StrategyComputerPlayer.java b/server/src/main/java/fr/lnl/game/server/games/player/StrategyComputerPlayer.java index 763c4f5..1e5a4bf 100644 --- a/server/src/main/java/fr/lnl/game/server/games/player/StrategyComputerPlayer.java +++ b/server/src/main/java/fr/lnl/game/server/games/player/StrategyComputerPlayer.java @@ -1,8 +1,16 @@ package fr.lnl.game.server.games.player; -import fr.lnl.game.server.games.action.Action; +import fr.lnl.game.server.games.Game; +import fr.lnl.game.server.games.action.*; +import fr.lnl.game.server.games.grid.Grid; +import fr.lnl.game.server.games.grid.elements.Box; +import fr.lnl.game.server.games.grid.elements.EnergyBall; +import fr.lnl.game.server.games.grid.elements.Explosive; import fr.lnl.game.server.utils.Point; +import java.util.List; +import java.util.Random; + public class StrategyComputerPlayer extends ComputerPlayer { public StrategyComputerPlayer(Integer id, Point point, ClassPlayer classPlayer) { @@ -10,7 +18,92 @@ public class StrategyComputerPlayer extends ComputerPlayer { } @Override - public Action choseAction() { - return null; + public Action strategy(Game game) { + Action action = null; + Grid grid = game.getGrid(); + for (Player player : game.getPlayers()) { + boolean danger = player.getActions().stream().anyMatch(a -> a instanceof Shot && a.getPoint().equals(getPosition())); + if(danger && (getEnergy() - getClassPlayer().getPenaltyShoot() <= 0)){ + action = new DeployShield(this); + } + } + List actions = generateAvailableActions(); + if(isInReunion(actions, Shot.class)){ + ReunionSameAction reunion = extractReunionSameAction(actions, Shot.class); + List actionList = reunion.getActions(); + action = actionList.get(0); + if(actionList.size() > 1){ + for (int i = 1; i < actionList.size(); i++) { + Point point = actionList.get(i).getPoint(); + if(grid.getGridPlayer(point).getEnergy() < grid.getGridPlayer(action.getPoint()).getEnergy()){ + action = actionList.get(i); + } + } + } + return action; + } + if(isInReunion(actions, Move.class)){ + ReunionSameAction reunion = extractReunionSameAction(actions, Move.class); + List actionList = reunion.getActions(); + for (Action value : actionList) { + Point point = value.getPoint(); + Box box = grid.getGridBox(point); + if(box instanceof EnergyBall){ + return value; + } + System.out.println("after move " + action); + } + Random random = new Random(); + int value = random.nextInt(0,2); + System.out.println(value); + if(value == 0){ + System.out.println("oui"); + do{ + action = actionList.get(random.nextInt(0, actionList.size())); + Box box = game.getGrid().getGridBox(action.getPoint()); + if(box instanceof Explosive) { + if (!((Explosive) box).getPlayer().equals(this)) { + action = null; + } + } + }while(action == null); + return action; + } + if(isInReunion(actions, Explosive.class)){ + List explosiveActions = extractReunionSameAction(actions, Move.class).getActions(); + if(explosiveActions.size() > 1){ + action = explosiveActions.get(random.nextInt(0, explosiveActions.size())); + return action; + } + System.out.println("explo " + action); + } + } + if(isInReunion(actions, Explosive.class)){ + Random random = new Random(); + List explosiveActions = extractReunionSameAction(actions, Move.class).getActions(); + if(explosiveActions.size() > 1){ + action = explosiveActions.get(random.nextInt(0, explosiveActions.size())); + } + return action; + } + else { + action = new Nothing(); + System.out.println("nothing " + action); + } + System.out.println("end " + action); + return action; + } + + public boolean isInReunion(List actions, Class clazz){ + return actions.stream().anyMatch(r -> r.getActionName().equals(clazz.getSimpleName())); + } + + public ReunionSameAction extractReunionSameAction(List actions, Class clazz){ + return actions.stream().filter(r -> r.getActionName().equals(clazz.getSimpleName())).findFirst().get(); + } + + @Override + public String toString() { + return "AI"; } }