Legger til korrekt kopiering av roboter for å forhindre Game i å direkte endre en robot

This commit is contained in:
Kristian Knarvik 2020-03-09 12:32:11 +01:00
parent 9101731c86
commit 1c1bbc6a79
2 changed files with 18 additions and 1 deletions

View File

@ -66,7 +66,9 @@ public class Board {
* @return A list of alive robots * @return A list of alive robots
*/ */
public List<Robot> getAliveRobots() { public List<Robot> getAliveRobots() {
return new ArrayList<>(robots.values()); List<Robot> robotsCopy = new ArrayList<>(robots.values());
robotsCopy.replaceAll(Robot::copy);
return robotsCopy;
} }
/** /**

View File

@ -145,4 +145,19 @@ public class Robot {
* @return amount of life left * @return amount of life left
*/ */
public int getAmountOfLives() { return this.amountOfLives; } public int getAmountOfLives() { return this.amountOfLives; }
/**
* Makes a copy of this robot with the same properties as this robot
* @return A copy of this robot
*/
public Robot copy() {
Robot copy = new Robot(this.robotId, this.currentPosition);
copy.facingDirection = this.facingDirection;
copy.lastFlagVisited = this.lastFlagVisited;
copy.amountOfLives = this.amountOfLives;
copy.backupPosition = this.backupPosition;
copy.inPowerDown = this.inPowerDown;
copy.robotDamageTaken = this.robotDamageTaken;
return copy;
}
} }