From 2b77a2a5665e9834c00a41267b48bce60a83af07 Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Thu, 9 Dec 2021 16:50:43 +0100 Subject: [PATCH] Add support to 8 axis to shot action --- .../java/fr/lnl/game/server/games/Game.java | 4 +- .../game/server/games/action/Direction.java | 37 ++-------------- .../server/games/action/Direction4Axis.java | 30 +++++++++++++ .../server/games/action/Direction8Axis.java | 44 +++++++++++++++++++ .../game/server/games/action/DropBomb.java | 2 +- .../game/server/games/action/DropMine.java | 2 +- .../game/server/games/action/DropObject.java | 6 +-- .../fr/lnl/game/server/games/action/Move.java | 6 +-- .../fr/lnl/game/server/games/action/Shot.java | 17 ++++--- .../games/player/StrategyComputerPlayer.java | 1 + .../fr/lnl/game/server/ActionPlayerTest.java | 10 ++--- 11 files changed, 102 insertions(+), 57 deletions(-) create mode 100644 server/src/main/java/fr/lnl/game/server/games/action/Direction4Axis.java create mode 100644 server/src/main/java/fr/lnl/game/server/games/action/Direction8Axis.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 70f177a..b6c0ea6 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 @@ -94,7 +94,7 @@ public class Game { */ public List generateAndGetPlayerActions(Player player) { List actions = new ArrayList<>(); - for(Direction direction : Direction.values()) { + for(Direction4Axis direction : Direction4Axis.values()) { try { actions.add(new Move(this, player, direction)); } catch (NotValidDirectionException ignored){} @@ -104,6 +104,8 @@ public class Game { try { actions.add(new DropMine(this, player, direction)); } catch (NotValidDirectionException ignored) {} + } + for(Direction8Axis direction : Direction8Axis.values()) { try { actions.add(new Shot(this, player, direction)); } catch (NotValidDirectionException | NoMoreBulletInWeaponException ignored) {} 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 index 5c7c8cb..55976c4 100644 --- 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 @@ -1,39 +1,8 @@ package fr.lnl.game.server.games.action; -/** - * Enum used to chose where to move, shot, etc. - */ -public enum Direction { +public interface Direction { - UP(-1, 0, true), - DOWN(1, 0, true), - LEFT(0, -1, false), - RIGHT(0, 1, false); + int getDeltaX(); - 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; - } - - /** - * Used by {@link Shot} to know if the direction is in vertical direction cause shot action can have a different - * distance depending of the direction - * @return true if the direction is vertical - */ - public boolean isVertical() { - return isVertical; - } + int getDeltaY(); } diff --git a/server/src/main/java/fr/lnl/game/server/games/action/Direction4Axis.java b/server/src/main/java/fr/lnl/game/server/games/action/Direction4Axis.java new file mode 100644 index 0000000..24dcf3d --- /dev/null +++ b/server/src/main/java/fr/lnl/game/server/games/action/Direction4Axis.java @@ -0,0 +1,30 @@ +package fr.lnl.game.server.games.action; + +/** + * Enum used to chose where to move, shot, etc. + */ +public enum Direction4Axis implements Direction { + + UP(-1, 0), + DOWN(1, 0), + LEFT(0, -1), + RIGHT(0, 1); + + private final int deltaX; + private final int deltaY; + + Direction4Axis(int i, int i1) { + this.deltaX = i; + this.deltaY = i1; + } + + @Override + public int getDeltaX() { + return deltaX; + } + + @Override + public int getDeltaY() { + return deltaY; + } +} diff --git a/server/src/main/java/fr/lnl/game/server/games/action/Direction8Axis.java b/server/src/main/java/fr/lnl/game/server/games/action/Direction8Axis.java new file mode 100644 index 0000000..3ed49b2 --- /dev/null +++ b/server/src/main/java/fr/lnl/game/server/games/action/Direction8Axis.java @@ -0,0 +1,44 @@ +package fr.lnl.game.server.games.action; + +public enum Direction8Axis implements Direction { + + UP(-1, 0, true), + DOWN(1, 0, true), + LEFT(0, -1, false), + RIGHT(0, 1, false), + UP_LEFT(-1, -1, true), + UP_RIGHT(-1, 1, true), + DOWN_RIGHT(1, 1, true), + DOWN_LEFT(1, -1, true); + + private final int deltaX; + private final int deltaY; + private final boolean isVertical; + + Direction8Axis(int i, int i1, boolean isVertical) { + this.deltaX = i; + this.deltaY = i1; + this.isVertical = isVertical; + } + + @Override + public int getDeltaX() { + return deltaX; + } + + @Override + public int getDeltaY() { + return deltaY; + } + + /** + * Used by {@link Shot} to know if the direction is in vertical direction cause shot action can have a different + * distance depending of the direction + * @return true if the direction is vertical + */ + public boolean isVertical() { + return isVertical; + } + + +} 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 c8ae35a..44e25bc 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 @@ -9,7 +9,7 @@ import fr.lnl.game.server.games.player.Player; */ public class DropBomb extends DropObject { - public DropBomb(Game game, Player player, Direction direction) throws NotValidDirectionException { + public DropBomb(Game game, Player player, Direction4Axis direction) throws NotValidDirectionException { super(game, player, direction); } 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 926c7d5..2728609 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,7 +9,7 @@ import fr.lnl.game.server.games.player.Player; */ public class DropMine extends DropObject { - public DropMine(Game game, Player player, Direction direction) throws NotValidDirectionException { + public DropMine(Game game, Player player, Direction4Axis direction) throws NotValidDirectionException { super(game, player, direction); } 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 6c56332..dedaabd 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 @@ -16,14 +16,14 @@ import java.util.List; public abstract class DropObject extends AbstractAction { protected final Point point; - private final Direction direction; + private final Direction4Axis direction; /** * @param player basically current player * @param direction chosen direction * @throws NotValidDirectionException throw when the chosen direction is invalid */ - public DropObject(Game game, Player player, Direction direction) throws NotValidDirectionException { + public DropObject(Game game, Player player, Direction4Axis direction) throws NotValidDirectionException { super(game, player); List points = getValidPoint(); Point playerPosition = player.getPosition(); @@ -67,7 +67,7 @@ public abstract class DropObject extends AbstractAction { return listMoves; } - public Direction getDirection() { + public Direction4Axis getDirection() { return direction; } 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 88d2a0c..ed51fae 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 @@ -18,9 +18,9 @@ import java.util.List; public class Move extends AbstractAction { private final Point point; - private final Direction direction; + private final Direction4Axis direction; - public Move(Game game, Player player, Direction direction) throws NotValidDirectionException { + public Move(Game game, Player player, Direction4Axis direction) throws NotValidDirectionException { super(game, player); List points = getValidPoint(); Point playerPosition = player.getPosition(); @@ -87,7 +87,7 @@ public class Move extends AbstractAction { return point; } - public Direction getDirection() { + public Direction4Axis getDirection() { return direction; } 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 322f2c4..9b4dff8 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 @@ -12,9 +12,9 @@ import java.util.List; public class Shot extends AbstractAction { private final Point point; - private final Direction direction; + private final Direction8Axis direction; - public Shot(Game game, Player player, Direction direction) throws NoMoreBulletInWeaponException, NotValidDirectionException { + public Shot(Game game, Player player, Direction8Axis direction) throws NoMoreBulletInWeaponException, NotValidDirectionException { super(game, player); if(player.getWeapon().getBullet() == 0) { throw new NoMoreBulletInWeaponException(); @@ -29,11 +29,10 @@ public class Shot extends AbstractAction { this.direction = direction; } - public Shot(Game game, Player player){ + public Shot(Game game, Player player) { super(game, player); - List points = getValidPoint(); - this.point = null; - this.direction = null; + point = null; + direction = null; } /** @@ -76,7 +75,7 @@ public class Shot extends AbstractAction { List listMoves = new ArrayList<>(); Point position = game.getCurrentPlayer().getPosition(); Weapon weapon = game.getCurrentPlayer().getWeapon(); - for(Direction direction : Direction.values()) { + for(Direction8Axis direction : Direction8Axis.values()) { Point neighbour = seeNeighbour(position, direction.getDeltaX(), direction.getDeltaY(), direction.isVertical() ? weapon.getVerticalDistance() : weapon.getHorizontalDistance()); if(neighbour != null) @@ -88,8 +87,8 @@ public class Shot extends AbstractAction { /** * * @param point player current position - * @param deltaX given by {@link Direction} - * @param deltaY given by {@link Direction} + * @param deltaX given by {@link Direction4Axis} + * @param deltaY given by {@link Direction4Axis} * @param range given by {@link Weapon#getHorizontalDistance()} or {@link Weapon#getVerticalDistance()} * @return true if there is a player in the chosen direction, false otherwise */ diff --git a/server/src/main/java/fr/lnl/game/server/games/player/StrategyComputerPlayer.java b/server/src/main/java/fr/lnl/game/server/games/player/StrategyComputerPlayer.java index 7182e55..0b63de2 100644 --- a/server/src/main/java/fr/lnl/game/server/games/player/StrategyComputerPlayer.java +++ b/server/src/main/java/fr/lnl/game/server/games/player/StrategyComputerPlayer.java @@ -10,6 +10,7 @@ import fr.lnl.game.server.utils.Point; import java.util.List; import java.util.Random; +import java.util.function.Predicate; import java.util.stream.Collectors; public class StrategyComputerPlayer extends ComputerPlayer { 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 fa475e8..fab0c87 100644 --- a/server/src/test/java/fr/lnl/game/server/ActionPlayerTest.java +++ b/server/src/test/java/fr/lnl/game/server/ActionPlayerTest.java @@ -35,8 +35,8 @@ public class ActionPlayerTest { public void moveActionTest() { Action move = null; Point oldPoint = game.getCurrentPlayer().getPosition(); - Direction savedDirection = null; - for(Direction direction : Direction.values()) { + Direction4Axis savedDirection = null; + for(Direction4Axis direction : Direction4Axis.values()) { try { move = new Move(game, game.getCurrentPlayer(), direction); savedDirection = direction; @@ -65,7 +65,7 @@ public class ActionPlayerTest { @Test public void shotActionTest(){ Action shot = null; - for(Direction direction : Direction.values()) { + for(Direction8Axis direction : Direction8Axis.values()) { try { shot = new Shot(game, game.getCurrentPlayer(), direction); break; @@ -84,8 +84,8 @@ public class ActionPlayerTest { public void dropBombActionTest() { Player player = game.getCurrentPlayer(); Action action = null; - Direction savedDirection = null; - for(Direction direction : Direction.values()) { + Direction4Axis savedDirection = null; + for(Direction4Axis direction : Direction4Axis.values()) { try { action = new DropBomb(game, game.getCurrentPlayer(), direction); savedDirection = direction;