diff --git a/.gitignore b/.gitignore index ebc73ff..48cb567 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,6 @@ .gradle # Ignore Gradle build output directory -build +server/build +client/build +buildSrc/build diff --git a/client/src/main/java/fr/lnl/game/client/App.java b/client/src/main/java/fr/lnl/game/client/App.java index c6e3767..579fac2 100644 --- a/client/src/main/java/fr/lnl/game/client/App.java +++ b/client/src/main/java/fr/lnl/game/client/App.java @@ -7,10 +7,9 @@ import fr.lnl.game.client.view.ViewManager; import fr.lnl.game.client.view.Window; import fr.lnl.game.server.games.Game; 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.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.player.*; -import fr.lnl.game.server.listener.GameFinishEvent; import fr.lnl.game.server.utils.CrashException; import fr.lnl.game.server.utils.Point; import javafx.application.Application; @@ -46,8 +45,8 @@ public class App extends Application { InstantiationException, IllegalAccessException { List players = parsePlayers(); Grid grid = new Grid(12, 12, players); - BuildStrategy buildStrategy = new LockStrategy(grid, 0.80F, 0.95F); - game = new Game(buildStrategy, players, new DisplayWinnerEvent()); + GridFactoryBuilder builder = LockGridFactoryBuilder.create().energyProbability(0.95F).wallProbability(0.80F).playersList(players).gridDimensions(12, 12); + game = new Game(builder, players, new DisplayWinnerEvent()); for (Player player : game.getPlayers()) { playerList.put(player, new ClientPlayer(player, lambda.createViewLambda(player))); } diff --git a/server/src/main/java/fr/lnl/game/server/games/Game.java b/server/src/main/java/fr/lnl/game/server/games/Game.java index cb2beca..c7cd4f2 100644 --- a/server/src/main/java/fr/lnl/game/server/games/Game.java +++ b/server/src/main/java/fr/lnl/game/server/games/Game.java @@ -2,9 +2,8 @@ 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.grid.build.GridFactoryBuilder; 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.listener.AbstractModelListening; import fr.lnl.game.server.listener.GameFinishEvent; @@ -17,7 +16,7 @@ import java.util.stream.Stream; public class Game { - private final BuildStrategy buildStrategy; + private final GridFactoryBuilder buildStrategy; private final Grid grid; private final List players; private final ModelListener gameFinishEvent; @@ -25,8 +24,8 @@ public class Game { private Player currentPlayer; private Action selectedAction = null; - public Game(BuildStrategy buildStrategy, List players, AbstractModelListening displayWinnerEvent) throws IllegalArgumentException { - this.grid = buildStrategy.getGrid(); + public Game(GridFactoryBuilder buildStrategy, List players, AbstractModelListening displayWinnerEvent) throws IllegalArgumentException { + this.grid = buildStrategy.build(); if(players.size() < 2) throw new IllegalArgumentException("The game need 2 or more player to start"); if(players.size() > grid.getNumberNeutralBox()){ diff --git a/server/src/main/java/fr/lnl/game/server/games/grid/build/AbstractBuildStrategy.java b/server/src/main/java/fr/lnl/game/server/games/grid/build/AbstractBuildStrategy.java deleted file mode 100644 index 3575fdc..0000000 --- a/server/src/main/java/fr/lnl/game/server/games/grid/build/AbstractBuildStrategy.java +++ /dev/null @@ -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); - - - -} diff --git a/server/src/main/java/fr/lnl/game/server/games/grid/build/AbstractGridFactoryBuilder.java b/server/src/main/java/fr/lnl/game/server/games/grid/build/AbstractGridFactoryBuilder.java new file mode 100644 index 0000000..b55cf8e --- /dev/null +++ b/server/src/main/java/fr/lnl/game/server/games/grid/build/AbstractGridFactoryBuilder.java @@ -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 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 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); + + + +} diff --git a/server/src/main/java/fr/lnl/game/server/games/grid/build/BuildStrategy.java b/server/src/main/java/fr/lnl/game/server/games/grid/build/BuildStrategy.java deleted file mode 100644 index 9d0b144..0000000 --- a/server/src/main/java/fr/lnl/game/server/games/grid/build/BuildStrategy.java +++ /dev/null @@ -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(); - -} diff --git a/server/src/main/java/fr/lnl/game/server/games/grid/build/GridFactoryBuilder.java b/server/src/main/java/fr/lnl/game/server/games/grid/build/GridFactoryBuilder.java new file mode 100644 index 0000000..499e79c --- /dev/null +++ b/server/src/main/java/fr/lnl/game/server/games/grid/build/GridFactoryBuilder.java @@ -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 players); + +} diff --git a/server/src/main/java/fr/lnl/game/server/games/grid/build/LockStrategy.java b/server/src/main/java/fr/lnl/game/server/games/grid/build/LockGridFactoryBuilder.java similarity index 95% rename from server/src/main/java/fr/lnl/game/server/games/grid/build/LockStrategy.java rename to server/src/main/java/fr/lnl/game/server/games/grid/build/LockGridFactoryBuilder.java index b71661b..13d0976 100644 --- a/server/src/main/java/fr/lnl/game/server/games/grid/build/LockStrategy.java +++ b/server/src/main/java/fr/lnl/game/server/games/grid/build/LockGridFactoryBuilder.java @@ -11,9 +11,14 @@ import fr.lnl.game.server.utils.Point; import java.util.Random; -public class LockStrategy extends AbstractBuildStrategy{ - public LockStrategy(Grid grid, float wallProbability, float energyProbability) { - super(grid, wallProbability, energyProbability); +public class LockGridFactoryBuilder extends AbstractGridFactoryBuilder { + + protected LockGridFactoryBuilder() { + + } + + public static GridFactoryBuilder create() { + return new LockGridFactoryBuilder(); } @Override diff --git a/server/src/test/java/fr/lnl/game/server/mock/Mock.java b/server/src/test/java/fr/lnl/game/server/mock/Mock.java index dfc33c2..c2c1858 100644 --- a/server/src/test/java/fr/lnl/game/server/mock/Mock.java +++ b/server/src/test/java/fr/lnl/game/server/mock/Mock.java @@ -2,21 +2,21 @@ package fr.lnl.game.server.mock; import fr.lnl.game.server.games.Game; 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 java.util.List; public class Mock { - public BuildStrategy buildStrategy; + public GridFactoryBuilder buildStrategy; public Game game; public Grid grid; public Mock(List 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()); - this.grid = buildStrategy.getGrid(); + this.grid = game.getGrid(); } } diff --git a/server/src/test/java/fr/lnl/game/server/mock/MockGridStrategy.java b/server/src/test/java/fr/lnl/game/server/mock/MockGridFactoryBuilder.java similarity index 77% rename from server/src/test/java/fr/lnl/game/server/mock/MockGridStrategy.java rename to server/src/test/java/fr/lnl/game/server/mock/MockGridFactoryBuilder.java index f75e917..170a755 100644 --- a/server/src/test/java/fr/lnl/game/server/mock/MockGridStrategy.java +++ b/server/src/test/java/fr/lnl/game/server/mock/MockGridFactoryBuilder.java @@ -1,15 +1,20 @@ package fr.lnl.game.server.mock; 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.Wall; 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) { - super(grid, wallProbability, energyProbability); + private MockGridFactoryBuilder() { + + } + + public static GridFactoryBuilder create() { + return new MockGridFactoryBuilder(); } @Override