Erstatter Objects.requireNonNull med korrekt sjekking av variabler

This commit is contained in:
Kristian Knarvik 2020-04-23 15:25:16 +02:00
parent d473b37252
commit 8676434326
2 changed files with 11 additions and 8 deletions

View File

@ -177,10 +177,11 @@ public class Board {
* @param powerDown The status of the power down * @param powerDown The status of the power down
*/ */
public void setPowerDown(RobotID robotID, Boolean powerDown) { public void setPowerDown(RobotID robotID, Boolean powerDown) {
Robot alternateRobot = getRobotFromDeadRobots(robotID);
if (robots.containsKey(robotID)) { if (robots.containsKey(robotID)) {
robots.get(robotID).setPowerDown(powerDown); robots.get(robotID).setPowerDown(powerDown);
} else if (getRobotFromDeadRobots(robotID) != null) { } else if ( alternateRobot != null) {
Objects.requireNonNull(getRobotFromDeadRobots(robotID)).setPowerDown(powerDown); alternateRobot.setPowerDown(powerDown);
} }
} }
@ -201,10 +202,11 @@ public class Board {
* @return The power down status of the robot * @return The power down status of the robot
*/ */
public boolean getPowerDown(RobotID robotID) { public boolean getPowerDown(RobotID robotID) {
Robot alternateRobot = getRobotFromDeadRobots(robotID);
if (robots.containsKey(robotID)) { if (robots.containsKey(robotID)) {
return robots.get(robotID).isInPowerDown(); return robots.get(robotID).isInPowerDown();
} else if (getRobotFromDeadRobots(robotID) != null) { } else if (alternateRobot != null) {
return Objects.requireNonNull(getRobotFromDeadRobots(robotID)).isInPowerDown(); return alternateRobot.isInPowerDown();
} }
return false; return false;
} }

View File

@ -15,7 +15,6 @@ import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
/** /**
* This class represent a game which is drawable using libgdx * This class represent a game which is drawable using libgdx
@ -59,8 +58,9 @@ public class RoboRallyGame implements DrawableGame, InteractableGame {
* @return Whether this player's robot is in power down * @return Whether this player's robot is in power down
*/ */
public Boolean getRobotPowerDown() { public Boolean getRobotPowerDown() {
if (getPlayerFromName(this.playerName) != null) { Player player = getPlayerFromName(this.playerName);
return gameBoard.getPowerDown(Objects.requireNonNull(getPlayerFromName(this.playerName)).getRobotID()); if (player != null) {
return gameBoard.getPowerDown(player.getRobotID());
} }
return false; return false;
} }
@ -172,7 +172,8 @@ public class RoboRallyGame implements DrawableGame, InteractableGame {
removeNonLockedProgrammingCardsFromPlayers(); removeNonLockedProgrammingCardsFromPlayers();
} }
if (getPlayerFromName(this.playerName) != null && gameBoard.getPowerDown(Objects.requireNonNull(getPlayerFromName(this.playerName)).getRobotID())) { Player player = getPlayerFromName(this.playerName);
if (player != null && gameBoard.getPowerDown(player.getRobotID())) {
setGameState(GameState.CHOOSING_STAY_IN_POWER_DOWN); setGameState(GameState.CHOOSING_STAY_IN_POWER_DOWN);
} else { } else {
setGameState(GameState.SKIP_STAY_IN_POWER_DOWN); setGameState(GameState.SKIP_STAY_IN_POWER_DOWN);