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 c415fc9..7061d40 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 @@ -1,22 +1,29 @@ 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; import java.util.Random; -public abstract class AbstractAction implements Action { +public abstract class AbstractAction implements Action { private final Game game; + private final Player player; - public AbstractAction(Game game){ + 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()) { @@ -24,6 +31,7 @@ public abstract class AbstractAction implements Action { break; case 1: point = getValidPoint.get(0); + break; default: { Random random = new Random(); point = getValidPoint.get(random.nextInt(0, 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 dba643d..0127a8c 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 @@ -7,8 +7,8 @@ import fr.lnl.game.server.utils.Point; import java.util.List; public class DeployShield extends AbstractAction { - public DeployShield(Game game){ - super(game); + public DeployShield(Game game, Player player){ + super(game, player); } @Override 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 d3fd35b..e78e0f5 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,8 +9,8 @@ import java.util.List; public class DropBomb extends DropObject { - public DropBomb(Game game){ - super(game); + public DropBomb(Game game, Player player){ + super(game, player); } // voir pour la redondance de code au niveau de DropBomb, DropObject,DropMine 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 c299649..93c0c6d 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,8 +8,8 @@ import fr.lnl.game.server.utils.Point; import java.util.List; public class DropMine extends DropObject { - public DropMine(Game game){ - super(game); + public DropMine(Game game, Player player){ + super(game, player); } @Override public void doAction() { 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 35e2a55..d3f7ef8 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,8 @@ import java.util.List; public abstract class DropObject extends AbstractAction { - public DropObject(Game game){ - super(game); + public DropObject(Game game, Player player){ + super(game, player); } @Override 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 2f725cc..28f077a 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 @@ -8,34 +8,43 @@ import fr.lnl.game.server.utils.Point; import java.util.ArrayList; import java.util.HashMap; +import java.util.HashSet; import java.util.List; public class Move extends AbstractAction { - public Move(Game game) { - super(game); + + private final Point point; + + public Move(Game game, Player player, Direction direction) throws NotValidDirectionException { + super(game, player); + HashSet points = new HashSet<>(getValidPoint()); + Point playerPosition = player.getPoint(); + Point newPosition = new Point(playerPosition.getA() + direction.deltaX, playerPosition.getB() + direction.deltaY); + if(!points.contains(newPosition)) { + throw new NotValidDirectionException(direction + " isn't a valid position"); + } + this.point = newPosition; } @Override public void doAction() { - List points = getValidPoint(); - Point nextPositon = choseRandomPoint(points); Player player = getGame().getCurrentPlayer(); getGame().getGrid().getBoard().get(player.getPoint()).setA(null); - getGame().getGrid().getBoard().get(nextPositon).setA(player); - player.setPoint(nextPositon); + getGame().getGrid().getBoard().get(this.point).setA(player); + player.setPoint(this.point); player.decrementEnergy(player.getClassPlayer().getMoveCost()); - Box box = getGame().getGrid().getBoard().get(nextPositon).getB(); + Box box = getGame().getGrid().getBoard().get(this.point).getB(); if (box instanceof Mine){ player.decrementEnergy(player.getClassPlayer().getPenaltyMine()); - getGame().getGrid().getBoard().get(nextPositon).setB(null); + getGame().getGrid().getBoard().get(this.point).setB(null); } if(box instanceof Bomb){ player.decrementEnergy(player.getClassPlayer().getPenaltyBomb()); - getGame().getGrid().getBoard().get(nextPositon).setB(null); + getGame().getGrid().getBoard().get(this.point).setB(null); } if(box instanceof EnergyBall){ player.incrementEnergy(player.getClassPlayer().getGainEnergy()); - getGame().getGrid().getBoard().get(nextPositon).setB(null); + getGame().getGrid().getBoard().get(this.point).setB(null); } } @@ -44,10 +53,11 @@ public class Move extends AbstractAction { return !getValidPoint().isEmpty(); } + @Override public List getValidPoint() { List listMoves = new ArrayList<>(); HashMap> board = getGame().getGrid().getBoard(); - Point position = getGame().getCurrentPlayer().getPoint(); + Point position = getPlayer().getPoint(); for (int deltarow = -1; deltarow <= 1; deltarow++) { for (int deltacolumn = -1; deltacolumn <= 1; deltacolumn++) { if(deltarow == 0 || deltacolumn == 0){ @@ -63,4 +73,28 @@ 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/NotValidDirectionException.java b/server/src/main/java/fr/lnl/game/server/games/action/NotValidDirectionException.java new file mode 100644 index 0000000..4958a88 --- /dev/null +++ b/server/src/main/java/fr/lnl/game/server/games/action/NotValidDirectionException.java @@ -0,0 +1,9 @@ +package fr.lnl.game.server.games.action; + +public class NotValidDirectionException extends Exception { + + public NotValidDirectionException(String message) { + super(message); + } + +} 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 26fbe54..e34898f 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,18 +1,19 @@ 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){ - super(game); + public Nothing(Game game, Player player){ + super(game, player); } - public Nothing(){ - super(null); + public Nothing() { + super(null, null); } @Override 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 4d31eba..e9af958 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,8 +10,8 @@ import java.util.ArrayList; import java.util.List; public class Shot extends AbstractAction { - public Shot(Game game) { - super(game); + public Shot(Game game, Player player) { + super(game, player); } @Override diff --git a/server/src/main/java/fr/lnl/game/server/games/player/AbstractPlayer.java b/server/src/main/java/fr/lnl/game/server/games/player/AbstractPlayer.java index 42952df..d2d31cb 100644 --- a/server/src/main/java/fr/lnl/game/server/games/player/AbstractPlayer.java +++ b/server/src/main/java/fr/lnl/game/server/games/player/AbstractPlayer.java @@ -4,6 +4,8 @@ import fr.lnl.game.server.games.action.Action; import fr.lnl.game.server.games.weapon.Weapon; import fr.lnl.game.server.utils.Point; +import java.util.List; + public abstract class AbstractPlayer implements Player { private final int id; @@ -11,7 +13,7 @@ public abstract class AbstractPlayer implements Player { private int energy; private Weapon weapon; private boolean shieldDeploy; - private Action[] actions; + private List actions; private final ClassPlayer classPlayer; public AbstractPlayer(Integer id, Point point, boolean shieldDeploy, ClassPlayer classPlayer) { @@ -23,62 +25,77 @@ public abstract class AbstractPlayer implements Player { this.point = point; } + @Override public boolean isAlive(){ return energy > 0; } + @Override public int getId() { return id; } + @Override public int getEnergy() { return energy; } + @Override public Weapon getWeapon() { return weapon; } + @Override public boolean isShieldDeploy() { return shieldDeploy; } + @Override public void setEnergy(int energy) { this.energy = energy; } + @Override public void setShieldDeploy(boolean shieldDeploy) { this.shieldDeploy = shieldDeploy; } + @Override public void setWeapon(Weapon weapon) { this.weapon = weapon; } - public Action[] getActions() { + @Override + public List getActions() { return actions; } - public void setActions(Action[] actions){ + @Override + public void setActions(List actions){ this.actions = actions; } + @Override public ClassPlayer getClassPlayer() { return classPlayer; } + @Override public Point getPoint() { return point; } + @Override public void setPoint(Point point){ this.point = point; } + @Override public void decrementEnergy(int energy){ this.energy -= energy; } + @Override public void incrementEnergy(int energy){ this.energy += energy; } diff --git a/server/src/main/java/fr/lnl/game/server/games/player/Player.java b/server/src/main/java/fr/lnl/game/server/games/player/Player.java index 69c6e15..37d7cf1 100644 --- a/server/src/main/java/fr/lnl/game/server/games/player/Player.java +++ b/server/src/main/java/fr/lnl/game/server/games/player/Player.java @@ -4,6 +4,8 @@ import fr.lnl.game.server.games.action.Action; import fr.lnl.game.server.games.weapon.Weapon; import fr.lnl.game.server.utils.Point; +import java.util.List; + public interface Player { Point getPoint(); boolean isAlive(); @@ -14,8 +16,8 @@ public interface Player { void setEnergy(int energy); void setShieldDeploy(boolean shieldDeploy); void setWeapon(Weapon weapon); - Action[] getActions(); - void setActions(Action[] actions); + List getActions(); + void setActions(List actions); Action choseAction(); ClassPlayer getClassPlayer(); void setPoint(Point point); diff --git a/server/src/main/java/fr/lnl/game/server/games/player/RandomComputerPlayer.java b/server/src/main/java/fr/lnl/game/server/games/player/RandomComputerPlayer.java index 3d815d5..4b44d77 100644 --- a/server/src/main/java/fr/lnl/game/server/games/player/RandomComputerPlayer.java +++ b/server/src/main/java/fr/lnl/game/server/games/player/RandomComputerPlayer.java @@ -15,14 +15,13 @@ public class RandomComputerPlayer extends AbstractPlayer{ @Override public Action choseAction() { Action action = null; - switch (getActions().length){ + switch (getActions().size()){ case 0 -> action = new Nothing(); - case 1 -> action = getActions()[0]; + case 1 -> action = getActions().get(0); default -> { Random random = new Random(); while (action == null || !action.isPossible()) { - action = getActions() - [random.nextInt(0,getActions().length)]; + action = getActions().get(random.nextInt(0,getActions().size())); } } } 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 cf3a551..3e1447d 100644 --- a/server/src/test/java/fr/lnl/game/server/ActionPlayerTest.java +++ b/server/src/test/java/fr/lnl/game/server/ActionPlayerTest.java @@ -1,6 +1,9 @@ package fr.lnl.game.server; import fr.lnl.game.server.games.Game; +import fr.lnl.game.server.games.action.Action; +import fr.lnl.game.server.games.action.Move; +import fr.lnl.game.server.games.action.NotValidDirectionException; import fr.lnl.game.server.games.action.Shot; import fr.lnl.game.server.games.grid.Grid; import fr.lnl.game.server.utils.Point; @@ -27,6 +30,25 @@ public class ActionPlayerTest { @Test public void moveActionTest() { Assertions.assertEquals(game.getPlayers().get(0), game.getCurrentPlayer()); + Action move = null; + Point oldPoint = game.getCurrentPlayer().getPoint(); + Move.Direction savedDirection = null; + for(Move.Direction direction : Move.Direction.values()) { + try { + move = new Move(game, game.getCurrentPlayer(), direction); + savedDirection = direction; + break; + } catch (NotValidDirectionException ignored) {} + } + Assertions.assertNotEquals(null, move); + assert move != null; + move.doAction(); + Point newPoint = game.getCurrentPlayer().getPoint(); + Assertions.assertEquals(newPoint, + new Point(oldPoint.getA() + savedDirection.getDeltaX(), + oldPoint.getA() + savedDirection.getDeltaY() + ) + ); } @@ -34,7 +56,7 @@ public class ActionPlayerTest { @Test public void shotActionTest(){ System.out.println(grid.toString()); - Shot shot = new Shot(game); + 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()); 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 8142b6f..fc89d00 100644 --- a/server/src/test/java/fr/lnl/game/server/GridTest.java +++ b/server/src/test/java/fr/lnl/game/server/GridTest.java @@ -11,6 +11,10 @@ import fr.lnl.game.server.utils.Point; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Random; + import static org.junit.jupiter.api.Assertions.assertEquals; public class GridTest { @@ -33,25 +37,42 @@ public class GridTest { assertEquals(new Wall(Cardinal.SOUTH_WEST, grid.getRow() - 1, 0), grid.getBoard().get(new Point(grid.getRow() - 1, 0)).getB()); assertEquals(new Wall(Cardinal.SOUTH_EAST, grid.getRow() - 1, grid.getColumn() - 1), grid.getBoard().get(new Point(grid.getRow() - 1, grid.getColumn() - 1)).getB()); // test placePlayersBRUT (mocked) - assertEquals(grid.getPlayers().get(0), grid.getBoard().get(new Point(1, 1)).getA()); - assertEquals(grid.getPlayers().get(1), grid.getBoard().get(new Point(14, 14)).getA()); + assertEquals(grid.getPlayers().get(0), grid.getBoard().get(new Point(7, 7)).getA()); + assertEquals(grid.getPlayers().get(1), grid.getBoard().get(new Point(7, 8)).getA()); // test placeEnergyBallBRUT (mocked) assertEquals(new EnergyBall(), grid.getBoard().get(new Point(2, 3)).getB()); - assertEquals(new EnergyBall(), grid.getBoard().get(new Point(7, 10)).getB()); + assertEquals(new EnergyBall(), grid.getBoard().get(new Point(8, 10)).getB()); } - @Test public void testPlay(){ - for (Player player: game.getPlayers()) { - player.setActions(new Action[]{new Nothing(game), new Shot(game), new Move(game), - new DeployShield(game), new DropBomb(game), new DropMine(game)}); - } 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(Move.Direction direction : Move.Direction.values()) { + try { + 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))); + player.setActions(actions); System.out.println(game.getGrid().toString()); - Action action = game.getCurrentPlayer().choseAction(); + Action action = null; + switch (player.getActions().size()){ + case 0 -> action = new Nothing(game, player); + 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");