Add placement rules for walls, energyballs and players

This commit is contained in:
Katchan 2021-12-02 19:59:53 +01:00
parent 572a20bcf7
commit d40dedfc17
2 changed files with 86 additions and 26 deletions

View File

@ -29,27 +29,19 @@ public class Game {
private final Object lock = new Object(); private final Object lock = new Object();
public Game(Grid grid, List<Player> players, ModelListener viewUpdate, ModelListener gameFinishEvent) throws IllegalArgumentException { public Game(Grid grid, List<Player> players, ModelListener viewUpdate, ModelListener gameFinishEvent) throws IllegalArgumentException {
if(players.size() < 2) if(players.size() < 2 || grid.getNumberNeutralBox() < players.size())
throw new IllegalArgumentException("The game need 2 or more player to start"); throw new IllegalArgumentException("The game need 2 or more player to start");
this.players = players; this.players = players;
this.currentPlayer = players.get(0); this.currentPlayer = players.get(0);
this.grid = grid; this.grid = grid;
this.viewUpdateEvent = viewUpdate; this.viewUpdateEvent = viewUpdate;
this.gameFinishEvent = gameFinishEvent; this.gameFinishEvent = gameFinishEvent;
placePlayersBRUT(); grid.initPlacePlayers();
} }
/** /**
* @deprecated utiliser pour le moment, nécessite une meilleure implémentation pour savoir ou placé les joueurs * @deprecated utiliser pour le moment, nécessite une meilleure implémentation pour savoir ou placé les joueurs
*/ */
@Deprecated
public void placePlayersBRUT(){
grid.getBoard().get(new Point(7,7)).setA(grid.getPlayers().get(0));
grid.getPlayers().get(0).setPosition(new Point(7, 7));
grid.getBoard().get(new Point(7,8)).setA(grid.getPlayers().get(1));
grid.getPlayers().get(1).setPosition(new Point(7, 8));
}
public void play() { public void play() {
while(!isOver()) { while(!isOver()) {

View File

@ -1,5 +1,6 @@
package fr.lnl.game.server.games.grid; package fr.lnl.game.server.games.grid;
import fr.lnl.game.server.games.player.AbstractPlayer;
import fr.lnl.game.server.games.player.Player; import fr.lnl.game.server.games.player.Player;
import fr.lnl.game.server.utils.Cardinal; import fr.lnl.game.server.utils.Cardinal;
import fr.lnl.game.server.utils.Pair; import fr.lnl.game.server.utils.Pair;
@ -7,6 +8,7 @@ import fr.lnl.game.server.utils.Point;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Random;
public class Grid { public class Grid {
@ -15,12 +17,18 @@ public class Grid {
private final int column; private final int column;
private final List<Player> players; private final List<Player> players;
public Grid(int row, int column, List<Player> players) { public Grid(int row, int column, List<Player> players, float wallProbability, float energyProbability) {
this.row = row; this.row = row;
this.column = column; this.column = column;
this.players = players; this.players = players;
board = new HashMap<>(); board = new HashMap<>();
initBoard(wallProbability, energyProbability);
}
public void initBoard(float wallProbability, float energyProbability){
initGrid(); initGrid();
initPlaceInternWall(wallProbability);
initPlaceEnergyBall(energyProbability);
} }
public void initGrid(){ public void initGrid(){
@ -51,21 +59,54 @@ public class Grid {
} }
} }
public void placePlayersBRUT(){ public void initPlacePlayers(){
board.get(new Point(1,1)).setA(players.get(0)); Random random = new Random();
board.get(new Point(14,14)).setA(players.get(1)); Box boxTargeted;
Player playerTargeted;
Point point;
for (Player player: players) {
do{
int i = random.nextInt(1,getRow() - 1);
int j = random.nextInt(1,getColumn() - 1);
point = new Point(i,j);
Pair<Player,Box> pairTargeted = getBoard().get(point);
boxTargeted = pairTargeted.getB();
playerTargeted = pairTargeted.getA();
}while(playerTargeted != null || !isNeutralBox(boxTargeted));
getBoard().get(point).setA(player);
player.setPosition(point);
}
} }
public void placeEnergyBallBRUT(){
board.get(new Point(2,3)).setB(new EnergyBall()); public void initPlaceEnergyBall(float probability){
board.get(new Point(7,10)).setB(new EnergyBall()); for (int i = 1; i < row - 1; i++) {
for (int j = 1; j < column - 1; j++) {
if(Math.random() >= probability){
Point point = new Point(i,j);
if(!(getBoard().get(point).getB() instanceof Wall)){
getBoard().get(point).setB(new EnergyBall());
}
}
}
}
} }
public void placeInternWallBRUT(){ public void initPlaceInternWall(float probability){
board.get(new Point(3,6)).setB(new Wall(Cardinal.NORTH,3,6)); for (int i = 1; i < row - 1; i++) {
board.get(new Point(7,14)).setB(new Wall(Cardinal.SOUTH,7,14)); for (int j = 1; j < column - 1; j++) {
board.get(new Point(10,7)).setB(new Wall(Cardinal.EAST,10,7)); if(Math.random() >= probability){
board.get(new Point(14,2)).setB(new Wall(Cardinal.WEST,14,2)); Point point = new Point(i,j);
if(getIllusionNumberWallNeighbour(point) <= 3){
getBoard().get(point).setB(new Wall(Cardinal.getRandom(),i,j));
}
else{
getBoard().get(point).setB(new AbstractBox());
getBoard().get(point).getB().setLock(true);
}
}
}
}
} }
public boolean boardPositionIsValid(int row, int deltaRow, int column, int deltaColumn){ public boolean boardPositionIsValid(int row, int deltaRow, int column, int deltaColumn){
@ -80,12 +121,39 @@ public class Grid {
return boardPositionIsValid(point.getA(), point.getB()); return boardPositionIsValid(point.getA(), point.getB());
} }
public boolean boardHorizontalIsValid(int column, int deltaColumn){ public int getIllusionNumberWallNeighbour(Point point){
return column + deltaColumn >= 0 && column + deltaColumn < this.column; int countWall = 0;
for (int deltaRow = -1; deltaRow <= 1; deltaRow++){
for (int deltaColomn = -1; deltaColomn <= 1; deltaColomn++) {
Point neighbour = new Point(point.getA() + deltaRow, point.getB() + deltaColomn);
if (boardPositionIsValid(neighbour)) {
Box box = getBoard().get(neighbour).getB();
if (box != null) {
if (box instanceof Wall || box.isLock()) {
countWall++;
}
}
}
}
}
return countWall;
} }
public boolean boardVerticalIsValid(int row, int deltaRow){ public int getNumberNeutralBox(){
return row + deltaRow >= 0 && row + deltaRow < this.row; int countBox = 0;
for (int i = 1; i < row - 1; i++) {
for (int j = 1; j < column - 1; j++) {
Box box = getBoard().get(new Point(i,j)).getB();
if(isNeutralBox(box)){
countBox++;
}
}
}
return countBox;
}
public boolean isNeutralBox(Box box){
return !(box instanceof Wall) && !(box instanceof EnergyBall);
} }
public HashMap<Point, Pair<Player, Box>> getBoard() { public HashMap<Point, Pair<Player, Box>> getBoard() {