From 8415739d18f1890a93d9805d7bbc401c0d8c129e Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Wed, 8 Dec 2021 18:43:34 +0100 Subject: [PATCH] add javadoc to grid's elements, deleted useless x and y parameters in Wall, adjustments to bombs timer and explosion size --- .../grid/build/LockGridFactoryBuilder.java | 20 +++++------ .../games/grid/elements/AbstractBox.java | 6 +++- .../game/server/games/grid/elements/Bomb.java | 33 +++++++++++++++++-- .../game/server/games/grid/elements/Box.java | 5 +++ .../games/grid/elements/CountdownBox.java | 6 ++++ .../games/grid/elements/EnergyBall.java | 26 +++++++++++---- .../server/games/grid/elements/Explosive.java | 10 ++++++ .../games/grid/elements/InteractiveBox.java | 10 +++++- .../game/server/games/grid/elements/Mine.java | 11 +++++++ .../game/server/games/grid/elements/Wall.java | 32 ++++++------------ .../java/fr/lnl/game/server/GridTest.java | 8 ++--- .../java/fr/lnl/game/server/mock/Mock.java | 2 +- .../server/mock/MockGridFactoryBuilder.java | 8 ++--- 13 files changed, 126 insertions(+), 51 deletions(-) diff --git a/server/src/main/java/fr/lnl/game/server/games/grid/build/LockGridFactoryBuilder.java b/server/src/main/java/fr/lnl/game/server/games/grid/build/LockGridFactoryBuilder.java index 0ff9339..6fd1cc8 100644 --- a/server/src/main/java/fr/lnl/game/server/games/grid/build/LockGridFactoryBuilder.java +++ b/server/src/main/java/fr/lnl/game/server/games/grid/build/LockGridFactoryBuilder.java @@ -38,21 +38,21 @@ public class LockGridFactoryBuilder extends AbstractGridFactoryBuilder { for (int j = 0; j < getGrid().getColumn(); j++) { Box box; if (i == 0 && j == 0) { - box = new Wall(i, j); + box = new Wall(); } else if (i == 0 && j == getGrid().getColumn()-1) { - box = new Wall(i, j); + box = new Wall(); } else if (i == getGrid().getRow()-1 && j == 0) { - box = new Wall(i, j); + box = new Wall(); } else if (i == getGrid().getRow()-1 && j == getGrid().getColumn()-1) { - box = new Wall(i, j); + box = new Wall(); } else if (i == 0) { - box = new Wall(i, j); + box = new Wall(); } else if (i == getGrid().getRow()-1) { - box = new Wall(i, j); + box = new Wall(); } else if (j == 0) { - box = new Wall(i, j); + box = new Wall(); } else if (j == getGrid().getColumn()-1) { - box = new Wall(i, j); + box = new Wall(); } else { box = null; } @@ -92,7 +92,7 @@ public class LockGridFactoryBuilder extends AbstractGridFactoryBuilder { if(Math.random() >= wallProbability){ Point point = new Point(i,j); if(getIllusionNumberWallNeighbour(point) <= 3){ - getGrid().getBoard().get(point).setB(new Wall(i,j)); + getGrid().getBoard().get(point).setB(new Wall()); } else{ getGrid().getBoard().get(point).setB(new AbstractBox()); @@ -132,7 +132,7 @@ public class LockGridFactoryBuilder extends AbstractGridFactoryBuilder { * @param point the position where we want to place a new wall * @return number of walls and locked place around {@code position} */ - public int getIllusionNumberWallNeighbour(Point point){ + private int getIllusionNumberWallNeighbour(Point point){ int countWall = 0; for (int deltaRow = -1; deltaRow <= 1; deltaRow++){ for (int deltaColomn = -1; deltaColomn <= 1; deltaColomn++) { diff --git a/server/src/main/java/fr/lnl/game/server/games/grid/elements/AbstractBox.java b/server/src/main/java/fr/lnl/game/server/games/grid/elements/AbstractBox.java index f48a2b1..44c3f94 100644 --- a/server/src/main/java/fr/lnl/game/server/games/grid/elements/AbstractBox.java +++ b/server/src/main/java/fr/lnl/game/server/games/grid/elements/AbstractBox.java @@ -1,6 +1,10 @@ package fr.lnl.game.server.games.grid.elements; -public class AbstractBox implements Box{ +/** + * AbstractBox is instantiable (not an abstract class), but when it's the case, instance doesn't represent anything, + * it's only to see if there is a lock on this position or not + */ +public class AbstractBox implements Box { boolean lock; diff --git a/server/src/main/java/fr/lnl/game/server/games/grid/elements/Bomb.java b/server/src/main/java/fr/lnl/game/server/games/grid/elements/Bomb.java index f801a79..a1681bf 100644 --- a/server/src/main/java/fr/lnl/game/server/games/grid/elements/Bomb.java +++ b/server/src/main/java/fr/lnl/game/server/games/grid/elements/Bomb.java @@ -5,12 +5,24 @@ import fr.lnl.game.server.games.grid.Grid; import fr.lnl.game.server.games.player.Player; import fr.lnl.game.server.utils.Point; +/** + * Bomb are elements which explode when someone walks on it or after a countdown, the explosion area is on multiple cases + */ public class Bomb extends Explosive implements CountdownBox { + /** + * Position of the bomb + */ private final Point point; private final Game game; - private int counter = 3; - private static int EXPLOSION_SIZE = 4; + /** + * Timer before explosion + */ + private int counter = 5; + /** + * Explosion size, size is circle, not square + */ + private static final int EXPLOSION_SIZE = 2; public Bomb(Point point, Game game) { super(game.getCurrentPlayer()); @@ -19,6 +31,14 @@ public class Bomb extends Explosive implements CountdownBox { counter = counter * game.getPlayers().size(); } + /** + * Decrement players energy around this element + * @param grid Game's grid + * @param player the player who walks on this element + * @param position position of this element on the grid + * @see InteractiveBox#interact(Grid, Player, Point) + * @see Explosive#interact(Grid, Player, Point) + */ @Override public void interact(Grid grid, /* Nullable */ Player player, Point position) { if(player != null) @@ -26,6 +46,10 @@ public class Bomb extends Explosive implements CountdownBox { super.interact(grid, player, position); } + /** + * When the timer (counter) goes down to 0, the bomb explode + * @see CountdownBox#update() + */ @Override public void update() { Grid grid = game.getGrid(); @@ -46,6 +70,11 @@ public class Bomb extends Explosive implements CountdownBox { } } + /** + * @param a adjacent side of a triangle + * @param b opposite side of a triangle + * @return Pythagoras' theorem value + */ public double pythagoras(double a, double b) { return Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2)); } diff --git a/server/src/main/java/fr/lnl/game/server/games/grid/elements/Box.java b/server/src/main/java/fr/lnl/game/server/games/grid/elements/Box.java index 2db46e6..3e7b5f4 100644 --- a/server/src/main/java/fr/lnl/game/server/games/grid/elements/Box.java +++ b/server/src/main/java/fr/lnl/game/server/games/grid/elements/Box.java @@ -2,6 +2,11 @@ package fr.lnl.game.server.games.grid.elements; public interface Box { + /** + * @return true if this box is locked (can't place wall on this position) + * @see fr.lnl.game.server.games.grid.build.LockGridFactoryBuilder + */ boolean isLock(); + void setLock(boolean lock); } diff --git a/server/src/main/java/fr/lnl/game/server/games/grid/elements/CountdownBox.java b/server/src/main/java/fr/lnl/game/server/games/grid/elements/CountdownBox.java index a9321f8..0fbdbe2 100644 --- a/server/src/main/java/fr/lnl/game/server/games/grid/elements/CountdownBox.java +++ b/server/src/main/java/fr/lnl/game/server/games/grid/elements/CountdownBox.java @@ -1,7 +1,13 @@ package fr.lnl.game.server.games.grid.elements; +/** + * A box implemented by CountdownBox is a box which do an action a certain time after being placed + */ public interface CountdownBox { + /** + * Call at each game tick (After a player do an action) + */ void update(); } diff --git a/server/src/main/java/fr/lnl/game/server/games/grid/elements/EnergyBall.java b/server/src/main/java/fr/lnl/game/server/games/grid/elements/EnergyBall.java index f6377a6..5d7e41f 100644 --- a/server/src/main/java/fr/lnl/game/server/games/grid/elements/EnergyBall.java +++ b/server/src/main/java/fr/lnl/game/server/games/grid/elements/EnergyBall.java @@ -4,17 +4,31 @@ import fr.lnl.game.server.games.grid.Grid; import fr.lnl.game.server.games.player.Player; import fr.lnl.game.server.utils.Point; +/** + * An EnergyBall is a box which give back a bit of player energy after it walks on the ball + */ public class EnergyBall extends AbstractBox implements InteractiveBox{ - @Override - public boolean equals(Object o) { - if (this == o) return true; - return o != null && getClass() == o.getClass();// no var to test - } - + /** + * Increment energy of player who walks on this element + * @param grid Game's grid + * @param player the player who walks on this element + * @param position position of this element on the grid + * @see InteractiveBox#interact(Grid, Player, Point) + */ @Override public void interact(Grid grid, Player player, Point position) { player.incrementEnergy(player.getClassPlayer().getGainEnergy()); grid.getBoard().get(position).setB(null); } + + /** + * Used by tests + * @param obj the object to compare + * @return true if obj is an instance of EnergyBall, false otherwise + */ + @Override + public boolean equals(Object obj) { + return obj instanceof EnergyBall; + } } diff --git a/server/src/main/java/fr/lnl/game/server/games/grid/elements/Explosive.java b/server/src/main/java/fr/lnl/game/server/games/grid/elements/Explosive.java index 136a9ce..e671f67 100644 --- a/server/src/main/java/fr/lnl/game/server/games/grid/elements/Explosive.java +++ b/server/src/main/java/fr/lnl/game/server/games/grid/elements/Explosive.java @@ -4,6 +4,9 @@ import fr.lnl.game.server.games.grid.Grid; import fr.lnl.game.server.games.player.Player; import fr.lnl.game.server.utils.Point; +/** + * Super class of {@link Bomb} and {@link Mine} + */ public abstract class Explosive extends AbstractBox implements InteractiveBox { Player player; @@ -12,6 +15,13 @@ public abstract class Explosive extends AbstractBox implements InteractiveBox { this.player = player; } + /** + * Destroy this element on explosion + * @param grid Game's grid + * @param player the player who walks on this element + * @param position position of this element on the grid + * @see InteractiveBox#interact(Grid, Player, Point) + */ @Override public void interact(Grid grid, Player player, Point position) { if(grid.getBoard().get(position).getB() == this){ diff --git a/server/src/main/java/fr/lnl/game/server/games/grid/elements/InteractiveBox.java b/server/src/main/java/fr/lnl/game/server/games/grid/elements/InteractiveBox.java index e6d0640..4c08e31 100644 --- a/server/src/main/java/fr/lnl/game/server/games/grid/elements/InteractiveBox.java +++ b/server/src/main/java/fr/lnl/game/server/games/grid/elements/InteractiveBox.java @@ -4,9 +4,17 @@ import fr.lnl.game.server.games.grid.Grid; import fr.lnl.game.server.games.player.Player; import fr.lnl.game.server.utils.Point; +/** + * A box implemented by InteractiveBox is a box which do an action when someone walks on it + */ public interface InteractiveBox { - + /** + * Call when a player walk on it + * @param grid Game's grid + * @param player the player who walks on this element + * @param position position of this element on the grid + */ void interact(Grid grid, Player player, Point position); } diff --git a/server/src/main/java/fr/lnl/game/server/games/grid/elements/Mine.java b/server/src/main/java/fr/lnl/game/server/games/grid/elements/Mine.java index 2818e38..82a0978 100644 --- a/server/src/main/java/fr/lnl/game/server/games/grid/elements/Mine.java +++ b/server/src/main/java/fr/lnl/game/server/games/grid/elements/Mine.java @@ -4,12 +4,23 @@ import fr.lnl.game.server.games.grid.Grid; import fr.lnl.game.server.games.player.Player; import fr.lnl.game.server.utils.Point; +/** + * A mine is an element which explose when someone walks on it, the explosion area is on 1 case only + */ public class Mine extends Explosive{ public Mine(Player player) { super(player); } + /** + * Decrement energy of the player who walks on this element + * @param grid Game's grid + * @param player the player who walks on this element + * @param position position of this element on the grid + * @see InteractiveBox#interact(Grid, Player, Point) + * @see Explosive#interact(Grid, Player, Point) + */ @Override public void interact(Grid grid, Player player, Point position) { player.decrementEnergy(player.getClassPlayer().getPenaltyMine()); diff --git a/server/src/main/java/fr/lnl/game/server/games/grid/elements/Wall.java b/server/src/main/java/fr/lnl/game/server/games/grid/elements/Wall.java index 745aab0..7747ce2 100644 --- a/server/src/main/java/fr/lnl/game/server/games/grid/elements/Wall.java +++ b/server/src/main/java/fr/lnl/game/server/games/grid/elements/Wall.java @@ -1,29 +1,17 @@ package fr.lnl.game.server.games.grid.elements; +/** + * A wall is an intraversable object + */ public class Wall extends AbstractBox { - private final int x; - private final int y; - - public Wall(int x, int y){ - this.x = x; - this.y = y; - } - - - public int getX() { - return x; - } - - public int getY() { - return y; - } - + /** + * Used by tests + * @param obj the object to compare + * @return true if obj is an instance of wall, false otherwise + */ @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - Wall other = (Wall) o; - return x == other.x && y == other.y; + public boolean equals(Object obj) { + return obj instanceof Wall; } } 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 9ecc58b..14604d6 100644 --- a/server/src/test/java/fr/lnl/game/server/GridTest.java +++ b/server/src/test/java/fr/lnl/game/server/GridTest.java @@ -36,10 +36,10 @@ public class GridTest { @Test public void testGrid() { // test Grid#initGrid() - assertEquals(new Wall(0, 0), grid.getBoard().get(new Point(0,0)).getB()); - assertEquals(new Wall(0, grid.getColumn() - 1), grid.getBoard().get(new Point(0, grid.getColumn() - 1)).getB()); - assertEquals(new Wall(grid.getRow() - 1, 0), grid.getBoard().get(new Point(grid.getRow() - 1, 0)).getB()); - assertEquals(new Wall(grid.getRow() - 1, grid.getColumn() - 1), grid.getBoard().get(new Point(grid.getRow() - 1, grid.getColumn() - 1)).getB()); + assertEquals(new Wall(), grid.getBoard().get(new Point(0,0)).getB()); + assertEquals(new Wall(), grid.getBoard().get(new Point(0, grid.getColumn() - 1)).getB()); + assertEquals(new Wall(), grid.getBoard().get(new Point(grid.getRow() - 1, 0)).getB()); + assertEquals(new Wall(), grid.getBoard().get(new Point(grid.getRow() - 1, grid.getColumn() - 1)).getB()); // test placePlayersBRUT (mocked) grid.getPlayers().forEach(p -> System.out.println(p.getId() + ": " + p.getPosition())); assertEquals(grid.getPlayers().get(0), grid.getBoard().get(new Point(7, 7)).getA()); 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 c2c1858..e8429dd 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 @@ -15,7 +15,7 @@ public class Mock { public Mock(List players) { 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); this.grid = game.getGrid(); } diff --git a/server/src/test/java/fr/lnl/game/server/mock/MockGridFactoryBuilder.java b/server/src/test/java/fr/lnl/game/server/mock/MockGridFactoryBuilder.java index 56af879..e4517ed 100644 --- a/server/src/test/java/fr/lnl/game/server/mock/MockGridFactoryBuilder.java +++ b/server/src/test/java/fr/lnl/game/server/mock/MockGridFactoryBuilder.java @@ -25,10 +25,10 @@ public class MockGridFactoryBuilder extends LockGridFactoryBuilder { @Override protected void initPlaceInternWall() { - grid.getBoard().get(new Point(3,6)).setB(new Wall(3,6)); - grid.getBoard().get(new Point(7,14)).setB(new Wall(7,14)); - grid.getBoard().get(new Point(10,7)).setB(new Wall(10,7)); - grid.getBoard().get(new Point(14,2)).setB(new Wall(14,2)); + grid.getBoard().get(new Point(3,6)).setB(new Wall()); + grid.getBoard().get(new Point(7,14)).setB(new Wall()); + grid.getBoard().get(new Point(10,7)).setB(new Wall()); + grid.getBoard().get(new Point(14,2)).setB(new Wall()); } @Override