diff --git a/server/src/main/java/fr/lnl/game/server/games/action/AbstractAction.java b/server/src/main/java/fr/lnl/game/server/games/action/AbstractAction.java index 7061d40..5a36fb6 100644 --- a/server/src/main/java/fr/lnl/game/server/games/action/AbstractAction.java +++ b/server/src/main/java/fr/lnl/game/server/games/action/AbstractAction.java @@ -8,22 +8,15 @@ import java.util.List; import java.util.Random; public abstract class AbstractAction implements Action { - private final Game game; - private final Player player; + + protected final Game game; + protected final Player player; public AbstractAction(Game game, Player player){ this.game = game; this.player = player; } - protected Game getGame() { - return game; - } - - protected Player getPlayer() { - return player; - } - protected Point choseRandomPoint(List getValidPoint) { Point point = null; switch (getValidPoint.size()) { diff --git a/server/src/main/java/fr/lnl/game/server/games/action/DeployShield.java b/server/src/main/java/fr/lnl/game/server/games/action/DeployShield.java index 0127a8c..559b62a 100644 --- a/server/src/main/java/fr/lnl/game/server/games/action/DeployShield.java +++ b/server/src/main/java/fr/lnl/game/server/games/action/DeployShield.java @@ -1,19 +1,18 @@ package fr.lnl.game.server.games.action; -import fr.lnl.game.server.games.Game; import fr.lnl.game.server.games.player.Player; import fr.lnl.game.server.utils.Point; import java.util.List; public class DeployShield extends AbstractAction { - public DeployShield(Game game, Player player){ - super(game, player); + + public DeployShield(Player player){ + super(null, player); } @Override public void doAction(){ - Player player = getGame().getCurrentPlayer(); player.setShieldDeploy(true); player.decrementEnergy(player.getClassPlayer().getShieldCost()); } 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 e78e0f5..66d49ed 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 @@ -13,13 +13,11 @@ public class DropBomb extends DropObject { super(game, player); } - // voir pour la redondance de code au niveau de DropBomb, DropObject,DropMine @Override public void doAction() { List points = getValidPoint(); Point point = choseRandomPoint(points); - getGame().getGrid().getBoard().get(point).setB(new Bomb()); - Player player = getGame().getCurrentPlayer(); + game.getGrid().getBoard().get(point).setB(new Bomb()); player.decrementEnergy(player.getClassPlayer().getBombCost()); } 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 93c0c6d..43b16c7 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 @@ -8,6 +8,7 @@ import fr.lnl.game.server.utils.Point; import java.util.List; public class DropMine extends DropObject { + public DropMine(Game game, Player player){ super(game, player); } @@ -15,10 +16,8 @@ public class DropMine extends DropObject { public void doAction() { List points = getValidPoint(); Point point = choseRandomPoint(points); - Mine mine = new Mine(); - getGame().getGrid().getBoard().get(point).setB(mine); - Player player = getGame().getCurrentPlayer(); - getGame().getCurrentPlayer().decrementEnergy(player.getClassPlayer().getMineCost()); + game.getGrid().getBoard().get(point).setB(new Mine()); + game.getCurrentPlayer().decrementEnergy(player.getClassPlayer().getMineCost()); } @Override 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 d3f7ef8..1eb982f 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,10 +16,6 @@ public abstract class DropObject extends AbstractAction { super(game, player); } - @Override - public void doAction() { - } - @Override public boolean isPossible() { return !getValidPoint().isEmpty(); @@ -27,11 +23,11 @@ public abstract class DropObject extends AbstractAction { public List getValidPoint() { List listMoves = new ArrayList<>(); - HashMap> board = getGame().getGrid().getBoard(); - Point position = getGame().getCurrentPlayer().getPoint(); + HashMap> board = game.getGrid().getBoard(); + Point position = player.getPoint(); for (int row = -1; row <= 1; row++) { for (int column = -1; column <= 1; column++) { - if(getGame().getGrid().boardPositionIsValid(position.getA(),row,position.getB(),column)){ + if(game.getGrid().boardPositionIsValid(position.getA(),row,position.getB(),column)){ Point neighbour = new Point(position.getA() + row, position.getB() + column); Pair state = board.get(neighbour); if(state.getA() == null && state.getB() == null){ 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 8d27b4a..d04d175 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 @@ -28,14 +28,13 @@ public class Move extends AbstractAction { @Override public void doAction() { - Player player = getGame().getCurrentPlayer(); - getGame().getGrid().getBoard().get(player.getPoint()).setA(null); - getGame().getGrid().getBoard().get(this.point).setA(player); + game.getGrid().getBoard().get(player.getPoint()).setA(null); + game.getGrid().getBoard().get(this.point).setA(player); player.setPoint(this.point); player.decrementEnergy(player.getClassPlayer().getMoveCost()); - Box box = getGame().getGrid().getBoard().get(this.point).getB(); + Box box = game.getGrid().getBoard().get(this.point).getB(); if(box instanceof InteractiveBox interactiveBox) { - interactiveBox.interact(getGame().getGrid(), player, this.point); + interactiveBox.interact(game.getGrid(), player, this.point); } } @@ -47,12 +46,12 @@ public class Move extends AbstractAction { @Override public List getValidPoint() { List listMoves = new ArrayList<>(); - HashMap> board = getGame().getGrid().getBoard(); - Point position = getPlayer().getPoint(); + HashMap> board = game.getGrid().getBoard(); + Point position = player.getPoint(); for (int deltarow = -1; deltarow <= 1; deltarow++) { for (int deltacolumn = -1; deltacolumn <= 1; deltacolumn++) { if(deltarow == 0 || deltacolumn == 0){ - if(getGame().getGrid().boardPositionIsValid(position.getA(),deltarow,position.getB(),deltacolumn)){ + if(game.getGrid().boardPositionIsValid(position.getA(),deltarow,position.getB(),deltacolumn)){ Point neighbour = new Point(position.getA() + deltarow, position.getB() + deltacolumn); Pair state = board.get(neighbour); if(state.getA() == null && !(state.getB() instanceof Wall)){ diff --git a/server/src/main/java/fr/lnl/game/server/games/action/Nothing.java b/server/src/main/java/fr/lnl/game/server/games/action/Nothing.java index e34898f..64793a2 100644 --- a/server/src/main/java/fr/lnl/game/server/games/action/Nothing.java +++ b/server/src/main/java/fr/lnl/game/server/games/action/Nothing.java @@ -1,17 +1,11 @@ package fr.lnl.game.server.games.action; -import fr.lnl.game.server.games.Game; -import fr.lnl.game.server.games.player.Player; import fr.lnl.game.server.utils.Point; import java.util.List; public class Nothing extends AbstractAction { - public Nothing(Game game, Player player){ - super(game, player); - } - public Nothing() { super(null, null); } 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 e9af958..11301e7 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 @@ -10,18 +10,20 @@ import java.util.ArrayList; import java.util.List; public class Shot extends AbstractAction { + public Shot(Game game, Player player) { super(game, player); } + /** + * @deprecated a rewrite -> L'aléatoire ne devrait pas être ici, mais au moment de l'instanciation comme dans {@link Move} + */ + @Deprecated @Override public void doAction() { - List points = getValidPoint(); - Point point = choseRandomPoint(points); - Player currentPlayer = getGame().getCurrentPlayer(); - currentPlayer.decrementEnergy(currentPlayer.getClassPlayer().getShootCost()); - Player targetPlayer = getGame().getGrid().getBoard().get(point).getA(); - targetPlayer.decrementEnergy(currentPlayer.getClassPlayer().getPenaltyShoot()); + player.decrementEnergy(player.getClassPlayer().getShootCost()); + game.getGrid().getBoard().get(choseRandomPoint(getValidPoint())).getA() + .decrementEnergy(player.getClassPlayer().getPenaltyShoot()); } @Override @@ -29,11 +31,15 @@ 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 = getGame().getCurrentPlayer().getPoint(); - Weapon weapon = getGame().getCurrentPlayer().getWeapon(); + 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); @@ -49,21 +55,22 @@ public class Shot extends AbstractAction { 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 (getGame().getGrid().boardVerticalIsValid(point.getA(), delta)) { + if (game.getGrid().boardVerticalIsValid(point.getA(), delta)) { neibourg = new Point(point.getA() + delta, point.getB()); } } else { - if (getGame().getGrid().boardHorizontalIsValid(point.getB(), delta)) { + if (game.getGrid().boardHorizontalIsValid(point.getB(), delta)) { neibourg = new Point(point.getA(), point.getB() + delta); } } - if (getGame().getGrid().getBoard().get(neibourg).getB() instanceof Wall || range + delta < 0) { + if (game.getGrid().getBoard().get(neibourg).getB() instanceof Wall || range + delta < 0) { return null; } - if(getGame().getGrid().getBoard().get(neibourg).getA() instanceof Player){ + if(game.getGrid().getBoard().get(neibourg).getA() instanceof Player){ return neibourg; } return seeNeibourg(neibourg,delta,range - 1,isVertical); 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 fc89d00..233af7c 100644 --- a/server/src/test/java/fr/lnl/game/server/GridTest.java +++ b/server/src/test/java/fr/lnl/game/server/GridTest.java @@ -56,13 +56,13 @@ public class GridTest { actions.add(new Move(game, player, direction)); } catch (NotValidDirectionException ignored){} } - actions.addAll(Arrays.asList(new Nothing(game, player), new Shot(game, player), - new DeployShield(game, player), new DropBomb(game, player), new DropMine(game, player))); + actions.addAll(Arrays.asList(new Nothing(), new Shot(game, player), + 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(game, player); + case 0 -> action = new Nothing(); case 1 -> action = game.getCurrentPlayer().getActions().get(0); default -> { Random random = new Random();