From 4e2da3330a91c269ac29ae88f4ecbf08f92ddbb8 Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Sat, 23 Oct 2021 19:15:08 +0200 Subject: [PATCH] Added very basic view support + reworked players' fields in Game.java and Grid.java --- .../java/fr/lnl/game/client/AbstractView.java | 8 +++ .../src/main/java/fr/lnl/game/client/App.java | 59 +++++++++++++------ .../java/fr/lnl/game/client/Terminal.java | 15 +++++ .../main/java/fr/lnl/game/client/Window.java | 22 +++++++ .../fr/lnl/game/client/graphics/Terminal.java | 6 -- .../fr/lnl/game/client/MessageUtilsTest.java | 16 ----- server/build.gradle | 2 +- .../java/fr/lnl/game/server/MessageUtils.java | 10 ---- .../java/fr/lnl/game/server/ServerMain.java | 12 ++-- .../java/fr/lnl/game/server/games/Game.java | 29 ++++----- .../fr/lnl/game/server/games/grid/Grid.java | 11 ++-- server/src/main/java/module-info.java | 4 ++ .../fr/lnl/game/server/ActionPlayerTest.java | 2 +- .../java/fr/lnl/game/server/GridTest.java | 4 +- .../test/java/fr/lnl/game/server/Mock.java | 15 +++-- 15 files changed, 129 insertions(+), 86 deletions(-) create mode 100644 client/src/main/java/fr/lnl/game/client/Terminal.java delete mode 100644 client/src/main/java/fr/lnl/game/client/graphics/Terminal.java delete mode 100644 client/src/test/java/fr/lnl/game/client/MessageUtilsTest.java delete mode 100644 server/src/main/java/fr/lnl/game/server/MessageUtils.java diff --git a/client/src/main/java/fr/lnl/game/client/AbstractView.java b/client/src/main/java/fr/lnl/game/client/AbstractView.java index d837f18..2eebd5e 100644 --- a/client/src/main/java/fr/lnl/game/client/AbstractView.java +++ b/client/src/main/java/fr/lnl/game/client/AbstractView.java @@ -1,4 +1,12 @@ package fr.lnl.game.client; +import fr.lnl.game.server.games.Game; + public abstract class AbstractView implements View { + + Game game; + + public AbstractView(Game game) { + this.game = game; + } } 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 76c205d..f8e16d1 100644 --- a/client/src/main/java/fr/lnl/game/client/App.java +++ b/client/src/main/java/fr/lnl/game/client/App.java @@ -1,31 +1,56 @@ -/* - * This Java source file was generated by the Gradle 'init' task. - */ package fr.lnl.game.client; +import fr.lnl.game.server.games.Game; +import fr.lnl.game.server.games.grid.Grid; +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 javafx.application.Application; -import javafx.scene.Scene; -import javafx.scene.control.Label; -import javafx.scene.layout.StackPane; import javafx.stage.Stage; -import static fr.lnl.game.server.MessageUtils.getMessage; +import java.util.Arrays; +import java.util.List; public class App extends Application { public static void main(String[] args) { - String result = getMessage(); - System.out.println(result); - launch(); + Class clazz = parseArgs(args); + if(clazz.equals(Terminal.class)) { + launchTerminal(); + } else { + launch(); + } + } + + public static Game startGame() { + List players = Arrays.asList(new ComputerPlayer(1, null, ClassPlayer.DEFAULT), + new ComputerPlayer(1, null, ClassPlayer.DEFAULT)); + return new Game(new Grid(12, 12, players), players); } @Override public void start(Stage stage) throws Exception { - System.out.println("Launching stage"); - String javaVersion = System.getProperty("java.version"); - String javafxVersion = System.getProperty("javafx.version"); - Label l = new Label("Hello, JavaFX version " + javafxVersion + ", running on Java " + javaVersion + "."); - Scene scene = new Scene(new StackPane(l), 640, 480); - stage.setScene(scene); - stage.show(); + Game game = startGame(); + Window window = new Window(stage, game); + // TODO: 23/10/2021 faire une view par joueur + window.show(); + } + + public static void launchTerminal() { + Game game = startGame(); + Terminal terminal = new Terminal(game); + // TODO: 23/10/2021 faire une view par joueur + } + + // TODO: 23/10/2021 nécessite un rework -> faire une view par joueur + public static Class parseArgs(String[] args) { + if(args.length >= 1) { + if(args[0].equalsIgnoreCase("terminal")) { + return Terminal.class; + } else { + return Window.class; + } + } else { + return Window.class; + } } } diff --git a/client/src/main/java/fr/lnl/game/client/Terminal.java b/client/src/main/java/fr/lnl/game/client/Terminal.java new file mode 100644 index 0000000..39af7e7 --- /dev/null +++ b/client/src/main/java/fr/lnl/game/client/Terminal.java @@ -0,0 +1,15 @@ +package fr.lnl.game.client; + +import fr.lnl.game.server.games.Game; + +public class Terminal extends AbstractView { + + public Terminal(Game game) { + super(game); + } + + public void displayGrid() { + + } + +} diff --git a/client/src/main/java/fr/lnl/game/client/Window.java b/client/src/main/java/fr/lnl/game/client/Window.java index 93fe618..c3e36e4 100644 --- a/client/src/main/java/fr/lnl/game/client/Window.java +++ b/client/src/main/java/fr/lnl/game/client/Window.java @@ -1,4 +1,26 @@ package fr.lnl.game.client; +import fr.lnl.game.server.games.Game; +import javafx.scene.Scene; +import javafx.scene.control.Label; +import javafx.scene.layout.StackPane; +import javafx.stage.Stage; + public class Window extends AbstractView { + + private final Stage stage; + + public Window(Stage stage, Game game) { + super(game); + this.stage = stage; + } + + public void show() { + String javaVersion = System.getProperty("java.version"); + String javafxVersion = System.getProperty("javafx.version"); + Label l = new Label("Hello, JavaFX version " + javafxVersion + ", running on Java " + javaVersion + "."); + Scene scene = new Scene(new StackPane(l), 640, 480); + stage.setScene(scene); + stage.show(); + } } diff --git a/client/src/main/java/fr/lnl/game/client/graphics/Terminal.java b/client/src/main/java/fr/lnl/game/client/graphics/Terminal.java deleted file mode 100644 index b17a216..0000000 --- a/client/src/main/java/fr/lnl/game/client/graphics/Terminal.java +++ /dev/null @@ -1,6 +0,0 @@ -package fr.lnl.game.client.graphics; - -import fr.lnl.game.client.AbstractView; - -public class Terminal extends AbstractView { -} diff --git a/client/src/test/java/fr/lnl/game/client/MessageUtilsTest.java b/client/src/test/java/fr/lnl/game/client/MessageUtilsTest.java deleted file mode 100644 index a90825d..0000000 --- a/client/src/test/java/fr/lnl/game/client/MessageUtilsTest.java +++ /dev/null @@ -1,16 +0,0 @@ -/* - * This Java source file was generated by the Gradle 'init' task. - */ -package fr.lnl.game.client; - -import fr.lnl.game.server.MessageUtils; -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -public class MessageUtilsTest { - @Test - public void testGetMessage() { - assertEquals("Hello World!", MessageUtils.getMessage()); - } -} diff --git a/server/build.gradle b/server/build.gradle index 2e4b59d..5b194ea 100644 --- a/server/build.gradle +++ b/server/build.gradle @@ -13,7 +13,7 @@ dependencies { } application { - mainClassName = 'fr.lnl.game.server.games.grid.Grid' + mainClassName = 'fr.lnl.game.server.ServerMain' } test { diff --git a/server/src/main/java/fr/lnl/game/server/MessageUtils.java b/server/src/main/java/fr/lnl/game/server/MessageUtils.java deleted file mode 100644 index 69efc96..0000000 --- a/server/src/main/java/fr/lnl/game/server/MessageUtils.java +++ /dev/null @@ -1,10 +0,0 @@ -/* - * This Java source file was generated by the Gradle 'init' task. - */ -package fr.lnl.game.server; - -public class MessageUtils { - public static String getMessage() { - return "Hello World!"; - } -} 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 0bf3337..60d97a9 100644 --- a/server/src/main/java/fr/lnl/game/server/ServerMain.java +++ b/server/src/main/java/fr/lnl/game/server/ServerMain.java @@ -6,18 +6,20 @@ 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) { - Player playerOne = new ComputerPlayer(1,null, ClassPlayer.DEFAULT); - Player playerTwo = new ComputerPlayer(2,null, ClassPlayer.DEFAULT); - - Grid grid = new Grid(16,16,new Player[]{playerOne,playerTwo}); + List players = Arrays.asList(new ComputerPlayer(1,null, ClassPlayer.DEFAULT), + new ComputerPlayer(2,null, ClassPlayer.DEFAULT)); + Grid grid = new Grid(16,16, players); grid.initGrid(); grid.placePlayersBRUT(); grid.placeEnergyBallBRUT(); grid.placeInternWallBRUT(); - Game game = new Game(grid,playerOne,playerTwo); + Game game = new Game(grid, players); game.getGrid().printGrid(); } } 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 fd1fd50..9cdf0ab 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 @@ -3,37 +3,32 @@ package fr.lnl.game.server.games; import fr.lnl.game.server.games.grid.Grid; import fr.lnl.game.server.games.player.Player; +import java.util.List; + public class Game { Grid grid; - Player player_One; - Player player_Two; Player current_player; - Player[] players; + List players; - public Game(Grid grid, Player player_One, Player player_Two){ - this.player_One = player_One; - this.player_Two = player_Two; - this.current_player = player_One; + public Game(Grid grid, List players) { + if(players.size() < 2) + throw new IllegalArgumentException("The game need 2 or more player to start"); + this.players = players; + this.current_player = players.get(0); this.grid = grid; - players = new Player[]{player_One, player_Two}; } public void play(){ } - public boolean isOver(){ - return players.length == 1; + public boolean isOver() { + return players.parallelStream().filter(player -> !player.isAlive()).count() == 1; } public Player getWinner(){ - // Quentin: simple avis: appel de isOver pas forcément nécessaire, puisqu'on appelera surement getWinner après - // un appel a isOver retournant true - if(isOver()){ - return players[0]; - } - return null; + return players.parallelStream().filter(player -> !player.isAlive()).findFirst().orElseThrow(ArrayIndexOutOfBoundsException::new); } public Player getCurrentPlayer() { @@ -44,7 +39,7 @@ public class Game { return grid; } - public Player[] getPlayers() { + public List getPlayers() { return players; } } \ No newline at end of file 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 6a21297..bb3806f 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 @@ -6,15 +6,16 @@ import fr.lnl.game.server.utils.Pair; import fr.lnl.game.server.utils.Point; import java.util.HashMap; +import java.util.List; public class Grid { private HashMap> board; private int row; private int column; - private Player[] players; + private List players; - public Grid(int row, int column, Player[] players){ + public Grid(int row, int column, List players) { this.row = row; this.column = column; this.players = players; @@ -50,8 +51,8 @@ public class Grid { } public void placePlayersBRUT(){ - board.get(new Point(1,1)).setA(players[0]); - board.get(new Point(14,14)).setA(players[1]); + board.get(new Point(1,1)).setA(players.get(0)); + board.get(new Point(14,14)).setA(players.get(1)); } public void placeEnergyBallBRUT(){ @@ -128,7 +129,7 @@ public class Grid { System.out.println(this); } - public Player[] getPlayers() { + public List getPlayers() { return players; } diff --git a/server/src/main/java/module-info.java b/server/src/main/java/module-info.java index b823752..fdbc89b 100644 --- a/server/src/main/java/module-info.java +++ b/server/src/main/java/module-info.java @@ -1,3 +1,7 @@ module server { exports fr.lnl.game.server; + exports fr.lnl.game.server.games; + exports fr.lnl.game.server.games.player; + exports fr.lnl.game.server.games.grid; + exports fr.lnl.game.server.utils; } \ No newline at end of file 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 6c887b7..09350b8 100644 --- a/server/src/test/java/fr/lnl/game/server/ActionPlayerTest.java +++ b/server/src/test/java/fr/lnl/game/server/ActionPlayerTest.java @@ -22,7 +22,7 @@ public class ActionPlayerTest { // en checkant son résultat @Test public void moveActionTest() { - Assertions.assertEquals(game.getPlayers()[0], game.getCurrentPlayer()); + Assertions.assertEquals(game.getPlayers().get(0), game.getCurrentPlayer()); } } 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 cead165..bf26d86 100644 --- a/server/src/test/java/fr/lnl/game/server/GridTest.java +++ b/server/src/test/java/fr/lnl/game/server/GridTest.java @@ -28,8 +28,8 @@ public class GridTest { assertEquals(new Wall(Cardinal.SOUTH_WEST, grid.getRow() - 1, 0), grid.getBoard().get(new Point(grid.getRow() - 1, 0)).getB()); assertEquals(new Wall(Cardinal.SOUTH_EAST, grid.getRow() - 1, grid.getColumn() - 1), grid.getBoard().get(new Point(grid.getRow() - 1, grid.getColumn() - 1)).getB()); // test placePlayersBRUT (mocked) - assertEquals(grid.getPlayers()[0], grid.getBoard().get(new Point(1, 1)).getA()); - assertEquals(grid.getPlayers()[1], grid.getBoard().get(new Point(14, 14)).getA()); + assertEquals(grid.getPlayers().get(0), grid.getBoard().get(new Point(1, 1)).getA()); + assertEquals(grid.getPlayers().get(1), grid.getBoard().get(new Point(14, 14)).getA()); // test placeEnergyBallBRUT (mocked) assertEquals(new EnergyBall(), grid.getBoard().get(new Point(2, 3)).getB()); assertEquals(new EnergyBall(), grid.getBoard().get(new Point(7, 10)).getB()); 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 6a4d463..e07f551 100644 --- a/server/src/test/java/fr/lnl/game/server/Mock.java +++ b/server/src/test/java/fr/lnl/game/server/Mock.java @@ -10,25 +10,28 @@ import fr.lnl.game.server.games.player.Player; import fr.lnl.game.server.utils.Cardinal; import fr.lnl.game.server.utils.Point; +import java.util.Arrays; +import java.util.List; + public class Mock { Grid grid; Game game; public Mock() { - Player playerOne = new ComputerPlayer(1,null, ClassPlayer.DEFAULT); - Player playerTwo = new ComputerPlayer(2,null, ClassPlayer.DEFAULT); - this.grid = new Grid(16,16,new Player[]{playerOne,playerTwo}); + 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(); - game = new Game(grid,playerOne,playerTwo); + game = new Game(grid, players); } public void placePlayersBRUT(){ - grid.getBoard().get(new Point(1,1)).setA(grid.getPlayers()[0]); - grid.getBoard().get(new Point(14,14)).setA(grid.getPlayers()[1]); + grid.getBoard().get(new Point(1,1)).setA(grid.getPlayers().get(0)); + grid.getBoard().get(new Point(14,14)).setA(grid.getPlayers().get(1)); } public void placeEnergyBallBRUT(){