Add Move isPossible and getValidPoint and refactor Grid attributes

This commit is contained in:
Katchan 2021-10-21 16:41:04 +02:00
parent 8f19cf400c
commit d2ccedb814
8 changed files with 81 additions and 30 deletions

View File

@ -9,8 +9,8 @@ import fr.lnl.game.server.games.player.Player;
public class ServerMain { public class ServerMain {
public static void main(String[] args) { public static void main(String[] args) {
Player playerOne = new ComputerPlayer(1,new DefaultClassPlayer()); Player playerOne = new ComputerPlayer(1,null,new DefaultClassPlayer());
Player playerTwo = new ComputerPlayer(2,new DefaultClassPlayer()); Player playerTwo = new ComputerPlayer(2,null,new DefaultClassPlayer());
Grid grid = new Grid(16,16,new Player[]{playerOne,playerTwo}); Grid grid = new Grid(16,16,new Player[]{playerOne,playerTwo});
grid.initGrid(); grid.initGrid();

View File

@ -4,7 +4,12 @@ import fr.lnl.game.server.games.Game;
public abstract class AbstractAction implements Action { public abstract class AbstractAction implements Action {
private Game game; private Game game;
public AbstractAction(Game game){ public AbstractAction(Game game){
this.game = game; this.game = game;
} }
protected Game getGame() {
return game;
}
} }

View File

@ -1,6 +1,16 @@
package fr.lnl.game.server.games.action; package fr.lnl.game.server.games.action;
import fr.lnl.game.server.games.Game; 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 class Move extends AbstractAction {
public Move(Game game) { public Move(Game game) {
@ -14,6 +24,25 @@ public class Move extends AbstractAction {
@Override @Override
public boolean isPossible() { public boolean isPossible() {
return false; return getValidPoint().isEmpty();
}
public List<Point> getValidPoint() {
List<Point> listMoves = new LinkedList<>();
HashMap<Point, Pair<Player, Box>> 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<Player, Box> state = board.get(neighbour);
if(state.getA() == null || state.getB() instanceof Wall){
listMoves.add(neighbour);
} }
} }
}
}
return listMoves;
}
}

View File

@ -10,36 +10,36 @@ import java.util.HashMap;
public class Grid { public class Grid {
private HashMap<Point, Pair<Player, Box>> board; private HashMap<Point, Pair<Player, Box>> board;
private int x; private int row;
private int y; private int column;
private Player[] players; private Player[] players;
public Grid(int x, int y, Player[] players){ public Grid(int row, int column, Player[] players){
this.x = x; this.row = row;
this.y = y; this.column = column;
this.players = players; this.players = players;
board = new HashMap<>(); board = new HashMap<>();
} }
public void initGrid(){ public void initGrid(){
for (int i = 0; i < x; i++) { for (int i = 0; i < row; i++) {
for (int j = 0; j < y; j++) { for (int j = 0; j < column; j++) {
Box box; Box box;
if (i == 0 && j == 0) { if (i == 0 && j == 0) {
box = new Wall(Cardinal.NORTH_WEST, i, j); 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); 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); 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); box = new Wall(Cardinal.SOUTH_EAST, i, j);
} else if (i == 0) { } else if (i == 0) {
box = new Wall(Cardinal.NORTH, i, j); box = new Wall(Cardinal.NORTH, i, j);
} else if (i == x-1) { } else if (i == row-1) {
box = new Wall(Cardinal.SOUTH, i, j); box = new Wall(Cardinal.SOUTH, i, j);
} else if (j == 0) { } else if (j == 0) {
box = new Wall(Cardinal.WEST, i, j); box = new Wall(Cardinal.WEST, i, j);
} else if (j == y-1) { } else if (j == column-1) {
box = new Wall(Cardinal.EAST, i, j); box = new Wall(Cardinal.EAST, i, j);
} else { } else {
box = null; box = null;
@ -66,11 +66,18 @@ public class Grid {
board.get(new Point(14,2)).setB(new Wall(Cardinal.WEST,14,2)); 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<Point, Pair<Player, Box>> getBoard() {
return board;
}
public void printGrid() { public void printGrid() {
for (int i = 0; i < x; i++) { for (int i = 0; i < row; i++) {
System.out.print("\n"); System.out.print("\n");
for (int j = 0; j < y; j++) { for (int j = 0; j < column; j++) {
Pair<Player, Box> value = board.get(new Point(i, j)); Pair<Player, Box> value = board.get(new Point(i, j));
if(value.getA() != null){ if(value.getA() != null){
System.out.print(" \033[0;34mP\033[0m"); System.out.print(" \033[0;34mP\033[0m");

View File

@ -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.action.Action;
import fr.lnl.game.server.games.weapon.Weapon; import fr.lnl.game.server.games.weapon.Weapon;
import fr.lnl.game.server.utils.Point;
public abstract class AbstractPlayer implements Player { public abstract class AbstractPlayer implements Player {
private int id; private int id;
private Point point;
private int energy; private int energy;
private Weapon weapon; private Weapon weapon;
private boolean shieldDeploy; private boolean shieldDeploy;
private Action[] actions; private Action[] actions;
private ClassPlayer classPlayer; 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.id = id;
this.classPlayer = classPlayer; this.classPlayer = classPlayer;
this.energy = classPlayer.getEnergy(); this.energy = classPlayer.getEnergy();
this.weapon = classPlayer.getWeapon(); this.weapon = classPlayer.getWeapon();
this.shieldDeploy = shieldDeploy; this.shieldDeploy = shieldDeploy;
this.point = point;
} }
public boolean isAlive(){ public boolean isAlive(){
@ -59,4 +62,12 @@ public abstract class AbstractPlayer implements Player {
public ClassPlayer getClassPlayer() { public ClassPlayer getClassPlayer() {
return classPlayer; return classPlayer;
} }
public Point getPoint() {
return point;
}
public void setPoint(Point point){
this.point = point;
}
} }

View File

@ -1,12 +1,10 @@
package fr.lnl.game.server.games.player; package fr.lnl.game.server.games.player;
import fr.lnl.game.server.utils.Point;
public class ComputerPlayer extends AbstractPlayer{ public class ComputerPlayer extends AbstractPlayer{
public ComputerPlayer(int id, boolean shieldDeploy, ClassPlayer classPlayer) { public ComputerPlayer(int id, Point point, ClassPlayer classPlayer) {
super(id, shieldDeploy, classPlayer); super(id,point,false, classPlayer);
}
public ComputerPlayer(int id, ClassPlayer classPlayer) {
super(id,false, classPlayer);
} }
} }

View File

@ -1,13 +1,11 @@
package fr.lnl.game.server.games.player; package fr.lnl.game.server.games.player;
import fr.lnl.game.server.utils.Point;
public class HumanPlayer extends AbstractPlayer { public class HumanPlayer extends AbstractPlayer {
public HumanPlayer(int id, boolean shieldDeploy, ClassPlayer classPlayer) { public HumanPlayer(int id, Point point, ClassPlayer classPlayer) {
super(id, shieldDeploy, classPlayer); super(id, point,false, classPlayer);
}
public HumanPlayer(int id, ClassPlayer classPlayer) {
super(id, false, classPlayer);
} }
} }

View File

@ -1,4 +1,7 @@
package fr.lnl.game.server.games.player; package fr.lnl.game.server.games.player;
import fr.lnl.game.server.utils.Point;
public interface Player { public interface Player {
Point getPoint();
} }