From 4ebe8cb1edbf1f72c28261356a5f7eb300b78c39 Mon Sep 17 00:00:00 2001 From: romanschenk37 <84532681+romanschenk37@users.noreply.github.com> Date: Thu, 10 Mar 2022 15:18:08 +0100 Subject: [PATCH 01/36] created Method initphase in Game.java --- src/main/java/ch/zhaw/pm2/racetrack/Game.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Game.java b/src/main/java/ch/zhaw/pm2/racetrack/Game.java index 27609ca..151a4a6 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Game.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Game.java @@ -2,6 +2,8 @@ package ch.zhaw.pm2.racetrack; import ch.zhaw.pm2.racetrack.given.GameSpecification; +import java.io.File; +import java.util.ArrayList; import java.util.List; import static ch.zhaw.pm2.racetrack.PositionVector.Direction; @@ -14,6 +16,22 @@ import static ch.zhaw.pm2.racetrack.PositionVector.Direction; public class Game implements GameSpecification { public static final int NO_WINNER = -1; + UserInterface userInterface; + + public Game(String welcometext) { + userInterface = new UserInterface(welcometext); + } + + public void initphase() { + File folder = new File("your/path"); + File[] listOfFiles = folder.listFiles(); + List tracks = new ArrayList<>(); + for(File file : listOfFiles){ + tracks.add(file.getName()); + } + userInterface.selectOption("Select Track file", tracks); + } + /** * 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. From 57a5d199a603b799d5e524edec8bd9e207edeac9 Mon Sep 17 00:00:00 2001 From: romanschenk37 <84532681+romanschenk37@users.noreply.github.com> Date: Thu, 10 Mar 2022 15:29:04 +0100 Subject: [PATCH 02/36] changes in Method initphase --- src/main/java/ch/zhaw/pm2/racetrack/Game.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Game.java b/src/main/java/ch/zhaw/pm2/racetrack/Game.java index 151a4a6..86a67c4 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Game.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Game.java @@ -3,6 +3,7 @@ package ch.zhaw.pm2.racetrack; import ch.zhaw.pm2.racetrack.given.GameSpecification; import java.io.File; +import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.List; @@ -15,6 +16,7 @@ import static ch.zhaw.pm2.racetrack.PositionVector.Direction; */ public class Game implements GameSpecification { public static final int NO_WINNER = -1; + private Track track; UserInterface userInterface; @@ -22,14 +24,15 @@ public class Game implements GameSpecification { userInterface = new UserInterface(welcometext); } - public void initphase() { + public void initphase() throws InvalidTrackFormatException, FileNotFoundException { File folder = new File("your/path"); File[] listOfFiles = folder.listFiles(); List tracks = new ArrayList<>(); for(File file : listOfFiles){ tracks.add(file.getName()); } - userInterface.selectOption("Select Track file", tracks); + File selectedTrack = listOfFiles[userInterface.selectOption("Select Track file", tracks)]; + track = new Track(selectedTrack); } /** From ddbf666cc83bf615757e9026281c26b826808031 Mon Sep 17 00:00:00 2001 From: Leonardo Brandenberger Date: Thu, 10 Mar 2022 15:33:20 +0100 Subject: [PATCH 03/36] finished methods getCarPosition, getCarVelocity and get CarID --- src/main/java/ch/zhaw/pm2/racetrack/Game.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Game.java b/src/main/java/ch/zhaw/pm2/racetrack/Game.java index 86a67c4..3e15501 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Game.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Game.java @@ -53,8 +53,7 @@ public class Game implements GameSpecification { */ @Override public char getCarId(int carIndex) { - // TODO: implementation - throw new UnsupportedOperationException(); + return track.getCarId(carIndex); } /** @@ -64,8 +63,7 @@ public class Game implements GameSpecification { */ @Override public PositionVector getCarPosition(int carIndex) { - // TODO: implementation - throw new UnsupportedOperationException(); + return track.getCarPos(carIndex); } /** @@ -75,8 +73,7 @@ public class Game implements GameSpecification { */ @Override public PositionVector getCarVelocity(int carIndex) { - // TODO: implementation - throw new UnsupportedOperationException(); + return track.getCarVelocity(carIndex); } /** From 6285cfe2154fd6eb78d08bdf23281984692db9bd Mon Sep 17 00:00:00 2001 From: Leonardo Brandenberger Date: Thu, 10 Mar 2022 15:39:58 +0100 Subject: [PATCH 04/36] merged track into game --- src/main/java/ch/zhaw/pm2/racetrack/Track.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Track.java b/src/main/java/ch/zhaw/pm2/racetrack/Track.java index e89b960..e72fb2c 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Track.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Track.java @@ -73,6 +73,7 @@ public class Track implements TrackSpecification { * @throws InvalidTrackFormatException if the track file contains invalid data (no tracklines, ...) */ public Track(File trackFile) throws FileNotFoundException, InvalidTrackFormatException { + track = new ArrayList<>(); cars = new ArrayList<>(); finishLine = new ArrayList<>(); From 3fe1bb14266b535a099913284285aa1c812badec Mon Sep 17 00:00:00 2001 From: romanschenk37 <84532681+romanschenk37@users.noreply.github.com> Date: Thu, 10 Mar 2022 16:30:33 +0100 Subject: [PATCH 05/36] completed Method initphase --- src/main/java/ch/zhaw/pm2/racetrack/Game.java | 34 ++++++++++++++++++- tracks/.gitignore | 1 - 2 files changed, 33 insertions(+), 2 deletions(-) delete mode 100644 tracks/.gitignore diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Game.java b/src/main/java/ch/zhaw/pm2/racetrack/Game.java index c969298..200895e 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Game.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Game.java @@ -1,6 +1,10 @@ package ch.zhaw.pm2.racetrack; import ch.zhaw.pm2.racetrack.given.GameSpecification; +import ch.zhaw.pm2.racetrack.strategy.DoNotMoveStrategy; +import ch.zhaw.pm2.racetrack.strategy.MoveListStrategy; +import ch.zhaw.pm2.racetrack.strategy.PathFollowerMoveStrategy; +import ch.zhaw.pm2.racetrack.strategy.UserMoveStrategy; import java.io.File; import java.io.FileNotFoundException; @@ -17,6 +21,7 @@ import static ch.zhaw.pm2.racetrack.PositionVector.Direction; public class Game implements GameSpecification { public static final int NO_WINNER = -1; private Track track; + int currentCarIndex; UserInterface userInterface; @@ -25,7 +30,7 @@ public class Game implements GameSpecification { } public void initphase() throws InvalidTrackFormatException, FileNotFoundException { - File folder = new File("your/path"); + File folder = new File("tracks"); File[] listOfFiles = folder.listFiles(); List tracks = new ArrayList<>(); for(File file : listOfFiles){ @@ -33,6 +38,33 @@ public class Game implements GameSpecification { } File selectedTrack = listOfFiles[userInterface.selectOption("Select Track file", tracks)]; track = new Track(selectedTrack); + List moveStrategies = new ArrayList<>(); + moveStrategies.add("Do not move Strategy"); + moveStrategies.add("User Move Strategy"); + moveStrategies.add("Move List Strategy"); + moveStrategies.add("Path Follow Move Strategy"); + for(int i = 0; i < track.getCarCount() ; i++ ){ + int moveStrategie = userInterface.selectOption( + "Select Strategy for Car " + i + " (" + track.getCarId(i) + ")", moveStrategies); + switch(moveStrategie) { //TODO: set Movestrategy with method in Track + case 1: + track.getCar(i).setMoveStrategy(new DoNotMoveStrategy()); //TODO: add Arguments + break; + case 2: + track.getCar(i).setMoveStrategy(new UserMoveStrategy()); //TODO: add Arguments + break; + case 3: + track.getCar(i).setMoveStrategy(new MoveListStrategy()); //TODO: add Arguments + break; + case 4: + track.getCar(i).setMoveStrategy(new PathFollowerMoveStrategy()); //TODO: add Arguments + break; + } + } + + + + userInterface.printTrack(track); } /** diff --git a/tracks/.gitignore b/tracks/.gitignore deleted file mode 100644 index e4af982..0000000 --- a/tracks/.gitignore +++ /dev/null @@ -1 +0,0 @@ -challenge_points.txt From dd625907c9f7c05dc5e05ff0c89ba86b70f42e59 Mon Sep 17 00:00:00 2001 From: romanschenk37 <84532681+romanschenk37@users.noreply.github.com> Date: Thu, 10 Mar 2022 16:48:14 +0100 Subject: [PATCH 06/36] changes in Method initphase added Method printInformation in UserInterface.java --- src/main/java/ch/zhaw/pm2/racetrack/Game.java | 72 ++++++++++--------- .../ch/zhaw/pm2/racetrack/UserInterface.java | 10 ++- 2 files changed, 49 insertions(+), 33 deletions(-) diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Game.java b/src/main/java/ch/zhaw/pm2/racetrack/Game.java index 200895e..f8b653e 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Game.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Game.java @@ -29,42 +29,50 @@ public class Game implements GameSpecification { userInterface = new UserInterface(welcometext); } - public void initphase() throws InvalidTrackFormatException, FileNotFoundException { + + public boolean initphase() throws InvalidTrackFormatException, FileNotFoundException { File folder = new File("tracks"); File[] listOfFiles = folder.listFiles(); - List tracks = new ArrayList<>(); - for(File file : listOfFiles){ - tracks.add(file.getName()); - } - File selectedTrack = listOfFiles[userInterface.selectOption("Select Track file", tracks)]; - track = new Track(selectedTrack); - List moveStrategies = new ArrayList<>(); - moveStrategies.add("Do not move Strategy"); - moveStrategies.add("User Move Strategy"); - moveStrategies.add("Move List Strategy"); - moveStrategies.add("Path Follow Move Strategy"); - for(int i = 0; i < track.getCarCount() ; i++ ){ - int moveStrategie = userInterface.selectOption( - "Select Strategy for Car " + i + " (" + track.getCarId(i) + ")", moveStrategies); - switch(moveStrategie) { //TODO: set Movestrategy with method in Track - case 1: - track.getCar(i).setMoveStrategy(new DoNotMoveStrategy()); //TODO: add Arguments - break; - case 2: - track.getCar(i).setMoveStrategy(new UserMoveStrategy()); //TODO: add Arguments - break; - case 3: - track.getCar(i).setMoveStrategy(new MoveListStrategy()); //TODO: add Arguments - break; - case 4: - track.getCar(i).setMoveStrategy(new PathFollowerMoveStrategy()); //TODO: add Arguments - break; + if(listOfFiles.length > 0) { + List tracks = new ArrayList<>(); + for(File file : listOfFiles){ + tracks.add(file.getName()); } + File selectedTrack = listOfFiles[userInterface.selectOption("Select Track file", tracks)]; + try { + track = new Track(selectedTrack); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + List moveStrategies = new ArrayList<>(); + moveStrategies.add("Do not move Strategy"); + moveStrategies.add("User Move Strategy"); + moveStrategies.add("Move List Strategy"); + moveStrategies.add("Path Follow Move Strategy"); + for(int i = 0; i < track.getCarCount() ; i++ ) { + int moveStrategie = userInterface.selectOption( + "Select Strategy for Car " + i + " (" + track.getCarId(i) + ")", moveStrategies); + switch (moveStrategie) { //TODO: set Movestrategy with method in Track + case 1: + track.getCar(i).setMoveStrategy(new DoNotMoveStrategy()); //TODO: add Arguments + break; + case 2: + track.getCar(i).setMoveStrategy(new UserMoveStrategy()); //TODO: add Arguments + break; + case 3: + track.getCar(i).setMoveStrategy(new MoveListStrategy()); //TODO: add Arguments + break; + case 4: + track.getCar(i).setMoveStrategy(new PathFollowerMoveStrategy()); //TODO: add Arguments + break; + } + } + return true; + } + else{ + userInterface.printInformation("No Trackfile found!"); + return false; } - - - - userInterface.printTrack(track); } /** diff --git a/src/main/java/ch/zhaw/pm2/racetrack/UserInterface.java b/src/main/java/ch/zhaw/pm2/racetrack/UserInterface.java index d800a95..b67d6f4 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/UserInterface.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/UserInterface.java @@ -22,6 +22,14 @@ public class UserInterface { textTerminal.println(welcomeText + "\n"); } + /** + * Prints the given Text in textTerminal + * @param text The Text which should be printed. + */ + public void printInformation(String text) { + textTerminal.println(text); + } + /** * asks the user to choose one of the options given. * @param text Text which is printed befor the options are printed. Example: "Select Track file:" @@ -29,7 +37,7 @@ public class UserInterface { * @return the list index of the selected option */ public int selectOption(String text, List options) { - textTerminal.println(text + ":\n"); + textTerminal.println(text + ":"); for(int option = 0; option < options.size(); option ++) { textTerminal.println(" " + (option + 1) + ": " + options.get(option)); } From bf08749f28893647e51d02bfca4ab10b45d1b66b Mon Sep 17 00:00:00 2001 From: Leonardo Brandenberger Date: Thu, 10 Mar 2022 19:04:09 +0100 Subject: [PATCH 07/36] implemented calculatePath method. --- src/main/java/ch/zhaw/pm2/racetrack/Game.java | 54 ++++++++++++++++++- 1 file changed, 52 insertions(+), 2 deletions(-) diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Game.java b/src/main/java/ch/zhaw/pm2/racetrack/Game.java index c969298..25ceac2 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Game.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Game.java @@ -141,8 +141,58 @@ public class Game implements GameSpecification { */ @Override public List calculatePath(PositionVector startPosition, PositionVector endPosition) { - // TODO: implementation - throw new UnsupportedOperationException(); + ArrayList pathList = new ArrayList(); + // Use Bresenham's algorithm to determine positions. + int x = startPosition.getX(); + int y = startPosition.getY(); + + // Relative Distance (x & y axis) between end- and starting position + int diffX = endPosition.getX() - startPosition.getY(); + int diffY = endPosition.getY() - startPosition.getY(); + + // Absolute distance (x & y axis) between end- and starting position + int distX = Math.abs(diffX); + int distY = Math.abs(diffY); + + // Direction of vector on x & y axis (-1: to left/down, 0: none, +1 : to right/up) + int dirX = Integer.signum(diffX); + int dirY = Integer.signum(diffY); + + // Determine which axis is the fast direction and set parallel/diagonal step values + int parallelStepX, parallelStepY; + int diagonalStepX, diagonalStepY; + 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 + 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 + distanceSlowAxis = distX; + distanceFastAxis = distY; + } + + 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 + // step into slow direction; diagonal step + x += diagonalStepX; + y += diagonalStepY; + } else { + // step into fast direction; parallel step + x += parallelStepX; + y += parallelStepY; + } + + pathList.add(new PositionVector(x,y)); + } + return pathList; } From e15927408594ddc426740bb89337edb8a2f02896 Mon Sep 17 00:00:00 2001 From: Leonardo Brandenberger Date: Fri, 11 Mar 2022 10:37:54 +0100 Subject: [PATCH 08/36] finished method currentCarIndex, switchToNextActiveCar, willCarCrash --- src/main/java/ch/zhaw/pm2/racetrack/Game.java | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Game.java b/src/main/java/ch/zhaw/pm2/racetrack/Game.java index db3fe53..85363e8 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Game.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Game.java @@ -82,8 +82,7 @@ public class Game implements GameSpecification { */ @Override public int getCurrentCarIndex() { - // TODO: implementation - throw new UnsupportedOperationException(); + return currentCarIndex; } /** @@ -163,8 +162,12 @@ public class Game implements GameSpecification { */ @Override public void switchToNextActiveCar() { - // TODO: implementation - throw new UnsupportedOperationException(); + do { + if (currentCarIndex++ > track.getCarCount()) { + currentCarIndex = 0; + } + } while (track.getCar(currentCarIndex).isCrashed()); + // TODO: evtl andere Kapselung } /** @@ -181,7 +184,7 @@ public class Game implements GameSpecification { */ @Override public List calculatePath(PositionVector startPosition, PositionVector endPosition) { - ArrayList pathList = new ArrayList(); + ArrayList pathList = new ArrayList<>(); // Use Bresenham's algorithm to determine positions. int x = startPosition.getX(); int y = startPosition.getY(); @@ -246,7 +249,13 @@ public class Game implements GameSpecification { @Override public boolean willCarCrash(int carIndex, PositionVector position) { + List positionList = calculatePath(track.getCarPos(carIndex),position); + for(PositionVector location : positionList) { + if(track.willCrashAtPosition(location)) { + return true; + } + } + return false; - return true; } } From 7e5d349f3f71f76febabe946314db6217f330b04 Mon Sep 17 00:00:00 2001 From: romanschenk37 <84532681+romanschenk37@users.noreply.github.com> Date: Fri, 11 Mar 2022 12:01:55 +0100 Subject: [PATCH 09/36] changes in Methods in Game.java --- src/main/java/ch/zhaw/pm2/racetrack/Game.java | 49 ++++++++++++++----- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Game.java b/src/main/java/ch/zhaw/pm2/racetrack/Game.java index 85363e8..2136aed 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Game.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Game.java @@ -25,12 +25,12 @@ public class Game implements GameSpecification { UserInterface userInterface; - public Game(String welcometext) { - userInterface = new UserInterface(welcometext); + public Game(UserInterface userInterface) { + this.userInterface = userInterface; } - public boolean initphase() throws InvalidTrackFormatException, FileNotFoundException { + public boolean initPhase() throws InvalidTrackFormatException, FileNotFoundException { File folder = new File("tracks"); File[] listOfFiles = folder.listFiles(); if(listOfFiles.length > 0) { @@ -154,7 +154,33 @@ public class Game implements GameSpecification { @Override public void doCarTurn(Direction acceleration) { // TODO: implementation - throw new UnsupportedOperationException(); + 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; + } + } + if(crashPosition != null) { + track.carDoesCrash(currentCarIndex, crashPosition); + } + else { + track.getCar(currentCarIndex).move(); + } + } + + public void gamePhase() { + do{ + userInterface.printTrack(track); + doCarTurn(userInterface.selectDirection(currentCarIndex, track.getCarId(currentCarIndex))); + if(getWinner() != NO_WINNER) { + return; + } + } while (!allCarsCrashed()); + } /** @@ -190,7 +216,7 @@ public class Game implements GameSpecification { int y = startPosition.getY(); // Relative Distance (x & y axis) between end- and starting position - int diffX = endPosition.getX() - startPosition.getY(); + int diffX = endPosition.getX() - startPosition.getX(); int diffY = endPosition.getY() - startPosition.getY(); // Absolute distance (x & y axis) between end- and starting position @@ -248,14 +274,15 @@ public class Game implements GameSpecification { */ @Override public boolean willCarCrash(int carIndex, PositionVector position) { + return track.willCrashAtPosition(position); //TODO: add carIndex + } - List positionList = calculatePath(track.getCarPos(carIndex),position); - for(PositionVector location : positionList) { - if(track.willCrashAtPosition(location)) { - return true; + public boolean allCarsCrashed() { + for(int carIndex = 0; carIndex < track.getCarCount(); carIndex ++) { + if(! track.getCar(carIndex).isCrashed()) { + return false; } } - return false; - + return true; } } From af6914f7955aa983e93e8d6ab943a3aab48bbb8b Mon Sep 17 00:00:00 2001 From: romanschenk37 <84532681+romanschenk37@users.noreply.github.com> Date: Fri, 11 Mar 2022 12:08:17 +0100 Subject: [PATCH 10/36] changes in Methods in Game.java created MainClass.java --- src/main/java/ch/zhaw/pm2/racetrack/Game.java | 8 +++++--- src/main/java/ch/zhaw/pm2/racetrack/Main.java | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 src/main/java/ch/zhaw/pm2/racetrack/Main.java diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Game.java b/src/main/java/ch/zhaw/pm2/racetrack/Game.java index 2136aed..d3b2ee1 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Game.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Game.java @@ -122,7 +122,7 @@ public class Game implements GameSpecification { @Override public int getWinner() { // TODO: implementation - throw new UnsupportedOperationException(); + return NO_WINNER; } /** @@ -168,14 +168,16 @@ public class Game implements GameSpecification { track.carDoesCrash(currentCarIndex, crashPosition); } else { - track.getCar(currentCarIndex).move(); + track.moveCar(currentCarIndex); } } public void gamePhase() { do{ userInterface.printTrack(track); - doCarTurn(userInterface.selectDirection(currentCarIndex, track.getCarId(currentCarIndex))); + track.getCar(currentCarIndex).getMoveStrategy(); //TODO Movestrategy berücksichtigen ?? + Direction direction = userInterface.selectDirection(currentCarIndex, track.getCarId(currentCarIndex)); + doCarTurn(direction); if(getWinner() != NO_WINNER) { return; } diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Main.java b/src/main/java/ch/zhaw/pm2/racetrack/Main.java new file mode 100644 index 0000000..a646252 --- /dev/null +++ b/src/main/java/ch/zhaw/pm2/racetrack/Main.java @@ -0,0 +1,17 @@ +package ch.zhaw.pm2.racetrack; + +import java.io.FileNotFoundException; + +public class Main { + + public static void main(String[] args) throws InvalidTrackFormatException, FileNotFoundException { + UserInterface userInterface = new UserInterface("Hello and Welcome"); + Game game = new Game(userInterface); + + if(game.initPhase()){ + game.gamePhase(); + int winner = game.getWinner(); + } + } + +} From e94053fee89e09a3226afa1d0f821e59bc3838ec Mon Sep 17 00:00:00 2001 From: Leonardo Brandenberger Date: Mon, 14 Mar 2022 00:17:45 +0100 Subject: [PATCH 11/36] started method calculateWinner --- src/main/java/ch/zhaw/pm2/racetrack/Game.java | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Game.java b/src/main/java/ch/zhaw/pm2/racetrack/Game.java index d3b2ee1..858486f 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Game.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Game.java @@ -266,6 +266,46 @@ public class Game implements GameSpecification { return pathList; } + 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).addWinPoint; + //TODO: add point + } + else if( start.getY() < finish.getY()) { + //TODO: deduct point + } + break; + case FINISH_DOWN: + if(start.getY() > finish.getY()){ + //track.getCar(carIndex).addWinPoint; + } + else if (start.getY() < finish.getY()){ + + } + break; + case FINISH_RIGHT: + if(start.getX() < finish.getX()){ + //track.getCar(carIndex).addWinPoint; + } + else if (start.getX() < finish.getX()){ + + } + break; + case FINISH_LEFT: + if(start.getX() > finish.getX()){ + //track.getCar(carIndex).addWinPoint; + } + else if (start.getX() < finish.getX()){ + + } + } + } + + } /** From b606f20d9f4bb26fd7961909660017508acef379 Mon Sep 17 00:00:00 2001 From: Leonardo Brandenberger Date: Fri, 18 Mar 2022 09:51:11 +0100 Subject: [PATCH 12/36] implemented calculate winner and needed int in in car to hold winpoints. --- src/main/java/ch/zhaw/pm2/racetrack/Car.java | 16 +++++++++++++ src/main/java/ch/zhaw/pm2/racetrack/Game.java | 24 +++++++++---------- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Car.java b/src/main/java/ch/zhaw/pm2/racetrack/Car.java index c8739f1..8e9255e 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Car.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Car.java @@ -24,6 +24,10 @@ public class Car implements CarSpecification { * Current position of the car on the track grid using a {@link PositionVector} */ private PositionVector position; + /** + * Points that car is holding for determining winner. + */ + private int winPoints; /** * Current velocity of the car using a {@link PositionVector} @@ -59,6 +63,18 @@ public class Car implements CarSpecification { return id; } + public void increaseWinPoints() { + winPoints ++; + } + + public void deductWinPoints() { + winPoints --; + } + + public int getWinPoints() { + return winPoints; + } + /** * Returns the current velocity of the car as a PositionVector. * diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Game.java b/src/main/java/ch/zhaw/pm2/racetrack/Game.java index 858486f..44c5bde 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Game.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Game.java @@ -266,44 +266,44 @@ public class Game implements GameSpecification { 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).addWinPoint; - //TODO: add point + track.getCar(carIndex).increaseWinPoints(); } - else if( start.getY() < finish.getY()) { - //TODO: deduct point + else if(start.getY() < finish.getY()) { + track.getCar(carIndex).deductWinPoints(); } break; case FINISH_DOWN: if(start.getY() > finish.getY()){ - //track.getCar(carIndex).addWinPoint; + 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).addWinPoint; + 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).addWinPoint; + track.getCar(carIndex).increaseWinPoints(); } else if (start.getX() < finish.getX()){ - + track.getCar(carIndex).increaseWinPoints(); } + break; + } } - } } From 06ad28fbe8541a4b62b5b4d38222bdce42906e60 Mon Sep 17 00:00:00 2001 From: Leonardo Brandenberger Date: Fri, 18 Mar 2022 09:58:13 +0100 Subject: [PATCH 13/36] implemented getWinner method --- 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 44c5bde..ac0b81e 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Game.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Game.java @@ -121,7 +121,12 @@ public class Game implements GameSpecification { */ @Override public int getWinner() { - // TODO: implementation + List cars = track.getCars(); + for (Car car: cars) { + if(car.getWinPoints() == 1){ + return car.getID(); + } + } return NO_WINNER; } From 9b6908ef472335352dcf92295ef63c2ee5386234 Mon Sep 17 00:00:00 2001 From: romanschenk37 <84532681+romanschenk37@users.noreply.github.com> Date: Fri, 18 Mar 2022 10:00:28 +0100 Subject: [PATCH 14/36] implemented Movestrategies in Game.java --- src/main/java/ch/zhaw/pm2/racetrack/Game.java | 15 ++++++++------- src/main/java/ch/zhaw/pm2/racetrack/Main.java | 3 +-- .../pm2/racetrack/strategy/DoNotMoveStrategy.java | 5 +++-- .../pm2/racetrack/strategy/UserMoveStrategy.java | 13 +++++++++++-- 4 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Game.java b/src/main/java/ch/zhaw/pm2/racetrack/Game.java index ac0b81e..32f8787 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Game.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Game.java @@ -57,7 +57,7 @@ public class Game implements GameSpecification { track.getCar(i).setMoveStrategy(new DoNotMoveStrategy()); //TODO: add Arguments break; case 2: - track.getCar(i).setMoveStrategy(new UserMoveStrategy()); //TODO: add Arguments + track.getCar(i).setMoveStrategy(new UserMoveStrategy(userInterface, i, track.getCarId(i))); //TODO: add Arguments break; case 3: track.getCar(i).setMoveStrategy(new MoveListStrategy()); //TODO: add Arguments @@ -174,20 +174,21 @@ public class Game implements GameSpecification { } else { track.moveCar(currentCarIndex); + calculateWinner(track.getCarPos(currentCarIndex), track.getCar(currentCarIndex).nextPosition(), currentCarIndex); } } - public void gamePhase() { + public int gamePhase() { do{ userInterface.printTrack(track); - track.getCar(currentCarIndex).getMoveStrategy(); //TODO Movestrategy berücksichtigen ?? - Direction direction = userInterface.selectDirection(currentCarIndex, track.getCarId(currentCarIndex)); + Direction direction = track.getCar(currentCarIndex).getMoveStrategy().nextMove(); doCarTurn(direction); - if(getWinner() != NO_WINNER) { - return; + int winner = getWinner(); + if(winner != NO_WINNER) { + return winner; } } while (!allCarsCrashed()); - + return NO_WINNER; } /** diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Main.java b/src/main/java/ch/zhaw/pm2/racetrack/Main.java index a646252..73da6d8 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Main.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Main.java @@ -9,8 +9,7 @@ public class Main { Game game = new Game(userInterface); if(game.initPhase()){ - game.gamePhase(); - int winner = game.getWinner(); + int winner = game.gamePhase(); } } diff --git a/src/main/java/ch/zhaw/pm2/racetrack/strategy/DoNotMoveStrategy.java b/src/main/java/ch/zhaw/pm2/racetrack/strategy/DoNotMoveStrategy.java index 24b09e0..62e15e3 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/strategy/DoNotMoveStrategy.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/strategy/DoNotMoveStrategy.java @@ -1,5 +1,7 @@ package ch.zhaw.pm2.racetrack.strategy; +import ch.zhaw.pm2.racetrack.PositionVector; + import static ch.zhaw.pm2.racetrack.PositionVector.Direction; /** @@ -9,7 +11,6 @@ public class DoNotMoveStrategy implements MoveStrategy { @Override public Direction nextMove() { - // TODO: implementation - throw new UnsupportedOperationException(); + return PositionVector.Direction.NONE; } } diff --git a/src/main/java/ch/zhaw/pm2/racetrack/strategy/UserMoveStrategy.java b/src/main/java/ch/zhaw/pm2/racetrack/strategy/UserMoveStrategy.java index b7df019..06589ff 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/strategy/UserMoveStrategy.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/strategy/UserMoveStrategy.java @@ -1,15 +1,24 @@ package ch.zhaw.pm2.racetrack.strategy; import ch.zhaw.pm2.racetrack.PositionVector.Direction; +import ch.zhaw.pm2.racetrack.UserInterface; /** * Let the user decide the next move. */ public class UserMoveStrategy implements MoveStrategy { + private UserInterface userInterface; + private int carIndex; + private char carID; + + public UserMoveStrategy(UserInterface userInterface, int carIndex, char carID) { + this.userInterface = userInterface; + this.carIndex = carIndex; + this.carID = carID; + } @Override public Direction nextMove() { - // TODO: implementation - throw new UnsupportedOperationException(); + return userInterface.selectDirection(carIndex, carID); } } From a723ea16ec82cb1397a0445de45e5e07dab5935e Mon Sep 17 00:00:00 2001 From: romanschenk37 <84532681+romanschenk37@users.noreply.github.com> Date: Fri, 18 Mar 2022 10:19:23 +0100 Subject: [PATCH 15/36] merge track into game and fix in initphase --- src/main/java/ch/zhaw/pm2/racetrack/Game.java | 12 ++++++------ src/main/java/ch/zhaw/pm2/racetrack/Main.java | 2 +- src/main/java/ch/zhaw/pm2/racetrack/Track.java | 1 - .../zhaw/pm2/racetrack/given/GameSpecification.java | 5 +++-- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Game.java b/src/main/java/ch/zhaw/pm2/racetrack/Game.java index 32f8787..0b7595f 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Game.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Game.java @@ -41,7 +41,7 @@ public class Game implements GameSpecification { File selectedTrack = listOfFiles[userInterface.selectOption("Select Track file", tracks)]; try { track = new Track(selectedTrack); - } catch (FileNotFoundException e) { + } catch (FileNotFoundException | PositionVectorNotValid e) { e.printStackTrace(); } List moveStrategies = new ArrayList<>(); @@ -52,7 +52,7 @@ public class Game implements GameSpecification { for(int i = 0; i < track.getCarCount() ; i++ ) { int moveStrategie = userInterface.selectOption( "Select Strategy for Car " + i + " (" + track.getCarId(i) + ")", moveStrategies); - switch (moveStrategie) { //TODO: set Movestrategy with method in Track + switch (moveStrategie + 1) { //TODO: set Movestrategy with method in Track case 1: track.getCar(i).setMoveStrategy(new DoNotMoveStrategy()); //TODO: add Arguments break; @@ -157,7 +157,7 @@ public class Game implements GameSpecification { * for this turn */ @Override - public void doCarTurn(Direction acceleration) { + public void doCarTurn(Direction acceleration) throws PositionVectorNotValid { // TODO: implementation track.getCar(currentCarIndex).accelerate(acceleration); @@ -178,7 +178,7 @@ public class Game implements GameSpecification { } } - public int gamePhase() { + public int gamePhase() throws PositionVectorNotValid { do{ userInterface.printTrack(track); Direction direction = track.getCar(currentCarIndex).getMoveStrategy().nextMove(); @@ -321,8 +321,8 @@ public class Game implements GameSpecification { * @return A boolean indicator if the car would crash with a WALL or another car. */ @Override - public boolean willCarCrash(int carIndex, PositionVector position) { - return track.willCrashAtPosition(position); //TODO: add carIndex + public boolean willCarCrash(int carIndex, PositionVector position) throws PositionVectorNotValid { + return track.willCrashAtPosition(carIndex, position); } public boolean allCarsCrashed() { diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Main.java b/src/main/java/ch/zhaw/pm2/racetrack/Main.java index 73da6d8..0817f16 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Main.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Main.java @@ -4,7 +4,7 @@ import java.io.FileNotFoundException; public class Main { - public static void main(String[] args) throws InvalidTrackFormatException, FileNotFoundException { + public static void main(String[] args) throws InvalidTrackFormatException, FileNotFoundException, PositionVectorNotValid { UserInterface userInterface = new UserInterface("Hello and Welcome"); Game game = new Game(userInterface); diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Track.java b/src/main/java/ch/zhaw/pm2/racetrack/Track.java index d2441bb..f3e51f0 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Track.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Track.java @@ -6,7 +6,6 @@ import ch.zhaw.pm2.racetrack.given.TrackSpecification; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import java.util.Scanner; diff --git a/src/main/java/ch/zhaw/pm2/racetrack/given/GameSpecification.java b/src/main/java/ch/zhaw/pm2/racetrack/given/GameSpecification.java index 3596335..c9148e5 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/given/GameSpecification.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/given/GameSpecification.java @@ -1,6 +1,7 @@ package ch.zhaw.pm2.racetrack.given; import ch.zhaw.pm2.racetrack.PositionVector; +import ch.zhaw.pm2.racetrack.PositionVectorNotValid; import java.util.List; @@ -18,11 +19,11 @@ public interface GameSpecification { int getWinner(); - void doCarTurn(PositionVector.Direction acceleration); + void doCarTurn(PositionVector.Direction acceleration) throws PositionVectorNotValid; void switchToNextActiveCar(); List calculatePath(PositionVector startPosition, PositionVector endPosition); - boolean willCarCrash(int carIndex, PositionVector position); + boolean willCarCrash(int carIndex, PositionVector position) throws PositionVectorNotValid; } From e5f45b7bb140e2c41488c500bc76311ce880f287 Mon Sep 17 00:00:00 2001 From: romanschenk37 <84532681+romanschenk37@users.noreply.github.com> Date: Fri, 18 Mar 2022 10:37:53 +0100 Subject: [PATCH 16/36] fixes in Game.java and UserInterface.java --- src/main/java/ch/zhaw/pm2/racetrack/Game.java | 1 + src/main/java/ch/zhaw/pm2/racetrack/Main.java | 5 +++-- src/main/java/ch/zhaw/pm2/racetrack/UserInterface.java | 2 +- 3 files changed, 5 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 0b7595f..48bd162 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Game.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Game.java @@ -187,6 +187,7 @@ public class Game implements GameSpecification { if(winner != NO_WINNER) { return winner; } + switchToNextActiveCar(); } while (!allCarsCrashed()); return NO_WINNER; } diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Main.java b/src/main/java/ch/zhaw/pm2/racetrack/Main.java index 0817f16..1364d4f 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Main.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Main.java @@ -7,10 +7,11 @@ public class Main { public static void main(String[] args) throws InvalidTrackFormatException, FileNotFoundException, PositionVectorNotValid { UserInterface userInterface = new UserInterface("Hello and Welcome"); Game game = new Game(userInterface); - + int winner = 0; if(game.initPhase()){ - int winner = game.gamePhase(); + winner = game.gamePhase(); } + userInterface.printInformation("Winner: " + winner); } } diff --git a/src/main/java/ch/zhaw/pm2/racetrack/UserInterface.java b/src/main/java/ch/zhaw/pm2/racetrack/UserInterface.java index b67d6f4..c19e7ca 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/UserInterface.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/UserInterface.java @@ -52,7 +52,7 @@ public class UserInterface { */ public PositionVector.Direction selectDirection(int playingCarIndex, char playingCarID) { PositionVector.Direction direction = null; - textTerminal.println("Playing Car " + playingCarIndex + ": " + playingCarIndex); + textTerminal.println("Playing Car " + playingCarIndex + ": " + playingCarID); textTerminal.println("Directions are based on the number pad:"); textTerminal.println("7 8 9 7=up-left, 8=up, 9=up-right"); textTerminal.println("4 5 6 4=left, 5=no acceleration, 6=right"); From 63204a4b29ee12a76a25dbbf7e6fbd0088d0b240 Mon Sep 17 00:00:00 2001 From: romanschenk37 <84532681+romanschenk37@users.noreply.github.com> Date: Fri, 18 Mar 2022 10:43:22 +0100 Subject: [PATCH 17/36] fixes in Game.java Method gamePhase --- src/main/java/ch/zhaw/pm2/racetrack/Game.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Game.java b/src/main/java/ch/zhaw/pm2/racetrack/Game.java index 48bd162..c7dbfbb 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Game.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Game.java @@ -179,17 +179,17 @@ public class Game implements GameSpecification { } public int gamePhase() throws PositionVectorNotValid { - do{ + while (getWinner() == NO_WINNER) { userInterface.printTrack(track); Direction direction = track.getCar(currentCarIndex).getMoveStrategy().nextMove(); doCarTurn(direction); int winner = getWinner(); - if(winner != NO_WINNER) { - return winner; + if(allCarsCrashed()) { + return NO_WINNER; } switchToNextActiveCar(); - } while (!allCarsCrashed()); - return NO_WINNER; + } + return getWinner(); } /** From e1e03d4ad4c9c3874ac58becd491249d5c652458 Mon Sep 17 00:00:00 2001 From: romanschenk37 <84532681+romanschenk37@users.noreply.github.com> Date: Fri, 18 Mar 2022 10:43:51 +0100 Subject: [PATCH 18/36] fixes in Game.java Method gamePhase --- src/main/java/ch/zhaw/pm2/racetrack/Game.java | 1 - 1 file changed, 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 c7dbfbb..56fbf31 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Game.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Game.java @@ -183,7 +183,6 @@ public class Game implements GameSpecification { userInterface.printTrack(track); Direction direction = track.getCar(currentCarIndex).getMoveStrategy().nextMove(); doCarTurn(direction); - int winner = getWinner(); if(allCarsCrashed()) { return NO_WINNER; } From 74a83e790a2ddedaa15d2a4925ae6dcd6a8dc59b Mon Sep 17 00:00:00 2001 From: romanschenk37 <84532681+romanschenk37@users.noreply.github.com> Date: Fri, 18 Mar 2022 10:55:31 +0100 Subject: [PATCH 19/36] fixes in Game.java Method switchToNextActiveCar --- src/main/java/ch/zhaw/pm2/racetrack/Game.java | 5 ++++- 1 file changed, 4 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 56fbf31..457d278 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Game.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Game.java @@ -197,9 +197,12 @@ public class Game implements GameSpecification { @Override public void switchToNextActiveCar() { do { - if (currentCarIndex++ > track.getCarCount()) { + if ((currentCarIndex + 1) == track.getCarCount()) { currentCarIndex = 0; } + else { + currentCarIndex ++; + } } while (track.getCar(currentCarIndex).isCrashed()); // TODO: evtl andere Kapselung } From 268506a0840fffbfc2fbb19aba06fe5eb779d9bf Mon Sep 17 00:00:00 2001 From: Leonardo Brandenberger Date: Fri, 18 Mar 2022 11:02:53 +0100 Subject: [PATCH 20/36] fixed errors in calculateWinner --- src/main/java/ch/zhaw/pm2/racetrack/Game.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Game.java b/src/main/java/ch/zhaw/pm2/racetrack/Game.java index 457d278..e59c530 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Game.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Game.java @@ -283,7 +283,7 @@ public class Game implements GameSpecification { if(start.getY() < finish.getY()) { track.getCar(carIndex).increaseWinPoints(); } - else if(start.getY() < finish.getY()) { + else if(start.getY() > finish.getY()) { track.getCar(carIndex).deductWinPoints(); } break; @@ -299,7 +299,7 @@ public class Game implements GameSpecification { if(start.getX() < finish.getX()){ track.getCar(carIndex).increaseWinPoints(); } - else if (start.getX() < finish.getX()){ + else if (start.getX() > finish.getX()){ track.getCar(carIndex).deductWinPoints(); } break; From 8dad26006ce4a56c4f7253d273e807d5b9ee231c Mon Sep 17 00:00:00 2001 From: romanschenk37 <84532681+romanschenk37@users.noreply.github.com> Date: Fri, 18 Mar 2022 13:34:51 +0100 Subject: [PATCH 21/36] change in Main.java --- src/main/java/ch/zhaw/pm2/racetrack/Main.java | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Main.java b/src/main/java/ch/zhaw/pm2/racetrack/Main.java index 1364d4f..bac1de9 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Main.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Main.java @@ -1,17 +1,28 @@ package ch.zhaw.pm2.racetrack; import java.io.FileNotFoundException; +import java.util.ArrayList; +import java.util.List; public class Main { public static void main(String[] args) throws InvalidTrackFormatException, FileNotFoundException, PositionVectorNotValid { - UserInterface userInterface = new UserInterface("Hello and Welcome"); - Game game = new Game(userInterface); - int winner = 0; - if(game.initPhase()){ - winner = game.gamePhase(); + boolean exit = false; + while (!exit) { + UserInterface userInterface = new UserInterface("Hello and Welcome"); + Game game = new Game(userInterface); + int winner = 0; + if (game.initPhase()) { + winner = game.gamePhase(); + } + List optionsNewGame = new ArrayList<>(); + optionsNewGame.add("exit"); + optionsNewGame.add("new game"); + int selectedOption = userInterface.selectOption("The Winner was Car : " + winner + "\nStart new Game?", optionsNewGame); + if(selectedOption == 0) { + exit = true; + } } - userInterface.printInformation("Winner: " + winner); } } From 848d62a5f96615197ce5ffdea7a1cbbda6babc7e Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Fri, 18 Mar 2022 14:06:14 +0100 Subject: [PATCH 22/36] Test commit --- src/main/java/ch/zhaw/pm2/racetrack/Track.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Track.java b/src/main/java/ch/zhaw/pm2/racetrack/Track.java index f3e51f0..5dab127 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Track.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Track.java @@ -87,7 +87,8 @@ public class Track implements TrackSpecification { private void readFile(File trackFile) throws FileNotFoundException { Scanner scanner = new Scanner(trackFile); while (scanner.hasNextLine()) { - track.add(scanner.nextLine()); + //track.add(scanner.nextLine()); + System.out.println(scanner.nextLine()); } } From 504d5c62efeb14ecd703ca5f67006e067edab22b Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Fri, 18 Mar 2022 14:18:07 +0100 Subject: [PATCH 23/36] Fixed wrong encoding error --- src/main/java/ch/zhaw/pm2/racetrack/Track.java | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Track.java b/src/main/java/ch/zhaw/pm2/racetrack/Track.java index 5dab127..640fe3e 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Track.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Track.java @@ -3,8 +3,7 @@ package ch.zhaw.pm2.racetrack; import ch.zhaw.pm2.racetrack.given.ConfigSpecification; import ch.zhaw.pm2.racetrack.given.TrackSpecification; -import java.io.File; -import java.io.FileNotFoundException; +import java.io.*; import java.util.ArrayList; import java.util.List; import java.util.Scanner; @@ -85,10 +84,9 @@ public class Track implements TrackSpecification { * @throws FileNotFoundException if the FilePath is invalid. */ private void readFile(File trackFile) throws FileNotFoundException { - Scanner scanner = new Scanner(trackFile); + Scanner scanner = new Scanner(new FileInputStream(trackFile),"UTF-8"); while (scanner.hasNextLine()) { - //track.add(scanner.nextLine()); - System.out.println(scanner.nextLine()); + track.add(scanner.nextLine()); } } 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 24/36] 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 25/36] 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 26/36] 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 27/36] 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; - } + } - } + } } } From e6fc0fde396ea5dfe433d08d00445859162f99ce Mon Sep 17 00:00:00 2001 From: romanschenk37 <84532681+romanschenk37@users.noreply.github.com> Date: Fri, 18 Mar 2022 17:23:50 +0100 Subject: [PATCH 28/36] fix in makeCarMoveInTrack in Track.java (redraw finish line if car was on finish line) --- .../java/ch/zhaw/pm2/racetrack/Track.java | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Track.java b/src/main/java/ch/zhaw/pm2/racetrack/Track.java index 0593222..a1ace12 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Track.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Track.java @@ -60,6 +60,7 @@ public class Track implements TrackSpecification { private List track; private List cars; private final List finishLine; + private ConfigSpecification.SpaceType finishTyp; /** * Initialize a Track from the given track file. @@ -132,7 +133,7 @@ public class Track implements TrackSpecification { if (finishLine.size() == 0) { throw new InvalidTrackFormatException(); } - ConfigSpecification.SpaceType finishTyp = getSpaceType(finishLine.get(0)); + finishTyp = getSpaceType(finishLine.get(0)); for (PositionVector positionVector : finishLine) { if (getSpaceType(positionVector) != finishTyp) { throw new InvalidTrackFormatException(); @@ -207,12 +208,20 @@ public class Track implements TrackSpecification { * @param carIndex of the current car */ private void makeCarMoveInTrack(int carIndex) { - PositionVector positionVector = findChar(getCarId(carIndex)); + PositionVector carPositionVector = findChar(getCarId(carIndex)); //Removes the Car at Current Pos - drawCharOnTrackIndicator(positionVector, ConfigSpecification.SpaceType.TRACK.getValue()); + drawCharOnTrackIndicator(carPositionVector, ConfigSpecification.SpaceType.TRACK.getValue()); + + //Redraw finishline if Car was on finish-line Position + for(PositionVector finishLinePositionVector : finishLine){ + if(finishLinePositionVector.equals(carPositionVector)){ + drawCharOnTrackIndicator(carPositionVector, finishTyp.getValue()); + } + } + //Adds Car at new Position - positionVector = cars.get(carIndex).nextPosition(); - drawCharOnTrackIndicator(positionVector, cars.get(carIndex).getID()); + carPositionVector = cars.get(carIndex).nextPosition(); + drawCharOnTrackIndicator(carPositionVector, cars.get(carIndex).getID()); } /** From dad57ea8ac44ca42ad4978eed23403d3e159a42e Mon Sep 17 00:00:00 2001 From: romanschenk37 <84532681+romanschenk37@users.noreply.github.com> Date: Fri, 18 Mar 2022 17:32:16 +0100 Subject: [PATCH 29/36] fix in CardoesCrash in Track.java (remove Car when crash) --- src/main/java/ch/zhaw/pm2/racetrack/Track.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Track.java b/src/main/java/ch/zhaw/pm2/racetrack/Track.java index a1ace12..c83839e 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Track.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Track.java @@ -231,7 +231,7 @@ public class Track implements TrackSpecification { * @return true if car would crash. Else false. */ public boolean willCrashAtPosition(int carIndex, PositionVector positionVector) throws PositionVectorNotValid { - isPositionVectorOnTrack(positionVector); + isPositionVectorOnTrack(positionVector); //TODO: remove this line? Or Method? char charAtPosition = track.get(positionVector.getY()).charAt(positionVector.getX()); if (getCarId(carIndex) == charAtPosition) return false; return !(charAtPosition == ConfigSpecification.SpaceType.TRACK.value || @@ -245,14 +245,16 @@ public class Track implements TrackSpecification { * This Method will make the Car Crash. In Track and in the Car Object * * @param carIndex representing current Car - * @param positionVector where the Crash did happen + * @param crashPositionVector where the Crash did happen */ - public void carDoesCrash(int carIndex, PositionVector positionVector) throws PositionVectorNotValid{ - isPositionVectorOnTrack(positionVector); + public void carDoesCrash(int carIndex, PositionVector crashPositionVector) throws PositionVectorNotValid{ + isPositionVectorOnTrack(crashPositionVector); //TODO: remove this line? and Method? + PositionVector currentCarPosition = getCarPos(carIndex); + drawCharOnTrackIndicator(new PositionVector(currentCarPosition.getX(), currentCarPosition.getY()), ConfigSpecification.SpaceType.TRACK.getValue()); Car car = cars.get(carIndex); car.crash(); - car.setPosition(positionVector); - drawCharOnTrackIndicator(new PositionVector(positionVector.getX(), positionVector.getY()), CRASH_INDICATOR); + car.setPosition(crashPositionVector); + drawCharOnTrackIndicator(new PositionVector(crashPositionVector.getX(), crashPositionVector.getY()), CRASH_INDICATOR); } /** From 21da4feaf569c468196f2e1d4d331fc5cc164975 Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Fri, 18 Mar 2022 18:02:10 +0100 Subject: [PATCH 30/36] dev MoveListStrategy and test --- src/main/java/ch/zhaw/pm2/racetrack/Game.java | 104 ++++++++++-------- .../racetrack/strategy/MoveListStrategy.java | 37 ++++++- .../zhaw/pm2/racetrack/MoveStrategyTest.java | 40 +++++++ 3 files changed, 131 insertions(+), 50 deletions(-) create mode 100644 src/test/java/ch/zhaw/pm2/racetrack/MoveStrategyTest.java diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Game.java b/src/main/java/ch/zhaw/pm2/racetrack/Game.java index e59c530..239d8c0 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Game.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Game.java @@ -1,10 +1,7 @@ package ch.zhaw.pm2.racetrack; import ch.zhaw.pm2.racetrack.given.GameSpecification; -import ch.zhaw.pm2.racetrack.strategy.DoNotMoveStrategy; -import ch.zhaw.pm2.racetrack.strategy.MoveListStrategy; -import ch.zhaw.pm2.racetrack.strategy.PathFollowerMoveStrategy; -import ch.zhaw.pm2.racetrack.strategy.UserMoveStrategy; +import ch.zhaw.pm2.racetrack.strategy.*; import java.io.File; import java.io.FileNotFoundException; @@ -33,9 +30,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,7 +46,7 @@ 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 @@ -60,7 +57,14 @@ public class Game implements GameSpecification { track.getCar(i).setMoveStrategy(new UserMoveStrategy(userInterface, i, track.getCarId(i))); //TODO: add Arguments break; case 3: - track.getCar(i).setMoveStrategy(new MoveListStrategy()); //TODO: add Arguments + String path = ".\\moves\\ " + selectedTrack.getName().split(".")[0] + "-car-" + track.getCar(i).getID() + ".txt"; + try { + MoveStrategy moveStrategy = new MoveListStrategy(path); + track.getCar(i).setMoveStrategy(moveStrategy); + } catch (FileNotFoundException e) { + //TODO: what if not valid + } + //TODO: Backslash kompatibel für Linux break; case 4: track.getCar(i).setMoveStrategy(new PathFollowerMoveStrategy()); //TODO: add Arguments @@ -68,8 +72,7 @@ public class Game implements GameSpecification { } } return true; - } - else{ + } else { userInterface.printInformation("No Trackfile found!"); return false; } @@ -78,6 +81,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 +91,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 +102,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 +113,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,13 +124,14 @@ 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){ + for (Car car : cars) { + if (car.getWinPoints() == 1) { return car.getID(); } } @@ -162,17 +170,16 @@ public class Game implements GameSpecification { track.getCar(currentCarIndex).accelerate(acceleration); PositionVector crashPosition = null; - List positionList = calculatePath(track.getCarPos(currentCarIndex),track.getCar(currentCarIndex).nextPosition()); + 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)) { + for (PositionVector location : positionList) { //todo: check if order must be reversed + if (willCarCrash(currentCarIndex, location)) { crashPosition = location; } } - if(crashPosition != null) { + if (crashPosition != null) { track.carDoesCrash(currentCarIndex, crashPosition); - } - else { + } else { track.moveCar(currentCarIndex); calculateWinner(track.getCarPos(currentCarIndex), track.getCar(currentCarIndex).nextPosition(), currentCarIndex); } @@ -183,7 +190,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 +206,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 +221,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 +251,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,55 +281,52 @@ 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){ + for (PositionVector point : path) { switch (track.getSpaceType(point)) { case FINISH_UP: - if(start.getY() < finish.getY()) { + if (start.getY() < finish.getY()) { track.getCar(carIndex).increaseWinPoints(); - } - else if(start.getY() > finish.getY()) { + } else if (start.getY() > finish.getY()) { track.getCar(carIndex).deductWinPoints(); } break; case FINISH_DOWN: - if(start.getY() > finish.getY()){ + if (start.getY() > finish.getY()) { track.getCar(carIndex).increaseWinPoints(); - } - else if (start.getY() < finish.getY()){ + } else if (start.getY() < finish.getY()) { track.getCar(carIndex).deductWinPoints(); } break; case FINISH_RIGHT: - if(start.getX() < finish.getX()){ + if (start.getX() < finish.getX()) { track.getCar(carIndex).increaseWinPoints(); - } - else if (start.getX() > finish.getX()){ + } else if (start.getX() > finish.getX()) { track.getCar(carIndex).deductWinPoints(); } break; - case FINISH_LEFT: - if(start.getX() > finish.getX()){ + case FINISH_LEFT: + if (start.getX() > finish.getX()) { track.getCar(carIndex).increaseWinPoints(); - } - else if (start.getX() < finish.getX()){ + } else if (start.getX() < finish.getX()) { track.getCar(carIndex).increaseWinPoints(); } break; - } } + } } /** * Does indicate if a car would have a crash with a WALL space or another car at the given position. + * * @param carIndex The zero-based carIndex number * @param position A PositionVector of the possible crash position * @return A boolean indicator if the car would crash with a WALL or another car. @@ -329,8 +337,8 @@ public class Game implements GameSpecification { } public boolean allCarsCrashed() { - for(int carIndex = 0; carIndex < track.getCarCount(); carIndex ++) { - if(! track.getCar(carIndex).isCrashed()) { + for (int carIndex = 0; carIndex < track.getCarCount(); carIndex++) { + if (!track.getCar(carIndex).isCrashed()) { return false; } } diff --git a/src/main/java/ch/zhaw/pm2/racetrack/strategy/MoveListStrategy.java b/src/main/java/ch/zhaw/pm2/racetrack/strategy/MoveListStrategy.java index 4432c69..7d1e68e 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/strategy/MoveListStrategy.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/strategy/MoveListStrategy.java @@ -2,11 +2,44 @@ package ch.zhaw.pm2.racetrack.strategy; import ch.zhaw.pm2.racetrack.PositionVector.Direction; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + public class MoveListStrategy implements MoveStrategy { + private List moveList; + private int pointer; + + public MoveListStrategy(String path) throws FileNotFoundException{ + moveList = new ArrayList<>(); + pointer = -1; + readFile(new File(path)); + } + + private void readFile(File trackFile) throws FileNotFoundException { + Scanner scanner = new Scanner(new FileInputStream(trackFile), "UTF-8"); + Direction[] directions = Direction.values(); + while (scanner.hasNextLine()) { + String line = scanner.nextLine(); + for (Direction dir : directions) { + if (dir.toString().equals(line)) { + moveList.add(dir); + break; + } + } + } + } + @Override public Direction nextMove() { - // TODO: implementation - throw new UnsupportedOperationException(); + pointer += 1; + if (pointer < moveList.size()) { + return moveList.get(pointer); + } + return null; } } diff --git a/src/test/java/ch/zhaw/pm2/racetrack/MoveStrategyTest.java b/src/test/java/ch/zhaw/pm2/racetrack/MoveStrategyTest.java new file mode 100644 index 0000000..f4e5afb --- /dev/null +++ b/src/test/java/ch/zhaw/pm2/racetrack/MoveStrategyTest.java @@ -0,0 +1,40 @@ +package ch.zhaw.pm2.racetrack; + +import ch.zhaw.pm2.racetrack.strategy.MoveListStrategy; +import ch.zhaw.pm2.racetrack.strategy.MoveStrategy; +import org.junit.jupiter.api.*; + +import java.io.FileNotFoundException; + +public class MoveStrategyTest { + + private MoveStrategy moveList; + + @Nested + @DisplayName("MoveListStrategy") + class MoveList { + + + @BeforeEach + void setup() { + try { + moveList = new MoveListStrategy(".\\moves\\challenge-car-a.txt"); + }catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + + @Test + void checkMove() { + Assertions.assertEquals(PositionVector.Direction.RIGHT,moveList.nextMove()); + for (int i = 0; i < 3; i++) { + moveList.nextMove(); + } + Assertions.assertEquals(PositionVector.Direction.NONE,moveList.nextMove()); + for (int i = 0; i < 40; i++) { + moveList.nextMove(); + } + Assertions.assertNull(moveList.nextMove()); + } + } +} From 837297cd3102a9691d3e148e3ee96f6fbad524dd Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Fri, 18 Mar 2022 19:44:47 +0100 Subject: [PATCH 31/36] MoveListStrategy implemented --- src/main/java/ch/zhaw/pm2/racetrack/Game.java | 47 ++++++++++--------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Game.java b/src/main/java/ch/zhaw/pm2/racetrack/Game.java index 7098a95..fa7df66 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Game.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Game.java @@ -47,28 +47,31 @@ public class Game implements GameSpecification { moveStrategies.add("Move List Strategy"); moveStrategies.add("Path Follow Move Strategy"); for (int i = 0; i < track.getCarCount(); i++) { - int moveStrategie = userInterface.selectOption( - "Select Strategy for Car " + i + " (" + track.getCarId(i) + ")", moveStrategies); - switch (moveStrategie + 1) { - case 1: - track.getCar(i).setMoveStrategy(new DoNotMoveStrategy()); - break; - case 2: - track.getCar(i).setMoveStrategy(new UserMoveStrategy(userInterface, i, track.getCarId(i))); - break; - case 3: - String path = ".\\moves\\ " + selectedTrack.getName().split("\\.")[0] + "-car-" + track.getCar(i).getID() + ".txt"; - try { - MoveStrategy moveStrategy = new MoveListStrategy(path); - track.getCar(i).setMoveStrategy(moveStrategy); - } catch (FileNotFoundException e) { - //TODO: what if not valid - } - //TODO: Backslash kompatibel für Linux - break; - case 4: - track.getCar(i).setMoveStrategy(new PathFollowerMoveStrategy()); //TODO: add Arguments - break; + while(track.getCar(i).getMoveStrategy() == null) { + int moveStrategie = userInterface.selectOption( + "Select Strategy for Car " + i + " (" + track.getCarId(i) + ")", moveStrategies); + switch (moveStrategie + 1) { + case 1: + track.getCar(i).setMoveStrategy(new DoNotMoveStrategy()); + break; + case 2: + track.getCar(i).setMoveStrategy(new UserMoveStrategy(userInterface, i, track.getCarId(i))); + break; + case 3: + String path = ".\\moves\\" + selectedTrack.getName().split("\\.")[0] + "-car-" + track.getCar(i).getID() + ".txt"; + System.out.println(path); + try { + MoveStrategy moveStrategy = new MoveListStrategy(path); + track.getCar(i).setMoveStrategy(moveStrategy); + } catch (FileNotFoundException e) { + userInterface.printInformation("There is no MoveList implemented. Choose another Strategy!"); + } + //TODO: Backslash kompatibel für Linux + break; + case 4: + track.getCar(i).setMoveStrategy(new PathFollowerMoveStrategy()); //TODO: add Arguments + break; + } } } return true; From 833b122d2bc6e12fad2782f07f84d4afd65e9c3c Mon Sep 17 00:00:00 2001 From: romanschenk37 Date: Fri, 18 Mar 2022 20:54:04 +0100 Subject: [PATCH 32/36] Update Game.java Removed System.out.println... --- src/main/java/ch/zhaw/pm2/racetrack/Game.java | 1 - 1 file changed, 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 fa7df66..2ae1957 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Game.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Game.java @@ -59,7 +59,6 @@ public class Game implements GameSpecification { break; case 3: String path = ".\\moves\\" + selectedTrack.getName().split("\\.")[0] + "-car-" + track.getCar(i).getID() + ".txt"; - System.out.println(path); try { MoveStrategy moveStrategy = new MoveListStrategy(path); track.getCar(i).setMoveStrategy(moveStrategy); From fc78b103650da7c884bdc06634527801174147bd Mon Sep 17 00:00:00 2001 From: romanschenk37 <84532681+romanschenk37@users.noreply.github.com> Date: Fri, 18 Mar 2022 21:48:27 +0100 Subject: [PATCH 33/36] optimized Main Class. added Method quit to UserInterface.java --- src/main/java/ch/zhaw/pm2/racetrack/Game.java | 8 +++-- src/main/java/ch/zhaw/pm2/racetrack/Main.java | 33 ++++++++++++------- .../ch/zhaw/pm2/racetrack/UserInterface.java | 9 +++++ 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Game.java b/src/main/java/ch/zhaw/pm2/racetrack/Game.java index 2ae1957..0b46109 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Game.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Game.java @@ -190,7 +190,7 @@ public class Game implements GameSpecification { } } - public int gamePhase() throws PositionVectorNotValid { + public String gamePhase() throws PositionVectorNotValid { while (CarsMoving() && getWinner() == NO_WINNER) { userInterface.printTrack(track); Direction direction = null; @@ -203,7 +203,11 @@ public class Game implements GameSpecification { switchToNextActiveCar(); } userInterface.printTrack(track); - return getWinner(); + int indexWinner = getWinner(); + if(indexWinner == NO_WINNER){ + return null; + } + return String.valueOf(track.getCar(indexWinner).getID()); } /** diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Main.java b/src/main/java/ch/zhaw/pm2/racetrack/Main.java index 1a7c197..4013616 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Main.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Main.java @@ -7,23 +7,34 @@ import java.util.List; 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 to Racetrack by Team02-\"AngryNerds\""); + while (true) { Game game = new Game(userInterface); - int winner = 0; + String winner; if (game.initPhase()) { winner = game.gamePhase(); + List optionsNewGame = new ArrayList<>(); + optionsNewGame.add("exit"); + optionsNewGame.add("new game"); + String winnerText; + if(winner == null){ + winnerText = "There was no winner."; + } + else { + winnerText = "The Winner was Car " + winner; + } + int selectedOption = userInterface.selectOption(winnerText + "\nStart new Game?", optionsNewGame); + if(selectedOption == 0) { + userInterface.quit("Thank you and goodbye\npress enter to close the application."); + break; + } } - List optionsNewGame = new ArrayList<>(); - optionsNewGame.add("exit"); - optionsNewGame.add("new game"); - int selectedOption = userInterface.selectOption("The Winner was Car : " + winner + "\nStart new Game?", optionsNewGame); - if(selectedOption == 0) { - exit = true; + else { + userInterface.quit("The initialisation of the game failed. Press enter to close the application."); + break; } } - userInterface.printInformation("Thank you and goodbye"); + } } diff --git a/src/main/java/ch/zhaw/pm2/racetrack/UserInterface.java b/src/main/java/ch/zhaw/pm2/racetrack/UserInterface.java index c19e7ca..96a8c04 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/UserInterface.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/UserInterface.java @@ -97,6 +97,15 @@ public class UserInterface { textTerminal.println(track.toString()); } + /** + * Method to dispose the Textterminal + * @param text OUtput Text + */ + public void quit(String text){ + textIO.newStringInputReader().withMinLength(0).read(text); + textTerminal.dispose(); + } + } From a80ea0fa1c9c6495a5386b946b202705fa5b8fc7 Mon Sep 17 00:00:00 2001 From: romanschenk37 <84532681+romanschenk37@users.noreply.github.com> Date: Fri, 18 Mar 2022 22:01:03 +0100 Subject: [PATCH 34/36] fix doCarTurn in Game.java --- src/main/java/ch/zhaw/pm2/racetrack/Game.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Game.java b/src/main/java/ch/zhaw/pm2/racetrack/Game.java index 0b46109..810ad78 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Game.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Game.java @@ -180,6 +180,7 @@ public class Game implements GameSpecification { } else { crashPosition = positionList.get(i - 1); } + break; } } if (crashPosition != null) { From 882656196324cbaa3ca95ec641c7efb171a9cd3e Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Sat, 19 Mar 2022 13:20:19 +0100 Subject: [PATCH 35/36] GameTest startet ++ Game and Main clean up --- src/main/java/ch/zhaw/pm2/racetrack/Game.java | 81 +++++++++----- src/main/java/ch/zhaw/pm2/racetrack/Main.java | 2 +- .../java/ch/zhaw/pm2/racetrack/GameTest.java | 104 ++++++++++++++++++ 3 files changed, 158 insertions(+), 29 deletions(-) create mode 100644 src/test/java/ch/zhaw/pm2/racetrack/GameTest.java diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Game.java b/src/main/java/ch/zhaw/pm2/racetrack/Game.java index 810ad78..413fef3 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Game.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Game.java @@ -18,7 +18,7 @@ import static ch.zhaw.pm2.racetrack.PositionVector.Direction; public class Game implements GameSpecification { public static final int NO_WINNER = -1; private Track track; - int currentCarIndex; + private int currentCarIndex; UserInterface userInterface; @@ -27,7 +27,7 @@ public class Game implements GameSpecification { } - public boolean initPhase() throws InvalidTrackFormatException, FileNotFoundException { + public boolean initPhase() throws InvalidTrackFormatException { File folder = new File("tracks"); File[] listOfFiles = folder.listFiles(); if (listOfFiles.length > 0) { @@ -36,39 +36,37 @@ public class Game implements GameSpecification { tracks.add(file.getName()); } File selectedTrack = listOfFiles[userInterface.selectOption("Select Track file", tracks)]; - try { - track = new Track(selectedTrack); - } catch (FileNotFoundException | PositionVectorNotValid e) { - e.printStackTrace(); - } + selectTrack(selectedTrack); List moveStrategies = new ArrayList<>(); moveStrategies.add("Do not move Strategy"); moveStrategies.add("User Move Strategy"); moveStrategies.add("Move List Strategy"); moveStrategies.add("Path Follow Move Strategy"); for (int i = 0; i < track.getCarCount(); i++) { - while(track.getCar(i).getMoveStrategy() == null) { + Car car = track.getCar(i); + while (car.getMoveStrategy() == null) { int moveStrategie = userInterface.selectOption( "Select Strategy for Car " + i + " (" + track.getCarId(i) + ")", moveStrategies); switch (moveStrategie + 1) { case 1: - track.getCar(i).setMoveStrategy(new DoNotMoveStrategy()); + selectMoveStrategy(car, new DoNotMoveStrategy()); break; case 2: - track.getCar(i).setMoveStrategy(new UserMoveStrategy(userInterface, i, track.getCarId(i))); + selectMoveStrategy(car, new UserMoveStrategy(userInterface, i, track.getCarId(i))); break; case 3: String path = ".\\moves\\" + selectedTrack.getName().split("\\.")[0] + "-car-" + track.getCar(i).getID() + ".txt"; try { MoveStrategy moveStrategy = new MoveListStrategy(path); - track.getCar(i).setMoveStrategy(moveStrategy); + selectMoveStrategy(car, moveStrategy); } catch (FileNotFoundException e) { userInterface.printInformation("There is no MoveList implemented. Choose another Strategy!"); } //TODO: Backslash kompatibel für Linux break; case 4: - track.getCar(i).setMoveStrategy(new PathFollowerMoveStrategy()); //TODO: add Arguments + //TODO: add Arguments + selectMoveStrategy(car, new PathFollowerMoveStrategy()); break; } } @@ -80,6 +78,31 @@ public class Game implements GameSpecification { } } + /** + * The functionality was taken out of init to automate testing + * + * @param selectedTrack + */ + Track selectTrack(File selectedTrack) { + try { + track = new Track(selectedTrack); + return track; + } catch (FileNotFoundException | PositionVectorNotValid | InvalidTrackFormatException e) { + e.printStackTrace(); + } + return null; + } + + /** + * The functionality was taken out of init to automate testing + * + * @param car to set the MoveStrategy + * @param strategy The movestrategy to set + */ + void selectMoveStrategy(Car car, MoveStrategy strategy) { + car.setMoveStrategy(strategy); + } + /** * 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. @@ -115,6 +138,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 */ @@ -192,20 +216,21 @@ public class Game implements GameSpecification { } public String gamePhase() throws PositionVectorNotValid { - while (CarsMoving() && getWinner() == NO_WINNER) { + while (carsMoving() && getWinner() == NO_WINNER) { userInterface.printTrack(track); - Direction direction = null; - direction= track.getCar(currentCarIndex).getMoveStrategy().nextMove(); - if(direction == null) { + Direction direction; + direction = track.getCar(currentCarIndex).getMoveStrategy().nextMove(); + if (direction == null) { track.getCar(currentCarIndex).setMoveStrategy(new DoNotMoveStrategy()); - direction= track.getCar(currentCarIndex).getMoveStrategy().nextMove(); + direction = track.getCar(currentCarIndex).getMoveStrategy().nextMove(); //TODO: Entfernen? + }else { + doCarTurn(direction); } - doCarTurn(direction); switchToNextActiveCar(); } userInterface.printTrack(track); int indexWinner = getWinner(); - if(indexWinner == NO_WINNER){ + if (indexWinner == NO_WINNER) { return null; } return String.valueOf(track.getCar(indexWinner).getID()); @@ -302,8 +327,7 @@ public class Game implements GameSpecification { private void calculateWinner(PositionVector start, PositionVector finish, int carIndex) { List path = calculatePath(start, finish); for (PositionVector point : path) { - if (track.getSpaceType(point) != null) - { + if (track.getSpaceType(point) != null) { switch (track.getSpaceType(point)) { case FINISH_UP: if (start.getY() < finish.getY()) { @@ -333,15 +357,16 @@ public class Game implements GameSpecification { track.getCar(carIndex).increaseWinPoints(); } break; - } - } + + } } } /** * Does indicate if a car would have a crash with a WALL space or another car at the given position. + * * @param carIndex The zero-based carIndex number * @param position A PositionVector of the possible crash position * @return A boolean indicator if the car would crash with a WALL or another car. @@ -353,17 +378,17 @@ public class Game implements GameSpecification { public boolean onlyOneCarLeft() { int carsLeft = 0; - for(int carIndex = 0; carIndex < track.getCarCount(); carIndex ++) { - if(! track.getCar(carIndex).isCrashed()) { + for (int carIndex = 0; carIndex < track.getCarCount(); carIndex++) { + if (!track.getCar(carIndex).isCrashed()) { carsLeft++; } } 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)) { + 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; } } diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Main.java b/src/main/java/ch/zhaw/pm2/racetrack/Main.java index 4013616..aec65bd 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Main.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Main.java @@ -6,7 +6,7 @@ import java.util.List; public class Main { - public static void main(String[] args) throws InvalidTrackFormatException, FileNotFoundException, PositionVectorNotValid { + public static void main(String[] args) throws InvalidTrackFormatException, PositionVectorNotValid { UserInterface userInterface = new UserInterface("Hello and Welcome to Racetrack by Team02-\"AngryNerds\""); while (true) { Game game = new Game(userInterface); diff --git a/src/test/java/ch/zhaw/pm2/racetrack/GameTest.java b/src/test/java/ch/zhaw/pm2/racetrack/GameTest.java new file mode 100644 index 0000000..03cb0f3 --- /dev/null +++ b/src/test/java/ch/zhaw/pm2/racetrack/GameTest.java @@ -0,0 +1,104 @@ +package ch.zhaw.pm2.racetrack; + +import ch.zhaw.pm2.racetrack.strategy.UserMoveStrategy; +import org.junit.jupiter.api.*; + +import java.io.File; + +import static ch.zhaw.pm2.racetrack.Game.NO_WINNER; + +class GameTest { + private UserInterface userInterface; + private Game game; + private Track track; + + @Nested + @DisplayName("Test correct Setup") + class Setup { + @BeforeEach + void setup() { + userInterface = new UserInterface("Test"); + game = new Game(userInterface); + track = game.selectTrack(new File(".\\tracks\\challenge.txt")); + game.selectMoveStrategy(track.getCar(0),new UserMoveStrategy(new UserInterface("Testing"),0, track.getCarId(0))); + game.selectMoveStrategy(track.getCar(1),new UserMoveStrategy(new UserInterface("Testing"),1, track.getCarId(1))); + + } + + + @Test + void getCurrentCarIndex() { + Assertions.assertEquals(0,game.getCurrentCarIndex()); + game.switchToNextActiveCar(); + Assertions.assertEquals(1,game.getCurrentCarIndex()); + } + + @Test + void getCarId() { + Assertions.assertEquals('a',game.getCarId(0)); + Assertions.assertEquals('b',game.getCarId(1)); + } + + @Test + void getCarPosition() { + Assertions.assertEquals(new PositionVector(24,22),game.getCarPosition(0)); + Assertions.assertEquals(new PositionVector(24,24),game.getCarPosition(1)); + } + + @Test + void getCarVelocity() { + Assertions.assertEquals(new PositionVector(0,0),game.getCarVelocity(0)); + Assertions.assertEquals(new PositionVector(0,0),game.getCarVelocity(1)); + } + + @Test + void getWinner() { + Assertions.assertEquals(NO_WINNER,game.getWinner()); + } + + @Test + void onlyOneCarLeft() { + Assertions.assertFalse(game.onlyOneCarLeft()); + } + + @Test + void carsMoving() { + Assertions.assertTrue(game.carsMoving()); + } + } + + @Nested + @DisplayName("Basic manipulation") + class manipulation { + @BeforeEach + void setup() { + userInterface = new UserInterface("Test"); + game = new Game(userInterface); + track = game.selectTrack(new File(".\\tracks\\challenge.txt")); + game.selectMoveStrategy(track.getCar(0),new UserMoveStrategy(new UserInterface("Testing"),0, track.getCarId(0))); + game.selectMoveStrategy(track.getCar(1),new UserMoveStrategy(new UserInterface("Testing"),1, track.getCarId(1))); + + } + + @Test + void carTurnCorrect() { + try { + game.doCarTurn(PositionVector.Direction.RIGHT); + Assertions.assertEquals(new PositionVector(1,0),game.getCarVelocity(0)); + } catch (PositionVectorNotValid positionVectorNotValid) { + positionVectorNotValid.printStackTrace(); + } + } + + @Test + void carCrash() { + try { + game.doCarTurn(PositionVector.Direction.UP); + Assertions.assertTrue(game.onlyOneCarLeft()); + } catch (PositionVectorNotValid positionVectorNotValid) { + positionVectorNotValid.printStackTrace(); + } + } + + } +} From b467a795961b3b5262682c1be7532ed561136e26 Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Sun, 20 Mar 2022 13:44:54 +0100 Subject: [PATCH 36/36] Changed mainClass in build.gradle to Main --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 669126f..9b73dff 100644 --- a/build.gradle +++ b/build.gradle @@ -44,7 +44,7 @@ version = '2022.1' application { // Define the main class for the application. - mainClass = 'ch.zhaw.pm2.racetrack.ConsoleApp' + mainClass = 'ch.zhaw.pm2.racetrack.Main' } run {