diff --git a/server/src/main/java/fr/lnl/game/server/games/action/Direction.java b/server/src/main/java/fr/lnl/game/server/games/action/Direction.java new file mode 100644 index 0000000..852519e --- /dev/null +++ b/server/src/main/java/fr/lnl/game/server/games/action/Direction.java @@ -0,0 +1,31 @@ +package fr.lnl.game.server.games.action; + +public enum Direction { + + UP(-1, 0, true), + DOWN(1, 0, true), + LEFT(0, -1, false), + RIGHT(0, 1, false); + + private final int deltaX; + private final int deltaY; + private final boolean isVertical; + + Direction(int i, int i1, boolean isVertical) { + this.deltaX = i; + this.deltaY = i1; + this.isVertical = isVertical; + } + + public int getDeltaX() { + return deltaX; + } + + public int getDeltaY() { + return deltaY; + } + + public boolean isVertical() { + return isVertical; + } +} 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 e4dd8d3..8b01a15 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 @@ -19,9 +19,9 @@ public class Move extends AbstractAction { public Move(Game game, Player player, Direction direction) throws NotValidDirectionException { super(game, player); - HashSet points = new HashSet<>(getValidPoint()); + List points = getValidPoint(); Point playerPosition = player.getPoint(); - Point newPosition = new Point(playerPosition.getA() + direction.deltaX, playerPosition.getB() + direction.deltaY); + Point newPosition = new Point(playerPosition.getA() + direction.getDeltaX(), playerPosition.getB() + direction.getDeltaY()); if(!points.contains(newPosition)) { throw new NotValidDirectionException(direction + " isn't a valid position"); } @@ -65,28 +65,4 @@ public class Move extends AbstractAction { } return listMoves; } - - public enum Direction { - - UP(-1, 0), - DOWN(1, 0), - LEFT(0, -1), - RIGHT(-1, 0); - - private final int deltaX; - private final int deltaY; - - Direction(int i, int i1) { - this.deltaX = i; - this.deltaY = i1; - } - - public int getDeltaX() { - return deltaX; - } - - public int getDeltaY() { - return deltaY; - } - } } diff --git a/server/src/main/java/fr/lnl/game/server/games/action/NoMoreBulletInWeaponException.java b/server/src/main/java/fr/lnl/game/server/games/action/NoMoreBulletInWeaponException.java new file mode 100644 index 0000000..b7cdb76 --- /dev/null +++ b/server/src/main/java/fr/lnl/game/server/games/action/NoMoreBulletInWeaponException.java @@ -0,0 +1,9 @@ +package fr.lnl.game.server.games.action; + +public class NoMoreBulletInWeaponException extends Exception { + + public NoMoreBulletInWeaponException() { + super(); + } + +} diff --git a/server/src/main/java/fr/lnl/game/server/games/action/Shot.java b/server/src/main/java/fr/lnl/game/server/games/action/Shot.java index bf77c75..ca69e49 100644 --- a/server/src/main/java/fr/lnl/game/server/games/action/Shot.java +++ b/server/src/main/java/fr/lnl/game/server/games/action/Shot.java @@ -7,24 +7,44 @@ import fr.lnl.game.server.games.weapon.Weapon; import fr.lnl.game.server.utils.Point; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; public class Shot extends AbstractAction { - public Shot(Game game, Player player) { + private final Point point; + private final Direction direction; + + public Shot(Game game, Player player, Direction direction) throws NoMoreBulletInWeaponException, NotValidDirectionException { super(game, player); + if(player.getWeapon().getBullet() == 0) { + throw new NoMoreBulletInWeaponException(); + } + List points = getValidPoint(); + Point playerPosition = player.getPoint(); + Point shotDirection = new Point(playerPosition.getA() + direction.getDeltaX(), playerPosition.getB() + direction.getDeltaY()); + if(!points.contains(shotDirection)) { + throw new NotValidDirectionException(direction + " isn't a valid position"); + } + this.point = shotDirection; + this.direction = direction; } - /** - * @deprecated a rewrite -> L'aléatoire ne devrait pas être ici, mais au moment de l'instanciation par exemple - * comme dans {@link Move} - */ - @Deprecated @Override public void doAction() { player.decrementEnergy(player.getClassPlayer().getShootCost()); - game.getGrid().getBoard().get(choseRandomPoint(getValidPoint())).getA() - .decrementEnergy(player.getClassPlayer().getPenaltyShoot()); + int range = direction.isVertical() ? player.getWeapon().getVerticalDistance() : player.getWeapon().getHorizontalDistance(); + for(int i=0; i < range; i++) { + Point point = new Point(this.point.getA() + (i * direction.getDeltaX()), + this.point.getB() + (i * direction.getDeltaY())); + Player player = game.getGrid().getBoard().get(point).getA(); + if(player != null) { + player.decrementEnergy(player.getClassPlayer().getPenaltyShoot()); + System.out.println("Not null: " + point); + } else { + System.out.println("null:" + point); + } + } } @Override @@ -32,48 +52,36 @@ public class Shot extends AbstractAction { return !getValidPoint().isEmpty(); } - /** - * @deprecated voir {@link Shot#doAction()}, surement renommé en isValidPoint(Point): bool après rework - */ - @Deprecated(forRemoval = true, since = "07/11/2021") @Override public List getValidPoint() { List listMoves = new ArrayList<>(); Point position = game.getCurrentPlayer().getPoint(); Weapon weapon = game.getCurrentPlayer().getWeapon(); - for (int delta = -1; delta <= 1; delta++) { - if(delta != 0){ - Point verticalNeibourg = seeNeibourg(position,delta,weapon.getVerticalDistance(),true); - if(verticalNeibourg != null){ - listMoves.add(verticalNeibourg); - } - Point horizontalNeibourg = seeNeibourg(position,delta,weapon.getHorizontalDistance(),false); - if(horizontalNeibourg != null){ - listMoves.add(horizontalNeibourg); - } - } + for(Direction direction : Direction.values()) { + Point neighbour = seeNeighbour(position, direction.getDeltaX(), direction.getDeltaY(), + direction.isVertical() ? weapon.getVerticalDistance() : weapon.getHorizontalDistance()); + if(neighbour != null) + listMoves.add(neighbour); } return listMoves; } - @Deprecated(since = "07/11/2021", forRemoval = true) - public Point seeNeibourg(Point point, int delta, int range, boolean isVertical) { - Point neibourg = null; - if (isVertical) { - if (game.getGrid().boardVerticalIsValid(point.getA(), delta)) { - neibourg = new Point(point.getA() + delta, point.getB()); - } - } else { - if (game.getGrid().boardHorizontalIsValid(point.getB(), delta)) { - neibourg = new Point(point.getA(), point.getB() + delta); - } - } - if (game.getGrid().getBoard().get(neibourg).getB() instanceof Wall || range + delta < 0) { + + public Point seeNeighbour(Point point, int deltaX, int deltaY, int range) { + if(range == 0) return null; + for(int i = 0; i < range; i++) { + Point neighbour = new Point(point.getA() + deltaX + (i * deltaX), point.getB() + deltaY + (i * deltaY)); + if(game.getGrid().boardPositionIsValid(point)) { + if(game.getGrid().getBoard().get(neighbour).getB() instanceof Wall) { + return null; + } + if(game.getGrid().getBoard().get(neighbour).getA() instanceof Player) { + System.out.println(game.getGrid().getBoard().get(neighbour).getA().getPoint()); + return neighbour; + } + } } - if(game.getGrid().getBoard().get(neibourg).getA() instanceof Player){ - return neibourg; - } - return seeNeibourg(neibourg,delta,range - 1,isVertical); + return null; } } 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 61da319..4f0f11e 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,15 @@ public class Grid { } 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; + return boardPositionIsValid(row + deltaRow, column + deltaColumn); + } + + public boolean boardPositionIsValid(int row, int column) { + return row >= 0 && column >= 0 && row < this.row && column < this.column; + } + + public boolean boardPositionIsValid(Point point) { + return boardPositionIsValid(point.getA(), point.getB()); } public boolean boardHorizontalIsValid(int column, int deltaColumn){ diff --git a/server/src/main/java/fr/lnl/game/server/games/player/ClassPlayer.java b/server/src/main/java/fr/lnl/game/server/games/player/ClassPlayer.java index b51943a..08aa12f 100644 --- a/server/src/main/java/fr/lnl/game/server/games/player/ClassPlayer.java +++ b/server/src/main/java/fr/lnl/game/server/games/player/ClassPlayer.java @@ -5,7 +5,7 @@ import fr.lnl.game.server.games.weapon.Weapon; public enum ClassPlayer { - DEFAULT(800, 25, 40, 30, 40, 10, 80, 20, 20, 15, new Firearm()), + DEFAULT(800, 25, 20, 30, 40, 10, 80, 40, 20, 15, new Firearm()), TANK(1000, 20, 20, 17, 23, 13, 80, 27, 30, 22, new Firearm()), DPS(800, 25, 16, 15, 20, 10, 80, 40, 40, 30, new Firearm()), SUPPORT(600, 25, 20, 11, 15, 7, 80, 45, 45, 35, new Firearm()); 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 eb31671..a5f1bcd 100644 --- a/server/src/test/java/fr/lnl/game/server/ActionPlayerTest.java +++ b/server/src/test/java/fr/lnl/game/server/ActionPlayerTest.java @@ -9,8 +9,6 @@ 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; @@ -28,16 +26,15 @@ public class ActionPlayerTest { public void moveActionTest() { Action move = null; Point oldPoint = game.getCurrentPlayer().getPoint(); - Move.Direction savedDirection = null; - for(Move.Direction direction : Move.Direction.values()) { + Direction savedDirection = null; + for(Direction direction : Direction.values()) { try { move = new Move(game, game.getCurrentPlayer(), direction); savedDirection = direction; break; } catch (NotValidDirectionException ignored) {} } - Assertions.assertNotEquals(null, move); - assert move != null; + Assertions.assertNotNull(move); move.doAction(); Point newPoint = game.getCurrentPlayer().getPoint(); Assertions.assertEquals(newPoint, @@ -56,16 +53,22 @@ public class ActionPlayerTest { Assertions.assertTrue(player.isShieldDeploy()); } - // 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, game.getCurrentPlayer()); - List points = shot.getValidPoint(); - System.out.println(points); - System.out.println("Before shot " + game.getPlayers().get(1).getEnergy()); + Action shot = null; + for(Direction direction : Direction.values()) { + try { + shot = new Shot(game, game.getCurrentPlayer(), direction); + break; + } catch (NoMoreBulletInWeaponException | NotValidDirectionException ignored) {} + } + Assertions.assertNotNull(shot); + Player otherPlayer = game.getPlayers().get(1); + int currentEnergyOtherPlayer = otherPlayer.getEnergy(); + int currentEnergyCurrentPlayer = game.getCurrentPlayer().getEnergy(); shot.doAction(); - System.out.println("After shot " + game.getPlayers().get(1).getEnergy()); + Assertions.assertEquals(currentEnergyCurrentPlayer - game.getCurrentPlayer().getClassPlayer().getShootCost(), game.getCurrentPlayer().getEnergy()); + Assertions.assertEquals(currentEnergyOtherPlayer - otherPlayer.getClassPlayer().getPenaltyShoot(), otherPlayer.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 233af7c..9519ca9 100644 --- a/server/src/test/java/fr/lnl/game/server/GridTest.java +++ b/server/src/test/java/fr/lnl/game/server/GridTest.java @@ -51,13 +51,16 @@ public class GridTest { game.getCurrentPlayer().getEnergy() + " points de vies restants"); Player player = game.getCurrentPlayer(); ArrayList actions = new ArrayList<>(); - for(Move.Direction direction : Move.Direction.values()) { + 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 Shot(game, player), - new DeployShield(player), new DropBomb(game, player), new DropMine(game, player))); + 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;