fix tests

This commit is contained in:
Quentin Legot 2021-12-07 17:37:32 +01:00
parent 2a8eac9ea2
commit cacf5ee8c3
8 changed files with 51 additions and 79 deletions

View File

@ -5,10 +5,10 @@ import fr.lnl.game.server.games.grid.Grid;
public abstract class AbstractBuildStrategy implements BuildStrategy{ public abstract class AbstractBuildStrategy implements BuildStrategy{
private final float WALL_PROBABILITY, ENERGY_PROBABILITY; private final float WALL_PROBABILITY, ENERGY_PROBABILITY;
private final Grid GRID; protected final Grid grid;
public AbstractBuildStrategy(Grid grid, float wallProbability, float energyProbability){ public AbstractBuildStrategy(Grid grid, float wallProbability, float energyProbability){
this.GRID = grid; this.grid = grid;
this.WALL_PROBABILITY = wallProbability; this.WALL_PROBABILITY = wallProbability;
this.ENERGY_PROBABILITY = energyProbability; this.ENERGY_PROBABILITY = energyProbability;
build(); build();
@ -30,12 +30,12 @@ public abstract class AbstractBuildStrategy implements BuildStrategy{
} }
public Grid getGrid() { public Grid getGrid() {
return GRID; return grid;
} }
abstract void initGrid(); protected abstract void initGrid();
abstract void initPlaceEnergyBall(float probability); protected abstract void initPlaceEnergyBall(float probability);
abstract void initPlaceInternWall(float probability); protected abstract void initPlaceInternWall(float probability);

View File

@ -17,7 +17,7 @@ public class LockStrategy extends AbstractBuildStrategy{
} }
@Override @Override
void initGrid() { protected void initGrid() {
for (int i = 0; i < getGrid().getRow(); i++) { for (int i = 0; i < getGrid().getRow(); i++) {
for (int j = 0; j < getGrid().getColumn(); j++) { for (int j = 0; j < getGrid().getColumn(); j++) {
Box box; Box box;
@ -46,7 +46,7 @@ public class LockStrategy extends AbstractBuildStrategy{
} }
@Override @Override
void initPlaceEnergyBall(float probability) { protected void initPlaceEnergyBall(float probability) {
for (int i = 1; i < getGrid().getRow() - 1; i++) { for (int i = 1; i < getGrid().getRow() - 1; i++) {
for (int j = 1; j < getGrid().getColumn() - 1; j++) { for (int j = 1; j < getGrid().getColumn() - 1; j++) {
if(Math.random() >= probability){ if(Math.random() >= probability){
@ -60,7 +60,7 @@ public class LockStrategy extends AbstractBuildStrategy{
} }
@Override @Override
void initPlaceInternWall(float probability) { protected void initPlaceInternWall(float probability) {
for (int i = 1; i < getGrid().getRow() - 1; i++) { for (int i = 1; i < getGrid().getRow() - 1; i++) {
for (int j = 1; j < getGrid().getColumn() - 1; j++) { for (int j = 1; j < getGrid().getColumn() - 1; j++) {
if(Math.random() >= probability){ if(Math.random() >= probability){

View File

@ -7,6 +7,7 @@ import fr.lnl.game.server.games.grid.elements.Bomb;
import fr.lnl.game.server.games.player.ClassPlayer; import fr.lnl.game.server.games.player.ClassPlayer;
import fr.lnl.game.server.games.player.Player; import fr.lnl.game.server.games.player.Player;
import fr.lnl.game.server.games.player.RandomComputerPlayer; import fr.lnl.game.server.games.player.RandomComputerPlayer;
import fr.lnl.game.server.mock.Mock;
import fr.lnl.game.server.utils.Point; import fr.lnl.game.server.utils.Point;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
@ -47,7 +48,7 @@ public class ActionPlayerTest {
Point newPoint = game.getCurrentPlayer().getPosition(); Point newPoint = game.getCurrentPlayer().getPosition();
Assertions.assertEquals(newPoint, Assertions.assertEquals(newPoint,
new Point(oldPoint.getA() + savedDirection.getDeltaX(), new Point(oldPoint.getA() + savedDirection.getDeltaX(),
oldPoint.getA() + savedDirection.getDeltaY() oldPoint.getB() + savedDirection.getDeltaY()
) )
); );
} }

View File

@ -8,6 +8,7 @@ import fr.lnl.game.server.games.grid.elements.Wall;
import fr.lnl.game.server.games.player.ClassPlayer; import fr.lnl.game.server.games.player.ClassPlayer;
import fr.lnl.game.server.games.player.Player; import fr.lnl.game.server.games.player.Player;
import fr.lnl.game.server.games.player.RandomComputerPlayer; import fr.lnl.game.server.games.player.RandomComputerPlayer;
import fr.lnl.game.server.mock.Mock;
import fr.lnl.game.server.utils.Pair; import fr.lnl.game.server.utils.Pair;
import fr.lnl.game.server.utils.Point; import fr.lnl.game.server.utils.Point;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
@ -40,6 +41,7 @@ public class GridTest {
assertEquals(new Wall(grid.getRow() - 1, 0), grid.getBoard().get(new Point(grid.getRow() - 1, 0)).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.getRow() - 1, grid.getColumn() - 1), grid.getBoard().get(new Point(grid.getRow() - 1, grid.getColumn() - 1)).getB());
// test placePlayersBRUT (mocked) // 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()); assertEquals(grid.getPlayers().get(0), grid.getBoard().get(new Point(7, 7)).getA());
assertEquals(grid.getPlayers().get(1), grid.getBoard().get(new Point(7, 8)).getA()); assertEquals(grid.getPlayers().get(1), grid.getBoard().get(new Point(7, 8)).getA());
// test placeEnergyBallBRUT (mocked) // test placeEnergyBallBRUT (mocked)

View File

@ -1,40 +0,0 @@
package fr.lnl.game.server.games;
import fr.lnl.game.server.Mock;
import fr.lnl.game.server.games.action.Action;
import fr.lnl.game.server.games.action.Nothing;
import fr.lnl.game.server.games.grid.Grid;
import fr.lnl.game.server.games.player.ClassPlayer;
import fr.lnl.game.server.games.player.HumanPlayer;
import fr.lnl.game.server.games.player.Player;
import fr.lnl.game.server.games.player.RandomComputerPlayer;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
public class GameTest {
private Grid grid;
private Game game;
@BeforeEach
public void mock() {
List<Player> players = Arrays.asList(new HumanPlayer(1,null, ClassPlayer.DEFAULT),
new HumanPlayer(2,null, ClassPlayer.DEFAULT));
Mock mock = new Mock(players);
grid = mock.grid;
game = mock.game;
}
@Test
public void testPlay(){
System.out.println(game.getGrid());
while (!game.isOver()){
game.play();
}
System.out.println("Le gagnant est " + game.getWinner().toString());
}
}

View File

@ -0,0 +1,22 @@
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.player.Player;
import java.util.List;
public class Mock {
public BuildStrategy buildStrategy;
public Game game;
public Grid grid;
public Mock(List<Player> players) {
this.buildStrategy = new MockGridStrategy(new Grid(16,16, players),0.80F, 0.95F);
game = new Game(buildStrategy, players, new MockDisplayWinner());
this.grid = buildStrategy.getGrid();
}
}

View File

@ -1,4 +1,4 @@
package fr.lnl.game.server; package fr.lnl.game.server.mock;
import fr.lnl.game.server.listener.AbstractModelListening; import fr.lnl.game.server.listener.AbstractModelListening;

View File

@ -1,49 +1,36 @@
package fr.lnl.game.server; 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.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.LockStrategy;
import fr.lnl.game.server.games.grid.elements.EnergyBall; import fr.lnl.game.server.games.grid.elements.EnergyBall;
import fr.lnl.game.server.games.grid.elements.Wall; import fr.lnl.game.server.games.grid.elements.Wall;
import fr.lnl.game.server.games.player.ClassPlayer;
import fr.lnl.game.server.games.player.Player;
import fr.lnl.game.server.games.player.RandomComputerPlayer;
import fr.lnl.game.server.listener.GameFinishEvent;
import fr.lnl.game.server.utils.Point; import fr.lnl.game.server.utils.Point;
import java.util.Arrays; public class MockGridStrategy extends LockStrategy {
import java.util.List;
public class Mock { public MockGridStrategy(Grid grid, float wallProbability, float energyProbability) {
super(grid, wallProbability, energyProbability);
public BuildStrategy buildStrategy;
public Game game;
public Grid grid;
public Mock(List<Player> players) {
this.buildStrategy = new LockStrategy(new Grid(16,16, players),0.80F, 0.95F);
game = new Game(buildStrategy, players, new MockDisplayWinner());
this.grid = buildStrategy.getGrid();
} }
public void placePlayersBRUT(){ @Override
grid.getBoard().get(new Point(7,7)).setA(grid.getPlayers().get(0)); protected void initPlaceEnergyBall(float probability) {
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 placeEnergyBallBRUT(){
grid.getBoard().get(new Point(2,3)).setB(new EnergyBall()); grid.getBoard().get(new Point(2,3)).setB(new EnergyBall());
grid.getBoard().get(new Point(8,10)).setB(new EnergyBall()); grid.getBoard().get(new Point(8,10)).setB(new EnergyBall());
} }
public void placeInternWallBRUT(){ @Override
protected void initPlaceInternWall(float probability) {
grid.getBoard().get(new Point(3,6)).setB(new Wall(3,6)); 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(7,14)).setB(new Wall(7,14));
grid.getBoard().get(new Point(10,7)).setB(new Wall(10,7)); 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(14,2)).setB(new Wall(14,2));
} }
@Override
public void initPlacePlayers() {
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));
}
} }