Fikser en potensiell evig løkke når transportbånd blir sjekket

Stopper rekursive kall om dybden er større enn åtte, siden dette bare skjer dersom roboter går i ring
This commit is contained in:
Kristian Knarvik 2020-03-28 15:34:47 +01:00
parent 256edc871f
commit 417fa6865c
2 changed files with 8 additions and 3 deletions

View File

@ -212,12 +212,17 @@ public class Board {
/** /**
* Checks whether a given conveyor belt is able to move in its direction * Checks whether a given conveyor belt is able to move in its direction
* @param conveyorBelt The conveyor belt to move * @param conveyorBelt The conveyor belt to move
* @param iterations The number of recursive calls already executed
* @return True if nothing is blocking its movement * @return True if nothing is blocking its movement
*/ */
public boolean conveyorBeltCanMove(BoardElementContainer<Tile> conveyorBelt) { public boolean conveyorBeltCanMove(BoardElementContainer<Tile> conveyorBelt, int iterations) {
if (!isConveyorBelt(conveyorBelt.getElement())) { if (!isConveyorBelt(conveyorBelt.getElement())) {
throw new IllegalArgumentException("Input to function is of invalid tile type."); throw new IllegalArgumentException("Input to function is of invalid tile type.");
} }
//Prevents an infinite loop if robots are in a small conveyor belt loop
if (iterations >= 8) {
return true;
}
Position conveyorBeltPosition = conveyorBelt.getPosition(); Position conveyorBeltPosition = conveyorBelt.getPosition();
Direction conveyorBeltDirection = conveyorBelt.getElement().getDirection(); Direction conveyorBeltDirection = conveyorBelt.getElement().getDirection();
//Ignore conveyor belts without a robot //Ignore conveyor belts without a robot
@ -243,7 +248,7 @@ public class Board {
if (!hasRobotOnPosition(positionInFront)) { if (!hasRobotOnPosition(positionInFront)) {
return true; return true;
} }
return conveyorBeltCanMove(new BoardElementContainer<>(tileInFront, positionInFront)); return conveyorBeltCanMove(new BoardElementContainer<>(tileInFront, positionInFront), iterations + 1);
} }
/** /**

View File

@ -309,7 +309,7 @@ public class RoboRallyGame implements IDrawableGame {
for (BoardElementContainer<Tile> conveyorBelt : conveyorBelts) { for (BoardElementContainer<Tile> conveyorBelt : conveyorBelts) {
Position conveyorBeltPosition = conveyorBelt.getPosition(); Position conveyorBeltPosition = conveyorBelt.getPosition();
Direction conveyorBeltDirection = conveyorBelt.getElement().getDirection(); Direction conveyorBeltDirection = conveyorBelt.getElement().getDirection();
if (gameBoard.conveyorBeltCanMove(conveyorBelt) && if (gameBoard.conveyorBeltCanMove(conveyorBelt, 0) &&
gameBoard.hasRobotOnPosition(conveyorBeltPosition)) { gameBoard.hasRobotOnPosition(conveyorBeltPosition)) {
updateConveyorBeltMaps(conveyorBeltPosition, conveyorBeltDirection, newPositions, moveNormally); updateConveyorBeltMaps(conveyorBeltPosition, conveyorBeltDirection, newPositions, moveNormally);
} }