This commit is contained in:
GabrielMagnus 2020-02-27 11:19:56 +01:00
commit 24db74ca4e
4 changed files with 69 additions and 56 deletions

View File

@ -34,6 +34,11 @@ public class Position {
return yCoordinate; return yCoordinate;
} }
@Override
public String toString() {
return String.format("X: %d, Y: %d", xCoordinate, yCoordinate);
}
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (obj.getClass() != Position.class) { if (obj.getClass() != Position.class) {

View File

@ -29,7 +29,7 @@ public class Game implements IDrawableGame {
try { try {
runGameLoop(); runGameLoop();
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); Thread.currentThread().interrupt();
} }
}).start(); }).start();
} catch (IOException e) { } catch (IOException e) {
@ -42,43 +42,48 @@ public class Game implements IDrawableGame {
* @throws InterruptedException If interrupted while trying to sleep * @throws InterruptedException If interrupted while trying to sleep
*/ */
private void runGameLoop() throws InterruptedException { private void runGameLoop() throws InterruptedException {
long cycleDelay = 600;
TimeUnit.SECONDS.sleep(3); TimeUnit.SECONDS.sleep(3);
gameBoard.rotateRobotRight(RobotID.ROBOT_1); gameBoard.rotateRobotRight(RobotID.ROBOT_1);
TimeUnit.MILLISECONDS.sleep(cycleDelay);
gameBoard.rotateRobotRight(RobotID.ROBOT_1); gameBoard.rotateRobotRight(RobotID.ROBOT_1);
TimeUnit.SECONDS.sleep(1); TimeUnit.MILLISECONDS.sleep(cycleDelay);
gameBoard.moveRobotForward(RobotID.ROBOT_1); gameBoard.moveRobotForward(RobotID.ROBOT_1);
TimeUnit.SECONDS.sleep(1); TimeUnit.MILLISECONDS.sleep(cycleDelay);
gameBoard.rotateRobotLeft(RobotID.ROBOT_1); gameBoard.rotateRobotLeft(RobotID.ROBOT_1);
TimeUnit.SECONDS.sleep(1); TimeUnit.MILLISECONDS.sleep(cycleDelay);
gameBoard.moveRobotForward(RobotID.ROBOT_1); gameBoard.moveRobotForward(RobotID.ROBOT_1);
TimeUnit.SECONDS.sleep(1); TimeUnit.MILLISECONDS.sleep(cycleDelay);
gameBoard.moveRobotForward(RobotID.ROBOT_1); gameBoard.moveRobotForward(RobotID.ROBOT_1);
TimeUnit.SECONDS.sleep(1); TimeUnit.MILLISECONDS.sleep(cycleDelay);
gameBoard.rotateRobotRight(RobotID.ROBOT_1); gameBoard.rotateRobotRight(RobotID.ROBOT_1);
TimeUnit.SECONDS.sleep(1); TimeUnit.MILLISECONDS.sleep(cycleDelay);
gameBoard.moveRobotForward(RobotID.ROBOT_1); gameBoard.moveRobotForward(RobotID.ROBOT_1);
TimeUnit.SECONDS.sleep(1); TimeUnit.MILLISECONDS.sleep(cycleDelay);
gameBoard.rotateRobotRight(RobotID.ROBOT_2); gameBoard.rotateRobotRight(RobotID.ROBOT_2);
TimeUnit.SECONDS.sleep(1); TimeUnit.MILLISECONDS.sleep(cycleDelay);
gameBoard.moveRobotForward(RobotID.ROBOT_2); gameBoard.moveRobotForward(RobotID.ROBOT_2);
TimeUnit.SECONDS.sleep(1); TimeUnit.MILLISECONDS.sleep(cycleDelay);
gameBoard.rotateRobotRight(RobotID.ROBOT_2); gameBoard.rotateRobotRight(RobotID.ROBOT_2);
TimeUnit.MILLISECONDS.sleep(cycleDelay);
gameBoard.rotateRobotRight(RobotID.ROBOT_2); gameBoard.rotateRobotRight(RobotID.ROBOT_2);
TimeUnit.SECONDS.sleep(1); TimeUnit.MILLISECONDS.sleep(cycleDelay);
gameBoard.moveRobotForward(RobotID.ROBOT_2); gameBoard.moveRobotForward(RobotID.ROBOT_2);
TimeUnit.SECONDS.sleep(1); TimeUnit.MILLISECONDS.sleep(cycleDelay);
gameBoard.rotateRobotRight(RobotID.ROBOT_2); gameBoard.rotateRobotRight(RobotID.ROBOT_2);
TimeUnit.MILLISECONDS.sleep(cycleDelay);
gameBoard.rotateRobotRight(RobotID.ROBOT_2); gameBoard.rotateRobotRight(RobotID.ROBOT_2);
TimeUnit.SECONDS.sleep(1); TimeUnit.MILLISECONDS.sleep(cycleDelay);
gameBoard.moveRobotForward(RobotID.ROBOT_2); gameBoard.moveRobotForward(RobotID.ROBOT_2);
TimeUnit.SECONDS.sleep(1); TimeUnit.MILLISECONDS.sleep(cycleDelay);
gameBoard.moveRobotForward(RobotID.ROBOT_2); gameBoard.moveRobotForward(RobotID.ROBOT_2);
TimeUnit.SECONDS.sleep(1); TimeUnit.MILLISECONDS.sleep(cycleDelay);
gameBoard.rotateRobotRight(RobotID.ROBOT_2); gameBoard.rotateRobotRight(RobotID.ROBOT_2);
TimeUnit.MILLISECONDS.sleep(cycleDelay);
gameBoard.rotateRobotRight(RobotID.ROBOT_2); gameBoard.rotateRobotRight(RobotID.ROBOT_2);
TimeUnit.SECONDS.sleep(1); TimeUnit.MILLISECONDS.sleep(cycleDelay);
gameBoard.moveRobotForward(RobotID.ROBOT_2); gameBoard.moveRobotForward(RobotID.ROBOT_2);
TimeUnit.SECONDS.sleep(1); TimeUnit.MILLISECONDS.sleep(cycleDelay);
gameBoard.moveRobotForward(RobotID.ROBOT_2); gameBoard.moveRobotForward(RobotID.ROBOT_2);
} }

View File

@ -85,14 +85,6 @@ public class Board {
return getAllElementsFromGrid(walls); return getAllElementsFromGrid(walls);
} }
/**
* Removes a dead robot from the board over to the dead robot list
* @param robot the dead robot
*/
public void removeDeadRobotFromBoard(Robot robot) {
robots.remove(robot.getRobotId());
}
/** /**
* Rotates a robot to the right * Rotates a robot to the right
* @param robotID The id of the robot to rotate * @param robotID The id of the robot to rotate
@ -208,7 +200,7 @@ public class Board {
*/ */
private void killRobot(Robot robot) { private void killRobot(Robot robot) {
robot.setAmountOfLives(robot.getAmountOfLives() - 1); robot.setAmountOfLives(robot.getAmountOfLives() - 1);
removeDeadRobotFromBoard(robot); robots.remove(robot.getRobotId());
deadRobots.add(robot); deadRobots.add(robot);
} }
@ -304,7 +296,6 @@ public class Board {
robot.setFacingDirection(Direction.NORTH); robot.setFacingDirection(Direction.NORTH);
robots.put(robot.getRobotId(), robot); robots.put(robot.getRobotId(), robot);
} }
else {deadRobots.remove(robot); }
} }
deadRobots = new ArrayList<>(); deadRobots = new ArrayList<>();
} }

View File

@ -6,6 +6,7 @@ import inf112.fiasko.roborally.element_properties.RobotID;
import inf112.fiasko.roborally.element_properties.TileType; import inf112.fiasko.roborally.element_properties.TileType;
import inf112.fiasko.roborally.element_properties.WallType; import inf112.fiasko.roborally.element_properties.WallType;
import org.junit.Before; import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import java.util.ArrayList; import java.util.ArrayList;
@ -19,25 +20,35 @@ import static org.junit.Assert.assertTrue;
public class BoardTest { public class BoardTest {
private Grid<Tile> tileGrid; private Grid<Tile> tileGrid;
private Grid<Wall> wallGrid; private Grid<Wall> wallGrid;
private Position someValidPosition1; private static Position zeroPosition;
private Position someValidPosition5; private static Position someValidPosition1;
private Position someValidPosition6; private static Position someValidPosition2;
private Position someValidPosition7; private static Position someValidPosition3;
private static Position someValidPosition4;
private static Position someValidPosition5;
private static Position someValidPosition6;
private static Position someValidPosition7;
private static Position someValidPosition8;
private List<Robot> robotList; private List<Robot> robotList;
private Board board; private Board board;
@BeforeClass
public static void globalSetUp() {
zeroPosition = new Position(0, 0);
someValidPosition1 = new Position(2, 2);
someValidPosition2 = new Position(2, 1);
someValidPosition3 = new Position(2, 3);
someValidPosition4 = new Position(2, 4);
someValidPosition5 = new Position(3, 1);
someValidPosition6 = new Position(3, 2);
someValidPosition7 = new Position(3, 3);
someValidPosition8 = new Position(3, 4);
}
@Before @Before
public void setUp() { public void setUp() {
tileGrid = new Grid<>(5, 5, new Tile(TileType.TILE, Direction.NORTH)); tileGrid = new Grid<>(5, 5, new Tile(TileType.TILE, Direction.NORTH));
wallGrid = new Grid<>(5, 5); wallGrid = new Grid<>(5, 5);
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);
robotList = new ArrayList<>(); robotList = new ArrayList<>();
robotList.add(new Robot(RobotID.ROBOT_1, someValidPosition1)); robotList.add(new Robot(RobotID.ROBOT_1, someValidPosition1));
robotList.add(new Robot(RobotID.ROBOT_2, someValidPosition2)); robotList.add(new Robot(RobotID.ROBOT_2, someValidPosition2));
@ -84,28 +95,30 @@ public class BoardTest {
@Test @Test
public void robotCanBeRotatedLeft() { public void robotCanBeRotatedLeft() {
assertEquals(Direction.NORTH, robotList.get(0).getFacingDirection()); Robot robot = robotList.get(0);
assertEquals(Direction.NORTH, robot.getFacingDirection());
board.rotateRobotLeft(RobotID.ROBOT_1); board.rotateRobotLeft(RobotID.ROBOT_1);
assertEquals(Direction.WEST, robotList.get(0).getFacingDirection()); assertEquals(Direction.WEST, robot.getFacingDirection());
board.rotateRobotLeft(RobotID.ROBOT_1); board.rotateRobotLeft(RobotID.ROBOT_1);
assertEquals(Direction.SOUTH, robotList.get(0).getFacingDirection()); assertEquals(Direction.SOUTH, robot.getFacingDirection());
board.rotateRobotLeft(RobotID.ROBOT_1); board.rotateRobotLeft(RobotID.ROBOT_1);
assertEquals(Direction.EAST, robotList.get(0).getFacingDirection()); assertEquals(Direction.EAST, robot.getFacingDirection());
board.rotateRobotLeft(RobotID.ROBOT_1); board.rotateRobotLeft(RobotID.ROBOT_1);
assertEquals(Direction.NORTH, robotList.get(0).getFacingDirection()); assertEquals(Direction.NORTH, robot.getFacingDirection());
} }
@Test @Test
public void robotCanBeRotatedRight() { public void robotCanBeRotatedRight() {
assertEquals(Direction.NORTH, robotList.get(0).getFacingDirection()); Robot robot = robotList.get(0);
assertEquals(Direction.NORTH, robot.getFacingDirection());
board.rotateRobotRight(RobotID.ROBOT_1); board.rotateRobotRight(RobotID.ROBOT_1);
assertEquals(Direction.EAST, robotList.get(0).getFacingDirection()); assertEquals(Direction.EAST, robot.getFacingDirection());
board.rotateRobotRight(RobotID.ROBOT_1); board.rotateRobotRight(RobotID.ROBOT_1);
assertEquals(Direction.SOUTH, robotList.get(0).getFacingDirection()); assertEquals(Direction.SOUTH, robot.getFacingDirection());
board.rotateRobotRight(RobotID.ROBOT_1); board.rotateRobotRight(RobotID.ROBOT_1);
assertEquals(Direction.WEST, robotList.get(0).getFacingDirection()); assertEquals(Direction.WEST, robot.getFacingDirection());
board.rotateRobotRight(RobotID.ROBOT_1); board.rotateRobotRight(RobotID.ROBOT_1);
assertEquals(Direction.NORTH, robotList.get(0).getFacingDirection()); assertEquals(Direction.NORTH, robot.getFacingDirection());
} }
@Test (expected = IllegalArgumentException.class) @Test (expected = IllegalArgumentException.class)
@ -123,9 +136,9 @@ public class BoardTest {
@Test @Test
public void killRobotReducesAmountOfLivesByOne() { public void killRobotReducesAmountOfLivesByOne() {
Robot robot = board.getAliveRobots().get(1); Robot robot = robotList.get(1);
assertEquals(3, robot.getAmountOfLives()); assertEquals(3, robot.getAmountOfLives());
robot.setPosition(new Position(0, 0)); robot.setPosition(zeroPosition);
board.moveRobot(robot.getRobotId(), Direction.NORTH); board.moveRobot(robot.getRobotId(), Direction.NORTH);
assertEquals(2, robot.getAmountOfLives()); assertEquals(2, robot.getAmountOfLives());
} }
@ -133,19 +146,18 @@ public class BoardTest {
@Test @Test
public void respawnRobotAtBackupPosition() { public void respawnRobotAtBackupPosition() {
Robot robot = robotList.get(0); Robot robot = robotList.get(0);
robot.setPosition(new Position(0, 0)); robot.setPosition(zeroPosition);
board.moveRobot(robot.getRobotId(), Direction.NORTH); board.moveRobot(robot.getRobotId(), Direction.NORTH);
board.removeDeadRobotFromBoard(robot);
board.respawnRobots(); board.respawnRobots();
assertEquals(robot.getBackupPosition(), someValidPosition1); assertEquals(robot.getBackupPosition(), robot.getPosition());
} }
@Test @Test
public void respawnRobotDoesNotRespawnARobotWithNoLives() { public void respawnRobotDoesNotRespawnARobotWithNoLives() {
Robot robot = board.getAliveRobots().get(0); Robot robot = robotList.get(0);
robot.setPosition(zeroPosition);
robot.setAmountOfLives(1); robot.setAmountOfLives(1);
board.moveRobot(robot.getRobotId(), Direction.NORTH); board.moveRobot(robot.getRobotId(), Direction.NORTH);
board.removeDeadRobotFromBoard(robot);
board.respawnRobots(); board.respawnRobots();
assertFalse(board.isRobotAlive(robot.getRobotId())); assertFalse(board.isRobotAlive(robot.getRobotId()));
} }