From 3684b5589e91411424ea344335180415cb215aa0 Mon Sep 17 00:00:00 2001 From: romanschenk37 <84532681+romanschenk37@users.noreply.github.com> Date: Fri, 18 Mar 2022 16:13:37 +0100 Subject: [PATCH] fixes in Game.java and Track.java --- src/main/java/ch/zhaw/pm2/racetrack/Game.java | 25 +++++++++++++------ src/main/java/ch/zhaw/pm2/racetrack/Main.java | 3 ++- .../java/ch/zhaw/pm2/racetrack/Track.java | 6 ++++- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Game.java b/src/main/java/ch/zhaw/pm2/racetrack/Game.java index 1dbd540..508244d 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Game.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Game.java @@ -125,6 +125,9 @@ public class Game implements GameSpecification { */ @Override public int getWinner() { + if (onlyOneCarLeft()) { + return currentCarIndex; + } List cars = track.getCars(); for (Car car : cars) { if (car.getWinPoints() == 1) { @@ -184,15 +187,13 @@ public class Game implements GameSpecification { } public int gamePhase() throws PositionVectorNotValid { - while (getWinner() == NO_WINNER) { + while (CarsMoving() && getWinner() == NO_WINNER) { userInterface.printTrack(track); Direction direction = track.getCar(currentCarIndex).getMoveStrategy().nextMove(); doCarTurn(direction); - if (allCarsCrashed()) { - return NO_WINNER; - } switchToNextActiveCar(); } + userInterface.printTrack(track); return getWinner(); } @@ -336,12 +337,22 @@ public class Game implements GameSpecification { return track.willCrashAtPosition(carIndex, position); } - public boolean allCarsCrashed() { //TODO: Finish game when only one car left? or if all cars crashed? + public boolean onlyOneCarLeft() { + int carsLeft = 0; for(int carIndex = 0; carIndex < track.getCarCount(); carIndex ++) { if(! track.getCar(carIndex).isCrashed()) { - return false; + carsLeft++; } } - return true; + return !(carsLeft > 1); + } + + public boolean CarsMoving() { + for(int carIndex = 0; carIndex < track.getCarCount(); carIndex ++) { + if(! (track.getCar(carIndex).isCrashed() || track.getCar(carIndex).getMoveStrategy().getClass() == DoNotMoveStrategy.class)) { + return true; + } + } + return false; } } diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Main.java b/src/main/java/ch/zhaw/pm2/racetrack/Main.java index bac1de9..1a7c197 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Main.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Main.java @@ -8,8 +8,8 @@ public class Main { public static void main(String[] args) throws InvalidTrackFormatException, FileNotFoundException, PositionVectorNotValid { boolean exit = false; + UserInterface userInterface = new UserInterface("Hello and Welcome"); while (!exit) { - UserInterface userInterface = new UserInterface("Hello and Welcome"); Game game = new Game(userInterface); int winner = 0; if (game.initPhase()) { @@ -23,6 +23,7 @@ public class Main { exit = true; } } + userInterface.printInformation("Thank you and goodbye"); } } diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Track.java b/src/main/java/ch/zhaw/pm2/racetrack/Track.java index 640fe3e..0593222 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Track.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Track.java @@ -225,7 +225,11 @@ public class Track implements TrackSpecification { isPositionVectorOnTrack(positionVector); char charAtPosition = track.get(positionVector.getY()).charAt(positionVector.getX()); if (getCarId(carIndex) == charAtPosition) return false; - return (charAtPosition == ConfigSpecification.SpaceType.WALL.value); + return !(charAtPosition == ConfigSpecification.SpaceType.TRACK.value || + charAtPosition == ConfigSpecification.SpaceType.FINISH_RIGHT.value || + charAtPosition == ConfigSpecification.SpaceType.FINISH_LEFT.value || + charAtPosition == ConfigSpecification.SpaceType.FINISH_UP.value || + charAtPosition == ConfigSpecification.SpaceType.FINISH_DOWN.value); } /**