From 2e99366e5646348bcde7f189d0b8df3cdd72d2db Mon Sep 17 00:00:00 2001 From: Katchan <35238981+KatchanNve@users.noreply.github.com> Date: Wed, 8 Dec 2021 18:52:12 +0100 Subject: [PATCH 1/3] Add utils functions --- .../main/java/fr/lnl/game/server/games/action/Action.java | 1 + .../java/fr/lnl/game/server/games/action/DeployShield.java | 5 +++++ .../java/fr/lnl/game/server/games/action/DropObject.java | 4 ++++ .../src/main/java/fr/lnl/game/server/games/action/Move.java | 6 ++++++ .../main/java/fr/lnl/game/server/games/action/Nothing.java | 5 +++++ .../java/fr/lnl/game/server/games/player/HumanPlayer.java | 4 ++++ 6 files changed, 25 insertions(+) diff --git a/server/src/main/java/fr/lnl/game/server/games/action/Action.java b/server/src/main/java/fr/lnl/game/server/games/action/Action.java index 325caa4..9179e65 100644 --- a/server/src/main/java/fr/lnl/game/server/games/action/Action.java +++ b/server/src/main/java/fr/lnl/game/server/games/action/Action.java @@ -22,6 +22,7 @@ public interface Action { * Used by {@link Move}, {@link Shot} and {@link DropObject} to list all direction when the action is possible * @return a list a point where the action is possible (not block by a wall per example) */ + Point getPoint(); List 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/player/HumanPlayer.java b/server/src/main/java/fr/lnl/game/server/games/player/HumanPlayer.java index a5ff86d..5cc31b8 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 @@ -9,4 +9,8 @@ public class HumanPlayer extends AbstractPlayer { } + @Override + public String toString() { + return "Human"; + } } From 4c52bedc9f59b47911cc8dbbed2d294236d59777 Mon Sep 17 00:00:00 2001 From: Katchan <35238981+KatchanNve@users.noreply.github.com> Date: Wed, 8 Dec 2021 18:54:52 +0100 Subject: [PATCH 2/3] Improve display of classes in the view, add player shield icon --- .../java/fr/lnl/game/client/view/Cell.java | 8 ++++++- .../fr/lnl/game/client/view/Terminal.java | 4 ++-- .../fr/lnl/game/client/view/ViewManager.java | 2 +- .../java/fr/lnl/game/client/view/Window.java | 13 ++++++----- client/src/main/resources/player_shield.png | Bin 0 -> 7833 bytes .../games/action/ReunionSameAction.java | 1 - .../fr/lnl/game/server/games/grid/Grid.java | 12 ++++++++-- .../games/player/RandomComputerPlayer.java | 21 +++++++++--------- 8 files changed, 38 insertions(+), 23 deletions(-) create mode 100644 client/src/main/resources/player_shield.png 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;i1jyE_yFcXti$?hv$a3JvbiNRZ$J_u%dh!2%WBoeqM!k$f6+=BLe^cG1X_tp~15&%G5 zoZq8*(whymkke2G03y%;5P^WI57fX5aIv;;XMF=Xd+=lZXEzXb+na~ zRFjvK1gg0@S=l;(006!IDqT-K%@O=u!^HUb$#;lyNL@(NNI;q}d~qKvhcN1RHFG6I zMreKM=CbsAhyC(Mb3>Rq3tjxdbpjN|(RRJs{7YJD(W%jn#U?WDP> zL?b#|la$;WeuoXP(_+=lKnof&rnknutf%}sWF2Wd;Fmx+B!;>L$ccwU=>2}IeR@Jd zHbSo>n@tl(eTMn<>x{o4aC3rmd(X82p^mfq3tF^)ez^Oy?U7nxfi4+BO7!YmgUFI*Sl*-4LksMQs;BXygy{5yPU76=|Sy8-_gCLF-za z*-t{Ql+)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bc%(XKhrSsx&JzD*hVRd4^tqS#6q0i3NM*=mZa?9#) z{}66QbgnFoVH=EtshWw^RrJ+|D&a5|VhN~SO2o-RxnI?WNP60BU**!Tm09CWn7uqS zwP#@K%^gVAiN?C!_;kCzi=EgrP)gEw_73qE40R#|mHEI_hq!cSb--yYZ~vt93x%nN zTCX7$Fd1<7 zijx?HL?6z`SOnKKD^bTa6qrgX<|v$CqnAz3_!_hq`pyu#N7n+mPy9Tcwe%r^&TIUx z=xla?*{K>+Jlm7s=NqMKAC=urwyhgkxuuO#^BIn(IQL_Bqt<8)EK153Ai z;XAgI374FYxY}wXq!~vYG9MrtdrRd^T7uU7zT)6{ getPlayers() { return players; } 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 f735b49..35ee9fa 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; @@ -13,18 +15,17 @@ public class RandomComputerPlayer extends ComputerPlayer { } @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"; + } } From 37d03e570a38f35e038be1f606f0ccdf52d11cb2 Mon Sep 17 00:00:00 2001 From: Katchan <35238981+KatchanNve@users.noreply.github.com> Date: Wed, 8 Dec 2021 18:56:26 +0100 Subject: [PATCH 3/3] Add basis strategy on StrategyComputerPlayer and implementation on this player in the game, view. --- .../src/main/java/fr/lnl/game/client/App.java | 10 ++ .../fr/lnl/game/client/view/AbstractView.java | 20 ---- .../java/fr/lnl/game/server/games/Game.java | 2 +- .../server/games/player/AbstractPlayer.java | 26 ++++- .../server/games/player/ComputerPlayer.java | 16 ++- .../lnl/game/server/games/player/Player.java | 3 + .../games/player/StrategyComputerPlayer.java | 99 ++++++++++++++++++- 7 files changed, 150 insertions(+), 26 deletions(-) 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"; } }