From f6115091756849a32f8b91b36410d4ea227d6a79 Mon Sep 17 00:00:00 2001 From: Katchan Date: Fri, 22 Oct 2021 13:03:14 +0200 Subject: [PATCH] Add playtest methode and diverses attributes and methods. Refactor isPossible method --- .../java/fr/lnl/game/server/games/Game.java | 25 ++++++++----- .../server/games/action/DeployShield.java | 2 +- .../game/server/games/action/DropMine.java | 2 +- .../fr/lnl/game/server/games/action/Move.java | 4 +-- .../lnl/game/server/games/action/Nothing.java | 1 - .../fr/lnl/game/server/games/grid/Grid.java | 2 +- .../server/games/player/AbstractPlayer.java | 4 +++ .../lnl/game/server/games/player/Player.java | 1 + .../fr/lnl/game/server/ActionPlayerTest.java | 2 +- .../java/fr/lnl/game/server/GridTest.java | 36 +++++++++++++++++++ 10 files changed, 63 insertions(+), 16 deletions(-) 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..49dd800 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,35 +3,34 @@ 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.ArrayList; +import java.util.Arrays; + public class Game { Grid grid; Player player_One; Player player_Two; Player current_player; - Player[] players; + ArrayList 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; this.grid = grid; - players = new Player[]{player_One, player_Two}; - } - - public void play(){ - + players = new ArrayList(Arrays.asList(player_One, player_Two)); } public boolean isOver(){ - return players.length == 1; + return players.size() == 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 players.get(0); } return null; } @@ -40,11 +39,19 @@ public class Game { return current_player; } + public void setCurrent_player(Player current_player) { + this.current_player = current_player; + } + + public void decrementPlayers(Player player){ + players.remove(player); + } + public Grid getGrid() { return grid; } - public Player[] getPlayers() { + public ArrayList getPlayers() { return players; } } \ No newline at end of file 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 4fec118..b3b85c5 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 @@ -10,7 +10,7 @@ public class DeployShield extends AbstractAction { @Override public void doAction(){ getGame().getCurrentPlayer().setShieldDeploy(true); - //TO-DO retirer les point du player + getGame().getCurrentPlayer().decrementEnergy(getGame().getCurrentPlayer().getClassPlayer().getShieldCost()); } @Override diff --git a/server/src/main/java/fr/lnl/game/server/games/action/DropMine.java b/server/src/main/java/fr/lnl/game/server/games/action/DropMine.java index 7d020ba..28ab389 100644 --- a/server/src/main/java/fr/lnl/game/server/games/action/DropMine.java +++ b/server/src/main/java/fr/lnl/game/server/games/action/DropMine.java @@ -18,7 +18,7 @@ public class DropMine extends DropObject { Point point = points.get(random.nextInt(0,points.size()-1)); Mine mine = new Mine(); getGame().getGrid().getBoard().get(point).setB(mine); - //TO-DO : retirer les points au player + getGame().getCurrentPlayer().decrementEnergy(getGame().getCurrentPlayer().getClassPlayer().getMineCost()); } @Override 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 0651104..0eb36fe 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 @@ -31,7 +31,7 @@ public class Move extends AbstractAction { @Override public boolean isPossible() { - return getValidPoint().isEmpty(); + return !getValidPoint().isEmpty(); } public List getValidPoint() { @@ -40,7 +40,7 @@ public class Move extends AbstractAction { Point position = getGame().getCurrentPlayer().getPoint(); for (int row = -1; row <= 1; row++) { for (int column = -1; column <= 1; column++) { - if(row == getGame().getGrid().getRow() || column == getGame().getGrid().getColumn()){ + if(row == position.getA() + row || column == position.getB() + column){ if(Grid.caseisValid(position.getA(),row,position.getB(),column)){ Point neighbour = new Point(position.getA() + row, position.getB() + column); Pair state = board.get(neighbour); 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 fa87d98..2b71ed0 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 @@ -9,7 +9,6 @@ public class Nothing extends AbstractAction { } @Override public void doAction(){ - } @Override 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..4fd1a30 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 @@ -67,7 +67,7 @@ public class Grid { } public static boolean caseisValid(int row, int column, int deltaRow, int deltaColumn){ - return row + deltaRow >= 0 && row + deltaRow <= row && column + deltaColumn >= 0 && column + deltaColumn <= column; + return row + deltaRow >= 0 && row + deltaRow < row && column + deltaColumn >= 0 && column + deltaColumn < column; } public HashMap> getBoard() { 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 83e2763..7dd2299 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 @@ -59,6 +59,10 @@ public abstract class AbstractPlayer implements Player { return actions; } + public void setActions(Action[] actions){ + this.actions = actions; + } + public ClassPlayer getClassPlayer() { return classPlayer; } 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 b858e5e..bbfffcc 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 @@ -15,6 +15,7 @@ public interface Player { void setShieldDeploy(boolean shieldDeploy); void setWeapon(Weapon weapon); Action[] getActions(); + void setActions(Action[] actions); ClassPlayer getClassPlayer(); void setPoint(Point point); void decrementEnergy(int energy); 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..b7ae83e 100644 --- a/server/src/test/java/fr/lnl/game/server/GridTest.java +++ b/server/src/test/java/fr/lnl/game/server/GridTest.java @@ -1,23 +1,30 @@ package fr.lnl.game.server; +import fr.lnl.game.server.games.Game; +import fr.lnl.game.server.games.action.*; 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.Player; import fr.lnl.game.server.utils.Cardinal; import fr.lnl.game.server.utils.Point; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import java.util.Random; + import static org.junit.jupiter.api.Assertions.*; public class GridTest { private Grid grid; + private Game game; @BeforeEach public void mock() { Mock mock = new Mock(); grid = mock.grid; + game = mock.game; } @Test @@ -35,4 +42,33 @@ public class GridTest { assertEquals(new EnergyBall(), grid.getBoard().get(new Point(7, 10)).getB()); } + @Test + public void testPlay(){ + for (Player player: game.getPlayers()) { + player.setActions(new Action[]{new Move(game)}); + } + System.out.println(game.getGrid().toString()); + while (!game.isOver()){ + Random random = new Random(); + Action action = null; + do { + action = game.getCurrentPlayer().getActions()[random.nextInt(0,game.getCurrentPlayer().getActions().length -1)]; + }while (!action.isPossible()); + action.doAction(); + System.out.println(game.getGrid().toString()); + if(game.getCurrentPlayer().getEnergy() <= 0){ + game.decrementPlayers(game.getCurrentPlayer()); + } + else{ + if(game.getCurrentPlayer() == game.getPlayers().get(0)){ + game.setCurrent_player(game.getPlayers().get(1)); + } + else{ + game.setCurrent_player(game.getPlayers().get(0)); + } + } + } + System.out.println("Le joueur gagnant : " + game.getWinner().getId()); + } + }