From 417fa6865c2c034ba8e71b3bdbf9176af4395a27 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Sat, 28 Mar 2020 15:34:47 +0100 Subject: [PATCH] =?UTF-8?q?Fikser=20en=20potensiell=20evig=20l=C3=B8kke=20?= =?UTF-8?q?n=C3=A5r=20transportb=C3=A5nd=20blir=20sjekket?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Stopper rekursive kall om dybden er større enn åtte, siden dette bare skjer dersom roboter går i ring --- src/main/java/inf112/fiasko/roborally/objects/Board.java | 9 +++++++-- .../inf112/fiasko/roborally/objects/RoboRallyGame.java | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) 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); }