From b702a4030fc0b0dc8db0b7790574e0a93dda6ee1 Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Fri, 26 Nov 2021 16:04:27 +0100 Subject: [PATCH] Reworked dropBomb and DropMine + reordered tests --- .../java/fr/lnl/game/server/games/Game.java | 23 ++++++++ .../game/server/games/action/DropBomb.java | 18 +----- .../game/server/games/action/DropMine.java | 17 +----- .../game/server/games/action/DropObject.java | 11 +++- .../fr/lnl/game/server/ActionPlayerTest.java | 19 +++++++ .../java/fr/lnl/game/server/GridTest.java | 42 -------------- .../test/java/fr/lnl/game/server/Mock.java | 4 +- .../fr/lnl/game/server/games/GameTest.java | 55 +++++++++++++++++++ 8 files changed, 114 insertions(+), 75 deletions(-) create mode 100644 server/src/test/java/fr/lnl/game/server/games/GameTest.java 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 fdd9f35..e73283b 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 @@ -1,8 +1,11 @@ package fr.lnl.game.server.games; +import fr.lnl.game.server.games.action.*; import fr.lnl.game.server.games.grid.Grid; import fr.lnl.game.server.games.player.Player; +import java.util.ArrayList; +import java.util.Arrays; import java.util.List; public class Game { @@ -23,6 +26,26 @@ public class Game { } + protected List generateAndGetPlayerActions(Player player) { + List actions = new ArrayList<>(); + for(Direction direction : Direction.values()) { + try { + actions.add(new Move(this, player, direction)); + } catch (NotValidDirectionException ignored){} + try { + new DropBomb(this, player, direction); + } catch (NotValidDirectionException ignored) {} + try { + new DropMine(this, player, direction); + } catch (NotValidDirectionException ignored) {} + try { + actions.add(new Shot(this, player, direction)); + } catch (NotValidDirectionException | NoMoreBulletInWeaponException ignored) {} + } + actions.addAll(Arrays.asList(new Nothing(), new DeployShield(player))); + return actions; + } + public boolean isOver() { return players.parallelStream().filter(player -> !player.isAlive()).count() == 1; } diff --git a/server/src/main/java/fr/lnl/game/server/games/action/DropBomb.java b/server/src/main/java/fr/lnl/game/server/games/action/DropBomb.java index 5fe8ad5..8c6c4df 100644 --- a/server/src/main/java/fr/lnl/game/server/games/action/DropBomb.java +++ b/server/src/main/java/fr/lnl/game/server/games/action/DropBomb.java @@ -3,24 +3,15 @@ package fr.lnl.game.server.games.action; import fr.lnl.game.server.games.Game; import fr.lnl.game.server.games.grid.Bomb; import fr.lnl.game.server.games.player.Player; -import fr.lnl.game.server.utils.Point; - -import java.util.List; public class DropBomb extends DropObject { - public DropBomb(Game game, Player player){ - super(game, player); + public DropBomb(Game game, Player player, Direction direction) throws NotValidDirectionException { + super(game, player, direction); } - /** - * @deprecated même principe que {@link Shot#doAction()} - */ - @Deprecated @Override public void doAction() { - List points = getValidPoint(); - Point point = choseRandomPoint(points); game.getGrid().getBoard().get(point).setB(new Bomb()); player.decrementEnergy(player.getClassPlayer().getBombCost()); } @@ -30,10 +21,5 @@ public class DropBomb extends DropObject { return super.isPossible(); } - @Override - public List getValidPoint() { - return super.getValidPoint(); - } - } 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 f22615e..5ec88f5 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 @@ -9,8 +9,9 @@ import java.util.List; public class DropMine extends DropObject { - public DropMine(Game game, Player player){ - super(game, player); + public DropMine(Game game, Player player, Direction direction) throws NotValidDirectionException { + super(game, player, direction); + } /** @@ -19,20 +20,8 @@ public class DropMine extends DropObject { @Deprecated @Override public void doAction() { - List points = getValidPoint(); - Point point = choseRandomPoint(points); game.getGrid().getBoard().get(point).setB(new Mine()); game.getCurrentPlayer().decrementEnergy(player.getClassPlayer().getMineCost()); } - @Override - public boolean isPossible() { - return super.isPossible(); - } - - @Override - public List getValidPoint() { - return super.getValidPoint(); - } - } diff --git a/server/src/main/java/fr/lnl/game/server/games/action/DropObject.java b/server/src/main/java/fr/lnl/game/server/games/action/DropObject.java index 1eb982f..5c96754 100644 --- a/server/src/main/java/fr/lnl/game/server/games/action/DropObject.java +++ b/server/src/main/java/fr/lnl/game/server/games/action/DropObject.java @@ -12,8 +12,17 @@ import java.util.List; public abstract class DropObject extends AbstractAction { - public DropObject(Game game, Player player){ + protected final Point point; + + public DropObject(Game game, Player player, Direction direction) throws NotValidDirectionException { super(game, player); + List points = getValidPoint(); + Point playerPosition = player.getPoint(); + Point dropDirection = new Point(playerPosition.getA() + direction.getDeltaX(), playerPosition.getB() + direction.getDeltaY()); + if(!points.contains(dropDirection)) { + throw new NotValidDirectionException(direction + " isn't a valid position"); + } + this.point = dropDirection; } @Override 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 a5f1bcd..f9f5f98 100644 --- a/server/src/test/java/fr/lnl/game/server/ActionPlayerTest.java +++ b/server/src/test/java/fr/lnl/game/server/ActionPlayerTest.java @@ -2,6 +2,7 @@ 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.Bomb; import fr.lnl.game.server.games.grid.Grid; import fr.lnl.game.server.games.player.Player; import fr.lnl.game.server.utils.Point; @@ -71,4 +72,22 @@ public class ActionPlayerTest { Assertions.assertEquals(currentEnergyOtherPlayer - otherPlayer.getClassPlayer().getPenaltyShoot(), otherPlayer.getEnergy()); } + @Test + public void dropBombActionTest() { + Player player = game.getCurrentPlayer(); + Action action = null; + Direction savedDirection = null; + for(Direction direction : Direction.values()) { + try { + action = new DropBomb(game, game.getCurrentPlayer(), direction); + savedDirection = direction; + break; + } catch (NotValidDirectionException ignored) {} + } + Assertions.assertNotNull(action); + action.doAction(); + Point bombPosition = new Point(player.getPoint().getA() + savedDirection.getDeltaX(), player.getPoint().getB() + savedDirection.getDeltaY()); + Assertions.assertTrue(game.getGrid().getBoard().get(bombPosition).getB() instanceof Bomb); + } + } 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 97665ba..244f38b 100644 --- a/server/src/test/java/fr/lnl/game/server/GridTest.java +++ b/server/src/test/java/fr/lnl/game/server/GridTest.java @@ -44,46 +44,4 @@ public class GridTest { assertEquals(new EnergyBall(), grid.getBoard().get(new Point(8, 10)).getB()); } - @Test - public void testPlay(){ - while (!game.isOver()){ - System.out.println(" Tour du joueur " + game.getCurrentPlayer().getId() + " : " + - game.getCurrentPlayer().getEnergy() + " points de vies restants"); - Player player = game.getCurrentPlayer(); - ArrayList actions = new ArrayList<>(); - for(Direction direction : Direction.values()) { - try { - actions.add(new Move(game, player, direction)); - } catch (NotValidDirectionException ignored){} - try { - actions.add(new Shot(game, player, direction)); - } catch (NotValidDirectionException | NoMoreBulletInWeaponException ignored) {} - } - actions.addAll(Arrays.asList(new Nothing(), new DeployShield(player), new DropBomb(game, player), - new DropMine(game, player))); - player.setActions(actions); - System.out.println(game.getGrid().toString()); - Action action = null; - switch (player.getActions().size()){ - case 0 -> action = new Nothing(); - case 1 -> action = game.getCurrentPlayer().getActions().get(0); - default -> { - Random random = new Random(); - while (action == null || !action.isPossible()) { - action = game.getCurrentPlayer().getActions().get( - random.nextInt(0,game.getCurrentPlayer().getActions().size()) - ); - } - } - } - action.doAction(); - System.out.println("Action " + action + " : " + game.getCurrentPlayer().getEnergy() + - " points de vies restants"); - game.nextCurrentPlayer(); - } - System.out.println(game.getGrid().toString()); - Player winner = game.getWinner(); - System.out.println(winner != null ? ("Le joueur gagnant : " + winner.getId()) : ("Partie nulle, aucun gagnant")); - } - } 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 8f09f38..2396f52 100644 --- a/server/src/test/java/fr/lnl/game/server/Mock.java +++ b/server/src/test/java/fr/lnl/game/server/Mock.java @@ -15,8 +15,8 @@ import java.util.List; public class Mock { - Grid grid; - Game game; + public Grid grid; + public Game game; public Mock() { List players = Arrays.asList(new RandomComputerPlayer(1,null, ClassPlayer.DEFAULT), diff --git a/server/src/test/java/fr/lnl/game/server/games/GameTest.java b/server/src/test/java/fr/lnl/game/server/games/GameTest.java new file mode 100644 index 0000000..3673011 --- /dev/null +++ b/server/src/test/java/fr/lnl/game/server/games/GameTest.java @@ -0,0 +1,55 @@ +package fr.lnl.game.server.games; + +import fr.lnl.game.server.Mock; +import fr.lnl.game.server.games.action.Action; +import fr.lnl.game.server.games.action.Nothing; +import fr.lnl.game.server.games.grid.Grid; +import fr.lnl.game.server.games.player.Player; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.Random; + +public class GameTest { + + private Grid grid; + private Game game; + + @BeforeEach + public void mock() { + Mock mock = new Mock(); + grid = mock.grid; + game = mock.game; + } + + @Test + public void testPlay(){ + while (!game.isOver()){ + System.out.println(" Tour du joueur " + game.getCurrentPlayer().getId() + " : " + + game.getCurrentPlayer().getEnergy() + " points de vies restants"); + Player player = game.getCurrentPlayer(); + player.setActions(game.generateAndGetPlayerActions(player)); + System.out.println(game.getGrid().toString()); + Action action = null; + switch (player.getActions().size()){ + case 0 -> action = new Nothing(); + case 1 -> action = game.getCurrentPlayer().getActions().get(0); + default -> { + Random random = new Random(); + while (action == null || !action.isPossible()) { + action = game.getCurrentPlayer().getActions().get( + random.nextInt(0,game.getCurrentPlayer().getActions().size()) + ); + } + } + } + action.doAction(); + System.out.println("Action " + action + " : " + game.getCurrentPlayer().getEnergy() + + " points de vies restants"); + game.nextCurrentPlayer(); + } + System.out.println(game.getGrid().toString()); + Player winner = game.getWinner(); + System.out.println(winner != null ? ("Le joueur gagnant : " + winner.getId()) : ("Partie nulle, aucun gagnant")); + } +}