From d40dedfc1764ee80b3a708dad5c1ca6645315626 Mon Sep 17 00:00:00 2001 From: Katchan <35238981+KatchanNve@users.noreply.github.com> Date: Thu, 2 Dec 2021 19:59:53 +0100 Subject: [PATCH] Add placement rules for walls, energyballs and players --- .../java/fr/lnl/game/server/games/Game.java | 12 +-- .../fr/lnl/game/server/games/grid/Grid.java | 100 +++++++++++++++--- 2 files changed, 86 insertions(+), 26 deletions(-) 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 993219d..97cd161 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 @@ -29,27 +29,19 @@ public class Game { private final Object lock = new Object(); public Game(Grid grid, List players, ModelListener viewUpdate, ModelListener gameFinishEvent) throws IllegalArgumentException { - if(players.size() < 2) + if(players.size() < 2 || grid.getNumberNeutralBox() < players.size()) throw new IllegalArgumentException("The game need 2 or more player to start"); this.players = players; this.currentPlayer = players.get(0); this.grid = grid; this.viewUpdateEvent = viewUpdate; this.gameFinishEvent = gameFinishEvent; - placePlayersBRUT(); - + grid.initPlacePlayers(); } /** * @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() { while(!isOver()) { 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..175f6c3 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 @@ -1,5 +1,6 @@ package fr.lnl.game.server.games.grid; +import fr.lnl.game.server.games.player.AbstractPlayer; import fr.lnl.game.server.games.player.Player; import fr.lnl.game.server.utils.Cardinal; import fr.lnl.game.server.utils.Pair; @@ -7,6 +8,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 +17,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 +59,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 +121,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() {