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/client/src/main/java/fr/lnl/game/client/view/Cell.java b/client/src/main/java/fr/lnl/game/client/view/Cell.java index d5d8e7f..23ccd9f 100644 --- a/client/src/main/java/fr/lnl/game/client/view/Cell.java +++ b/client/src/main/java/fr/lnl/game/client/view/Cell.java @@ -17,6 +17,7 @@ import javafx.scene.shape.Rectangle; public class Cell extends Rectangle { private static final Image PLAYER_IMAGE = new Image("player.png"); + private static final Image PLAYER_SHIELD_IMAGE = new Image("player_shield.png"); private static final Image ENERGY_BALL_IMAGE = new Image("energyBall.png"); private static final Image BOMB_IMAGE = new Image("bomb.png"); private static final Image MINE_IMAGE = new Image("mine.png"); @@ -36,7 +37,12 @@ public class Cell extends Rectangle { StackPane sp = new StackPane(); Image in; if(object instanceof Player){ - in = PLAYER_IMAGE; + if(((Player) object).isShieldDeploy()){ + in = PLAYER_SHIELD_IMAGE; + } + else{ + in = PLAYER_IMAGE; + } } else if(object instanceof EnergyBall){ in = ENERGY_BALL_IMAGE; } else if(object instanceof Bomb){ diff --git a/client/src/main/java/fr/lnl/game/client/view/Terminal.java b/client/src/main/java/fr/lnl/game/client/view/Terminal.java index 1138bd9..0162831 100644 --- a/client/src/main/java/fr/lnl/game/client/view/Terminal.java +++ b/client/src/main/java/fr/lnl/game/client/view/Terminal.java @@ -24,12 +24,12 @@ public class Terminal extends AbstractView { @Override public void displayWinner(Player winner) { - System.out.println("Le joueur " + winner + " a gagné la partie"); + System.out.println(winner + " " + winner.getId() + " a gagné la partie"); } @Override public Action choseAction() { - List actions = generateAvailableActions(); + List actions = player.generateAvailableActions(); List listActions = choseReunionSameAction(actions).getActions(); Action action = null; String error = "Veuillez renseigner une valeur numérique comprise entre 1 et " + listActions.size(); diff --git a/client/src/main/java/fr/lnl/game/client/view/ViewManager.java b/client/src/main/java/fr/lnl/game/client/view/ViewManager.java index 0280a77..6e89f8c 100644 --- a/client/src/main/java/fr/lnl/game/client/view/ViewManager.java +++ b/client/src/main/java/fr/lnl/game/client/view/ViewManager.java @@ -27,7 +27,7 @@ public record ViewManager( } boolean isOver = game.play(); System.out.println("Le joueur ordinateur numéro " + player.getId() + " a joué"); - System.out.println("Il a joué l'action: " + game.getSelectedAction()); + System.out.println("Il a joué l'action: " + game.getSelectedAction().getClass().getSimpleName()); if (isOver) { displayWinnerEvent.updateModel(game.getWinner()); System.exit(0); diff --git a/client/src/main/java/fr/lnl/game/client/view/Window.java b/client/src/main/java/fr/lnl/game/client/view/Window.java index 0815b0f..059433a 100644 --- a/client/src/main/java/fr/lnl/game/client/view/Window.java +++ b/client/src/main/java/fr/lnl/game/client/view/Window.java @@ -62,7 +62,7 @@ public class Window extends AbstractView { @Override public Action choseAction() { - List actions = generateAvailableActions(); + List actions = player.generateAvailableActions(); List listActions = choseReunionSameAction(actions).getActions(); Action action = null; do { @@ -125,7 +125,7 @@ public class Window extends AbstractView { public void putStatePlayerPane(Pane principalPane){ int Y = 0; for(int i=0;i getValidPoint(); } diff --git a/server/src/main/java/fr/lnl/game/server/games/action/DeployShield.java b/server/src/main/java/fr/lnl/game/server/games/action/DeployShield.java index 955812f..ffc67df 100644 --- a/server/src/main/java/fr/lnl/game/server/games/action/DeployShield.java +++ b/server/src/main/java/fr/lnl/game/server/games/action/DeployShield.java @@ -32,6 +32,11 @@ public class DeployShield extends AbstractAction { return true; } + @Override + public Point getPoint() { + return null; + } + @Override public List getValidPoint() { return null; diff --git a/server/src/main/java/fr/lnl/game/server/games/action/DropObject.java b/server/src/main/java/fr/lnl/game/server/games/action/DropObject.java index e6f123b..6c56332 100644 --- a/server/src/main/java/fr/lnl/game/server/games/action/DropObject.java +++ b/server/src/main/java/fr/lnl/game/server/games/action/DropObject.java @@ -71,4 +71,8 @@ public abstract class DropObject extends AbstractAction { return direction; } + @Override + public Point getPoint() { + return point; + } } diff --git a/server/src/main/java/fr/lnl/game/server/games/action/Move.java b/server/src/main/java/fr/lnl/game/server/games/action/Move.java index 597cf1c..88d2a0c 100644 --- a/server/src/main/java/fr/lnl/game/server/games/action/Move.java +++ b/server/src/main/java/fr/lnl/game/server/games/action/Move.java @@ -60,6 +60,7 @@ public class Move extends AbstractAction { * We add a point to the list where there is nothing on the board. * @see Action#getValidPoint() */ + @Override public List getValidPoint() { List listMoves = new ArrayList<>(); @@ -81,6 +82,11 @@ public class Move extends AbstractAction { return listMoves; } + @Override + public Point getPoint() { + return point; + } + public Direction getDirection() { return direction; } diff --git a/server/src/main/java/fr/lnl/game/server/games/action/Nothing.java b/server/src/main/java/fr/lnl/game/server/games/action/Nothing.java index c6c2b20..1157815 100644 --- a/server/src/main/java/fr/lnl/game/server/games/action/Nothing.java +++ b/server/src/main/java/fr/lnl/game/server/games/action/Nothing.java @@ -29,6 +29,11 @@ public class Nothing extends AbstractAction { return true; } + @Override + public Point getPoint() { + return null; + } + @Override public List getValidPoint() { return null; diff --git a/server/src/main/java/fr/lnl/game/server/games/action/ReunionSameAction.java b/server/src/main/java/fr/lnl/game/server/games/action/ReunionSameAction.java index 553562d..dbd3f1c 100644 --- a/server/src/main/java/fr/lnl/game/server/games/action/ReunionSameAction.java +++ b/server/src/main/java/fr/lnl/game/server/games/action/ReunionSameAction.java @@ -1,7 +1,6 @@ package fr.lnl.game.server.games.action; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; /** diff --git a/server/src/main/java/fr/lnl/game/server/games/grid/Grid.java b/server/src/main/java/fr/lnl/game/server/games/grid/Grid.java index 5ff993f..a3378d3 100644 --- a/server/src/main/java/fr/lnl/game/server/games/grid/Grid.java +++ b/server/src/main/java/fr/lnl/game/server/games/grid/Grid.java @@ -90,7 +90,7 @@ public class Grid { str.append(" \033[0;34m").append(value.getA().getId()).append("\033[0m"); } else if (value.getB() instanceof Wall) { - str.append(" \033[0;32m□\033[0m"); + str.append(" \033[0;32m#\033[0m"); } else if(value.getB() instanceof EnergyBall){ str.append(" \033[0;31mE\033[0m"); @@ -119,7 +119,7 @@ public class Grid { str.append(" \033[0;34m").append(value.getA().getId()).append("\033[0m"); } else if (value.getB() instanceof Wall) { - str.append(" \033[0;32m□\033[0m"); + str.append(" \033[0;32m#\033[0m"); } else if(value.getB() instanceof EnergyBall){ str.append(" \033[0;31mE\033[0m"); @@ -149,6 +149,14 @@ public class Grid { return board; } + public Player getGridPlayer(Point point){ + return getBoard().get(point).getA(); + } + + public Box getGridBox(Point point){ + return getBoard().get(point).getB(); + } + public List getPlayers() { return players; } 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 9ac44d8..eaa6e65 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(/* NotNull */ 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 c670048..40eb8e9 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; /** @@ -16,5 +18,17 @@ public abstract class ComputerPlayer extends AbstractPlayer { * Call when an AI need to choose an action to execute * @return the chosen action */ - 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/HumanPlayer.java b/server/src/main/java/fr/lnl/game/server/games/player/HumanPlayer.java index 52ada24..f27e31e 100644 --- a/server/src/main/java/fr/lnl/game/server/games/player/HumanPlayer.java +++ b/server/src/main/java/fr/lnl/game/server/games/player/HumanPlayer.java @@ -14,4 +14,8 @@ public class HumanPlayer extends AbstractPlayer { } + @Override + public String toString() { + return "Human"; + } } 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/RandomComputerPlayer.java b/server/src/main/java/fr/lnl/game/server/games/player/RandomComputerPlayer.java index 8c322f8..3a1af8b 100644 --- a/server/src/main/java/fr/lnl/game/server/games/player/RandomComputerPlayer.java +++ b/server/src/main/java/fr/lnl/game/server/games/player/RandomComputerPlayer.java @@ -1,7 +1,9 @@ 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.games.grid.Grid; import fr.lnl.game.server.utils.Point; import java.util.Random; @@ -17,18 +19,17 @@ public class RandomComputerPlayer extends ComputerPlayer { * @return an action between all available */ @Override - public Action choseAction() { + public Action strategy(Game game) { Action action = null; - switch (getActions().size()){ - case 0 -> action = new Nothing(); - case 1 -> action = getActions().get(0); - default -> { - Random random = new Random(); - while (action == null || !action.isPossible()) { - action = getActions().get(random.nextInt(0,getActions().size())); - } - } + Random random = new Random(); + while (action == null || !action.isPossible()) { + action = getActions().get(random.nextInt(0, getActions().size())); } return action; } + + @Override + public String toString() { + return "Random"; + } } 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"; } }