Reworked GridStrategy and renamed it to GridFactoryBuilder

This commit is contained in:
Quentin Legot 2021-12-07 18:10:18 +01:00
parent cacf5ee8c3
commit bf37f32407
10 changed files with 111 additions and 75 deletions

4
.gitignore vendored
View File

@ -7,4 +7,6 @@
.gradle .gradle
# Ignore Gradle build output directory # Ignore Gradle build output directory
build server/build
client/build
buildSrc/build

View File

@ -7,10 +7,9 @@ import fr.lnl.game.client.view.ViewManager;
import fr.lnl.game.client.view.Window; import fr.lnl.game.client.view.Window;
import fr.lnl.game.server.games.Game; import fr.lnl.game.server.games.Game;
import fr.lnl.game.server.games.grid.Grid; import fr.lnl.game.server.games.grid.Grid;
import fr.lnl.game.server.games.grid.build.BuildStrategy; import fr.lnl.game.server.games.grid.build.GridFactoryBuilder;
import fr.lnl.game.server.games.grid.build.LockStrategy; import fr.lnl.game.server.games.grid.build.LockGridFactoryBuilder;
import fr.lnl.game.server.games.player.*; import fr.lnl.game.server.games.player.*;
import fr.lnl.game.server.listener.GameFinishEvent;
import fr.lnl.game.server.utils.CrashException; import fr.lnl.game.server.utils.CrashException;
import fr.lnl.game.server.utils.Point; import fr.lnl.game.server.utils.Point;
import javafx.application.Application; import javafx.application.Application;
@ -46,8 +45,8 @@ public class App extends Application {
InstantiationException, IllegalAccessException { InstantiationException, IllegalAccessException {
List<Player> players = parsePlayers(); List<Player> players = parsePlayers();
Grid grid = new Grid(12, 12, players); Grid grid = new Grid(12, 12, players);
BuildStrategy buildStrategy = new LockStrategy(grid, 0.80F, 0.95F); GridFactoryBuilder builder = LockGridFactoryBuilder.create().energyProbability(0.95F).wallProbability(0.80F).playersList(players).gridDimensions(12, 12);
game = new Game(buildStrategy, players, new DisplayWinnerEvent()); game = new Game(builder, players, new DisplayWinnerEvent());
for (Player player : game.getPlayers()) { for (Player player : game.getPlayers()) {
playerList.put(player, new ClientPlayer(player, lambda.createViewLambda(player))); playerList.put(player, new ClientPlayer(player, lambda.createViewLambda(player)));
} }

View File

@ -2,9 +2,8 @@ package fr.lnl.game.server.games;
import fr.lnl.game.server.games.action.*; import fr.lnl.game.server.games.action.*;
import fr.lnl.game.server.games.grid.Grid; import fr.lnl.game.server.games.grid.Grid;
import fr.lnl.game.server.games.grid.build.BuildStrategy; import fr.lnl.game.server.games.grid.build.GridFactoryBuilder;
import fr.lnl.game.server.games.grid.elements.CountdownBox; import fr.lnl.game.server.games.grid.elements.CountdownBox;
import fr.lnl.game.server.games.player.ComputerPlayer;
import fr.lnl.game.server.games.player.Player; import fr.lnl.game.server.games.player.Player;
import fr.lnl.game.server.listener.AbstractModelListening; import fr.lnl.game.server.listener.AbstractModelListening;
import fr.lnl.game.server.listener.GameFinishEvent; import fr.lnl.game.server.listener.GameFinishEvent;
@ -17,7 +16,7 @@ import java.util.stream.Stream;
public class Game { public class Game {
private final BuildStrategy buildStrategy; private final GridFactoryBuilder buildStrategy;
private final Grid grid; private final Grid grid;
private final List<Player> players; private final List<Player> players;
private final ModelListener gameFinishEvent; private final ModelListener gameFinishEvent;
@ -25,8 +24,8 @@ public class Game {
private Player currentPlayer; private Player currentPlayer;
private Action selectedAction = null; private Action selectedAction = null;
public Game(BuildStrategy buildStrategy, List<Player> players, AbstractModelListening displayWinnerEvent) throws IllegalArgumentException { public Game(GridFactoryBuilder buildStrategy, List<Player> players, AbstractModelListening displayWinnerEvent) throws IllegalArgumentException {
this.grid = buildStrategy.getGrid(); this.grid = buildStrategy.build();
if(players.size() < 2) if(players.size() < 2)
throw new IllegalArgumentException("The game need 2 or more player to start"); throw new IllegalArgumentException("The game need 2 or more player to start");
if(players.size() > grid.getNumberNeutralBox()){ if(players.size() > grid.getNumberNeutralBox()){

View File

@ -1,42 +0,0 @@
package fr.lnl.game.server.games.grid.build;
import fr.lnl.game.server.games.grid.Grid;
public abstract class AbstractBuildStrategy implements BuildStrategy{
private final float WALL_PROBABILITY, ENERGY_PROBABILITY;
protected final Grid grid;
public AbstractBuildStrategy(Grid grid, float wallProbability, float energyProbability){
this.grid = grid;
this.WALL_PROBABILITY = wallProbability;
this.ENERGY_PROBABILITY = energyProbability;
build();
}
@Override
public void build() {
initGrid();
initPlaceInternWall(WALL_PROBABILITY);
initPlaceEnergyBall(ENERGY_PROBABILITY);
}
private float getEnergyProbability() {
return ENERGY_PROBABILITY;
}
private float getWallProbability() {
return WALL_PROBABILITY;
}
public Grid getGrid() {
return grid;
}
protected abstract void initGrid();
protected abstract void initPlaceEnergyBall(float probability);
protected abstract void initPlaceInternWall(float probability);
}

View File

@ -0,0 +1,62 @@
package fr.lnl.game.server.games.grid.build;
import fr.lnl.game.server.games.grid.Grid;
import fr.lnl.game.server.games.player.Player;
import java.util.List;
public abstract class AbstractGridFactoryBuilder implements GridFactoryBuilder {
protected float wallProbability, energyProbability;
protected Grid grid;
private int row;
private int columns;
private List<Player> players;
protected AbstractGridFactoryBuilder() {
}
public GridFactoryBuilder wallProbability(float probability) {
this.wallProbability = probability;
return this;
}
public GridFactoryBuilder energyProbability(float probability) {
this.energyProbability = probability;
return this;
}
public GridFactoryBuilder gridDimensions(int row, int columns) {
this.row = row;
this.columns = columns;
return this;
}
public GridFactoryBuilder playersList(List<Player> players) {
this.players = players;
return this;
}
@Override
public Grid build() {
this.grid = new Grid(row, columns, players);
initGrid();
initPlaceInternWall(wallProbability);
initPlaceEnergyBall(energyProbability);
return grid;
}
protected Grid getGrid() {
return grid;
}
protected abstract void initGrid();
protected abstract void initPlaceEnergyBall(float probability);
protected abstract void initPlaceInternWall(float probability);
}

View File

@ -1,11 +0,0 @@
package fr.lnl.game.server.games.grid.build;
import fr.lnl.game.server.games.grid.Grid;
public interface BuildStrategy {
void build();
void initPlacePlayers();
Grid getGrid();
}

View File

@ -0,0 +1,17 @@
package fr.lnl.game.server.games.grid.build;
import fr.lnl.game.server.games.grid.Grid;
import fr.lnl.game.server.games.player.Player;
import java.util.List;
public interface GridFactoryBuilder {
Grid build();
void initPlacePlayers();
GridFactoryBuilder energyProbability(float probability);
GridFactoryBuilder wallProbability(float probability);
GridFactoryBuilder gridDimensions(int row, int columns);
GridFactoryBuilder playersList(List<Player> players);
}

View File

@ -11,9 +11,14 @@ import fr.lnl.game.server.utils.Point;
import java.util.Random; import java.util.Random;
public class LockStrategy extends AbstractBuildStrategy{ public class LockGridFactoryBuilder extends AbstractGridFactoryBuilder {
public LockStrategy(Grid grid, float wallProbability, float energyProbability) {
super(grid, wallProbability, energyProbability); protected LockGridFactoryBuilder() {
}
public static GridFactoryBuilder create() {
return new LockGridFactoryBuilder();
} }
@Override @Override

View File

@ -2,21 +2,21 @@ package fr.lnl.game.server.mock;
import fr.lnl.game.server.games.Game; import fr.lnl.game.server.games.Game;
import fr.lnl.game.server.games.grid.Grid; import fr.lnl.game.server.games.grid.Grid;
import fr.lnl.game.server.games.grid.build.BuildStrategy; import fr.lnl.game.server.games.grid.build.GridFactoryBuilder;
import fr.lnl.game.server.games.player.Player; import fr.lnl.game.server.games.player.Player;
import java.util.List; import java.util.List;
public class Mock { public class Mock {
public BuildStrategy buildStrategy; public GridFactoryBuilder buildStrategy;
public Game game; public Game game;
public Grid grid; public Grid grid;
public Mock(List<Player> players) { public Mock(List<Player> players) {
this.buildStrategy = new MockGridStrategy(new Grid(16,16, players),0.80F, 0.95F); this.buildStrategy = MockGridFactoryBuilder.create().gridDimensions(16, 16).playersList(players).wallProbability(0.80F).energyProbability(0.95F);
game = new Game(buildStrategy, players, new MockDisplayWinner()); game = new Game(buildStrategy, players, new MockDisplayWinner());
this.grid = buildStrategy.getGrid(); this.grid = game.getGrid();
} }
} }

View File

@ -1,15 +1,20 @@
package fr.lnl.game.server.mock; package fr.lnl.game.server.mock;
import fr.lnl.game.server.games.grid.Grid; import fr.lnl.game.server.games.grid.Grid;
import fr.lnl.game.server.games.grid.build.LockStrategy; import fr.lnl.game.server.games.grid.build.GridFactoryBuilder;
import fr.lnl.game.server.games.grid.build.LockGridFactoryBuilder;
import fr.lnl.game.server.games.grid.elements.EnergyBall; import fr.lnl.game.server.games.grid.elements.EnergyBall;
import fr.lnl.game.server.games.grid.elements.Wall; import fr.lnl.game.server.games.grid.elements.Wall;
import fr.lnl.game.server.utils.Point; import fr.lnl.game.server.utils.Point;
public class MockGridStrategy extends LockStrategy { public class MockGridFactoryBuilder extends LockGridFactoryBuilder {
public MockGridStrategy(Grid grid, float wallProbability, float energyProbability) { private MockGridFactoryBuilder() {
super(grid, wallProbability, energyProbability);
}
public static GridFactoryBuilder create() {
return new MockGridFactoryBuilder();
} }
@Override @Override