From e84bce43afe8614f3b38624f6daa8d08fae713c0 Mon Sep 17 00:00:00 2001 From: romanschenk37 <84532681+romanschenk37@users.noreply.github.com> Date: Thu, 24 Mar 2022 19:04:43 +0100 Subject: [PATCH] fix getWinner in Game.java working on PathFinderMoveStrategy --- src/main/java/ch/zhaw/pm2/racetrack/Game.java | 18 +++++++-------- .../java/ch/zhaw/pm2/racetrack/Track.java | 2 +- .../strategy/PathFinderMoveStrategy.java | 22 +++++++++---------- tracks/quarter-mile.txt | 2 +- 4 files changed, 21 insertions(+), 23 deletions(-) diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Game.java b/src/main/java/ch/zhaw/pm2/racetrack/Game.java index 27b7152..c644e6e 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Game.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Game.java @@ -169,14 +169,14 @@ public class Game implements GameSpecification { */ @Override public int getWinner() { - if (onlyOneCarLeft()) { - return currentCarIndex; - } for (int i = 0; i < track.getCarCount(); i++) { if (track.getCar(i).getWinPoints() == 1) { return i; } } + if (onlyOneCarLeft()) { + return currentCarIndex; + } return NO_WINNER; } @@ -221,15 +221,15 @@ public class Game implements GameSpecification { break; } } + int newWinPoints = track.calculateNewWinPoints(track.getCarPos(currentCarIndex), track.getCar(currentCarIndex).nextPosition()); + if(newWinPoints == 1){ + track.getCar(currentCarIndex).increaseWinPoints(); + }else if(newWinPoints == -1){ + track.getCar(currentCarIndex).deductWinPoints(); + } if (crashPosition != null) { track.carDoesCrash(currentCarIndex, crashPosition); } else { - int newWinPoints = track.calculateNewWinPoints(track.getCarPos(currentCarIndex), track.getCar(currentCarIndex).nextPosition()); - if(newWinPoints == 1){ - track.getCar(currentCarIndex).increaseWinPoints(); - }else if(newWinPoints == 1){ - track.getCar(currentCarIndex).deductWinPoints(); - } track.moveCar(currentCarIndex); } } diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Track.java b/src/main/java/ch/zhaw/pm2/racetrack/Track.java index 0c5fe2f..af8773b 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Track.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Track.java @@ -284,7 +284,7 @@ public class Track implements TrackSpecification { } } - return null; + return ConfigSpecification.SpaceType.WALL; } /** diff --git a/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderMoveStrategy.java b/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderMoveStrategy.java index bb020ae..b9339a5 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderMoveStrategy.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderMoveStrategy.java @@ -33,13 +33,17 @@ public class PathFinderMoveStrategy implements MoveStrategy{ for(PossibleMove previousMove : possibleMoves){ for(PositionVector.Direction direction : allDirections){ PossibleMove newMove = new PossibleMove(previousMove, direction); - if(! (newMove.crashed() || newMove.drivingLoop())){ + if(! (newMove.crashed() || newMove.drivingLoop() || finishedMove != null)){ + if(newMove.finished()){ + finishedMove = newMove; + break; + } newMoves.add(newMove); } } } possibleMoves = newMoves; - finishedMove = findFinishedMove(possibleMoves); + } moveList = finishedMove.directions; @@ -47,15 +51,6 @@ public class PathFinderMoveStrategy implements MoveStrategy{ } - private PossibleMove findFinishedMove(List moveList){ - for(PossibleMove move : moveList){ - if(move.finished()){ - return move; - } - } - return null; - } - public class PossibleMove { List directions; @@ -91,10 +86,13 @@ public class PathFinderMoveStrategy implements MoveStrategy{ public boolean crashed() { for(PositionVector point : track.calculatePointsOnPath(positions.get(positions.size()-2), positions.get(positions.size()-1))) { - if (track.willCrashAtPosition(carIndex, point)) { + if (track.willCrashAtPosition(carIndex, point)){ return true; } } + if(track.calculateNewWinPoints(positions.get(positions.size()-2), positions.get(positions.size()-1)) == -1){ + return true; + } return false; } diff --git a/tracks/quarter-mile.txt b/tracks/quarter-mile.txt index 5a55e57..97d816b 100644 --- a/tracks/quarter-mile.txt +++ b/tracks/quarter-mile.txt @@ -1,7 +1,7 @@ ############################################################ ############################################################ ####### < ## -####### < § ## +####### < § ## ####### < ## ####### < @ ## ####### < ##