Legger til automatisk rotasjon av brett med større bredde enn høyde

This commit is contained in:
Kristian Knarvik 2020-05-05 12:33:20 +02:00
parent a1c8b495b8
commit 979703bcf9
2 changed files with 27 additions and 36 deletions

View File

@ -51,6 +51,26 @@ public final class BoardLoaderUtil {
return boards;
}
/**
* Loads a board described in a file
*
* @param boardFile The file containing the board description
* @param robotList A list of robots on the board
* @return A board
* @throws IOException If the board file cannot be loaded
*/
public static Board loadBoard(String boardFile, List<Robot> robotList) throws IOException {
TwoTuple<Grid<Tile>, Grid<Wall>> grids = loadBoardGrids(boardFile);
Grid<Tile> tileGrid = grids.value1;
Grid<Wall> wallGrid = grids.value2;
if (grids.value1.getHeight() < grids.value1.getWidth()) {
tileGrid = rotateGrid(tileGrid, true);
wallGrid = rotateGrid(wallGrid, true);
}
adjustRobotRotationToBoardRotation(tileGrid, robotList);
return new Board(tileGrid, wallGrid, robotList);
}
/**
* Gets a list of all available boards
*
@ -69,21 +89,6 @@ public final class BoardLoaderUtil {
return boardList;
}
/**
* Loads and rotates a board described in a file
*
* @param boardFile The file containing the board description
* @param robotList A list of robots on the board
* @param clockwise Whether to rotate the board clockwise
* @return A board
* @throws IOException If the board file cannot be loaded
*/
public static Board loadBoardRotated(String boardFile, List<Robot> robotList, boolean clockwise) throws IOException {
TwoTuple<Grid<Tile>, Grid<Wall>> grids = loadBoardGrids(boardFile);
adjustRobotRotationToBoardRotation(grids.value1, robotList);
return new Board(rotateGrid(grids.value1, clockwise), rotateGrid(grids.value2, clockwise), robotList);
}
/**
* Loads the grids necessary to create a board
*
@ -138,20 +143,6 @@ public final class BoardLoaderUtil {
return newGrid;
}
/**
* Loads a board described in a file
*
* @param boardFile The file containing the board description
* @param robotList A list of robots on the board
* @return A board
* @throws IOException If the board file cannot be loaded
*/
public static Board loadBoard(String boardFile, List<Robot> robotList) throws IOException {
TwoTuple<Grid<Tile>, Grid<Wall>> grids = loadBoardGrids(boardFile);
adjustRobotRotationToBoardRotation(grids.value1, robotList);
return new Board(grids.value1, grids.value2, robotList);
}
/**
* Changes the direction of robots to the direction which is up
*
@ -159,13 +150,13 @@ public final class BoardLoaderUtil {
* @param robotList The list of robots on the board
*/
private static void adjustRobotRotationToBoardRotation(Grid<Tile> tileGrid, List<Robot> robotList) {
//The flags are always in the up direction
List<BoardElementContainer<Tile>> flags = GridUtil.getMatchingElements(TileType.ROBOT_SPAWN_1, tileGrid);
//The spawns are always in the up direction
List<BoardElementContainer<Tile>> spawns = GridUtil.getMatchingElements(TileType.ROBOT_SPAWN_1, tileGrid);
Direction boardDirection;
if (flags.size() == 0) {
if (spawns.size() == 0) {
boardDirection = Direction.NORTH;
} else {
boardDirection = flags.get(0).getElement().getDirection();
boardDirection = spawns.get(0).getElement().getDirection();
}
for (Robot robot : robotList) {
robot.setFacingDirection(boardDirection);

View File

@ -86,21 +86,21 @@ public class PhaseTest {
assertTrue(robot.hasTouchedFlagThisTurn());
robot.setHasTouchedFlagThisTurn(false);
assertNull(testGame.getWinningPlayerName());
board.moveRobot(robotID, Direction.EAST);
board.moveRobot(robotID, Direction.SOUTH);
testPhase.checkAllFlags();
//Should have registered second flag
assertEquals(2, robot.getLastFlagVisited());
assertTrue(robot.hasTouchedFlagThisTurn());
assertNull(testGame.getWinningPlayerName());
robot.setHasTouchedFlagThisTurn(false);
board.moveRobot(robotID, Direction.EAST);
board.moveRobot(robotID, Direction.SOUTH);
testPhase.checkAllFlags();
//Should have registered third flag
assertEquals(3, robot.getLastFlagVisited());
assertTrue(robot.hasTouchedFlagThisTurn());
assertNull(testGame.getWinningPlayerName());
robot.setHasTouchedFlagThisTurn(false);
board.moveRobot(robotID, Direction.EAST);
board.moveRobot(robotID, Direction.SOUTH);
testPhase.checkAllFlags();
//Should have registered fourth and last flag
assertEquals(4, robot.getLastFlagVisited());