This commit is contained in:
Quentin Legot 2021-10-21 16:47:41 +02:00
parent d2ccedb814
commit f2e42c68bf
5 changed files with 134 additions and 15 deletions

View File

@ -7,7 +7,16 @@ plugins {
id 'fr.lnl.game.java-library-conventions' id 'fr.lnl.game.java-library-conventions'
} }
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.3.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.1'
}
application { application {
mainClassName = 'fr.lnl.game.server.games.grid.Grid' mainClassName = 'fr.lnl.game.server.games.grid.Grid'
} }
test {
useJUnitPlatform()
}

View File

@ -1,4 +1,11 @@
package fr.lnl.game.server.games.grid; package fr.lnl.game.server.games.grid;
public class EnergyBall implements Box{ public class EnergyBall implements Box{
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
return true; // no var to test
}
} }

View File

@ -49,16 +49,28 @@ public class Grid {
} }
} }
/**
* @deprecated A utiliser seulement pour les tests, voir GridTest dans le dossier test
*/
@Deprecated
public void placePlayersBRUT(){ public void placePlayersBRUT(){
board.get(new Point(1,1)).setA(players[0]); board.get(new Point(1,1)).setA(players[0]);
board.get(new Point(14,14)).setA(players[1]); board.get(new Point(14,14)).setA(players[1]);
} }
/**
* @deprecated idem que {@link Grid#placePlayersBRUT()}
*/
@Deprecated
public void placeEnergyBallBRUT(){ public void placeEnergyBallBRUT(){
board.get(new Point(2,3)).setB(new EnergyBall()); board.get(new Point(2,3)).setB(new EnergyBall());
board.get(new Point(7,10)).setB(new EnergyBall()); board.get(new Point(7,10)).setB(new EnergyBall());
} }
/**
* @deprecated idem que {@link Grid#placePlayersBRUT()}
*/
@Deprecated
public void placeInternWallBRUT(){ public void placeInternWallBRUT(){
board.get(new Point(3,6)).setB(new Wall(Cardinal.NORTH,3,6)); 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(7,14)).setB(new Wall(Cardinal.SOUTH,7,14));
@ -74,46 +86,69 @@ public class Grid {
return board; return board;
} }
public void printGrid() { @Override
public String toString() {
StringBuilder str = new StringBuilder();
for (int i = 0; i < row; i++) { for (int i = 0; i < row; i++) {
System.out.print("\n"); str.append("\n");
for (int j = 0; j < column; j++) { for (int j = 0; j < column; j++) {
Pair<Player, Box> value = board.get(new Point(i, j)); Pair<Player, Box> value = board.get(new Point(i, j));
if(value.getA() != null){ if(value.getA() != null){
System.out.print(" \033[0;34mP\033[0m"); str.append(" \033[0;34mP\033[0m");
} }
else if (value.getB() instanceof Wall) { else if (value.getB() instanceof Wall) {
if (((Wall) value.getB()).getCardinal() == Cardinal.NORTH) { if (((Wall) value.getB()).getCardinal() == Cardinal.NORTH) {
System.out.print(" \033[0;34m—\033[0m"); str.append(" \033[0;34m—\033[0m");
} else if (((Wall) value.getB()).getCardinal() == Cardinal.SOUTH) { } else if (((Wall) value.getB()).getCardinal() == Cardinal.SOUTH) {
System.out.print(" \033[0;31m—\033[0m"); str.append(" \033[0;31m—\033[0m");
} else if (((Wall) value.getB()).getCardinal() == Cardinal.WEST) { } else if (((Wall) value.getB()).getCardinal() == Cardinal.WEST) {
System.out.print(" \033[0;33m|\033[0m"); str.append(" \033[0;33m|\033[0m");
} else if (((Wall) value.getB()).getCardinal() == Cardinal.EAST) { } else if (((Wall) value.getB()).getCardinal() == Cardinal.EAST) {
System.out.print(" \033[0;32m|\033[0m"); str.append(" \033[0;32m|\033[0m");
} else if (((Wall) value.getB()).getCardinal() == Cardinal.NORTH_EAST) { } else if (((Wall) value.getB()).getCardinal() == Cardinal.NORTH_EAST) {
System.out.print(" \033[0;32mN\033[0m"); str.append(" \033[0;32mN\033[0m");
} else if (((Wall) value.getB()).getCardinal() == Cardinal.NORTH_WEST) { } else if (((Wall) value.getB()).getCardinal() == Cardinal.NORTH_WEST) {
System.out.print(" \033[0;33mN\033[0m"); str.append(" \033[0;33mN\033[0m");
} else if (((Wall) value.getB()).getCardinal() == Cardinal.SOUTH_EAST) { } else if (((Wall) value.getB()).getCardinal() == Cardinal.SOUTH_EAST) {
System.out.print(" \033[0;32mS\033[0m"); str.append(" \033[0;32mS\033[0m");
} else if (((Wall) value.getB()).getCardinal() == Cardinal.SOUTH_WEST) { } else if (((Wall) value.getB()).getCardinal() == Cardinal.SOUTH_WEST) {
System.out.print(" \033[0;33mS\033[0m"); str.append(" \033[0;33mS\033[0m");
} }
} }
else if(value.getB() instanceof EnergyBall){ else if(value.getB() instanceof EnergyBall){
System.out.print(" \033[0;31mO\033[0m"); str.append(" \033[0;31mO\033[0m");
} }
else if(value.getB() instanceof Mine){ else if(value.getB() instanceof Mine){
System.out.print(" \033[0;31mX\033[0m"); str.append(" \033[0;31mX\033[0m");
} }
else if(value.getB() instanceof Bomb){ else if(value.getB() instanceof Bomb){
System.out.print(" \033[0;31mI\033[0m"); str.append(" \033[0;31mI\033[0m");
} }
else { else {
System.out.print(" \033[0;31m.\033[0m"); str.append(" \033[0;31m.\033[0m");
} }
} }
} }
return str.toString();
}
/**
* @deprecated modèle mvc non respecté
*/
@Deprecated
public void printGrid() {
System.out.println(this);
}
public Player[] getPlayers() {
return players;
}
public int getRow() {
return row;
}
public int getColumn() {
return column;
} }
} }

View File

@ -2,6 +2,8 @@ package fr.lnl.game.server.games.grid;
import fr.lnl.game.server.utils.Cardinal; import fr.lnl.game.server.utils.Cardinal;
import java.util.Objects;
public class Wall implements Box { public class Wall implements Box {
private Cardinal cardinal; private Cardinal cardinal;
@ -25,4 +27,12 @@ public class Wall implements Box {
public int getY() { public int getY() {
return y; return y;
} }
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Wall other = (Wall) o;
return Objects.equals(cardinal, other.cardinal) && x == other.x && y == other.y;
}
} }

View File

@ -0,0 +1,58 @@
package fr.lnl.game.server;
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.ComputerPlayer;
import fr.lnl.game.server.games.player.DefaultClassPlayer;
import fr.lnl.game.server.games.player.Player;
import fr.lnl.game.server.utils.Cardinal;
import fr.lnl.game.server.utils.Point;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class GridTest {
private Grid grid;
@Test
public void testGrid() {
Player playerOne = new ComputerPlayer(1,null, new DefaultClassPlayer());
Player playerTwo = new ComputerPlayer(2,null, new DefaultClassPlayer());
this.grid = new Grid(16,16,new Player[]{playerOne,playerTwo});
grid.initGrid();
placePlayersBRUT();
placeEnergyBallBRUT();
placeInternWallBRUT();
// test Grid#initGrid()
assertEquals(new Wall(Cardinal.NORTH_WEST, 0, 0), grid.getBoard().get(new Point(0,0)).getB());
assertEquals(new Wall(Cardinal.NORTH_EAST, 0, grid.getColumn() - 1), grid.getBoard().get(new Point(0, grid.getColumn() - 1)).getB());
assertEquals(new Wall(Cardinal.SOUTH_WEST, grid.getRow() - 1, 0), grid.getBoard().get(new Point(grid.getRow() - 1, 0)).getB());
assertEquals(new Wall(Cardinal.SOUTH_EAST, grid.getRow() - 1, grid.getColumn() - 1), grid.getBoard().get(new Point(grid.getRow() - 1, grid.getColumn() - 1)).getB());
// test placePlayersBRUT (mocked)
assertEquals(grid.getPlayers()[0], grid.getBoard().get(new Point(1, 1)).getA());
assertEquals(grid.getPlayers()[1], grid.getBoard().get(new Point(14, 14)).getA());
// test placeEnergyBallBRUT (mocked)
assertEquals(new EnergyBall(), grid.getBoard().get(new Point(2, 3)).getB());
assertEquals(new EnergyBall(), grid.getBoard().get(new Point(7, 10)).getB());
}
public void placePlayersBRUT(){
grid.getBoard().get(new Point(1,1)).setA(grid.getPlayers()[0]);
grid.getBoard().get(new Point(14,14)).setA(grid.getPlayers()[1]);
}
public void placeEnergyBallBRUT(){
grid.getBoard().get(new Point(2,3)).setB(new EnergyBall());
grid.getBoard().get(new Point(7,10)).setB(new EnergyBall());
}
public void placeInternWallBRUT(){
grid.getBoard().get(new Point(3,6)).setB(new Wall(Cardinal.NORTH,3,6));
grid.getBoard().get(new Point(7,14)).setB(new Wall(Cardinal.SOUTH,7,14));
grid.getBoard().get(new Point(10,7)).setB(new Wall(Cardinal.EAST,10,7));
grid.getBoard().get(new Point(14,2)).setB(new Wall(Cardinal.WEST,14,2));
}
}