2020-02-22 23:27:59 +01:00
|
|
|
package inf112.fiasko.roborally.objects;
|
|
|
|
|
2020-02-23 00:02:03 +01:00
|
|
|
import inf112.fiasko.roborally.element_properties.Direction;
|
|
|
|
import inf112.fiasko.roborally.element_properties.Position;
|
|
|
|
import inf112.fiasko.roborally.element_properties.RobotID;
|
|
|
|
import inf112.fiasko.roborally.element_properties.TileType;
|
|
|
|
import inf112.fiasko.roborally.element_properties.WallType;
|
2020-02-22 23:27:59 +01:00
|
|
|
import org.junit.Before;
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2020-02-23 20:25:30 +01:00
|
|
|
import static org.junit.Assert.assertEquals;
|
2020-02-22 23:27:59 +01:00
|
|
|
import static org.junit.Assert.assertFalse;
|
2020-02-25 17:45:39 +01:00
|
|
|
import static org.junit.Assert.assertNotEquals;
|
2020-02-22 23:27:59 +01:00
|
|
|
import static org.junit.Assert.assertTrue;
|
|
|
|
|
|
|
|
public class BoardTest {
|
|
|
|
private Grid<Tile> tileGrid;
|
|
|
|
private Grid<Wall> wallGrid;
|
2020-02-25 17:45:39 +01:00
|
|
|
private Position someValidPosition1;
|
|
|
|
private Position someValidPosition5;
|
|
|
|
private Position someValidPosition6;
|
|
|
|
private Position someValidPosition7;
|
2020-02-22 23:27:59 +01:00
|
|
|
private List<Robot> robotList;
|
|
|
|
private Board board;
|
|
|
|
|
|
|
|
@Before
|
|
|
|
public void setUp() {
|
|
|
|
tileGrid = new Grid<>(5, 5, new Tile(TileType.TILE, Direction.NORTH));
|
|
|
|
wallGrid = new Grid<>(5, 5);
|
2020-02-25 17:45:39 +01:00
|
|
|
someValidPosition1 = new Position(2, 2);
|
|
|
|
Position someValidPosition2 = new Position(2, 1);
|
|
|
|
Position someValidPosition3 = new Position(2, 3);
|
|
|
|
Position someValidPosition4 = new Position(2, 4);
|
|
|
|
someValidPosition5 = new Position(3, 1);
|
|
|
|
someValidPosition6 = new Position(3, 2);
|
|
|
|
someValidPosition7 = new Position(3, 3);
|
|
|
|
Position someValidPosition8 = new Position(3, 4);
|
2020-02-22 23:27:59 +01:00
|
|
|
robotList = new ArrayList<>();
|
2020-02-25 17:45:39 +01:00
|
|
|
robotList.add(new Robot(RobotID.ROBOT_1, someValidPosition1));
|
|
|
|
robotList.add(new Robot(RobotID.ROBOT_2, someValidPosition2));
|
|
|
|
robotList.add(new Robot(RobotID.ROBOT_3, someValidPosition3));
|
|
|
|
robotList.add(new Robot(RobotID.ROBOT_4, someValidPosition4));
|
|
|
|
robotList.add(new Robot(RobotID.ROBOT_5, someValidPosition5));
|
|
|
|
robotList.add(new Robot(RobotID.ROBOT_6, someValidPosition6));
|
|
|
|
robotList.add(new Robot(RobotID.ROBOT_7, someValidPosition7));
|
|
|
|
robotList.add(new Robot(RobotID.ROBOT_8, someValidPosition8));
|
2020-02-24 22:27:19 +01:00
|
|
|
wallGrid.setElement(2, 1, new Wall(WallType.WALL_NORMAL, Direction.SOUTH));
|
2020-02-22 23:27:59 +01:00
|
|
|
wallGrid.setElement(2, 2, new Wall(WallType.WALL_NORMAL, Direction.EAST));
|
|
|
|
wallGrid.setElement(1, 2, new Wall(WallType.WALL_CORNER, Direction.NORTH_EAST));
|
|
|
|
board = new Board(tileGrid, wallGrid, robotList);
|
|
|
|
}
|
|
|
|
|
2020-02-25 17:45:39 +01:00
|
|
|
@Test
|
|
|
|
public void robotCanPushRobots() {
|
|
|
|
board.moveRobot(RobotID.ROBOT_5, Direction.SOUTH);
|
|
|
|
assertNotEquals(someValidPosition5, robotList.get(4).getPosition());
|
|
|
|
assertNotEquals(someValidPosition6, robotList.get(5).getPosition());
|
|
|
|
assertNotEquals(someValidPosition7, robotList.get(6).getPosition());
|
|
|
|
assertFalse(board.getAliveRobots().contains(robotList.get(7)));
|
|
|
|
}
|
|
|
|
|
2020-02-22 23:27:59 +01:00
|
|
|
@Test
|
|
|
|
public void robotCanMove() {
|
|
|
|
assertTrue(board.moveRobot(RobotID.ROBOT_1, Direction.SOUTH));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void robotIsStoppedByWallOnSameTile() {
|
|
|
|
assertFalse(board.moveRobot(RobotID.ROBOT_1, Direction.EAST));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void robotIsStoppedByWallOnAdjacentTile() {
|
|
|
|
assertFalse(board.moveRobot(RobotID.ROBOT_1, Direction.NORTH));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void robotIsStoppedByCornerWall() {
|
|
|
|
assertFalse(board.moveRobot(RobotID.ROBOT_1, Direction.WEST));
|
|
|
|
}
|
|
|
|
|
2020-02-23 20:25:30 +01:00
|
|
|
@Test
|
|
|
|
public void robotCanBeRotatedLeft() {
|
|
|
|
assertEquals(Direction.NORTH, robotList.get(0).getFacingDirection());
|
|
|
|
board.rotateRobotLeft(RobotID.ROBOT_1);
|
|
|
|
assertEquals(Direction.WEST, robotList.get(0).getFacingDirection());
|
|
|
|
board.rotateRobotLeft(RobotID.ROBOT_1);
|
|
|
|
assertEquals(Direction.SOUTH, robotList.get(0).getFacingDirection());
|
|
|
|
board.rotateRobotLeft(RobotID.ROBOT_1);
|
|
|
|
assertEquals(Direction.EAST, robotList.get(0).getFacingDirection());
|
|
|
|
board.rotateRobotLeft(RobotID.ROBOT_1);
|
|
|
|
assertEquals(Direction.NORTH, robotList.get(0).getFacingDirection());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void robotCanBeRotatedRight() {
|
2020-02-25 17:45:39 +01:00
|
|
|
assertEquals(Direction.NORTH, robotList.get(0).getFacingDirection());
|
|
|
|
board.rotateRobotRight(RobotID.ROBOT_1);
|
|
|
|
assertEquals(Direction.EAST, robotList.get(0).getFacingDirection());
|
|
|
|
board.rotateRobotRight(RobotID.ROBOT_1);
|
|
|
|
assertEquals(Direction.SOUTH, robotList.get(0).getFacingDirection());
|
|
|
|
board.rotateRobotRight(RobotID.ROBOT_1);
|
|
|
|
assertEquals(Direction.WEST, robotList.get(0).getFacingDirection());
|
|
|
|
board.rotateRobotRight(RobotID.ROBOT_1);
|
|
|
|
assertEquals(Direction.NORTH, robotList.get(0).getFacingDirection());
|
2020-02-23 20:25:30 +01:00
|
|
|
}
|
|
|
|
|
2020-02-22 23:27:59 +01:00
|
|
|
@Test (expected = IllegalArgumentException.class)
|
|
|
|
public void gridsOfDifferentSizeThrowsError() {
|
|
|
|
IGrid<Wall> wallGrid = new Grid<>(1, 1);
|
|
|
|
new Board(tileGrid, wallGrid, robotList);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test (expected = IllegalArgumentException.class)
|
|
|
|
public void multipleRobotsWithSameIDThrowsError() {
|
2020-02-25 17:45:39 +01:00
|
|
|
Robot robot = new Robot(RobotID.ROBOT_1, someValidPosition1);
|
2020-02-22 23:27:59 +01:00
|
|
|
robotList.add(robot);
|
|
|
|
new Board(tileGrid, wallGrid, robotList);
|
|
|
|
}
|
2020-02-25 17:45:39 +01:00
|
|
|
|
2020-02-25 15:48:34 +01:00
|
|
|
@Test
|
2020-02-25 17:45:39 +01:00
|
|
|
public void killRobotReducesAmountOfLivesByOne() {
|
|
|
|
Robot robot = board.getAliveRobots().get(1);
|
|
|
|
assertEquals(3, robot.getAmountOfLives());
|
|
|
|
robot.setPosition(new Position(0, 0));
|
|
|
|
board.moveRobot(robot.getRobotId(), Direction.NORTH);
|
2020-02-25 15:48:34 +01:00
|
|
|
assertEquals(2, robot.getAmountOfLives());
|
|
|
|
}
|
2020-02-26 20:22:19 +01:00
|
|
|
|
|
|
|
@Test
|
|
|
|
public void respawnRobotAtBackupPosition() {
|
2020-02-26 20:52:55 +01:00
|
|
|
Robot robot = robotList.get(0);
|
|
|
|
robot.setPosition(new Position(0, 0));
|
2020-02-26 20:22:19 +01:00
|
|
|
board.moveRobot(robot.getRobotId(), Direction.NORTH);
|
|
|
|
board.removeDeadRobotFromBoard(robot);
|
|
|
|
board.respawnRobots();
|
|
|
|
assertEquals(robot.getBackupPosition(), someValidPosition1);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void respawnRobotDoesNotRespawnARobotWithNoLives() {
|
|
|
|
Robot robot = board.getAliveRobots().get(0);
|
|
|
|
robot.setAmountOfLives(1);
|
|
|
|
board.moveRobot(robot.getRobotId(), Direction.NORTH);
|
|
|
|
board.removeDeadRobotFromBoard(robot);
|
|
|
|
board.respawnRobots();
|
|
|
|
assertFalse(board.isRobotAlive(robot.getRobotId()));
|
|
|
|
}
|
2020-02-22 23:27:59 +01:00
|
|
|
}
|