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 4e7cfa5..f5a07c2 100644 --- a/client/src/main/java/fr/lnl/game/client/App.java +++ b/client/src/main/java/fr/lnl/game/client/App.java @@ -38,7 +38,7 @@ public class App extends Application { public static void startGame(ViewLambda lambda) throws IllegalArgumentException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { List players = parsePlayers(); - game = new Game(new Grid(12, 12, players), players, new GameFinishEvent()); + game = new Game(new Grid(12, 12, players, 0.80F,0.95F), players, new GameFinishEvent()); for (Player player : game.getPlayers()) { playerList.put(player, new ClientPlayer(player, lambda.createViewLambda(player))); } diff --git a/client/src/main/java/fr/lnl/game/client/view/Cell.java b/client/src/main/java/fr/lnl/game/client/view/Cell.java index 7028f9d..2874278 100644 --- a/client/src/main/java/fr/lnl/game/client/view/Cell.java +++ b/client/src/main/java/fr/lnl/game/client/view/Cell.java @@ -3,8 +3,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.player.Player; +import fr.lnl.game.server.utils.Cardinal; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.layout.StackPane; @@ -37,17 +37,34 @@ public class Cell extends Rectangle { if(object instanceof Player){ image = new Image("file:resources/images/player.png"); } - if(object instanceof EnergyBall){ + else if(object instanceof EnergyBall){ image = new Image("file:resources/images/energyBall.png"); } - if(object instanceof Bomb){ + else if(object instanceof Bomb){ image = new Image("file:resources/images/bomb.jpg"); } - if(object instanceof Mine){ + else if(object instanceof Mine){ image = new Image("file:resources/images/mine.webp"); } else{ - image = new Image("file:resources/images/wall.jpg"); + //test + if(((Wall)object).getCardinal()== Cardinal.NORTH){ + image = new Image("file:resources/topWall.png"); + }else if(((Wall)object).getCardinal()== Cardinal.SOUTH) { + image = new Image("file:resources/bottomWall.png"); + }else if(((Wall)object).getCardinal()== Cardinal.EAST) { + image = new Image("file:resources/rightWall.png"); + }else if(((Wall)object).getCardinal()== Cardinal.WEST) { + image = new Image("file:resources/leftWall.png"); + }else if(((Wall)object).getCardinal()== Cardinal.NORTH_EAST) { + image = new Image("file:resources/topRightWall.png"); + }else if(((Wall)object).getCardinal()== Cardinal.NORTH_WEST) { + image = new Image("file:resources/topLeftWall.png"); + }else if(((Wall)object).getCardinal()== Cardinal.SOUTH_EAST) { + image = new Image("file:resources/bottomRightWall.png"); + }else{ + image = new Image("file:resources/bottomLeftWall.png"); + } } ImageView iv = new ImageView(image); diff --git a/client/src/main/java/fr/lnl/game/client/view/ClientEventHandler.java b/client/src/main/java/fr/lnl/game/client/view/ClientEventHandler.java index f587948..47d8d9a 100644 --- a/client/src/main/java/fr/lnl/game/client/view/ClientEventHandler.java +++ b/client/src/main/java/fr/lnl/game/client/view/ClientEventHandler.java @@ -1,7 +1,7 @@ package fr.lnl.game.client.view; -import fr.lnl.game.server.listener.ModelListener; import fr.lnl.game.client.listener.ButtonListener; +import fr.lnl.game.server.listener.ModelListener; import javafx.event.ActionEvent; import javafx.event.EventHandler; diff --git a/client/src/main/java/fr/lnl/game/client/view/GUI.java b/client/src/main/java/fr/lnl/game/client/view/GUI.java index 175918f..917913c 100644 --- a/client/src/main/java/fr/lnl/game/client/view/GUI.java +++ b/client/src/main/java/fr/lnl/game/client/view/GUI.java @@ -1,9 +1,7 @@ package fr.lnl.game.client.view; -import fr.lnl.game.server.games.Game; import fr.lnl.game.server.games.grid.*; -import fr.lnl.game.server.games.player.AbstractPlayer; import fr.lnl.game.server.games.player.ClassPlayer; import fr.lnl.game.server.games.player.Player; import fr.lnl.game.server.games.player.RandomComputerPlayer; @@ -52,9 +50,8 @@ public class GUI { private Parent createContent() { Pane principalPane = new Pane(); principalPane.setPrefSize(width * cellSize, height * cellSize); - //PARTIE1 - //à définir avec n pour moduler la taille du plateau + //à définir avec n pour moduler la taille du plateau for (int i = 0; i < 16; i++) { for (int j = 0; j < 16; j++) { Cell cell = new Cell(i, j); @@ -62,7 +59,6 @@ public class GUI { } } - //PARTIE2 board = grid.getBoard(); for (int i = 0; i < 16; i++) { for (int j = 0; j < 16; j++) { @@ -71,11 +67,20 @@ public class GUI { addToPrincipalPanel(value.getA(), principalPane, i, j); } if (value.getB() instanceof Wall || value.getB() instanceof EnergyBall || value.getB() instanceof Mine || value.getB() instanceof Bomb) { + System.out.println(value.getB()); addToPrincipalPanel(value.getB(), principalPane, i, j); } } } - //PARTIE3 + + Rectangle shape = new Rectangle(); + shape.setX(700); + shape.setY(20); + shape.setWidth(200); + shape.setHeight(600); + shape.setFill(javafx.scene.paint.Color.WHITE); + + Button followingButton = new Button("SUIVANT"); followingButton.setLayoutX(775); followingButton.setLayoutY(600); @@ -84,6 +89,7 @@ public class GUI { //add un eventListener au button principalPane.getChildren().add(followingButton); + //pas compris le principe return principalPane; } @@ -100,10 +106,8 @@ public class GUI { private Grid getGrid() { List players = Arrays.asList(new RandomComputerPlayer(1, null, ClassPlayer.DEFAULT), new RandomComputerPlayer(2, null, ClassPlayer.DEFAULT)); - Grid grid = new Grid(16, 16, players); - grid.placePlayersBRUT(); - grid.placeEnergyBallBRUT(); - grid.placeInternWallBRUT(); + Grid grid = new Grid(16, 16, players, 0.80F, 0.95F); + //grid.placePlayersBRUT(); return grid; } diff --git a/client/src/main/java/fr/lnl/game/client/view/Window.java b/client/src/main/java/fr/lnl/game/client/view/Window.java index 9ad05fa..65b7a05 100644 --- a/client/src/main/java/fr/lnl/game/client/view/Window.java +++ b/client/src/main/java/fr/lnl/game/client/view/Window.java @@ -3,14 +3,11 @@ package fr.lnl.game.client.view; import fr.lnl.game.client.listener.ButtonListener; import fr.lnl.game.server.games.Game; import fr.lnl.game.server.games.player.Player; -import javafx.concurrent.ScheduledService; -import javafx.concurrent.Task; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.layout.GridPane; import javafx.stage.Stage; -import javafx.util.Duration; public class Window extends AbstractView { diff --git a/client/src/main/resources/images/bottomLeftWall.PNG b/client/src/main/resources/images/bottomLeftWall.PNG new file mode 100644 index 0000000..af2c630 Binary files /dev/null and b/client/src/main/resources/images/bottomLeftWall.PNG differ diff --git a/client/src/main/resources/images/bottomRightWall.PNG b/client/src/main/resources/images/bottomRightWall.PNG new file mode 100644 index 0000000..3e87801 Binary files /dev/null and b/client/src/main/resources/images/bottomRightWall.PNG differ diff --git a/client/src/main/resources/images/bottomWall.PNG b/client/src/main/resources/images/bottomWall.PNG new file mode 100644 index 0000000..3287196 Binary files /dev/null and b/client/src/main/resources/images/bottomWall.PNG differ diff --git a/client/src/main/resources/images/rightWall.PNG b/client/src/main/resources/images/rightWall.PNG new file mode 100644 index 0000000..f2f5278 Binary files /dev/null and b/client/src/main/resources/images/rightWall.PNG differ diff --git a/client/src/main/resources/images/topLeftWall.PNG b/client/src/main/resources/images/topLeftWall.PNG new file mode 100644 index 0000000..73a2dd5 Binary files /dev/null and b/client/src/main/resources/images/topLeftWall.PNG differ diff --git a/client/src/main/resources/images/topRightWall.PNG b/client/src/main/resources/images/topRightWall.PNG new file mode 100644 index 0000000..74ff2dc Binary files /dev/null and b/client/src/main/resources/images/topRightWall.PNG differ diff --git a/client/src/main/resources/images/topWall.PNG b/client/src/main/resources/images/topWall.PNG new file mode 100644 index 0000000..c769e82 Binary files /dev/null and b/client/src/main/resources/images/topWall.PNG differ 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 0966e38..1406c89 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 @@ -25,25 +25,17 @@ public class Game { public Game(Grid grid, List players, ModelListener gameFinishEvent) throws IllegalArgumentException { 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; - placePlayersBRUT(); + this.grid.initPlacePlayers(); currentPlayer.setActions(generateAndGetPlayerActions(currentPlayer)); } - /** - * @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() { if (currentPlayer instanceof ComputerPlayer player) { selectedAction = player.choseAction(); diff --git a/server/src/main/java/fr/lnl/game/server/games/action/DropMine.java b/server/src/main/java/fr/lnl/game/server/games/action/DropMine.java index 5ec88f5..6aecc67 100644 --- a/server/src/main/java/fr/lnl/game/server/games/action/DropMine.java +++ b/server/src/main/java/fr/lnl/game/server/games/action/DropMine.java @@ -3,9 +3,6 @@ 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.player.Player; -import fr.lnl.game.server.utils.Point; - -import java.util.List; public class DropMine extends DropObject { diff --git a/server/src/main/java/fr/lnl/game/server/games/grid/AbstractBox.java b/server/src/main/java/fr/lnl/game/server/games/grid/AbstractBox.java new file mode 100644 index 0000000..ebf58ee --- /dev/null +++ b/server/src/main/java/fr/lnl/game/server/games/grid/AbstractBox.java @@ -0,0 +1,21 @@ +package fr.lnl.game.server.games.grid; + +public class AbstractBox implements Box{ + + boolean lock; + + public AbstractBox(){ + lock = false; + } + + @Override + public void setLock(boolean lock) { + this.lock = lock; + } + + @Override + public boolean isLock() { + return lock; + } +} + diff --git a/server/src/main/java/fr/lnl/game/server/games/grid/Box.java b/server/src/main/java/fr/lnl/game/server/games/grid/Box.java index 593512e..1a7186f 100644 --- a/server/src/main/java/fr/lnl/game/server/games/grid/Box.java +++ b/server/src/main/java/fr/lnl/game/server/games/grid/Box.java @@ -1,4 +1,7 @@ package fr.lnl.game.server.games.grid; public interface Box { + + boolean isLock(); + void setLock(boolean lock); } diff --git a/server/src/main/java/fr/lnl/game/server/games/grid/EnergyBall.java b/server/src/main/java/fr/lnl/game/server/games/grid/EnergyBall.java index 1fd9193..88e9ce3 100644 --- a/server/src/main/java/fr/lnl/game/server/games/grid/EnergyBall.java +++ b/server/src/main/java/fr/lnl/game/server/games/grid/EnergyBall.java @@ -3,7 +3,7 @@ package fr.lnl.game.server.games.grid; import fr.lnl.game.server.games.player.Player; import fr.lnl.game.server.utils.Point; -public class EnergyBall implements Box, InteractiveBox { +public class EnergyBall extends AbstractBox implements InteractiveBox{ @Override public boolean equals(Object o) { diff --git a/server/src/main/java/fr/lnl/game/server/games/grid/Explosive.java b/server/src/main/java/fr/lnl/game/server/games/grid/Explosive.java index 8861a4b..e4c6321 100644 --- a/server/src/main/java/fr/lnl/game/server/games/grid/Explosive.java +++ b/server/src/main/java/fr/lnl/game/server/games/grid/Explosive.java @@ -3,7 +3,7 @@ package fr.lnl.game.server.games.grid; import fr.lnl.game.server.games.player.Player; import fr.lnl.game.server.utils.Point; -public abstract class Explosive implements Box, InteractiveBox { +public abstract class Explosive extends AbstractBox implements InteractiveBox { @Override public void interact(Grid grid, Player player, Point position) { diff --git a/server/src/main/java/fr/lnl/game/server/games/grid/Grid.java b/server/src/main/java/fr/lnl/game/server/games/grid/Grid.java index 4f0f11e..2db43c1 100644 --- a/server/src/main/java/fr/lnl/game/server/games/grid/Grid.java +++ b/server/src/main/java/fr/lnl/game/server/games/grid/Grid.java @@ -7,6 +7,7 @@ import fr.lnl.game.server.utils.Point; import java.util.HashMap; import java.util.List; +import java.util.Random; public class Grid { @@ -15,12 +16,18 @@ public class Grid { private final int column; private final List players; - public Grid(int row, int column, List players) { + public Grid(int row, int column, List players, float wallProbability, float energyProbability) { 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(){ @@ -51,21 +58,54 @@ public class Grid { } } - public void placePlayersBRUT(){ - board.get(new Point(1,1)).setA(players.get(0)); - board.get(new Point(14,14)).setA(players.get(1)); + 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 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()); - board.get(new Point(7,10)).setB(new EnergyBall()); + + 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 placeInternWallBRUT(){ - board.get(new Point(3,6)).setB(new Wall(Cardinal.NORTH,3,6)); - board.get(new Point(7,14)).setB(new Wall(Cardinal.SOUTH,7,14)); - board.get(new Point(10,7)).setB(new Wall(Cardinal.EAST,10,7)); - board.get(new Point(14,2)).setB(new Wall(Cardinal.WEST,14,2)); + 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(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){ @@ -80,12 +120,39 @@ public class Grid { return boardPositionIsValid(point.getA(), point.getB()); } - public boolean boardHorizontalIsValid(int column, int deltaColumn){ - return column + deltaColumn >= 0 && column + deltaColumn < this.column; + 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 boolean boardVerticalIsValid(int row, int deltaRow){ - return row + deltaRow >= 0 && row + deltaRow < this.row; + 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(); + if(isNeutralBox(box)){ + countBox++; + } + } + } + return countBox; + } + + public boolean isNeutralBox(Box box){ + return !(box instanceof Wall) && !(box instanceof EnergyBall); } public HashMap> getBoard() { diff --git a/server/src/main/java/fr/lnl/game/server/games/grid/Wall.java b/server/src/main/java/fr/lnl/game/server/games/grid/Wall.java index 06d8853..dd52cec 100644 --- a/server/src/main/java/fr/lnl/game/server/games/grid/Wall.java +++ b/server/src/main/java/fr/lnl/game/server/games/grid/Wall.java @@ -4,7 +4,7 @@ import fr.lnl.game.server.utils.Cardinal; import java.util.Objects; -public class Wall implements Box { +public class Wall extends AbstractBox { private final Cardinal cardinal; private final int x; diff --git a/server/src/main/java/fr/lnl/game/server/games/player/AbstractPlayer.java b/server/src/main/java/fr/lnl/game/server/games/player/AbstractPlayer.java index 7f85b36..41dcfdd 100644 --- a/server/src/main/java/fr/lnl/game/server/games/player/AbstractPlayer.java +++ b/server/src/main/java/fr/lnl/game/server/games/player/AbstractPlayer.java @@ -87,6 +87,9 @@ public abstract class AbstractPlayer implements Player { @Override public void setPosition(Point position){ + if(position == null){ + throw new IllegalArgumentException("Position is null"); + } this.position = position; } diff --git a/server/src/main/java/fr/lnl/game/server/games/player/HumanPlayer.java b/server/src/main/java/fr/lnl/game/server/games/player/HumanPlayer.java index 2be409c..676f572 100644 --- a/server/src/main/java/fr/lnl/game/server/games/player/HumanPlayer.java +++ b/server/src/main/java/fr/lnl/game/server/games/player/HumanPlayer.java @@ -1,6 +1,5 @@ package fr.lnl.game.server.games.player; -import fr.lnl.game.server.games.action.Action; import fr.lnl.game.server.utils.Point; public class HumanPlayer extends AbstractPlayer { diff --git a/server/src/main/java/fr/lnl/game/server/utils/Cardinal.java b/server/src/main/java/fr/lnl/game/server/utils/Cardinal.java index b7dab95..3b8ff91 100644 --- a/server/src/main/java/fr/lnl/game/server/utils/Cardinal.java +++ b/server/src/main/java/fr/lnl/game/server/utils/Cardinal.java @@ -8,5 +8,11 @@ public enum Cardinal { NORTH_EAST, NORTH_WEST, SOUTH_EAST, - SOUTH_WEST + SOUTH_WEST; + + public static Cardinal getRandom() { + return values()[(int) (Math.random() * values().length)]; + } } + + diff --git a/server/src/test/java/fr/lnl/game/server/GridTest.java b/server/src/test/java/fr/lnl/game/server/GridTest.java index 244f38b..bc2bbab 100644 --- a/server/src/test/java/fr/lnl/game/server/GridTest.java +++ b/server/src/test/java/fr/lnl/game/server/GridTest.java @@ -1,20 +1,17 @@ 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.Box; import fr.lnl.game.server.games.grid.EnergyBall; import fr.lnl.game.server.games.grid.Grid; import fr.lnl.game.server.games.grid.Wall; import fr.lnl.game.server.games.player.Player; import fr.lnl.game.server.utils.Cardinal; +import fr.lnl.game.server.utils.Pair; import fr.lnl.game.server.utils.Point; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Random; - import static org.junit.jupiter.api.Assertions.assertEquals; public class GridTest { @@ -44,4 +41,35 @@ public class GridTest { assertEquals(new EnergyBall(), grid.getBoard().get(new Point(8, 10)).getB()); } + @Test + public void testLock() { + System.out.println("=================== GRID COMPLETE ===================="); + System.out.println(grid.toString()); + System.out.println("=================== GRID AVEC LOCK ==================="); + StringBuilder str = new StringBuilder(); + for (int i = 0; i < grid.getRow(); i++) { + str.append("\n"); + for (int j = 0; j < grid.getColumn(); j++) { + Pair value = grid.getBoard().get(new Point(i, j)); + if(value.getB() != null){ + if(value.getB().isLock()){ + str.append(" \033[0;35mL\033[0m"); + } + if(value.getB() instanceof Wall){ + str.append(" \033[0;32mW\033[0m"); + } + if(value.getB() instanceof EnergyBall){ + str.append(" \033[0;31mE\033[0m"); + } + } + else if(value.getA() != null){ + str.append(" \033[0;34mP\033[0m"); + } + else { + str.append(" \033[0;37m.\033[0m"); + } + } + } + System.out.println(str); + } } diff --git a/server/src/test/java/fr/lnl/game/server/Mock.java b/server/src/test/java/fr/lnl/game/server/Mock.java index 89aa984..98644cb 100644 --- a/server/src/test/java/fr/lnl/game/server/Mock.java +++ b/server/src/test/java/fr/lnl/game/server/Mock.java @@ -22,10 +22,7 @@ public class Mock { public Mock() { List players = Arrays.asList(new RandomComputerPlayer(1,null, ClassPlayer.DEFAULT), new RandomComputerPlayer(2,null, ClassPlayer.DEFAULT)); - this.grid = new Grid(16,16, players); - placePlayersBRUT(); - placeEnergyBallBRUT(); - placeInternWallBRUT(); + this.grid = new Grid(16,16, players,0.80F, 0.95F); game = new Game(grid, players, new GameFinishEvent()); }