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 7285a36..fa52263 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 @@ -47,6 +47,10 @@ public class Game { } while(!currentPlayer.isAlive()); // On arrête la boucle dès qu'on trouve un joueur en vie } + public void setCurrent_player(Player current_player) { + this.currentPlayer = current_player; + } + public Grid getGrid() { return grid; } 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 e35113a..85d473e 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 @@ -17,9 +17,9 @@ public class DropMine extends DropObject { List points = getValidPoint(); Random random = new Random(); Point point = points.get(random.nextInt(0,points.size()-1)); - getGame().getGrid().getBoard().get(point).setB(new Mine()); - Player player = getGame().getCurrentPlayer(); - player.decrementEnergy(player.getClassPlayer().getMineCost()); + Mine mine = new Mine(); + getGame().getGrid().getBoard().get(point).setB(mine); + 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 2ba60ba..ce0b559 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 @@ -69,7 +69,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 c4d61f6..e174fb5 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/GridTest.java b/server/src/test/java/fr/lnl/game/server/GridTest.java index bf26d86..783f917 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()); + } + }