From d777b875495ec5647bdb6dcec2e04bfa5012becd Mon Sep 17 00:00:00 2001 From: romanschenk37 <84532681+romanschenk37@users.noreply.github.com> Date: Thu, 24 Mar 2022 18:18:07 +0100 Subject: [PATCH] fix in Method calculateNewWinPoints --- src/main/java/ch/zhaw/pm2/racetrack/Game.java | 53 +------------- .../java/ch/zhaw/pm2/racetrack/Track.java | 73 ++++++++++--------- .../strategy/PathFinderMoveStrategy.java | 11 +-- 3 files changed, 45 insertions(+), 92 deletions(-) diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Game.java b/src/main/java/ch/zhaw/pm2/racetrack/Game.java index 4e8f8a8..27b7152 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Game.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Game.java @@ -224,10 +224,10 @@ public class Game implements GameSpecification { if (crashPosition != null) { track.carDoesCrash(currentCarIndex, crashPosition); } else { - int newWinPoints = calculateNewWinPoints(track.getCarPos(currentCarIndex), track.getCar(currentCarIndex).nextPosition()); + int newWinPoints = track.calculateNewWinPoints(track.getCarPos(currentCarIndex), track.getCar(currentCarIndex).nextPosition()); if(newWinPoints == 1){ track.getCar(currentCarIndex).increaseWinPoints(); - }else{ + }else if(newWinPoints == 1){ track.getCar(currentCarIndex).deductWinPoints(); } track.moveCar(currentCarIndex); @@ -293,55 +293,6 @@ public class Game implements GameSpecification { } - /** - * This method will check if a car is passing the finishline. - * If the car is passing the finishline in the wrong direction, the car will lose a winpoint. - * If the car is passing the finishline in the correct direction, the car will gain a winpoint. - * @param start the startposition of the car - * @param finish the expected finishpositon of the car after the move - * @param carIndex of the current player. - */ - private int calculateNewWinPoints(PositionVector start, PositionVector finish) { - List path = calculatePath(start, finish); - for (PositionVector point : path) { - if (track.getSpaceType(point) != null) { - switch (track.getSpaceType(point)) { - case FINISH_UP: - if (start.getY() < finish.getY()) { - return 1; - } else if (start.getY() > finish.getY()) { - return -1; - } - break; - case FINISH_DOWN: - if (start.getY() > finish.getY()) { - return 1; - } else if (start.getY() < finish.getY()) { - return -1; - } - break; - case FINISH_RIGHT: - if (start.getX() < finish.getX()) { - return 1; - } else if (start.getX() > finish.getX()) { - return -1; - } - break; - case FINISH_LEFT: - if (start.getX() > finish.getX()) { - return 1; - } else if (start.getX() < finish.getX()) { - return -1; - } - break; - default: - return 0; - } - - } - } - return 0; - } /** diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Track.java b/src/main/java/ch/zhaw/pm2/racetrack/Track.java index 56cc5f4..0c5fe2f 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Track.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Track.java @@ -423,44 +423,49 @@ public class Track implements TrackSpecification { return pathList; } + /** + * This method will check if a car is passing the finishline. + * If the car is passing the finishline in the wrong direction, the car will lose a winpoint. + * If the car is passing the finishline in the correct direction, the car will gain a winpoint. + * @param start the startposition of the car + * @param finish the expected finishpositon of the car after the move + * @return Number of new Winpoints for the current player. + */ public int calculateNewWinPoints(PositionVector start, PositionVector finish) { List path = calculatePointsOnPath(start, finish); for (PositionVector point : path) { - if (getSpaceType(point) != null) { - switch (getSpaceType(point)) { - case FINISH_UP: - if (start.getY() < finish.getY()) { - return 1; - } else if (start.getY() > finish.getY()) { - return -1; - } - break; - case FINISH_DOWN: - if (start.getY() > finish.getY()) { - return 1; - } else if (start.getY() < finish.getY()) { - return -1; - } - break; - case FINISH_RIGHT: - if (start.getX() < finish.getX()) { - return 1; - } else if (start.getX() > finish.getX()) { - return -1; - } - break; - case FINISH_LEFT: - if (start.getX() > finish.getX()) { - return 1; - } else if (start.getX() < finish.getX()) { - return -1; - } - break; - default: - return 0; - } - + switch (getSpaceType(point)) { + case FINISH_UP: + if (start.getY() < finish.getY()) { + return 1; + } else if (start.getY() > finish.getY()) { + return -1; + } + break; + case FINISH_DOWN: + if (start.getY() > finish.getY()) { + return 1; + } else if (start.getY() < finish.getY()) { + return -1; + } + break; + case FINISH_RIGHT: + if (start.getX() < finish.getX()) { + return 1; + } else if (start.getX() > finish.getX()) { + return -1; + } + break; + case FINISH_LEFT: + if (start.getX() > finish.getX()) { + return 1; + } else if (start.getX() < finish.getX()) { + return -1; + } + break; } + + } return 0; } 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 51026d0..bb020ae 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderMoveStrategy.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFinderMoveStrategy.java @@ -18,6 +18,7 @@ public class PathFinderMoveStrategy implements MoveStrategy{ this.carIndex = carIndex; allDirections = Arrays.asList(PositionVector.Direction.values()); createMoveList(); + pointer = -1; } @@ -32,7 +33,6 @@ 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())){ newMoves.add(newMove); } @@ -44,8 +44,6 @@ public class PathFinderMoveStrategy implements MoveStrategy{ moveList = finishedMove.directions; - pointer = moveList.size(); - } @@ -92,7 +90,7 @@ public class PathFinderMoveStrategy implements MoveStrategy{ } public boolean crashed() { - for(PositionVector point : track.calculatePointsOnPath(positions.get(positions.size()-1), positions.get(positions.size()-2))) { + for(PositionVector point : track.calculatePointsOnPath(positions.get(positions.size()-2), positions.get(positions.size()-1))) { if (track.willCrashAtPosition(carIndex, point)) { return true; } @@ -115,9 +113,8 @@ public class PathFinderMoveStrategy implements MoveStrategy{ @Override public PositionVector.Direction nextMove() { - pointer -= 1; - //TODO: Check if crash. if yes --> createMoveList(); - if (pointer >= 0) { + pointer += 1; + if (pointer < moveList.size()) { return moveList.get(pointer); } return null;