From 2e86f71e94f1e12077d90247a50ef724a4ab489d Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Tue, 26 Oct 2021 16:04:12 +0200 Subject: [PATCH] Improved argument parser and fix a bug with grid not initialize on new instance --- .../src/main/java/fr/lnl/game/client/App.java | 55 ++++++++++++------- .../java/fr/lnl/game/client/ClientPlayer.java | 10 +--- .../fr/lnl/game/client/CrashException.java | 9 --- .../fr/lnl/game/client/view/Terminal.java | 2 +- .../java/fr/lnl/game/server/ServerMain.java | 9 --- .../java/fr/lnl/game/server/games/Game.java | 19 ++++--- .../server/games/action/AbstractAction.java | 2 +- .../game/server/games/action/DropBomb.java | 3 +- .../game/server/games/grid/EnergyBall.java | 3 +- .../fr/lnl/game/server/games/grid/Grid.java | 17 ++---- .../fr/lnl/game/server/games/grid/Wall.java | 6 +- .../server/games/player/AbstractPlayer.java | 4 +- .../lnl/game/server/utils/CrashException.java | 9 +++ .../fr/lnl/game/server/ActionPlayerTest.java | 2 +- .../test/java/fr/lnl/game/server/Mock.java | 1 - 15 files changed, 71 insertions(+), 80 deletions(-) delete mode 100644 client/src/main/java/fr/lnl/game/client/CrashException.java create mode 100644 server/src/main/java/fr/lnl/game/server/utils/CrashException.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 a2880d4..1ab0d7c 100644 --- a/client/src/main/java/fr/lnl/game/client/App.java +++ b/client/src/main/java/fr/lnl/game/client/App.java @@ -5,6 +5,7 @@ import fr.lnl.game.client.view.Window; import fr.lnl.game.server.games.Game; import fr.lnl.game.server.games.grid.Grid; import fr.lnl.game.server.games.player.*; +import fr.lnl.game.server.utils.CrashException; import fr.lnl.game.server.utils.Point; import javafx.application.Application; import javafx.stage.Stage; @@ -20,47 +21,56 @@ public class App extends Application { public static void main(String[] args) { argsList = new LinkedList<>(Arrays.asList(args)); - Class clazz = parseView(); + argsList.removeIf(s -> s.startsWith("-D") || s.equals("fr.lnl.game.client.App")); // remove given parameters from gradle + Class clazz; + try { + clazz = parseView(); + } catch (IllegalArgumentException e) { + throw new CrashException(e.getMessage(), e); + } if(clazz.equals(Terminal.class)) { - try { - launchTerminal(); - } catch (InvocationTargetException | IllegalAccessException | InstantiationException | - NoSuchMethodException e) { - throw new CrashException(e.getCause()); - } + launchTerminal(); } else { launch(); } } - public static Game startGame() - throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { + public static Game startGame() throws IllegalArgumentException, InvocationTargetException, NoSuchMethodException, + InstantiationException, IllegalAccessException { List players = parsePlayers(); - System.out.println(players); return new Game(new Grid(12, 12, players), players); } @Override - public void start(Stage stage) throws Exception { - game = startGame(); + public void start(Stage stage) { + try { + game = startGame(); + } catch (IllegalArgumentException | InvocationTargetException | NoSuchMethodException | InstantiationException + | IllegalAccessException e) { + throw new CrashException(e.getMessage(), e); + } for (Player player : game.getPlayers()) { playerList.put(player, new ClientPlayer(player, new Window(stage, game, player))); } playerList.get(game.getCurrentPlayer()).getView().show(); } - public static void launchTerminal() - throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { - game = startGame(); + public static void launchTerminal() { + try { + game = startGame(); + } catch (IllegalArgumentException | InvocationTargetException | NoSuchMethodException | InstantiationException + | IllegalAccessException e) { + throw new CrashException(e.getMessage(), e); + } + for (Player player : game.getPlayers()) { playerList.put(player, new ClientPlayer(player, new Terminal(game, player))); } playerList.get(game.getCurrentPlayer()).getView().show(); } - // TODO: 23/10/2021 nécessite un rework -> faire une view par joueur - public static List parsePlayers() - throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { + public static List parsePlayers() throws IllegalArgumentException, NoSuchMethodException, + InvocationTargetException, InstantiationException, IllegalAccessException { List playerList = new ArrayList<>(); Class playerClass = null; ClassPlayer classPlayer = null; @@ -85,6 +95,7 @@ public class App extends Application { playerClass = ComputerPlayer.class; } case "default" -> classPlayer = ClassPlayer.DEFAULT; + default -> throw new IllegalArgumentException("Unknown argument: " + str); } } if(playerClass != null) @@ -104,14 +115,16 @@ public class App extends Application { public static Class parseView() { Class clazz; if(!argsList.isEmpty()) { - if(argsList.get(0).equalsIgnoreCase("terminal")) { + if(argsList.get(0).equals("terminal")) { clazz = Terminal.class; - } else { + } else if(argsList.get(0).equals("window")){ clazz = Window.class; + } else { + throw new IllegalArgumentException("Unknown argument: " + argsList.get(0)); } argsList.removeFirst(); } else { - clazz = Window.class; + throw new IllegalArgumentException("No argument given"); } return clazz; } diff --git a/client/src/main/java/fr/lnl/game/client/ClientPlayer.java b/client/src/main/java/fr/lnl/game/client/ClientPlayer.java index d81626c..6206ab5 100644 --- a/client/src/main/java/fr/lnl/game/client/ClientPlayer.java +++ b/client/src/main/java/fr/lnl/game/client/ClientPlayer.java @@ -3,15 +3,7 @@ package fr.lnl.game.client; import fr.lnl.game.client.view.View; import fr.lnl.game.server.games.player.Player; -public class ClientPlayer { - - private final Player serverPlayer; - private final View view; - - public ClientPlayer(Player serverPlayer, View view) { - this.serverPlayer = serverPlayer; - this.view = view; - } +public record ClientPlayer(Player serverPlayer, View view) { public Player getServerPlayer() { return serverPlayer; diff --git a/client/src/main/java/fr/lnl/game/client/CrashException.java b/client/src/main/java/fr/lnl/game/client/CrashException.java deleted file mode 100644 index 9c6b3a8..0000000 --- a/client/src/main/java/fr/lnl/game/client/CrashException.java +++ /dev/null @@ -1,9 +0,0 @@ -package fr.lnl.game.client; - -public class CrashException extends RuntimeException { - - public CrashException(Throwable cause) { - super(cause); - } - -} 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 53ca27a..5066261 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 @@ -11,7 +11,7 @@ public class Terminal extends AbstractView { public void show() { // TODO: 26/10/2021 - game.getGrid().printGrid(); + System.out.println(game.getGrid().toString()); } } diff --git a/server/src/main/java/fr/lnl/game/server/ServerMain.java b/server/src/main/java/fr/lnl/game/server/ServerMain.java index cc8844c..aa21823 100644 --- a/server/src/main/java/fr/lnl/game/server/ServerMain.java +++ b/server/src/main/java/fr/lnl/game/server/ServerMain.java @@ -1,14 +1,5 @@ package fr.lnl.game.server; -import fr.lnl.game.server.games.Game; -import fr.lnl.game.server.games.grid.Grid; -import fr.lnl.game.server.games.player.ComputerPlayer; -import fr.lnl.game.server.games.player.ClassPlayer; -import fr.lnl.game.server.games.player.Player; - -import java.util.Arrays; -import java.util.List; - public class ServerMain { public static void main(String[] args) { 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 15be9c7..7285a36 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 @@ -11,7 +11,7 @@ public class Game { Player currentPlayer; List players; - public Game(Grid grid, List players) { + public Game(Grid grid, List players) throws IllegalArgumentException { if(players.size() < 2) throw new IllegalArgumentException("The game need 2 or more player to start"); this.players = players; @@ -19,7 +19,7 @@ public class Game { this.grid = grid; } - public void play(){ + public void play() { } @@ -27,7 +27,7 @@ public class Game { return players.parallelStream().filter(player -> !player.isAlive()).count() == 1; } - public Player getWinner(){ + public Player getWinner() { return players.parallelStream().filter(player -> !player.isAlive()).findFirst().orElseThrow(ArrayIndexOutOfBoundsException::new); } @@ -35,11 +35,16 @@ public class Game { return currentPlayer; } + /** + * Change player to the next available in the list + */ public void nextCurrentPlayer() { - int index = players.indexOf(currentPlayer) + 1; - if(index == players.size()) - index = 0; - currentPlayer = players.get(index); + do { + int index = players.indexOf(currentPlayer) + 1; + if(index == players.size()) + index = 0; + currentPlayer = players.get(index); + } while(!currentPlayer.isAlive()); // On arrête la boucle dès qu'on trouve un joueur en vie } public Grid getGrid() { diff --git a/server/src/main/java/fr/lnl/game/server/games/action/AbstractAction.java b/server/src/main/java/fr/lnl/game/server/games/action/AbstractAction.java index ffc6059..eb3d74d 100644 --- a/server/src/main/java/fr/lnl/game/server/games/action/AbstractAction.java +++ b/server/src/main/java/fr/lnl/game/server/games/action/AbstractAction.java @@ -3,7 +3,7 @@ package fr.lnl.game.server.games.action; import fr.lnl.game.server.games.Game; public abstract class AbstractAction implements Action { - private Game game; + private final Game game; public AbstractAction(Game game){ this.game = game; diff --git a/server/src/main/java/fr/lnl/game/server/games/action/DropBomb.java b/server/src/main/java/fr/lnl/game/server/games/action/DropBomb.java index 7126873..62e0638 100644 --- a/server/src/main/java/fr/lnl/game/server/games/action/DropBomb.java +++ b/server/src/main/java/fr/lnl/game/server/games/action/DropBomb.java @@ -2,7 +2,6 @@ package fr.lnl.game.server.games.action; import fr.lnl.game.server.games.Game; import fr.lnl.game.server.games.grid.Bomb; -import fr.lnl.game.server.games.grid.Mine; import fr.lnl.game.server.games.player.Player; import fr.lnl.game.server.utils.Point; @@ -15,7 +14,7 @@ public class DropBomb extends DropObject { super(game); } - //voir pour la redondance de code au niveau de DropBomb,DropObject,DropMine + // voir pour la redondance de code au niveau de DropBomb, DropObject,DropMine @Override public void doAction() { List points = getValidPoint(); diff --git a/server/src/main/java/fr/lnl/game/server/games/grid/EnergyBall.java b/server/src/main/java/fr/lnl/game/server/games/grid/EnergyBall.java index 7ecfbfd..303c715 100644 --- a/server/src/main/java/fr/lnl/game/server/games/grid/EnergyBall.java +++ b/server/src/main/java/fr/lnl/game/server/games/grid/EnergyBall.java @@ -5,7 +5,6 @@ public class EnergyBall implements Box{ @Override public boolean equals(Object o) { if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - return true; // no var to test + return o != null && getClass() == o.getClass();// no var to test } } 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 bb3806f..2ba60ba 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 @@ -10,16 +10,17 @@ import java.util.List; public class Grid { - private HashMap> board; - private int row; - private int column; - private List players; + private final HashMap> board; + private final int row; + private final int column; + private final List players; public Grid(int row, int column, List players) { this.row = row; this.column = column; this.players = players; board = new HashMap<>(); + initGrid(); } public void initGrid(){ @@ -121,14 +122,6 @@ public class Grid { return str.toString(); } - /** - * @deprecated modèle mvc non respecté - */ - @Deprecated - public void printGrid() { - System.out.println(this); - } - public List getPlayers() { return players; } diff --git a/server/src/main/java/fr/lnl/game/server/games/grid/Wall.java b/server/src/main/java/fr/lnl/game/server/games/grid/Wall.java index 7dfb7df..06d8853 100644 --- a/server/src/main/java/fr/lnl/game/server/games/grid/Wall.java +++ b/server/src/main/java/fr/lnl/game/server/games/grid/Wall.java @@ -6,9 +6,9 @@ import java.util.Objects; public class Wall implements Box { - private Cardinal cardinal; - private int x; - private int y; + private final Cardinal cardinal; + private final int x; + private final int y; public Wall(Cardinal cardinal, int x, int y){ this.cardinal = cardinal; 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 1354ba2..c4d61f6 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 @@ -6,13 +6,13 @@ import fr.lnl.game.server.utils.Point; public abstract class AbstractPlayer implements Player { - private int id; + private final int id; private Point point; private int energy; private Weapon weapon; private boolean shieldDeploy; private Action[] actions; - private ClassPlayer classPlayer; + private final ClassPlayer classPlayer; public AbstractPlayer(Integer id, Point point, boolean shieldDeploy, ClassPlayer classPlayer) { this.id = id; diff --git a/server/src/main/java/fr/lnl/game/server/utils/CrashException.java b/server/src/main/java/fr/lnl/game/server/utils/CrashException.java new file mode 100644 index 0000000..f49ea1c --- /dev/null +++ b/server/src/main/java/fr/lnl/game/server/utils/CrashException.java @@ -0,0 +1,9 @@ +package fr.lnl.game.server.utils; + +public class CrashException extends RuntimeException { + + public CrashException(String message, Throwable cause) { + super(message, cause); + } + +} diff --git a/server/src/test/java/fr/lnl/game/server/ActionPlayerTest.java b/server/src/test/java/fr/lnl/game/server/ActionPlayerTest.java index 09350b8..90e905c 100644 --- a/server/src/test/java/fr/lnl/game/server/ActionPlayerTest.java +++ b/server/src/test/java/fr/lnl/game/server/ActionPlayerTest.java @@ -18,7 +18,7 @@ public class ActionPlayerTest { this.game = mock.game; } - // TODO: 21/10/2021 Vérifier sur Move effectue la bonne action en pensant a appeller isPossible() avant et + // TODO: 21/10/2021 Vérifier sur Move effectue la bonne action en pensant a appeler isPossible() avant et // en checkant son résultat @Test public void moveActionTest() { 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 e07f551..1ab5628 100644 --- a/server/src/test/java/fr/lnl/game/server/Mock.java +++ b/server/src/test/java/fr/lnl/game/server/Mock.java @@ -22,7 +22,6 @@ public class Mock { List players = Arrays.asList(new ComputerPlayer(1,null, ClassPlayer.DEFAULT), new ComputerPlayer(2,null, ClassPlayer.DEFAULT)); this.grid = new Grid(16,16, players); - grid.initGrid(); placePlayersBRUT(); placeEnergyBallBRUT(); placeInternWallBRUT();