diff --git a/src/main/java/inf112/fiasko/roborally/objects/Board.java b/src/main/java/inf112/fiasko/roborally/objects/Board.java index 77b55f7..fa2034e 100644 --- a/src/main/java/inf112/fiasko/roborally/objects/Board.java +++ b/src/main/java/inf112/fiasko/roborally/objects/Board.java @@ -212,12 +212,17 @@ public class Board { /** * Checks whether a given conveyor belt is able to move in its direction * @param conveyorBelt The conveyor belt to move + * @param iterations The number of recursive calls already executed * @return True if nothing is blocking its movement */ - public boolean conveyorBeltCanMove(BoardElementContainer conveyorBelt) { + public boolean conveyorBeltCanMove(BoardElementContainer conveyorBelt, int iterations) { if (!isConveyorBelt(conveyorBelt.getElement())) { 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(); Direction conveyorBeltDirection = conveyorBelt.getElement().getDirection(); //Ignore conveyor belts without a robot @@ -243,7 +248,7 @@ public class Board { if (!hasRobotOnPosition(positionInFront)) { return true; } - return conveyorBeltCanMove(new BoardElementContainer<>(tileInFront, positionInFront)); + return conveyorBeltCanMove(new BoardElementContainer<>(tileInFront, positionInFront), iterations + 1); } /** diff --git a/src/main/java/inf112/fiasko/roborally/objects/RoboRallyGame.java b/src/main/java/inf112/fiasko/roborally/objects/RoboRallyGame.java index e26a7ac..928c4be 100644 --- a/src/main/java/inf112/fiasko/roborally/objects/RoboRallyGame.java +++ b/src/main/java/inf112/fiasko/roborally/objects/RoboRallyGame.java @@ -309,7 +309,7 @@ public class RoboRallyGame implements IDrawableGame { for (BoardElementContainer conveyorBelt : conveyorBelts) { Position conveyorBeltPosition = conveyorBelt.getPosition(); Direction conveyorBeltDirection = conveyorBelt.getElement().getDirection(); - if (gameBoard.conveyorBeltCanMove(conveyorBelt) && + if (gameBoard.conveyorBeltCanMove(conveyorBelt, 0) && gameBoard.hasRobotOnPosition(conveyorBeltPosition)) { updateConveyorBeltMaps(conveyorBeltPosition, conveyorBeltDirection, newPositions, moveNormally); }