From 52f9eb76295158209f2f67902a62d9350f558fa5 Mon Sep 17 00:00:00 2001 From: romanschenk37 <84532681+romanschenk37@users.noreply.github.com> Date: Fri, 25 Mar 2022 16:31:37 +0100 Subject: [PATCH] fixing function quit in usermovestrategy --- src/main/java/ch/zhaw/pm2/racetrack/Game.java | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Game.java b/src/main/java/ch/zhaw/pm2/racetrack/Game.java index 80f8eb7..d9ff44b 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Game.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Game.java @@ -2,6 +2,7 @@ package ch.zhaw.pm2.racetrack; import ch.zhaw.pm2.racetrack.given.GameSpecification; import ch.zhaw.pm2.racetrack.strategy.*; +import org.hamcrest.core.IsInstanceOf; import java.io.File; import java.io.FileNotFoundException; @@ -240,17 +241,25 @@ public class Game implements GameSpecification { * @return the ID of the winning car return null if there is no winner. */ public String gamePhase() { - while (carsMoving() && getWinner() == NO_WINNER) { + boolean quit = false; + while (carsMoving() && getWinner() == NO_WINNER && !quit) { + userInterface.printInformation("Player " + track.getCar(currentCarIndex).getID() + "'s turn!"); userInterface.printTrack(track); Direction direction; direction = track.getCar(currentCarIndex).getMoveStrategy().nextMove(); if (direction == null) { - track.getCar(currentCarIndex).setMoveStrategy(new DoNotMoveStrategy()); - direction = track.getCar(currentCarIndex).getMoveStrategy().nextMove(); + if(track.getCar(currentCarIndex).getMoveStrategy() instanceof UserMoveStrategy){ + quit = true; + direction = Direction.NONE; + } else { + track.getCar(currentCarIndex).setMoveStrategy(new DoNotMoveStrategy()); + direction = track.getCar(currentCarIndex).getMoveStrategy().nextMove(); + } } doCarTurn(direction); switchToNextActiveCar(); } + userInterface.printInformation("The game has finished!"); userInterface.printTrack(track); int indexWinner = getWinner(); if (indexWinner == NO_WINNER) {