add comments to Grid package, delete arguments to GridFacotryBuilder's methods where it's unnecessary
This commit is contained in:
parent
03be6c2a78
commit
ea07ca0c6a
@ -1,4 +1,5 @@
|
|||||||
package fr.lnl.game.server.games.grid;
|
package fr.lnl.game.server.games.grid;
|
||||||
|
|
||||||
import fr.lnl.game.server.games.grid.elements.*;
|
import fr.lnl.game.server.games.grid.elements.*;
|
||||||
import fr.lnl.game.server.games.player.Player;
|
import fr.lnl.game.server.games.player.Player;
|
||||||
import fr.lnl.game.server.utils.Pair;
|
import fr.lnl.game.server.utils.Pair;
|
||||||
@ -8,7 +9,9 @@ import java.util.ArrayList;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class managing the board
|
||||||
|
*/
|
||||||
public class Grid {
|
public class Grid {
|
||||||
private final HashMap<Point, Pair<Player, Box>> board;
|
private final HashMap<Point, Pair<Player, Box>> board;
|
||||||
private final int row;
|
private final int row;
|
||||||
@ -22,18 +25,32 @@ public class Grid {
|
|||||||
board = new HashMap<>();
|
board = new HashMap<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see Grid#boardPositionIsValid(int, int)
|
||||||
|
*/
|
||||||
public boolean boardPositionIsValid(int row, int deltaRow, int column, int deltaColumn){
|
public boolean boardPositionIsValid(int row, int deltaRow, int column, int deltaColumn){
|
||||||
return boardPositionIsValid(row + deltaRow, column + deltaColumn);
|
return boardPositionIsValid(row + deltaRow, column + deltaColumn);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if given position is in grid area
|
||||||
|
* @return true if position is valid, false otehrwise
|
||||||
|
*/
|
||||||
public boolean boardPositionIsValid(int row, int column) {
|
public boolean boardPositionIsValid(int row, int column) {
|
||||||
return row >= 0 && column >= 0 && row < this.row && column < this.column;
|
return row >= 0 && column >= 0 && row < this.row && column < this.column;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see Grid#boardPositionIsValid(int, int)
|
||||||
|
*/
|
||||||
public boolean boardPositionIsValid(Point point) {
|
public boolean boardPositionIsValid(Point point) {
|
||||||
return boardPositionIsValid(point.getA(), point.getB());
|
return boardPositionIsValid(point.getA(), point.getB());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the number of neutral box
|
||||||
|
* @see Grid#isNeutralBox(Box)
|
||||||
|
*/
|
||||||
public int getNumberNeutralBox(){
|
public int getNumberNeutralBox(){
|
||||||
int countBox = 0;
|
int countBox = 0;
|
||||||
for (int i = 1; i < row - 1; i++) {
|
for (int i = 1; i < row - 1; i++) {
|
||||||
@ -47,10 +64,21 @@ public class Grid {
|
|||||||
return countBox;
|
return countBox;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param box the box we'll look state
|
||||||
|
* @return true if {@code box} isn't a {@link Wall} or a {@link EnergyBall}
|
||||||
|
*/
|
||||||
public boolean isNeutralBox(Box box){
|
public boolean isNeutralBox(Box box){
|
||||||
return !(box instanceof Wall) && !(box instanceof EnergyBall);
|
return !(box instanceof Wall) && !(box instanceof EnergyBall);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Given a string representation of the board.<br>
|
||||||
|
* Some characters in given string are in UTF-8 and can be poorly displayed if using an incompatible encoding, like
|
||||||
|
* on Windows where french regional encoding is {@code windows-1252}
|
||||||
|
* @return a string view of a board
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
StringBuilder str = new StringBuilder();
|
StringBuilder str = new StringBuilder();
|
||||||
|
@ -17,35 +17,64 @@ public abstract class AbstractGridFactoryBuilder implements GridFactoryBuilder {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param probability wall probability, which will be used by
|
||||||
|
* {@link AbstractGridFactoryBuilder#initPlaceInternWall()} when calling {@link GridFactoryBuilder#build()}
|
||||||
|
* @return {@code this}
|
||||||
|
* @see GridFactoryBuilder#wallProbability(float)
|
||||||
|
*/
|
||||||
public GridFactoryBuilder wallProbability(float probability) {
|
public GridFactoryBuilder wallProbability(float probability) {
|
||||||
this.wallProbability = probability;
|
this.wallProbability = probability;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param probability energy probability, which will be used by
|
||||||
|
* {@link AbstractGridFactoryBuilder#initPlaceEnergyBall()} when calling {@link GridFactoryBuilder#build()}
|
||||||
|
* @return {@code this}
|
||||||
|
* @see GridFactoryBuilder#energyProbability(float)
|
||||||
|
*/
|
||||||
public GridFactoryBuilder energyProbability(float probability) {
|
public GridFactoryBuilder energyProbability(float probability) {
|
||||||
this.energyProbability = probability;
|
this.energyProbability = probability;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param row row grid's size
|
||||||
|
* @param columns columns grid's size
|
||||||
|
* @return {@code this}
|
||||||
|
* @see GridFactoryBuilder#gridDimensions(int, int)
|
||||||
|
*/
|
||||||
public GridFactoryBuilder gridDimensions(int row, int columns) {
|
public GridFactoryBuilder gridDimensions(int row, int columns) {
|
||||||
this.row = row;
|
this.row = row;
|
||||||
this.columns = columns;
|
this.columns = columns;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param players list a players
|
||||||
|
* @return {@code this}
|
||||||
|
* @see GridFactoryBuilder#playersList(List)
|
||||||
|
*/
|
||||||
public GridFactoryBuilder playersList(List<Player> players) {
|
public GridFactoryBuilder playersList(List<Player> players) {
|
||||||
this.players = players;
|
this.players = players;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Call this method after you call {{@link GridFactoryBuilder#energyProbability(float)}},
|
||||||
|
* {@link GridFactoryBuilder#wallProbability(float)}, {@link GridFactoryBuilder#gridDimensions(int, int)} and
|
||||||
|
* {@link GridFactoryBuilder#playersList(List)}. It'll instantiate a new {@link Grid} and initialize his components
|
||||||
|
* like border walls, intern walls, and energy using parameters given previously
|
||||||
|
* @return an instance of {@link Grid}
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Grid build() {
|
public Grid build() {
|
||||||
this.grid = new Grid(row, columns, players);
|
this.grid = new Grid(row, columns, players);
|
||||||
initGrid();
|
initGrid();
|
||||||
initPlaceInternWall(wallProbability);
|
initPlaceInternWall();
|
||||||
initPlaceEnergyBall(energyProbability);
|
initPlaceEnergyBall();
|
||||||
return grid;
|
return grid;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -53,9 +82,25 @@ public abstract class AbstractGridFactoryBuilder implements GridFactoryBuilder {
|
|||||||
return grid;
|
return grid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* abstract method used to initialize grid<br>
|
||||||
|
* We let implementation of this class the way to initialize the grid
|
||||||
|
*/
|
||||||
protected abstract void initGrid();
|
protected abstract void initGrid();
|
||||||
protected abstract void initPlaceEnergyBall(float probability);
|
|
||||||
protected abstract void initPlaceInternWall(float probability);
|
/**
|
||||||
|
* abstract method used to place energy ball<br>
|
||||||
|
* We let implementation of this class the way to initialize the grid<br>
|
||||||
|
* Implementation need to use {@link AbstractGridFactoryBuilder#energyProbability} to place energy balls
|
||||||
|
*/
|
||||||
|
protected abstract void initPlaceEnergyBall();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* abstract method used to place intern walls(namely not border walls)<br>
|
||||||
|
* We let implementation of this class the way to initialize the grid.<br>
|
||||||
|
* Implementation need to use {@link AbstractGridFactoryBuilder#wallProbability} to place energy balls
|
||||||
|
*/
|
||||||
|
protected abstract void initPlaceInternWall();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -5,13 +5,56 @@ import fr.lnl.game.server.games.player.Player;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Factory pattern
|
||||||
|
*/
|
||||||
public interface GridFactoryBuilder {
|
public interface GridFactoryBuilder {
|
||||||
|
|
||||||
Grid build();
|
/**
|
||||||
void initPlacePlayers();
|
* Set energy probability<br>
|
||||||
|
* @param probability energy probability, which will be used by
|
||||||
|
* {@link AbstractGridFactoryBuilder#initPlaceEnergyBall()} when calling {@link GridFactoryBuilder#build()}
|
||||||
|
* @return {@code this}
|
||||||
|
*/
|
||||||
GridFactoryBuilder energyProbability(float probability);
|
GridFactoryBuilder energyProbability(float probability);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set wall probability<br>
|
||||||
|
* @param probability wall probability, which will be used by
|
||||||
|
* {@link AbstractGridFactoryBuilder#initPlaceInternWall()} when calling {@link GridFactoryBuilder#build()}
|
||||||
|
* @return {@code this}
|
||||||
|
*/
|
||||||
GridFactoryBuilder wallProbability(float probability);
|
GridFactoryBuilder wallProbability(float probability);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set grid dimensions.
|
||||||
|
* Arguments are given when instancing {@link Grid} when using {@link GridFactoryBuilder#build()}
|
||||||
|
* @param row row grid's size
|
||||||
|
* @param columns columns grid's size
|
||||||
|
* @return {@code this}
|
||||||
|
*/
|
||||||
GridFactoryBuilder gridDimensions(int row, int columns);
|
GridFactoryBuilder gridDimensions(int row, int columns);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set players list.
|
||||||
|
* Argument given to {@link Grid} when calling {@link GridFactoryBuilder#build()}
|
||||||
|
* @param players list a players
|
||||||
|
* @return {@code this}
|
||||||
|
*/
|
||||||
GridFactoryBuilder playersList(List<Player> players);
|
GridFactoryBuilder playersList(List<Player> players);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Call this method after you call {{@link GridFactoryBuilder#energyProbability(float)}},
|
||||||
|
* {@link GridFactoryBuilder#wallProbability(float)}, {@link GridFactoryBuilder#gridDimensions(int, int)} and
|
||||||
|
* {@link GridFactoryBuilder#playersList(List)}, It'll instantiate a new {@link Grid} and initialize his components
|
||||||
|
* like border walls, intern walls, and energy using parameters given previously
|
||||||
|
* @return an instance of {@link Grid}
|
||||||
|
*/
|
||||||
|
Grid build();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* call when initializing the game, it'll place player depending on the strategy used by its implementation
|
||||||
|
*/
|
||||||
|
void initPlacePlayers();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -13,14 +13,25 @@ import java.util.Random;
|
|||||||
|
|
||||||
public class LockGridFactoryBuilder extends AbstractGridFactoryBuilder {
|
public class LockGridFactoryBuilder extends AbstractGridFactoryBuilder {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* this method is protected to avoid new instance outside of {@link GridFactoryBuilder} context
|
||||||
|
*/
|
||||||
protected LockGridFactoryBuilder() {
|
protected LockGridFactoryBuilder() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return a new instance of {@link LockGridFactoryBuilder} used to construct a {@link Grid}
|
||||||
|
*/
|
||||||
public static GridFactoryBuilder create() {
|
public static GridFactoryBuilder create() {
|
||||||
return new LockGridFactoryBuilder();
|
return new LockGridFactoryBuilder();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method used to initialize grid<br>
|
||||||
|
* We place walls on every border, and we initialize board value as null where there is no need o border walls
|
||||||
|
* @see AbstractGridFactoryBuilder#initGrid()
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected void initGrid() {
|
protected void initGrid() {
|
||||||
for (int i = 0; i < getGrid().getRow(); i++) {
|
for (int i = 0; i < getGrid().getRow(); i++) {
|
||||||
@ -50,11 +61,16 @@ public class LockGridFactoryBuilder extends AbstractGridFactoryBuilder {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method used to initialize energy balls, we place energy balls using a random value and a probability and where
|
||||||
|
* there is no walls
|
||||||
|
* @see AbstractGridFactoryBuilder#initPlaceEnergyBall()
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected void initPlaceEnergyBall(float probability) {
|
protected void initPlaceEnergyBall() {
|
||||||
for (int i = 1; i < getGrid().getRow() - 1; i++) {
|
for (int i = 1; i < getGrid().getRow() - 1; i++) {
|
||||||
for (int j = 1; j < getGrid().getColumn() - 1; j++) {
|
for (int j = 1; j < getGrid().getColumn() - 1; j++) {
|
||||||
if(Math.random() >= probability){
|
if(Math.random() >= energyProbability){
|
||||||
Point point = new Point(i,j);
|
Point point = new Point(i,j);
|
||||||
if(!(getGrid().getBoard().get(point).getB() instanceof Wall)){
|
if(!(getGrid().getBoard().get(point).getB() instanceof Wall)){
|
||||||
getGrid().getBoard().get(point).setB(new EnergyBall());
|
getGrid().getBoard().get(point).setB(new EnergyBall());
|
||||||
@ -64,11 +80,16 @@ public class LockGridFactoryBuilder extends AbstractGridFactoryBuilder {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method used to place intern walls (opposite of border walls), we place walls using a random value and a
|
||||||
|
* probability and where there is no walls or where the position haven't a lock
|
||||||
|
* @see LockGridFactoryBuilder#getIllusionNumberWallNeighbour(Point)
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected void initPlaceInternWall(float probability) {
|
protected void initPlaceInternWall() {
|
||||||
for (int i = 1; i < getGrid().getRow() - 1; i++) {
|
for (int i = 1; i < getGrid().getRow() - 1; i++) {
|
||||||
for (int j = 1; j < getGrid().getColumn() - 1; j++) {
|
for (int j = 1; j < getGrid().getColumn() - 1; j++) {
|
||||||
if(Math.random() >= probability){
|
if(Math.random() >= wallProbability){
|
||||||
Point point = new Point(i,j);
|
Point point = new Point(i,j);
|
||||||
if(getIllusionNumberWallNeighbour(point) <= 3){
|
if(getIllusionNumberWallNeighbour(point) <= 3){
|
||||||
getGrid().getBoard().get(point).setB(new Wall(i,j));
|
getGrid().getBoard().get(point).setB(new Wall(i,j));
|
||||||
@ -82,6 +103,9 @@ public class LockGridFactoryBuilder extends AbstractGridFactoryBuilder {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Simply place player randomly where it's possible
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void initPlacePlayers() {
|
public void initPlacePlayers() {
|
||||||
Random random = new Random();
|
Random random = new Random();
|
||||||
@ -102,6 +126,12 @@ public class LockGridFactoryBuilder extends AbstractGridFactoryBuilder {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A locked place is used to try to avoid player to be blocked when playing,
|
||||||
|
* A locked place can't have a wall on it
|
||||||
|
* @param point the position where we want to place a new wall
|
||||||
|
* @return number of walls and locked place around {@code position}
|
||||||
|
*/
|
||||||
public int getIllusionNumberWallNeighbour(Point point){
|
public int getIllusionNumberWallNeighbour(Point point){
|
||||||
int countWall = 0;
|
int countWall = 0;
|
||||||
for (int deltaRow = -1; deltaRow <= 1; deltaRow++){
|
for (int deltaRow = -1; deltaRow <= 1; deltaRow++){
|
||||||
|
@ -18,13 +18,13 @@ public class MockGridFactoryBuilder extends LockGridFactoryBuilder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void initPlaceEnergyBall(float probability) {
|
protected void initPlaceEnergyBall() {
|
||||||
grid.getBoard().get(new Point(2,3)).setB(new EnergyBall());
|
grid.getBoard().get(new Point(2,3)).setB(new EnergyBall());
|
||||||
grid.getBoard().get(new Point(8,10)).setB(new EnergyBall());
|
grid.getBoard().get(new Point(8,10)).setB(new EnergyBall());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void initPlaceInternWall(float probability) {
|
protected void initPlaceInternWall() {
|
||||||
grid.getBoard().get(new Point(3,6)).setB(new Wall(3,6));
|
grid.getBoard().get(new Point(3,6)).setB(new Wall(3,6));
|
||||||
grid.getBoard().get(new Point(7,14)).setB(new Wall(7,14));
|
grid.getBoard().get(new Point(7,14)).setB(new Wall(7,14));
|
||||||
grid.getBoard().get(new Point(10,7)).setB(new Wall(10,7));
|
grid.getBoard().get(new Point(10,7)).setB(new Wall(10,7));
|
||||||
|
Loading…
Reference in New Issue
Block a user