diff --git a/server/build.gradle b/server/build.gradle index 87df045..2e4b59d 100644 --- a/server/build.gradle +++ b/server/build.gradle @@ -7,7 +7,16 @@ plugins { id 'fr.lnl.game.java-library-conventions' } +dependencies { + testImplementation 'org.junit.jupiter:junit-jupiter-api:5.3.1' + testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.1' +} + application { mainClassName = 'fr.lnl.game.server.games.grid.Grid' } +test { + useJUnitPlatform() +} + 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 7b018b7..7ecfbfd 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 @@ -1,4 +1,11 @@ package fr.lnl.game.server.games.grid; 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 + } } 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 8ccee05..a89b997 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 @@ -49,16 +49,28 @@ public class Grid { } } + /** + * @deprecated A utiliser seulement pour les tests, voir GridTest dans le dossier test + */ + @Deprecated public void placePlayersBRUT(){ board.get(new Point(1,1)).setA(players[0]); board.get(new Point(14,14)).setA(players[1]); } + /** + * @deprecated idem que {@link Grid#placePlayersBRUT()} + */ + @Deprecated public void placeEnergyBallBRUT(){ board.get(new Point(2,3)).setB(new EnergyBall()); board.get(new Point(7,10)).setB(new EnergyBall()); } + /** + * @deprecated idem que {@link Grid#placePlayersBRUT()} + */ + @Deprecated public void placeInternWallBRUT(){ board.get(new Point(3,6)).setB(new Wall(Cardinal.NORTH,3,6)); board.get(new Point(7,14)).setB(new Wall(Cardinal.SOUTH,7,14)); @@ -74,46 +86,69 @@ public class Grid { return board; } - public void printGrid() { + @Override + public String toString() { + StringBuilder str = new StringBuilder(); for (int i = 0; i < row; i++) { - System.out.print("\n"); + str.append("\n"); for (int j = 0; j < column; j++) { Pair value = board.get(new Point(i, j)); if(value.getA() != null){ - System.out.print(" \033[0;34mP\033[0m"); + str.append(" \033[0;34mP\033[0m"); } else if (value.getB() instanceof Wall) { if (((Wall) value.getB()).getCardinal() == Cardinal.NORTH) { - System.out.print(" \033[0;34m—\033[0m"); + str.append(" \033[0;34m—\033[0m"); } else if (((Wall) value.getB()).getCardinal() == Cardinal.SOUTH) { - System.out.print(" \033[0;31m—\033[0m"); + str.append(" \033[0;31m—\033[0m"); } else if (((Wall) value.getB()).getCardinal() == Cardinal.WEST) { - System.out.print(" \033[0;33m|\033[0m"); + str.append(" \033[0;33m|\033[0m"); } else if (((Wall) value.getB()).getCardinal() == Cardinal.EAST) { - System.out.print(" \033[0;32m|\033[0m"); + str.append(" \033[0;32m|\033[0m"); } else if (((Wall) value.getB()).getCardinal() == Cardinal.NORTH_EAST) { - System.out.print(" \033[0;32mN\033[0m"); + str.append(" \033[0;32mN\033[0m"); } else if (((Wall) value.getB()).getCardinal() == Cardinal.NORTH_WEST) { - System.out.print(" \033[0;33mN\033[0m"); + str.append(" \033[0;33mN\033[0m"); } else if (((Wall) value.getB()).getCardinal() == Cardinal.SOUTH_EAST) { - System.out.print(" \033[0;32mS\033[0m"); + str.append(" \033[0;32mS\033[0m"); } else if (((Wall) value.getB()).getCardinal() == Cardinal.SOUTH_WEST) { - System.out.print(" \033[0;33mS\033[0m"); + str.append(" \033[0;33mS\033[0m"); } } else if(value.getB() instanceof EnergyBall){ - System.out.print(" \033[0;31mO\033[0m"); + str.append(" \033[0;31mO\033[0m"); } else if(value.getB() instanceof Mine){ - System.out.print(" \033[0;31mX\033[0m"); + str.append(" \033[0;31mX\033[0m"); } else if(value.getB() instanceof Bomb){ - System.out.print(" \033[0;31mI\033[0m"); + str.append(" \033[0;31mI\033[0m"); } else { - System.out.print(" \033[0;31m.\033[0m"); + str.append(" \033[0;31m.\033[0m"); } } } + return str.toString(); + } + + /** + * @deprecated modèle mvc non respecté + */ + @Deprecated + public void printGrid() { + System.out.println(this); + } + + public Player[] getPlayers() { + return players; + } + + public int getRow() { + return row; + } + + public int getColumn() { + return column; } } 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 06208d4..7dfb7df 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 @@ -2,6 +2,8 @@ package fr.lnl.game.server.games.grid; import fr.lnl.game.server.utils.Cardinal; +import java.util.Objects; + public class Wall implements Box { private Cardinal cardinal; @@ -25,4 +27,12 @@ public class Wall implements Box { public int getY() { return y; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Wall other = (Wall) o; + return Objects.equals(cardinal, other.cardinal) && x == other.x && y == other.y; + } } diff --git a/server/src/test/java/fr/lnl/game/server/GridTest.java b/server/src/test/java/fr/lnl/game/server/GridTest.java new file mode 100644 index 0000000..06fbd22 --- /dev/null +++ b/server/src/test/java/fr/lnl/game/server/GridTest.java @@ -0,0 +1,58 @@ +package fr.lnl.game.server; + +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.ComputerPlayer; +import fr.lnl.game.server.games.player.DefaultClassPlayer; +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.Test; + +import static org.junit.jupiter.api.Assertions.*; + +public class GridTest { + + private Grid grid; + + @Test + public void testGrid() { + Player playerOne = new ComputerPlayer(1,null, new DefaultClassPlayer()); + Player playerTwo = new ComputerPlayer(2,null, new DefaultClassPlayer()); + this.grid = new Grid(16,16,new Player[]{playerOne,playerTwo}); + grid.initGrid(); + placePlayersBRUT(); + placeEnergyBallBRUT(); + placeInternWallBRUT(); + // test Grid#initGrid() + assertEquals(new Wall(Cardinal.NORTH_WEST, 0, 0), grid.getBoard().get(new Point(0,0)).getB()); + assertEquals(new Wall(Cardinal.NORTH_EAST, 0, grid.getColumn() - 1), grid.getBoard().get(new Point(0, grid.getColumn() - 1)).getB()); + 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()); + // 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()); + } + + 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]); + } + + public void placeEnergyBallBRUT(){ + grid.getBoard().get(new Point(2,3)).setB(new EnergyBall()); + grid.getBoard().get(new Point(7,10)).setB(new EnergyBall()); + } + + public void placeInternWallBRUT(){ + grid.getBoard().get(new Point(3,6)).setB(new Wall(Cardinal.NORTH,3,6)); + grid.getBoard().get(new Point(7,14)).setB(new Wall(Cardinal.SOUTH,7,14)); + grid.getBoard().get(new Point(10,7)).setB(new Wall(Cardinal.EAST,10,7)); + grid.getBoard().get(new Point(14,2)).setB(new Wall(Cardinal.WEST,14,2)); + } + +}