Endrer checkAllFlags til å bruke hjelpemetoder fra Board

This commit is contained in:
GabrielMagnus 2020-04-16 12:36:33 +02:00
parent d8167ba250
commit 4ddc2a1a83

View File

@ -554,15 +554,26 @@ public class RoboRallyGame implements IRoboRallyGame {
TileType.FLAG_2, TileType.FLAG_3, TileType.FLAG_4); TileType.FLAG_2, TileType.FLAG_3, TileType.FLAG_4);
for (BoardElementContainer<Tile> flag:listOfFlags) { for (BoardElementContainer<Tile> flag:listOfFlags) {
Position flagPosition = flag.getPosition(); Position flagPosition = flag.getPosition();
if (gameBoard.hasRobotOnPosition(flagPosition)) { if (!gameBoard.hasRobotOnPosition(flagPosition)) {
continue;
}
RobotID robotID = gameBoard.getRobotOnPosition(flagPosition); RobotID robotID = gameBoard.getRobotOnPosition(flagPosition);
for (Robot robot : gameBoard.getAliveRobots()) { if (gameBoard.isHasTouchedFlagThisTurnFromRobotID(robotID)) {
if (robot.getRobotId() != robotID || robot.isHasTouchedFlagThisTurn()) {
continue; continue;
} }
gameBoard.updateFlagOnRobot(robotID, flag.getElement().getTileType()); gameBoard.updateFlagOnRobot(robotID, flag.getElement().getTileType());
robot.setHasTouchedFlagThisTurn(true); gameBoard.setHasTouchedFlagThisTurnFromRobotID(robotID,true);
if (victoryCheck(robot.getLastFlagVisited(), listOfFlags.size())) { checkIfPlayerWon(robotID, listOfFlags.size());
}
}
/**
* Checks if the player won, and shows the victory screen
* @param robotID The robot to be checked
* @param numberOfFlags The number of flags on the map
*/
private void checkIfPlayerWon(RobotID robotID, int numberOfFlags) {
if (victoryCheck(gameBoard.getLastFlagVisitedFromRobotID(robotID), numberOfFlags)) {
for (Player player : playerList) { for (Player player : playerList) {
if (player.getRobotID() != robotID) { if (player.getRobotID() != robotID) {
continue; continue;
@ -572,13 +583,17 @@ public class RoboRallyGame implements IRoboRallyGame {
} }
} }
} }
}
}
}
/**
* Checks if last flag visited is the last flag
* @param lastFlagVisited The last flag a robot visited
* @param lastFlag The last flag on the map
* @return If the robot won
*/
private boolean victoryCheck(int lastFlagVisited, int lastFlag) { private boolean victoryCheck(int lastFlagVisited, int lastFlag) {
return (lastFlagVisited == lastFlag); return (lastFlagVisited == lastFlag);
} }
/** /**
* Fires all lasers on the game board * Fires all lasers on the game board
*/ */