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 8ac55ad..7d4526a 100644 --- a/client/src/main/java/fr/lnl/game/client/App.java +++ b/client/src/main/java/fr/lnl/game/client/App.java @@ -51,7 +51,7 @@ public class App extends Application { public static void startGame(ViewLambda lambda) throws IllegalArgumentException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { List players = parsePlayers(); - GridFactoryBuilder builder = LockGridFactoryBuilder.create().energyProbability(0.95F).wallProbability(0.80F).playersList(players).gridDimensions(12, 12); + GridFactoryBuilder builder = LockGridFactoryBuilder.create().energyProbability(0.95F).wallProbability(0.80F).gridDimensions(12, 12); game = new Game(builder, players); for (Player player : game.getPlayers()) { playerList.put(player, new ClientPlayer(player, lambda.createViewLambda(player))); 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 cfaba42..9dcb991 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 @@ -16,32 +16,40 @@ import static java.util.function.Predicate.not; public class Game { - private final GridFactoryBuilder buildStrategy; private final Grid grid; private final List players; private Player currentPlayer; private Action selectedAction = null; + /** + * @param buildStrategy used to build a grid + * @param players a list players in the game + * @throws IllegalArgumentException if number of players given in parameters is too many or inferior to 2 + */ public Game(GridFactoryBuilder buildStrategy, List players) throws IllegalArgumentException { - this.grid = buildStrategy.build(); + this.grid = buildStrategy.playersList(players).build(); if(players.size() < 2) throw new IllegalArgumentException("The game need 2 or more player to start"); if(players.size() > grid.getNumberNeutralBox()){ throw new IllegalArgumentException("There are too many players for the number of box available"); } - this.buildStrategy = buildStrategy; this.players = players; this.currentPlayer = players.get(0); - initGame(); + initGame(buildStrategy); } - public void initGame(){ + /** + * Initialize a game by placing players on the grid and by generating current player available actions + * @param buildStrategy builder used to create a grid + */ + public void initGame(GridFactoryBuilder buildStrategy){ buildStrategy.initPlacePlayers(); currentPlayer.setActions(generateAndGetPlayerActions(currentPlayer)); } /** - * + * Game "main" method, call by a controller after human chose an action or when a computer player play an action + * Method is call everytime an action has been chosen by a human or when aa computer player need to play * @return true if game is over, false otherwise */ public boolean play() { @@ -56,17 +64,28 @@ public class Game { return isOver(); } + /** + * Remove dead players from the grid + */ private void gridPlayersUpdate(){ for (Player player: getPlayersNotAlive().toList()) { getGrid().getBoard().get(player.getPosition()).setA(null); } } + /** + * play grid's elements that use a timer like {@link fr.lnl.game.server.games.grid.elements.Bomb} at each game tick + */ private void countdownGridElementsUpdate() { List countdownBoxes = this.getGrid().getAllCountdownElements(); countdownBoxes.forEach(CountdownBox::update); } + /** + * Used to list all actions a player can execute at current time + * @param player the player to generate actions + * @return a list of available actions + */ public List generateAndGetPlayerActions(Player player) { List actions = new ArrayList<>(); for(Direction direction : Direction.values()) { @@ -87,29 +106,52 @@ public class Game { return actions; } + /** + * + * @return a list of alive players + */ public Stream getPlayersAlive() { return players.parallelStream().filter(Player::isAlive); } + /** + * Opposite of {@link Game#getPlayersAlive()} + * @return a list of dead players + */ public Stream getPlayersNotAlive() { return players.parallelStream().filter(not(Player::isAlive)); } + /** + * A game is over if the number of alive players is inferior to 2 + * @return true if game is over, false otherwise + */ public boolean isOver() { return getPlayersAlive().count() <= 1; } + /** + * + * @return the winner of the game if exists, null otherwise.
+ * return the only remaining alive player when it exists, or null if everyone is dead, per example when a bomb kill + * the 2 remaining players + */ public Player getWinner() { // On part du principe que isOver est forcément appelé avant d'appeler getWinner return getPlayersAlive().findFirst().orElse(null); } + /** + * + * @return the player who is currently playing + */ public Player getCurrentPlayer() { return currentPlayer; } /** - * Change player to the next available in the list + * Change player to the next available in the list.
+ * We set its shield deploy state to false. */ public void nextCurrentPlayer() { do { @@ -121,6 +163,9 @@ public class Game { currentPlayer.setShieldDeploy(false); // on reset son état } + /** + * @param current_player the new current player + */ public void setCurrentPlayer(Player current_player) { this.currentPlayer = current_player; } @@ -133,10 +178,17 @@ public class Game { return players; } + /** + * + * @return action selected by current player if not already executed or last player if already executed + */ public Action getSelectedAction() { return selectedAction; } + /** + * @param selectedAction set the action selected by current player before doing it + */ public void setSelectedAction(Action selectedAction) { this.selectedAction = selectedAction; } diff --git a/server/src/main/java/fr/lnl/game/server/games/InterfaceAction.java b/server/src/main/java/fr/lnl/game/server/games/InterfaceAction.java deleted file mode 100644 index 2322f1f..0000000 --- a/server/src/main/java/fr/lnl/game/server/games/InterfaceAction.java +++ /dev/null @@ -1,8 +0,0 @@ -package fr.lnl.game.server.games; - -public enum InterfaceAction { - - SELECT_ACTION, - DIALOG_TO_CONTINUE_COMPUTER_IA - -} diff --git a/server/src/main/java/fr/lnl/game/server/games/grid/build/GridFactoryBuilder.java b/server/src/main/java/fr/lnl/game/server/games/grid/build/GridFactoryBuilder.java index 7f19269..f320411 100644 --- a/server/src/main/java/fr/lnl/game/server/games/grid/build/GridFactoryBuilder.java +++ b/server/src/main/java/fr/lnl/game/server/games/grid/build/GridFactoryBuilder.java @@ -53,7 +53,8 @@ public interface GridFactoryBuilder { Grid build(); /** - * call when initializing the game, it'll place player depending on the strategy used by its implementation + * call when initializing the game, it'll place player depending on the strategy used by its implementation, + * need to be call after build, call an NullPointerException otherwise */ void initPlacePlayers(); diff --git a/server/src/main/java/fr/lnl/game/server/games/package-info.java b/server/src/main/java/fr/lnl/game/server/games/package-info.java new file mode 100644 index 0000000..1cd178d --- /dev/null +++ b/server/src/main/java/fr/lnl/game/server/games/package-info.java @@ -0,0 +1,4 @@ +/** + * Game package, contains all classes and sub-packages mainly related game works + */ +package fr.lnl.game.server.games; \ No newline at end of file diff --git a/server/src/test/java/fr/lnl/game/server/mock/Mock.java b/server/src/test/java/fr/lnl/game/server/mock/Mock.java index e8429dd..e5fba08 100644 --- a/server/src/test/java/fr/lnl/game/server/mock/Mock.java +++ b/server/src/test/java/fr/lnl/game/server/mock/Mock.java @@ -14,7 +14,7 @@ public class Mock { public Grid grid; public Mock(List players) { - this.buildStrategy = MockGridFactoryBuilder.create().gridDimensions(16, 16).playersList(players).wallProbability(0.80F).energyProbability(0.95F); + this.buildStrategy = MockGridFactoryBuilder.create().gridDimensions(16, 16).wallProbability(0.80F).energyProbability(0.95F); game = new Game(buildStrategy, players); this.grid = game.getGrid(); }