From 8a35d22b583262c5b1a554aa5aa662394a10c86f Mon Sep 17 00:00:00 2001 From: Katchan Date: Thu, 28 Oct 2021 23:21:53 +0200 Subject: [PATCH] Add basis Play test and Shot test --- .../java/fr/lnl/game/server/games/Game.java | 5 ++- .../fr/lnl/game/server/games/grid/Grid.java | 20 ++++++++---- .../fr/lnl/game/server/ActionPlayerTest.java | 17 ++++++++++ .../java/fr/lnl/game/server/GridTest.java | 31 ++++++++++++------- .../test/java/fr/lnl/game/server/Mock.java | 8 +++-- 5 files changed, 59 insertions(+), 22 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 fa52263..473af24 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 @@ -38,13 +38,16 @@ public class Game { /** * Change player to the next available in the list */ - public void nextCurrentPlayer() { + public boolean nextCurrentPlayer() { + if(isOver()) + return false; do { int index = players.indexOf(currentPlayer) + 1; if(index == players.size()) index = 0; currentPlayer = players.get(index); } while(!currentPlayer.isAlive()); // On arrête la boucle dès qu'on trouve un joueur en vie + return true; } public void setCurrent_player(Player current_player) { 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 ce0b559..61da319 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 @@ -68,8 +68,16 @@ public class Grid { board.get(new Point(14,2)).setB(new Wall(Cardinal.WEST,14,2)); } - 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; + public boolean boardPositionIsValid(int row, int deltaRow, int column, int deltaColumn){ + return row + deltaRow >= 0 && row + deltaRow < this.row && column + deltaColumn >= 0 && column + deltaColumn < this.column; + } + + public boolean boardHorizontalIsValid(int column, int deltaColumn){ + return column + deltaColumn >= 0 && column + deltaColumn < this.column; + } + + public boolean boardVerticalIsValid(int row, int deltaRow){ + return row + deltaRow >= 0 && row + deltaRow < this.row; } public HashMap> getBoard() { @@ -106,16 +114,16 @@ public class Grid { } } else if(value.getB() instanceof EnergyBall){ - str.append(" \033[0;31mO\033[0m"); + str.append(" \033[0;31mE\033[0m"); } else if(value.getB() instanceof Mine){ - str.append(" \033[0;31mX\033[0m"); + str.append(" \033[0;35mM\033[0m"); } else if(value.getB() instanceof Bomb){ - str.append(" \033[0;31mI\033[0m"); + str.append(" \033[0;36mB\033[0m"); } else { - str.append(" \033[0;31m.\033[0m"); + str.append(" \033[0;37m.\033[0m"); } } } 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 90e905c..cf3a551 100644 --- a/server/src/test/java/fr/lnl/game/server/ActionPlayerTest.java +++ b/server/src/test/java/fr/lnl/game/server/ActionPlayerTest.java @@ -1,11 +1,15 @@ package fr.lnl.game.server; import fr.lnl.game.server.games.Game; +import fr.lnl.game.server.games.action.Shot; import fr.lnl.game.server.games.grid.Grid; +import fr.lnl.game.server.utils.Point; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import java.util.List; + public class ActionPlayerTest { private Grid grid; @@ -25,4 +29,17 @@ public class ActionPlayerTest { Assertions.assertEquals(game.getPlayers().get(0), game.getCurrentPlayer()); } + + // TODO: 10/28/2021 pas un vrai test et marche qu'avec le mock actuel + @Test + public void shotActionTest(){ + System.out.println(grid.toString()); + Shot shot = new Shot(game); + List points = shot.getValidPoint(); + System.out.println(points); + System.out.println("Before shot " + game.getPlayers().get(1).getEnergy()); + shot.doAction(); + System.out.println("After shot " + game.getPlayers().get(1).getEnergy()); + } + } 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 815f1e5..a85a146 100644 --- a/server/src/test/java/fr/lnl/game/server/GridTest.java +++ b/server/src/test/java/fr/lnl/game/server/GridTest.java @@ -13,7 +13,7 @@ import org.junit.jupiter.api.Test; import java.util.Random; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; public class GridTest { @@ -42,28 +42,35 @@ 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)}); + player.setActions(new Action[]{new Nothing(game), new Shot(game), new Move(game), + new DeployShield(game), new DropBomb(game), new DropMine(game)}); } - System.out.println(game.getGrid().toString()); while (!game.isOver()){ - Random random = new Random(); + System.out.println(" Tour du joueur " + game.getCurrentPlayer().getId() + " : " + + game.getCurrentPlayer().getEnergy() + " points de vies restants"); + System.out.println(game.getGrid().toString()); Action action = null; - while ((action == null || !action.isPossible()) && game.getCurrentPlayer().getActions().length != 0) { - if(game.getCurrentPlayer().getActions().length == 1) { - action = game.getCurrentPlayer().getActions()[0]; - } else { - action = game.getCurrentPlayer().getActions()[random.nextInt(0,game.getCurrentPlayer().getActions().length -1)]; + switch (game.getCurrentPlayer().getActions().length){ + case 0 -> action = new Nothing(game); + case 1 -> action = game.getCurrentPlayer().getActions()[0]; + default -> { + Random random = new Random(); + while (action == null || !action.isPossible()) { + action = game.getCurrentPlayer().getActions() + [random.nextInt(0,game.getCurrentPlayer().getActions().length)]; + } } - } action.doAction(); - System.out.println(game.getGrid().toString()); + System.out.println("Action " + action + " : " + game.getCurrentPlayer().getEnergy() + + " points de vies restants"); game.nextCurrentPlayer(); - } + System.out.println(game.getGrid().toString()); System.out.println("Le joueur gagnant : " + game.getWinner().getId()); } 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 1ab5628..aedaf87 100644 --- a/server/src/test/java/fr/lnl/game/server/Mock.java +++ b/server/src/test/java/fr/lnl/game/server/Mock.java @@ -29,13 +29,15 @@ public class Mock { } public void placePlayersBRUT(){ - grid.getBoard().get(new Point(1,1)).setA(grid.getPlayers().get(0)); - grid.getBoard().get(new Point(14,14)).setA(grid.getPlayers().get(1)); + grid.getBoard().get(new Point(7,7)).setA(grid.getPlayers().get(0)); + grid.getPlayers().get(0).setPoint(new Point(7, 7)); + grid.getBoard().get(new Point(7,8)).setA(grid.getPlayers().get(1)); + grid.getPlayers().get(1).setPoint(new Point(7, 8)); } public void placeEnergyBallBRUT(){ grid.getBoard().get(new Point(2,3)).setB(new EnergyBall()); - grid.getBoard().get(new Point(7,10)).setB(new EnergyBall()); + grid.getBoard().get(new Point(8,10)).setB(new EnergyBall()); } public void placeInternWallBRUT(){