From 3ccc26592c6e0e79f73bed57b71dfb046a3f29ff Mon Sep 17 00:00:00 2001 From: Katchan Date: Fri, 5 Nov 2021 15:35:25 +0100 Subject: [PATCH] Add differents ComputerPlayer class + refactor calls --- .../src/main/java/fr/lnl/game/client/App.java | 2 +- .../lnl/game/server/games/action/Nothing.java | 5 +++ .../server/games/player/ComputerPlayer.java | 10 ------ .../game/server/games/player/HumanPlayer.java | 7 ++++- .../lnl/game/server/games/player/Player.java | 3 ++ .../games/player/RandomComputerPlayer.java | 31 +++++++++++++++++++ .../games/player/StrategyComputerPlayer.java | 16 ++++++++++ .../java/fr/lnl/game/server/GridTest.java | 13 +------- .../test/java/fr/lnl/game/server/Mock.java | 6 ++-- 9 files changed, 66 insertions(+), 27 deletions(-) delete mode 100644 server/src/main/java/fr/lnl/game/server/games/player/ComputerPlayer.java create mode 100644 server/src/main/java/fr/lnl/game/server/games/player/RandomComputerPlayer.java create mode 100644 server/src/main/java/fr/lnl/game/server/games/player/StrategyComputerPlayer.java 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 2ab7b3f..bb07de1 100644 --- a/client/src/main/java/fr/lnl/game/client/App.java +++ b/client/src/main/java/fr/lnl/game/client/App.java @@ -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; 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 3b2d350..26fbe54 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 @@ -10,6 +10,11 @@ public class Nothing extends AbstractAction { public Nothing(Game game){ super(game); } + + public Nothing(){ + super(null); + } + @Override public void doAction(){ } 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 deleted file mode 100644 index 4e6b1c1..0000000 --- a/server/src/main/java/fr/lnl/game/server/games/player/ComputerPlayer.java +++ /dev/null @@ -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); - } -} 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 8672b1d..ab43868 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 @@ -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; + } } 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 ea0ad36..3e12082 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,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); + } 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 new file mode 100644 index 0000000..3d815d5 --- /dev/null +++ b/server/src/main/java/fr/lnl/game/server/games/player/RandomComputerPlayer.java @@ -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; + } +} 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 new file mode 100644 index 0000000..c47f230 --- /dev/null +++ b/server/src/main/java/fr/lnl/game/server/games/player/StrategyComputerPlayer.java @@ -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; + } +} diff --git a/server/src/test/java/fr/lnl/game/server/GridTest.java b/server/src/test/java/fr/lnl/game/server/GridTest.java index a85a146..b606a97 100644 --- a/server/src/test/java/fr/lnl/game/server/GridTest.java +++ b/server/src/test/java/fr/lnl/game/server/GridTest.java @@ -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"); diff --git a/server/src/test/java/fr/lnl/game/server/Mock.java b/server/src/test/java/fr/lnl/game/server/Mock.java index aedaf87..8f09f38 100644 --- a/server/src/test/java/fr/lnl/game/server/Mock.java +++ b/server/src/test/java/fr/lnl/game/server/Mock.java @@ -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 players = Arrays.asList(new ComputerPlayer(1,null, ClassPlayer.DEFAULT), - new ComputerPlayer(2,null, ClassPlayer.DEFAULT)); + List players = Arrays.asList(new RandomComputerPlayer(1,null, ClassPlayer.DEFAULT), + new RandomComputerPlayer(2,null, ClassPlayer.DEFAULT)); this.grid = new Grid(16,16, players); placePlayersBRUT(); placeEnergyBallBRUT();