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;
|
2020-02-27 10:38:26 +01:00
|
|
|
import org.junit.BeforeClass;
|
2020-02-22 23:27:59 +01:00
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2020-03-16 16:45:45 +01:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.HashMap;
|
2020-02-22 23:27:59 +01:00
|
|
|
import java.util.List;
|
2020-03-16 16:45:45 +01:00
|
|
|
import java.util.function.Predicate;
|
2020-02-22 23:27:59 +01:00
|
|
|
|
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-27 10:38:26 +01:00
|
|
|
private static Position zeroPosition;
|
|
|
|
private static Position someValidPosition1;
|
|
|
|
private static Position someValidPosition2;
|
|
|
|
private static Position someValidPosition3;
|
|
|
|
private static Position someValidPosition4;
|
|
|
|
private static Position someValidPosition5;
|
|
|
|
private static Position someValidPosition6;
|
|
|
|
private static Position someValidPosition7;
|
|
|
|
private static Position someValidPosition8;
|
2020-02-22 23:27:59 +01:00
|
|
|
private List<Robot> robotList;
|
|
|
|
private Board board;
|
2020-03-16 16:45:45 +01:00
|
|
|
private Board boardWithDifferentAmountOfAllTypes;
|
|
|
|
private Map<WallType,Integer> wallTypeNumberMap = new HashMap<>();
|
|
|
|
private Map<TileType,Integer> tileTypeNumberMap = new HashMap<>();
|
2020-02-22 23:27:59 +01:00
|
|
|
|
2020-02-27 10:38:26 +01:00
|
|
|
@BeforeClass
|
|
|
|
public static void globalSetUp() {
|
|
|
|
zeroPosition = new Position(0, 0);
|
2020-02-25 17:45:39 +01:00
|
|
|
someValidPosition1 = new Position(2, 2);
|
2020-02-27 10:38:26 +01:00
|
|
|
someValidPosition2 = new Position(2, 1);
|
|
|
|
someValidPosition3 = new Position(2, 3);
|
|
|
|
someValidPosition4 = new Position(2, 4);
|
2020-02-25 17:45:39 +01:00
|
|
|
someValidPosition5 = new Position(3, 1);
|
|
|
|
someValidPosition6 = new Position(3, 2);
|
|
|
|
someValidPosition7 = new Position(3, 3);
|
2020-02-27 10:38:26 +01:00
|
|
|
someValidPosition8 = new Position(3, 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Before
|
|
|
|
public void setUp() {
|
|
|
|
tileGrid = new Grid<>(5, 5, new Tile(TileType.TILE, Direction.NORTH));
|
|
|
|
wallGrid = new Grid<>(5, 5);
|
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-03-16 16:45:45 +01:00
|
|
|
|
|
|
|
Grid<Tile> tileGridAllTypes = new Grid<>(6,6);
|
|
|
|
Grid<Wall> wallGridAllTypes = new Grid<>(6,6);
|
|
|
|
List<Robot> emptyRobotList = new ArrayList<>();
|
|
|
|
wallGridAllTypes.setElement(1, 1, new Wall(WallType.WALL_NORMAL, Direction.SOUTH));
|
|
|
|
wallGridAllTypes.setElement(1, 2, new Wall(WallType.WALL_NORMAL, Direction.SOUTH));
|
|
|
|
wallGridAllTypes.setElement(1, 3, new Wall(WallType.WALL_NORMAL, Direction.SOUTH));
|
|
|
|
wallGridAllTypes.setElement(2, 1, new Wall(WallType.WALL_CORNER, Direction.EAST));
|
|
|
|
wallGridAllTypes.setElement(2, 2, new Wall(WallType.WALL_CORNER, Direction.EAST));
|
|
|
|
tileGridAllTypes.setElement(1, 1, new Tile(TileType.COGWHEEL_LEFT, Direction.NORTH));
|
|
|
|
tileGridAllTypes.setElement(3, 1, new Tile(TileType.COGWHEEL_RIGHT, Direction.NORTH));
|
|
|
|
tileGridAllTypes.setElement(3, 2, new Tile(TileType.COGWHEEL_RIGHT, Direction.NORTH));
|
|
|
|
tileGridAllTypes.setElement(3, 3, new Tile(TileType.COGWHEEL_RIGHT, Direction.NORTH));
|
|
|
|
tileGridAllTypes.setElement(3, 4, new Tile(TileType.COGWHEEL_RIGHT, Direction.NORTH));
|
|
|
|
tileGridAllTypes.setElement(2, 1, new Tile(TileType.TILE, Direction.WEST));
|
|
|
|
tileGridAllTypes.setElement(2, 2, new Tile(TileType.TILE, Direction.WEST));
|
|
|
|
tileGridAllTypes.setElement(2, 3, new Tile(TileType.TILE, Direction.WEST));
|
|
|
|
tileGridAllTypes.setElement(2, 4, new Tile(TileType.TILE, Direction.WEST));
|
|
|
|
tileGridAllTypes.setElement(2, 5, new Tile(TileType.TILE, Direction.WEST));
|
|
|
|
wallTypeNumberMap.put(WallType.WALL_NORMAL, 3);
|
|
|
|
wallTypeNumberMap.put(WallType.WALL_CORNER, 2);
|
|
|
|
tileTypeNumberMap.put(TileType.COGWHEEL_RIGHT, 4);
|
|
|
|
tileTypeNumberMap.put(TileType.COGWHEEL_LEFT, 1);
|
|
|
|
tileTypeNumberMap.put(TileType.TILE, 5);
|
|
|
|
boardWithDifferentAmountOfAllTypes = new Board(tileGridAllTypes,wallGridAllTypes,emptyRobotList);
|
2020-02-22 23:27:59 +01:00
|
|
|
}
|
|
|
|
|
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() {
|
2020-02-27 10:38:26 +01:00
|
|
|
Robot robot = robotList.get(0);
|
|
|
|
assertEquals(Direction.NORTH, robot.getFacingDirection());
|
2020-02-23 20:25:30 +01:00
|
|
|
board.rotateRobotLeft(RobotID.ROBOT_1);
|
2020-02-27 10:38:26 +01:00
|
|
|
assertEquals(Direction.WEST, robot.getFacingDirection());
|
2020-02-23 20:25:30 +01:00
|
|
|
board.rotateRobotLeft(RobotID.ROBOT_1);
|
2020-02-27 10:38:26 +01:00
|
|
|
assertEquals(Direction.SOUTH, robot.getFacingDirection());
|
2020-02-23 20:25:30 +01:00
|
|
|
board.rotateRobotLeft(RobotID.ROBOT_1);
|
2020-02-27 10:38:26 +01:00
|
|
|
assertEquals(Direction.EAST, robot.getFacingDirection());
|
2020-02-23 20:25:30 +01:00
|
|
|
board.rotateRobotLeft(RobotID.ROBOT_1);
|
2020-02-27 10:38:26 +01:00
|
|
|
assertEquals(Direction.NORTH, robot.getFacingDirection());
|
2020-02-23 20:25:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void robotCanBeRotatedRight() {
|
2020-02-27 10:38:26 +01:00
|
|
|
Robot robot = robotList.get(0);
|
|
|
|
assertEquals(Direction.NORTH, robot.getFacingDirection());
|
2020-02-25 17:45:39 +01:00
|
|
|
board.rotateRobotRight(RobotID.ROBOT_1);
|
2020-02-27 10:38:26 +01:00
|
|
|
assertEquals(Direction.EAST, robot.getFacingDirection());
|
2020-02-25 17:45:39 +01:00
|
|
|
board.rotateRobotRight(RobotID.ROBOT_1);
|
2020-02-27 10:38:26 +01:00
|
|
|
assertEquals(Direction.SOUTH, robot.getFacingDirection());
|
2020-02-25 17:45:39 +01:00
|
|
|
board.rotateRobotRight(RobotID.ROBOT_1);
|
2020-02-27 10:38:26 +01:00
|
|
|
assertEquals(Direction.WEST, robot.getFacingDirection());
|
2020-02-25 17:45:39 +01:00
|
|
|
board.rotateRobotRight(RobotID.ROBOT_1);
|
2020-02-27 10:38:26 +01:00
|
|
|
assertEquals(Direction.NORTH, robot.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() {
|
2020-02-27 10:38:26 +01:00
|
|
|
Robot robot = robotList.get(1);
|
2020-02-25 17:45:39 +01:00
|
|
|
assertEquals(3, robot.getAmountOfLives());
|
2020-02-27 10:38:26 +01:00
|
|
|
robot.setPosition(zeroPosition);
|
2020-02-25 17:45:39 +01:00
|
|
|
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);
|
2020-02-27 10:38:26 +01:00
|
|
|
robot.setPosition(zeroPosition);
|
2020-02-26 20:22:19 +01:00
|
|
|
board.moveRobot(robot.getRobotId(), Direction.NORTH);
|
|
|
|
board.respawnRobots();
|
2020-02-27 10:38:26 +01:00
|
|
|
assertEquals(robot.getBackupPosition(), robot.getPosition());
|
2020-02-26 20:22:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void respawnRobotDoesNotRespawnARobotWithNoLives() {
|
2020-02-27 10:38:26 +01:00
|
|
|
Robot robot = robotList.get(0);
|
|
|
|
robot.setPosition(zeroPosition);
|
2020-02-26 20:22:19 +01:00
|
|
|
robot.setAmountOfLives(1);
|
|
|
|
board.moveRobot(robot.getRobotId(), Direction.NORTH);
|
|
|
|
board.respawnRobots();
|
|
|
|
assertFalse(board.isRobotAlive(robot.getRobotId()));
|
|
|
|
}
|
2020-03-16 16:45:45 +01:00
|
|
|
|
|
|
|
@Test
|
|
|
|
public void getPositionsOfTileOnBoardGivesCorrectAmountOfCogwheelLeftTiles() {
|
|
|
|
assertEquals((int)tileTypeNumberMap.get(TileType.COGWHEEL_LEFT),
|
|
|
|
boardWithDifferentAmountOfAllTypes.getPositionsOfTileOnBoard(TileType.COGWHEEL_LEFT).size());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void getPositionsOfTileOnBoardHasTypeCogwheelLeft() {
|
|
|
|
List<BoardElementContainer<Tile>> boardElemList = boardWithDifferentAmountOfAllTypes.getPositionsOfTileOnBoard(TileType.COGWHEEL_LEFT);
|
|
|
|
|
|
|
|
for (BoardElementContainer<Tile> elem : boardElemList) {
|
|
|
|
assertEquals(elem.getObject().getTileType(), TileType.COGWHEEL_LEFT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void getPositionsOfTileOnBoardGivesCorrectAmountOfTileTiles() {
|
|
|
|
assertEquals((int)tileTypeNumberMap.get(TileType.TILE),
|
|
|
|
boardWithDifferentAmountOfAllTypes.getPositionsOfTileOnBoard(TileType.TILE).size());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void getPositionsOfTileOnBoardHasTypeTile() {
|
|
|
|
List<BoardElementContainer<Tile>> boardElemList = boardWithDifferentAmountOfAllTypes.getPositionsOfTileOnBoard(TileType.TILE);
|
|
|
|
|
|
|
|
for (BoardElementContainer<Tile> elem : boardElemList) {
|
|
|
|
assertEquals(elem.getObject().getTileType(), TileType.TILE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void getPositionsOfWallOnBoardGivesCorrectAmountOfWallNormalWalls() {
|
|
|
|
assertEquals((int)wallTypeNumberMap.get(WallType.WALL_NORMAL),
|
|
|
|
boardWithDifferentAmountOfAllTypes.getPositionsOfWallOnBoard(WallType.WALL_NORMAL).size());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void getPositionsOfWallOnBoardHasTypeWallNormal() {
|
|
|
|
List<BoardElementContainer<Wall>> boardElemList = boardWithDifferentAmountOfAllTypes.getPositionsOfWallOnBoard(WallType.WALL_NORMAL);
|
|
|
|
|
|
|
|
for (BoardElementContainer<Wall> elem : boardElemList) {
|
|
|
|
assertEquals(elem.getObject().getWallType(), WallType.WALL_NORMAL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void getPositionsOfWallOnBoardGivesCorrectAmountOfWallCornerWalls() {
|
|
|
|
assertEquals((int)wallTypeNumberMap.get(WallType.WALL_CORNER),
|
|
|
|
boardWithDifferentAmountOfAllTypes.getPositionsOfWallOnBoard(WallType.WALL_CORNER).size());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void getPositionsOfWallOnBoardHasTypeWallCorner() {
|
|
|
|
List<BoardElementContainer<Wall>> boardElemList = boardWithDifferentAmountOfAllTypes.getPositionsOfWallOnBoard(WallType.WALL_CORNER);
|
|
|
|
|
|
|
|
for (BoardElementContainer<Wall> elem : boardElemList) {
|
|
|
|
assertEquals(elem.getObject().getWallType(), WallType.WALL_CORNER);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void getPositionsOfWallOnBoardHasCorrect() {
|
|
|
|
List<BoardElementContainer<Wall>> boardElemList = boardWithDifferentAmountOfAllTypes.getPositionsOfWallOnBoard(WallType.WALL_CORNER);
|
|
|
|
Predicate<BoardElementContainer<Wall>> pred = (element) -> element.getObject().getWallType() == WallType.WALL_CORNER;
|
|
|
|
boardElemList.removeIf(pred);
|
|
|
|
assertEquals(0, boardElemList.size());
|
|
|
|
}
|
2020-02-22 23:27:59 +01:00
|
|
|
}
|