Add buildGrid Strategy Pattern

This commit is contained in:
Katchan 2021-12-04 15:17:41 +01:00
parent daa07887f7
commit f4477ac1e0
21 changed files with 61 additions and 148 deletions

View File

@ -1,9 +1,8 @@
package fr.lnl.game.client.view;
import fr.lnl.game.server.games.grid.Bomb;
import fr.lnl.game.server.games.grid.EnergyBall;
import fr.lnl.game.server.games.grid.Mine;
import fr.lnl.game.server.games.grid.Wall;
import fr.lnl.game.server.games.grid.elements.Bomb;
import fr.lnl.game.server.games.grid.elements.EnergyBall;
import fr.lnl.game.server.games.grid.elements.Mine;
import fr.lnl.game.server.games.player.Player;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

View File

@ -2,6 +2,7 @@ package fr.lnl.game.client.view;
import fr.lnl.game.server.games.grid.*;
import fr.lnl.game.server.games.grid.elements.*;
import fr.lnl.game.server.games.player.ClassPlayer;
import fr.lnl.game.server.games.player.Player;
import fr.lnl.game.server.games.player.RandomComputerPlayer;

View File

@ -2,6 +2,7 @@ package fr.lnl.game.server.games;
import fr.lnl.game.server.games.action.*;
import fr.lnl.game.server.games.grid.Grid;
import fr.lnl.game.server.games.grid.build.BuildStrategy;
import fr.lnl.game.server.games.player.ComputerPlayer;
import fr.lnl.game.server.games.player.HumanPlayer;
import fr.lnl.game.server.games.player.Player;
@ -16,23 +17,30 @@ import java.util.stream.Stream;
public class Game {
private final BuildStrategy buildStrategy;
private final Grid grid;
private final List<Player> players;
private final ModelListener gameFinishEvent;
private Player currentPlayer;
private Action selectedAction = null;
public Game(Grid grid, List<Player> players, ModelListener gameFinishEvent) throws IllegalArgumentException {
public Game(BuildStrategy buildStrategy, List<Player> players, ModelListener gameFinishEvent) throws IllegalArgumentException {
this.grid = buildStrategy.getGrid();
this.buildStrategy = buildStrategy;
this.players = players;
this.currentPlayer = players.get(0);
this.gameFinishEvent = gameFinishEvent;
initGame();
}
public void initGame(){
buildStrategy.build();
if(players.size() < 2)
throw new IllegalArgumentException("The game need 2 or more player to start");
if(players.size() > grid.getNumberNeutralBox()){
throw new IllegalArgumentException("There are too many players for the number of box available");
}
this.players = players;
this.currentPlayer = players.get(0);
this.grid = grid;
this.gameFinishEvent = gameFinishEvent;
this.grid.initPlacePlayers();
buildStrategy.initPlacePlayers();
currentPlayer.setActions(generateAndGetPlayerActions(currentPlayer));
}

View File

@ -1,7 +1,7 @@
package fr.lnl.game.server.games.action;
import fr.lnl.game.server.games.Game;
import fr.lnl.game.server.games.grid.Bomb;
import fr.lnl.game.server.games.grid.elements.Bomb;
import fr.lnl.game.server.games.player.Player;
public class DropBomb extends DropObject {

View File

@ -1,7 +1,7 @@
package fr.lnl.game.server.games.action;
import fr.lnl.game.server.games.Game;
import fr.lnl.game.server.games.grid.Mine;
import fr.lnl.game.server.games.grid.elements.Mine;
import fr.lnl.game.server.games.player.Player;
public class DropMine extends DropObject {

View File

@ -1,7 +1,7 @@
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.elements.Box;
import fr.lnl.game.server.games.player.Player;
import fr.lnl.game.server.utils.Pair;
import fr.lnl.game.server.utils.Point;

View File

@ -1,9 +1,9 @@
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.InteractiveBox;
import fr.lnl.game.server.games.grid.Wall;
import fr.lnl.game.server.games.grid.elements.Box;
import fr.lnl.game.server.games.grid.elements.InteractiveBox;
import fr.lnl.game.server.games.grid.elements.Wall;
import fr.lnl.game.server.games.player.Player;
import fr.lnl.game.server.utils.Pair;
import fr.lnl.game.server.utils.Point;

View File

@ -1,7 +1,7 @@
package fr.lnl.game.server.games.action;
import fr.lnl.game.server.games.Game;
import fr.lnl.game.server.games.grid.Wall;
import fr.lnl.game.server.games.grid.elements.Wall;
import fr.lnl.game.server.games.player.Player;
import fr.lnl.game.server.games.weapon.Weapon;
import fr.lnl.game.server.utils.Point;

View File

@ -1,12 +1,11 @@
package fr.lnl.game.server.games.grid;
import fr.lnl.game.server.games.grid.elements.*;
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.List;
import java.util.Random;
public class Grid {
@ -15,96 +14,11 @@ public class Grid {
private final int column;
private final List<Player> players;
public Grid(int row, int column, List<Player> players, float wallProbability, float energyProbability) {
public Grid(int row, int column, List<Player> players) {
this.row = row;
this.column = column;
this.players = players;
board = new HashMap<>();
initBoard(wallProbability, energyProbability);
}
public void initBoard(float wallProbability, float energyProbability){
initGrid();
initPlaceInternWall(wallProbability);
initPlaceEnergyBall(energyProbability);
}
public void initGrid(){
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
Box box;
if (i == 0 && j == 0) {
box = new Wall(i, j);
} else if (i == 0 && j == column-1) {
box = new Wall(i, j);
} else if (i == row-1 && j == 0) {
box = new Wall(i, j);
} else if (i == row-1 && j == column-1) {
box = new Wall(i, j);
} else if (i == 0) {
box = new Wall(i, j);
} else if (i == row-1) {
box = new Wall(i, j);
} else if (j == 0) {
box = new Wall(i, j);
} else if (j == column-1) {
box = new Wall(i, j);
} else {
box = null;
}
board.put(new Point(i,j), new Pair<>(null,box));
}
}
}
public void initPlacePlayers(){
Random random = new Random();
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 initPlaceEnergyBall(float probability){
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 initPlaceInternWall(float probability){
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(getIllusionNumberWallNeighbour(point) <= 3){
getBoard().get(point).setB(new Wall(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){
@ -119,29 +33,11 @@ public class Grid {
return boardPositionIsValid(point.getA(), point.getB());
}
public int getIllusionNumberWallNeighbour(Point point){
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 int getNumberNeutralBox(){
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();
Box box = board.get(new Point(i,j)).getB();
if(isNeutralBox(box)){
countBox++;
}
@ -154,10 +50,6 @@ public class Grid {
return !(box instanceof Wall) && !(box instanceof EnergyBall);
}
public HashMap<Point, Pair<Player, Box>> getBoard() {
return board;
}
@Override
public String toString() {
StringBuilder str = new StringBuilder();
@ -188,6 +80,10 @@ public class Grid {
return str.toString();
}
public HashMap<Point, Pair<Player, Box>> getBoard() {
return board;
}
public List<Player> getPlayers() {
return players;
}
@ -199,4 +95,5 @@ public class Grid {
public int getColumn() {
return column;
}
}

View File

@ -1,4 +1,4 @@
package fr.lnl.game.server.games.grid;
package fr.lnl.game.server.games.grid.elements;
public class AbstractBox implements Box{

View File

@ -1,5 +1,6 @@
package fr.lnl.game.server.games.grid;
package fr.lnl.game.server.games.grid.elements;
import fr.lnl.game.server.games.grid.Grid;
import fr.lnl.game.server.games.player.Player;
import fr.lnl.game.server.utils.Point;

View File

@ -1,4 +1,4 @@
package fr.lnl.game.server.games.grid;
package fr.lnl.game.server.games.grid.elements;
public interface Box {

View File

@ -1,5 +1,6 @@
package fr.lnl.game.server.games.grid;
package fr.lnl.game.server.games.grid.elements;
import fr.lnl.game.server.games.grid.Grid;
import fr.lnl.game.server.games.player.Player;
import fr.lnl.game.server.utils.Point;

View File

@ -1,5 +1,6 @@
package fr.lnl.game.server.games.grid;
package fr.lnl.game.server.games.grid.elements;
import fr.lnl.game.server.games.grid.Grid;
import fr.lnl.game.server.games.player.Player;
import fr.lnl.game.server.utils.Point;

View File

@ -1,5 +1,6 @@
package fr.lnl.game.server.games.grid;
package fr.lnl.game.server.games.grid.elements;
import fr.lnl.game.server.games.grid.Grid;
import fr.lnl.game.server.games.player.Player;
import fr.lnl.game.server.utils.Point;

View File

@ -1,5 +1,6 @@
package fr.lnl.game.server.games.grid;
package fr.lnl.game.server.games.grid.elements;
import fr.lnl.game.server.games.grid.Grid;
import fr.lnl.game.server.games.player.Player;
import fr.lnl.game.server.utils.Point;

View File

@ -1,6 +1,4 @@
package fr.lnl.game.server.games.grid;
import java.util.Objects;
package fr.lnl.game.server.games.grid.elements;
public class Wall extends AbstractBox {

View File

@ -7,4 +7,5 @@ module server {
exports fr.lnl.game.server.games.action;
exports fr.lnl.game.server.utils;
exports fr.lnl.game.server.listener;
exports fr.lnl.game.server.games.grid.elements;
}

View File

@ -2,7 +2,7 @@ package fr.lnl.game.server;
import fr.lnl.game.server.games.Game;
import fr.lnl.game.server.games.action.*;
import fr.lnl.game.server.games.grid.Bomb;
import fr.lnl.game.server.games.grid.elements.Bomb;
import fr.lnl.game.server.games.grid.Grid;
import fr.lnl.game.server.games.player.Player;
import fr.lnl.game.server.utils.Point;

View File

@ -1,10 +1,10 @@
package fr.lnl.game.server;
import fr.lnl.game.server.games.Game;
import fr.lnl.game.server.games.grid.Box;
import fr.lnl.game.server.games.grid.EnergyBall;
import fr.lnl.game.server.games.grid.elements.Box;
import fr.lnl.game.server.games.grid.elements.EnergyBall;
import fr.lnl.game.server.games.grid.Grid;
import fr.lnl.game.server.games.grid.Wall;
import fr.lnl.game.server.games.grid.elements.Wall;
import fr.lnl.game.server.games.player.Player;
import fr.lnl.game.server.utils.Pair;
import fr.lnl.game.server.utils.Point;
@ -55,7 +55,7 @@ public class GridTest {
str.append(" \033[0;35mL\033[0m");
}
if(value.getB() instanceof Wall){
str.append(" \033[0;32mW\033[0m");
str.append(" \033[0;32m\033[0m");
}
if(value.getB() instanceof EnergyBall){
str.append(" \033[0;31mE\033[0m");

View File

@ -1,9 +1,11 @@
package fr.lnl.game.server;
import fr.lnl.game.server.games.Game;
import fr.lnl.game.server.games.grid.EnergyBall;
import fr.lnl.game.server.games.grid.build.BuildStrategy;
import fr.lnl.game.server.games.grid.build.LockStrategy;
import fr.lnl.game.server.games.grid.elements.EnergyBall;
import fr.lnl.game.server.games.grid.Grid;
import fr.lnl.game.server.games.grid.Wall;
import fr.lnl.game.server.games.grid.elements.Wall;
import fr.lnl.game.server.games.player.ClassPlayer;
import fr.lnl.game.server.games.player.Player;
import fr.lnl.game.server.games.player.RandomComputerPlayer;
@ -15,14 +17,16 @@ import java.util.List;
public class Mock {
public Grid grid;
public BuildStrategy buildStrategy;
public Game game;
public Grid grid;
public Mock() {
List<Player> players = Arrays.asList(new RandomComputerPlayer(1,null, ClassPlayer.DEFAULT),
new RandomComputerPlayer(2,null, ClassPlayer.DEFAULT));
this.grid = new Grid(16,16, players,0.80F, 0.95F);
game = new Game(grid, players, new GameFinishEvent());
this.buildStrategy = new LockStrategy(new Grid(16,16, players),0.80F, 0.95F);
game = new Game(buildStrategy, players, new GameFinishEvent());
this.grid = buildStrategy.getGrid();
}
public void placePlayersBRUT(){