From d2ccedb8145da1e47a563c130461296c174ab5bb Mon Sep 17 00:00:00 2001 From: Katchan Date: Thu, 21 Oct 2021 16:41:04 +0200 Subject: [PATCH] Add Move isPossible and getValidPoint and refactor Grid attributes --- .../java/fr/lnl/game/server/ServerMain.java | 4 +-- .../server/games/action/AbstractAction.java | 5 +++ .../fr/lnl/game/server/games/action/Move.java | 31 +++++++++++++++- .../fr/lnl/game/server/games/grid/Grid.java | 35 +++++++++++-------- .../server/games/player/AbstractPlayer.java | 13 ++++++- .../server/games/player/ComputerPlayer.java | 10 +++--- .../game/server/games/player/HumanPlayer.java | 10 +++--- .../lnl/game/server/games/player/Player.java | 3 ++ 8 files changed, 81 insertions(+), 30 deletions(-) diff --git a/server/src/main/java/fr/lnl/game/server/ServerMain.java b/server/src/main/java/fr/lnl/game/server/ServerMain.java index 52782d8..a4257da 100644 --- a/server/src/main/java/fr/lnl/game/server/ServerMain.java +++ b/server/src/main/java/fr/lnl/game/server/ServerMain.java @@ -9,8 +9,8 @@ import fr.lnl.game.server.games.player.Player; public class ServerMain { public static void main(String[] args) { - Player playerOne = new ComputerPlayer(1,new DefaultClassPlayer()); - Player playerTwo = new ComputerPlayer(2,new DefaultClassPlayer()); + Player playerOne = new ComputerPlayer(1,null,new DefaultClassPlayer()); + Player playerTwo = new ComputerPlayer(2,null,new DefaultClassPlayer()); Grid grid = new Grid(16,16,new Player[]{playerOne,playerTwo}); grid.initGrid(); 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 bb67d47..ffc6059 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 @@ -4,7 +4,12 @@ import fr.lnl.game.server.games.Game; public abstract class AbstractAction implements Action { private Game game; + public AbstractAction(Game game){ this.game = game; } + + protected Game getGame() { + return game; + } } 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 b502127..b37e418 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 @@ -1,6 +1,16 @@ package fr.lnl.game.server.games.action; import fr.lnl.game.server.games.Game; +import fr.lnl.game.server.games.grid.Box; +import fr.lnl.game.server.games.grid.Grid; +import fr.lnl.game.server.games.grid.Wall; +import fr.lnl.game.server.games.player.Player; +import fr.lnl.game.server.utils.Pair; +import fr.lnl.game.server.utils.Point; + +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; public class Move extends AbstractAction { public Move(Game game) { @@ -14,6 +24,25 @@ public class Move extends AbstractAction { @Override public boolean isPossible() { - return false; + return getValidPoint().isEmpty(); } + + public List getValidPoint() { + List listMoves = new LinkedList<>(); + HashMap> board = getGame().getGrid().getBoard(); + Point position = getGame().getCurrent_player().getPoint(); + for (int row = -1; row <= 1; row++) { + for (int column = -1; column <= 1; column++) { + if(Grid.caseisValid(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() instanceof Wall){ + listMoves.add(neighbour); + } + } + } + } + return listMoves; + } + } 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 b532d89..8ccee05 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 @@ -10,36 +10,36 @@ import java.util.HashMap; public class Grid { private HashMap> board; - private int x; - private int y; + private int row; + private int column; private Player[] players; - public Grid(int x, int y, Player[] players){ - this.x = x; - this.y = y; + public Grid(int row, int column, Player[] players){ + this.row = row; + this.column = column; this.players = players; board = new HashMap<>(); } public void initGrid(){ - for (int i = 0; i < x; i++) { - for (int j = 0; j < y; j++) { + for (int i = 0; i < row; i++) { + for (int j = 0; j < column; j++) { Box box; if (i == 0 && j == 0) { box = new Wall(Cardinal.NORTH_WEST, i, j); - } else if (i == 0 && j == y-1) { + } else if (i == 0 && j == column-1) { box = new Wall(Cardinal.NORTH_EAST, i, j); - } else if (i == x-1 && j == 0) { + } else if (i == row-1 && j == 0) { box = new Wall(Cardinal.SOUTH_WEST, i, j); - } else if (i == x-1 && j == y-1) { + } else if (i == row-1 && j == column-1) { box = new Wall(Cardinal.SOUTH_EAST, i, j); } else if (i == 0) { box = new Wall(Cardinal.NORTH, i, j); - } else if (i == x-1) { + } else if (i == row-1) { box = new Wall(Cardinal.SOUTH, i, j); } else if (j == 0) { box = new Wall(Cardinal.WEST, i, j); - } else if (j == y-1) { + } else if (j == column-1) { box = new Wall(Cardinal.EAST, i, j); } else { box = null; @@ -66,11 +66,18 @@ public class Grid { board.get(new Point(14,2)).setB(new Wall(Cardinal.WEST,14,2)); } + public static boolean caseisValid(int row, int column, int deltaRow, int deltaColumn){ + return row + deltaRow >= 0 && row + deltaRow <= row && column + deltaColumn >= 0 && column + deltaColumn <= column; + } + + public HashMap> getBoard() { + return board; + } public void printGrid() { - for (int i = 0; i < x; i++) { + for (int i = 0; i < row; i++) { System.out.print("\n"); - for (int j = 0; j < y; j++) { + for (int j = 0; j < column; j++) { Pair value = board.get(new Point(i, j)); if(value.getA() != null){ System.out.print(" \033[0;34mP\033[0m"); 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 0ff96ad..28df0a1 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 @@ -2,22 +2,25 @@ package fr.lnl.game.server.games.player; import fr.lnl.game.server.games.action.Action; import fr.lnl.game.server.games.weapon.Weapon; +import fr.lnl.game.server.utils.Point; public abstract class AbstractPlayer implements Player { private int id; + private Point point; private int energy; private Weapon weapon; private boolean shieldDeploy; private Action[] actions; private ClassPlayer classPlayer; - public AbstractPlayer(int id, boolean shieldDeploy, ClassPlayer classPlayer) { + public AbstractPlayer(int id, Point point, boolean shieldDeploy, ClassPlayer classPlayer) { this.id = id; this.classPlayer = classPlayer; this.energy = classPlayer.getEnergy(); this.weapon = classPlayer.getWeapon(); this.shieldDeploy = shieldDeploy; + this.point = point; } public boolean isAlive(){ @@ -59,4 +62,12 @@ public abstract class AbstractPlayer implements Player { public ClassPlayer getClassPlayer() { return classPlayer; } + + public Point getPoint() { + return point; + } + + public void setPoint(Point point){ + this.point = point; + } } diff --git a/server/src/main/java/fr/lnl/game/server/games/player/ComputerPlayer.java b/server/src/main/java/fr/lnl/game/server/games/player/ComputerPlayer.java index ed38862..33de86e 100644 --- a/server/src/main/java/fr/lnl/game/server/games/player/ComputerPlayer.java +++ b/server/src/main/java/fr/lnl/game/server/games/player/ComputerPlayer.java @@ -1,12 +1,10 @@ package fr.lnl.game.server.games.player; +import fr.lnl.game.server.utils.Point; + public class ComputerPlayer extends AbstractPlayer{ - public ComputerPlayer(int id, boolean shieldDeploy, ClassPlayer classPlayer) { - super(id, shieldDeploy, classPlayer); - } - - public ComputerPlayer(int id, ClassPlayer classPlayer) { - super(id,false, classPlayer); + public ComputerPlayer(int id, Point point, ClassPlayer classPlayer) { + super(id,point,false, classPlayer); } } diff --git a/server/src/main/java/fr/lnl/game/server/games/player/HumanPlayer.java b/server/src/main/java/fr/lnl/game/server/games/player/HumanPlayer.java index 42cb219..45fa0b3 100644 --- a/server/src/main/java/fr/lnl/game/server/games/player/HumanPlayer.java +++ b/server/src/main/java/fr/lnl/game/server/games/player/HumanPlayer.java @@ -1,13 +1,11 @@ package fr.lnl.game.server.games.player; +import fr.lnl.game.server.utils.Point; + public class HumanPlayer extends AbstractPlayer { - public HumanPlayer(int id, boolean shieldDeploy, ClassPlayer classPlayer) { - super(id, shieldDeploy, classPlayer); - } - - public HumanPlayer(int id, ClassPlayer classPlayer) { - super(id, false, classPlayer); + public HumanPlayer(int id, Point point, ClassPlayer classPlayer) { + super(id, point,false, classPlayer); } } 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 a4ba85f..ab14fbc 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 @@ -1,4 +1,7 @@ package fr.lnl.game.server.games.player; +import fr.lnl.game.server.utils.Point; + public interface Player { + Point getPoint(); }