From fd4513e0d0d11850b44ff5e7ac8ce7350c138cd9 Mon Sep 17 00:00:00 2001 From: romanschenk37 <84532681+romanschenk37@users.noreply.github.com> Date: Fri, 18 Mar 2022 15:37:17 +0100 Subject: [PATCH 1/4] fixes in Game.java --- src/main/java/ch/zhaw/pm2/racetrack/Game.java | 148 +++++++++--------- 1 file changed, 78 insertions(+), 70 deletions(-) diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Game.java b/src/main/java/ch/zhaw/pm2/racetrack/Game.java index e59c530..1dbd540 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Game.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Game.java @@ -33,9 +33,9 @@ public class Game implements GameSpecification { public boolean initPhase() throws InvalidTrackFormatException, FileNotFoundException { File folder = new File("tracks"); File[] listOfFiles = folder.listFiles(); - if(listOfFiles.length > 0) { + if (listOfFiles.length > 0) { List tracks = new ArrayList<>(); - for(File file : listOfFiles){ + for (File file : listOfFiles) { tracks.add(file.getName()); } File selectedTrack = listOfFiles[userInterface.selectOption("Select Track file", tracks)]; @@ -49,15 +49,15 @@ public class Game implements GameSpecification { moveStrategies.add("User Move Strategy"); moveStrategies.add("Move List Strategy"); moveStrategies.add("Path Follow Move Strategy"); - for(int i = 0; i < track.getCarCount() ; i++ ) { + for (int i = 0; i < track.getCarCount(); i++) { int moveStrategie = userInterface.selectOption( "Select Strategy for Car " + i + " (" + track.getCarId(i) + ")", moveStrategies); - switch (moveStrategie + 1) { //TODO: set Movestrategy with method in Track + switch (moveStrategie + 1) { case 1: - track.getCar(i).setMoveStrategy(new DoNotMoveStrategy()); //TODO: add Arguments + track.getCar(i).setMoveStrategy(new DoNotMoveStrategy()); break; case 2: - track.getCar(i).setMoveStrategy(new UserMoveStrategy(userInterface, i, track.getCarId(i))); //TODO: add Arguments + track.getCar(i).setMoveStrategy(new UserMoveStrategy(userInterface, i, track.getCarId(i))); break; case 3: track.getCar(i).setMoveStrategy(new MoveListStrategy()); //TODO: add Arguments @@ -68,8 +68,7 @@ public class Game implements GameSpecification { } } return true; - } - else{ + } else { userInterface.printInformation("No Trackfile found!"); return false; } @@ -78,6 +77,7 @@ public class Game implements GameSpecification { /** * Return the index of the current active car. * Car indexes are zero-based, so the first car is 0, and the last car is getCarCount() - 1. + * * @return The zero-based number of the current car */ @Override @@ -87,6 +87,7 @@ public class Game implements GameSpecification { /** * Get the id of the specified car. + * * @param carIndex The zero-based carIndex number * @return A char containing the id of the car */ @@ -97,6 +98,7 @@ public class Game implements GameSpecification { /** * Get the position of the specified car. + * * @param carIndex The zero-based carIndex number * @return A PositionVector containing the car's current position */ @@ -107,6 +109,7 @@ public class Game implements GameSpecification { /** * Get the velocity of the specified car. + * * @param carIndex The zero-based carIndex number * @return A PositionVector containing the car's current velocity */ @@ -117,14 +120,15 @@ public class Game implements GameSpecification { /** * Return the winner of the game. If the game is still in progress, returns NO_WINNER. + * * @return The winning car's index (zero-based, see getCurrentCar()), or NO_WINNER if the game is still in progress */ @Override public int getWinner() { List cars = track.getCars(); - for (Car car: cars) { - if(car.getWinPoints() == 1){ - return car.getID(); + for (Car car : cars) { + if (car.getWinPoints() == 1) { + return car.getID(); // TODO: Index not ID } } return NO_WINNER; @@ -158,23 +162,24 @@ public class Game implements GameSpecification { */ @Override public void doCarTurn(Direction acceleration) throws PositionVectorNotValid { - // TODO: implementation track.getCar(currentCarIndex).accelerate(acceleration); PositionVector crashPosition = null; - List positionList = calculatePath(track.getCarPos(currentCarIndex),track.getCar(currentCarIndex).nextPosition()); - //TODO: check if Method calculatePath contains endposition - for(PositionVector location : positionList) { //todo: check if order must be reversed - if(willCarCrash(currentCarIndex, location)) { - crashPosition = location; + List positionList = calculatePath(track.getCarPos(currentCarIndex), track.getCar(currentCarIndex).nextPosition()); + for (int i = 0; i < positionList.size(); i++) { + if (willCarCrash(currentCarIndex, positionList.get(i))) { + if (i == 0) { + crashPosition = track.getCarPos(currentCarIndex); + } else { + crashPosition = positionList.get(i - 1); + } } } - if(crashPosition != null) { + if (crashPosition != null) { track.carDoesCrash(currentCarIndex, crashPosition); - } - else { - track.moveCar(currentCarIndex); + } else { calculateWinner(track.getCarPos(currentCarIndex), track.getCar(currentCarIndex).nextPosition(), currentCarIndex); + track.moveCar(currentCarIndex); } } @@ -183,7 +188,7 @@ public class Game implements GameSpecification { userInterface.printTrack(track); Direction direction = track.getCar(currentCarIndex).getMoveStrategy().nextMove(); doCarTurn(direction); - if(allCarsCrashed()) { + if (allCarsCrashed()) { return NO_WINNER; } switchToNextActiveCar(); @@ -199,9 +204,8 @@ public class Game implements GameSpecification { do { if ((currentCarIndex + 1) == track.getCarCount()) { currentCarIndex = 0; - } - else { - currentCarIndex ++; + } else { + currentCarIndex++; } } while (track.getCar(currentCarIndex).isCrashed()); // TODO: evtl andere Kapselung @@ -215,8 +219,9 @@ public class Game implements GameSpecification { * - Detect which axis of the distance vector is longer (faster movement) * - for each pixel on the 'faster' axis calculate the position on the 'slower' axis. * Direction of the movement has to correctly considered + * * @param startPosition Starting position as a PositionVector - * @param endPosition Ending position as a PositionVector + * @param endPosition Ending position as a PositionVector * @return Intervening grid positions as a List of PositionVector's, including the starting and ending positions. */ @Override @@ -244,20 +249,24 @@ public class Game implements GameSpecification { int distanceSlowAxis, distanceFastAxis; if (distX > distY) { // x axis is the 'fast' direction - parallelStepX = dirX; parallelStepY = 0; // parallel step only moves in x direction - diagonalStepX = dirX; diagonalStepY = dirY; // diagonal step moves in both directions + parallelStepX = dirX; + parallelStepY = 0; // parallel step only moves in x direction + diagonalStepX = dirX; + diagonalStepY = dirY; // diagonal step moves in both directions distanceSlowAxis = distY; distanceFastAxis = distX; } else { // y axis is the 'fast' direction - parallelStepX = 0; parallelStepY = dirY; // parallel step only moves in y direction - diagonalStepX = dirX; diagonalStepY = dirY; // diagonal step moves in both directions + parallelStepX = 0; + parallelStepY = dirY; // parallel step only moves in y direction + diagonalStepX = dirX; + diagonalStepY = dirY; // diagonal step moves in both directions distanceSlowAxis = distX; distanceFastAxis = distY; } - int error = distanceFastAxis/2; - for(int step = 0; step < distanceFastAxis; step ++) { + int error = distanceFastAxis / 2; + for (int step = 0; step < distanceFastAxis; step++) { error -= distanceSlowAxis; if (error < 0) { error += distanceFastAxis; // correct error value to be positive again @@ -270,50 +279,49 @@ public class Game implements GameSpecification { y += parallelStepY; } - pathList.add(new PositionVector(x,y)); + pathList.add(new PositionVector(x, y)); } return pathList; } - private void calculateWinner(PositionVector start, PositionVector finish, int carIndex ) { + private void calculateWinner(PositionVector start, PositionVector finish, int carIndex) { List path = calculatePath(start, finish); - for (PositionVector point : path){ - switch (track.getSpaceType(point)) { - case FINISH_UP: - if(start.getY() < finish.getY()) { - track.getCar(carIndex).increaseWinPoints(); - } - else if(start.getY() > finish.getY()) { - track.getCar(carIndex).deductWinPoints(); - } - break; - case FINISH_DOWN: - if(start.getY() > finish.getY()){ - track.getCar(carIndex).increaseWinPoints(); - } - else if (start.getY() < finish.getY()){ - track.getCar(carIndex).deductWinPoints(); - } - break; - case FINISH_RIGHT: - if(start.getX() < finish.getX()){ - track.getCar(carIndex).increaseWinPoints(); - } - else if (start.getX() > finish.getX()){ - track.getCar(carIndex).deductWinPoints(); - } - break; - case FINISH_LEFT: - if(start.getX() > finish.getX()){ - track.getCar(carIndex).increaseWinPoints(); - } - else if (start.getX() < finish.getX()){ - track.getCar(carIndex).increaseWinPoints(); - } - break; + for (PositionVector point : path) { + if (track.getSpaceType(point) != null) + { + switch (track.getSpaceType(point)) { //TODO: Case null + case FINISH_UP: + if (start.getY() < finish.getY()) { + track.getCar(carIndex).increaseWinPoints(); + } else if (start.getY() > finish.getY()) { + track.getCar(carIndex).deductWinPoints(); + } + break; + case FINISH_DOWN: + if (start.getY() > finish.getY()) { + track.getCar(carIndex).increaseWinPoints(); + } else if (start.getY() < finish.getY()) { + track.getCar(carIndex).deductWinPoints(); + } + break; + case FINISH_RIGHT: + if (start.getX() < finish.getX()) { + track.getCar(carIndex).increaseWinPoints(); + } else if (start.getX() > finish.getX()) { + track.getCar(carIndex).deductWinPoints(); + } + break; + case FINISH_LEFT: + if (start.getX() > finish.getX()) { + track.getCar(carIndex).increaseWinPoints(); + } else if (start.getX() < finish.getX()) { + track.getCar(carIndex).increaseWinPoints(); + } + break; } - } + } + } } @@ -328,7 +336,7 @@ public class Game implements GameSpecification { return track.willCrashAtPosition(carIndex, position); } - public boolean allCarsCrashed() { + public boolean allCarsCrashed() { //TODO: Finish game when only one car left? or if all cars crashed? for(int carIndex = 0; carIndex < track.getCarCount(); carIndex ++) { if(! track.getCar(carIndex).isCrashed()) { return false; 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 2/4] 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); } /** From 1c618ad09aaafa036eb8ccbf35a9e7b5b0f4fb84 Mon Sep 17 00:00:00 2001 From: romanschenk37 <84532681+romanschenk37@users.noreply.github.com> Date: Fri, 18 Mar 2022 16:45:16 +0100 Subject: [PATCH 3/4] change in gamephase in Game.java --- src/main/java/ch/zhaw/pm2/racetrack/Game.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Game.java b/src/main/java/ch/zhaw/pm2/racetrack/Game.java index 508244d..cdac7ab 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Game.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Game.java @@ -189,7 +189,12 @@ public class Game implements GameSpecification { public int gamePhase() throws PositionVectorNotValid { while (CarsMoving() && getWinner() == NO_WINNER) { userInterface.printTrack(track); - Direction direction = track.getCar(currentCarIndex).getMoveStrategy().nextMove(); + Direction direction = null; + direction= track.getCar(currentCarIndex).getMoveStrategy().nextMove(); + if(direction == null) { + track.getCar(currentCarIndex).setMoveStrategy(new DoNotMoveStrategy()); + direction= track.getCar(currentCarIndex).getMoveStrategy().nextMove(); + } doCarTurn(direction); switchToNextActiveCar(); } From 9869e8e74d1241fb263346244a62c812c311c80e Mon Sep 17 00:00:00 2001 From: romanschenk37 <84532681+romanschenk37@users.noreply.github.com> Date: Fri, 18 Mar 2022 16:48:37 +0100 Subject: [PATCH 4/4] fix in getWinner in Game.java --- src/main/java/ch/zhaw/pm2/racetrack/Game.java | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Game.java b/src/main/java/ch/zhaw/pm2/racetrack/Game.java index cdac7ab..94af85b 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Game.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Game.java @@ -128,10 +128,9 @@ public class Game implements GameSpecification { if (onlyOneCarLeft()) { return currentCarIndex; } - List cars = track.getCars(); - for (Car car : cars) { - if (car.getWinPoints() == 1) { - return car.getID(); // TODO: Index not ID + for (int i = 0; i < track.getCarCount(); i++) { + if (track.getCar(i).getWinPoints() == 1) { + return i; } } return NO_WINNER; @@ -294,8 +293,8 @@ public class Game implements GameSpecification { List path = calculatePath(start, finish); for (PositionVector point : path) { if (track.getSpaceType(point) != null) - { - switch (track.getSpaceType(point)) { //TODO: Case null + { + switch (track.getSpaceType(point)) { case FINISH_UP: if (start.getY() < finish.getY()) { track.getCar(carIndex).increaseWinPoints(); @@ -324,9 +323,9 @@ public class Game implements GameSpecification { track.getCar(carIndex).increaseWinPoints(); } break; - } + } - } + } } }