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/45] 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/45] 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/45] 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/45] 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/45] 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/45] 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/45] 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/45] 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/45] 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/45] 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/45] 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/45] 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/45] 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/45] 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/45] 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/45] 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/45] 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/45] 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/45] 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/45] 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/45] 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/45] 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/45] 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/45] 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/45] 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/45] 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/45] 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/45] 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/45] 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/45] 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/45] 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/45] 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/45] 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/45] 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/45] 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/45] 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 { From eddf9d3daa0efcd6688795b962eeca0022b8358d Mon Sep 17 00:00:00 2001 From: romanschenk37 <84532681+romanschenk37@users.noreply.github.com> Date: Tue, 22 Mar 2022 18:20:23 +0100 Subject: [PATCH 37/45] implemented PathFollowerMoveStrategy.java --- follower/challenge_points.txt | 19 ++++ src/main/java/ch/zhaw/pm2/racetrack/Game.java | 24 +++-- .../racetrack/strategy/MoveListStrategy.java | 8 +- .../strategy/PathFollowerMoveStrategy.java | 93 ++++++++++++++++++- 4 files changed, 129 insertions(+), 15 deletions(-) create mode 100644 follower/challenge_points.txt diff --git a/follower/challenge_points.txt b/follower/challenge_points.txt new file mode 100644 index 0000000..d794046 --- /dev/null +++ b/follower/challenge_points.txt @@ -0,0 +1,19 @@ +(X:40, Y:22) +(X:43, Y:22) +(X:46, Y:21) +(X:48, Y:19) +(X:48, Y:17) +(X:46, Y:15) +(X:41, Y:13) +(X:41, Y:10) +(X:46, Y:9) +(X:49, Y:4) +(X:40, Y:2) +(X:30, Y:2) +(X:21, Y:3) +(X:16, Y:7) +(X:13, Y:10) +(X:14, Y:14) +(X:11, Y:19) +(X:13, Y:22) +(X:24, Y:22) diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Game.java b/src/main/java/ch/zhaw/pm2/racetrack/Game.java index 413fef3..406cf40 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Game.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Game.java @@ -44,32 +44,38 @@ public class Game implements GameSpecification { moveStrategies.add("Path Follow Move Strategy"); for (int i = 0; i < track.getCarCount(); i++) { Car car = track.getCar(i); - while (car.getMoveStrategy() == null) { + MoveStrategy moveStrategy = null; + while (moveStrategy == null) { + String filePath; int moveStrategie = userInterface.selectOption( "Select Strategy for Car " + i + " (" + track.getCarId(i) + ")", moveStrategies); switch (moveStrategie + 1) { case 1: - selectMoveStrategy(car, new DoNotMoveStrategy()); + moveStrategy = new DoNotMoveStrategy(); break; case 2: - selectMoveStrategy(car, new UserMoveStrategy(userInterface, i, track.getCarId(i))); + moveStrategy = new UserMoveStrategy(userInterface, i, track.getCarId(i)); break; case 3: - String path = ".\\moves\\" + selectedTrack.getName().split("\\.")[0] + "-car-" + track.getCar(i).getID() + ".txt"; + filePath = ".\\moves\\" + selectedTrack.getName().split("\\.")[0] + "-car-" + track.getCar(i).getID() + ".txt"; try { - MoveStrategy moveStrategy = new MoveListStrategy(path); - selectMoveStrategy(car, moveStrategy); + moveStrategy = new MoveListStrategy(filePath); } catch (FileNotFoundException e) { - userInterface.printInformation("There is no MoveList implemented. Choose another Strategy!"); + userInterface.printInformation("There is no Move-List implemented. Choose another Strategy!"); } //TODO: Backslash kompatibel für Linux break; case 4: - //TODO: add Arguments - selectMoveStrategy(car, new PathFollowerMoveStrategy()); + filePath = ".\\follower\\" + selectedTrack.getName().split("\\.")[0] + "_points.txt"; + try { + moveStrategy = new PathFollowerMoveStrategy(filePath, track.getCarPos(i)); + } catch (FileNotFoundException e) { + userInterface.printInformation("There is no Point-List implemented. Choose another Strategy!"); + } break; } } + selectMoveStrategy(car, moveStrategy); } return true; } else { 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 7d1e68e..774b323 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/strategy/MoveListStrategy.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/strategy/MoveListStrategy.java @@ -20,14 +20,14 @@ public class MoveListStrategy implements MoveStrategy { readFile(new File(path)); } - private void readFile(File trackFile) throws FileNotFoundException { + protected 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); + for (Direction direction : directions) { + if (direction.toString().equals(line)) { + moveList.add(direction); break; } } diff --git a/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFollowerMoveStrategy.java b/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFollowerMoveStrategy.java index 23ad51f..f85a0e9 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFollowerMoveStrategy.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFollowerMoveStrategy.java @@ -1,15 +1,104 @@ package ch.zhaw.pm2.racetrack.strategy; +import ch.zhaw.pm2.racetrack.PositionVector; 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.Scanner; + /** * The PathFollowerMoveStrategy class determines the next move based on a file containing points on a path. */ public class PathFollowerMoveStrategy implements MoveStrategy { + private PositionVector currentPosition; + private PositionVector currentVelocity; + private ArrayList pointList; + private int pointer; + + + public PathFollowerMoveStrategy(String path, PositionVector startPosition) throws FileNotFoundException { + pointList = new ArrayList<>(); + pointer = 0; + readFile(new File(path)); + currentPosition = startPosition; + currentVelocity = new PositionVector(0, 0); + } + + public void readFile(File trackFile) throws FileNotFoundException { + Scanner scanner = new Scanner(new FileInputStream(trackFile), "UTF-8"); + while (scanner.hasNextLine()) { + String line = scanner.nextLine(); + String[] coordinates = line.split("(\\(X:|, Y:|\\))"); + pointList.add(new PositionVector(Integer.parseInt(coordinates[1]), Integer.parseInt(coordinates[2]))); + } + } @Override public Direction nextMove() { - // TODO: implementation - throw new UnsupportedOperationException(); + if (pointer >= pointList.size()) { + return null; + } + int accelerationX = 0; + int accelerationY = 0; + if (pointList.get(pointer).equals(currentPosition)) { + pointer ++; + } + + + if(currentVelocity.getX() > 0){ + accelerationX = -1; + } else if(currentVelocity.getX() < 0) { + accelerationX = 1; + } + if(currentVelocity.getY() > 0){ + accelerationY = -1; + } else if(currentVelocity.getY() < 0) { + accelerationY = 1; + } + + + PositionVector movementVector = new PositionVector(pointList.get(pointer).getX() - currentPosition.getX(), pointList.get(pointer).getY() - currentPosition.getY()); + if(movementVector.getX() > 0 && movementVector.getX()/2.0 > currentVelocity.getX()) { + accelerationX = 1; + } + else if(movementVector.getX() > 0 && movementVector.getX()/2.0 <= currentVelocity.getX()) { + accelerationX = -1; + } + else if(movementVector.getX() < 0 && movementVector.getX()/2.0 < currentVelocity.getX()) { + accelerationX = -1; + } + else if(movementVector.getX() < 0 && movementVector.getX()/2.0 >= currentVelocity.getX()) { + accelerationX = 1; + } + + if(movementVector.getY() > 0 && movementVector.getY()/2.0 > currentVelocity.getY()) { + accelerationY = 1; + } + else if(movementVector.getY() > 0 && movementVector.getY()/2.0 <= currentVelocity.getY()) { + accelerationY = -1; + } + else if(movementVector.getY() < 0 && movementVector.getY()/2.0 < currentVelocity.getY()) { + accelerationY = -1; + } + else if(movementVector.getY() < 0 && movementVector.getY()/2.0 >= currentVelocity.getY()) { + accelerationY = 1; + } + + + currentVelocity = new PositionVector(currentVelocity.getX() + accelerationX, currentVelocity.getY() + accelerationY); + currentPosition = new PositionVector(currentPosition.getX() + currentVelocity.getX(), currentPosition.getY() + currentVelocity.getY()); + + PositionVector acceleration = new PositionVector(accelerationX, accelerationY); + Direction[] directions = Direction.values(); + for (Direction direction : directions) { + if (direction.vector.equals(acceleration)) { + return direction; + } + } + return null; + } } From c59e4a30d87bc869a95787a2835c0b6fa7c64149 Mon Sep 17 00:00:00 2001 From: romanschenk37 Date: Tue, 22 Mar 2022 18:31:15 +0100 Subject: [PATCH 38/45] Update MoveListStrategy.java --- .../java/ch/zhaw/pm2/racetrack/strategy/MoveListStrategy.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 774b323..3de3f23 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/strategy/MoveListStrategy.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/strategy/MoveListStrategy.java @@ -20,7 +20,7 @@ public class MoveListStrategy implements MoveStrategy { readFile(new File(path)); } - protected void readFile(File trackFile) throws FileNotFoundException { + private void readFile(File trackFile) throws FileNotFoundException { Scanner scanner = new Scanner(new FileInputStream(trackFile), "UTF-8"); Direction[] directions = Direction.values(); while (scanner.hasNextLine()) { From 8877f1476f826cf5f4b11b6aefeeeb9b354b4524 Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Tue, 22 Mar 2022 18:47:14 +0100 Subject: [PATCH 39/45] GameTest --- .../java/ch/zhaw/pm2/racetrack/GameTest.java | 129 ++++++++++++++++-- 1 file changed, 114 insertions(+), 15 deletions(-) diff --git a/src/test/java/ch/zhaw/pm2/racetrack/GameTest.java b/src/test/java/ch/zhaw/pm2/racetrack/GameTest.java index 03cb0f3..ebfb473 100644 --- a/src/test/java/ch/zhaw/pm2/racetrack/GameTest.java +++ b/src/test/java/ch/zhaw/pm2/racetrack/GameTest.java @@ -1,11 +1,14 @@ package ch.zhaw.pm2.racetrack; import ch.zhaw.pm2.racetrack.strategy.UserMoveStrategy; +import org.junit.Before; import org.junit.jupiter.api.*; import java.io.File; +import java.util.List; import static ch.zhaw.pm2.racetrack.Game.NO_WINNER; +import static ch.zhaw.pm2.racetrack.PositionVector.Direction.*; class GameTest { private UserInterface userInterface; @@ -20,40 +23,40 @@ class GameTest { 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))); + 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()); + Assertions.assertEquals(0, game.getCurrentCarIndex()); game.switchToNextActiveCar(); - Assertions.assertEquals(1,game.getCurrentCarIndex()); + Assertions.assertEquals(1, game.getCurrentCarIndex()); } @Test void getCarId() { - Assertions.assertEquals('a',game.getCarId(0)); - Assertions.assertEquals('b',game.getCarId(1)); + 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)); + 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)); + 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()); + Assertions.assertEquals(NO_WINNER, game.getWinner()); } @Test @@ -75,16 +78,16 @@ class GameTest { 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))); + 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)); + game.doCarTurn(RIGHT); + Assertions.assertEquals(new PositionVector(1, 0), game.getCarVelocity(0)); } catch (PositionVectorNotValid positionVectorNotValid) { positionVectorNotValid.printStackTrace(); } @@ -99,6 +102,102 @@ class GameTest { positionVectorNotValid.printStackTrace(); } } + } + + @Nested + @DisplayName("Playtrough") + class Play { + private Game game; + + @Test + void winner() { + game = new Game(new interFace("Test")); + try { + game.initPhase(); + Assertions.assertEquals('a',game.gamePhase()); + } catch (InvalidTrackFormatException | PositionVectorNotValid e) { + e.printStackTrace(); + } + } + + + + } + + private class interFace extends UserInterface { + + private static PositionVector.Direction[] directions = {RIGHT, + RIGHT, + RIGHT, + NONE, + NONE, + NONE, + NONE, + UP, + LEFT, + LEFT, + LEFT, + LEFT, + UP_LEFT, + NONE, + RIGHT, + RIGHT, + RIGHT, + NONE, + LEFT, + DOWN_LEFT, + DOWN_LEFT, + LEFT, + LEFT, + NONE, + RIGHT, + NONE, + DOWN, + DOWN, + RIGHT, + NONE, + RIGHT, + DOWN, + NONE, + UP_RIGHT, + RIGHT, + UP_RIGHT, + UP_RIGHT, + RIGHT, + RIGHT}; + + private static int pointer = -1; + + public interFace(String welcometxt) { + super(welcometxt); + } + + @Override + public int selectOption(String text, List options) { + if (text.equals("Select Track file")) { + return 1; + } else if (text.contains("Select Strategy for Car")) { + return 2; + } + return 0; + } + + public void printInformation(String text) { + } + + public void printTrack(Track track) { + } + + public void quit(String text) { + } + + public PositionVector.Direction selectDirection(int playingCarIndex, char playingCarID) { + pointer += 1; + if(pointer < directions.length) { + return directions[pointer]; + } + return NONE; + } } } From dc34d984f8212477bd53da9c532c41baea3f07ba Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Tue, 22 Mar 2022 19:04:26 +0100 Subject: [PATCH 40/45] -CarTest added winpoint test -added Klassendiagramm -create Constructor InvalidTrackFormatException --- Klassendiagramm.drawio | 1 + .../InvalidTrackFormatException.java | 3 ++ .../java/ch/zhaw/pm2/racetrack/CarTest.java | 37 ++++++++++++++++++- 3 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 Klassendiagramm.drawio diff --git a/Klassendiagramm.drawio b/Klassendiagramm.drawio new file mode 100644 index 0000000..afde28c --- /dev/null +++ b/Klassendiagramm.drawio @@ -0,0 +1 @@ +7Vtbd6I6GP01PraLi7c+qr3MzOn0OGNPe16jRMgxEE+Itc6vny8QQExVaGVorWt1rZKPkED2zpedDTbsgf98w9Hc+84cTBuW4Tw37MuGZZnwB/9kZBVHOu1mHHA5cVSlLDAiv7AKGiq6IA4OcxUFY1SQeT44YUGAJyIXQ5yzZb7alNF8r3PkYi0wmiCqRx+JI7w42rU6WfwLJq6X9Gy2L+IzPkoqqycJPeSw5VrIvmrYA86YiI/85wGmcvCScXn8unqkt7P2zbcf4f/on/5f93cPZ3Fj12UuSR+B40C8uulfs+n1l4fmf//Oh9c/lsZNb/ztTF1iPCG6UOM1xDxkgXpisUqGMVwSn6IASv0pC8RInYFB6CNK3ACOJ3B3mEPgCXNBAIGeOiHYHKITj1DnFq3YQj5DKNBklpT6HuPkFzSLKJwyIQCnuVBkstq5GiN5JYQNiHIcQp1hMjBmGrpFoVB1JoxSNA/JOLphWcVH3CVBnwnB/KQhtggc7KhSinRUEJzNUu7I6wvCoWCTo4Gf18io4LnBzMeCr6CKOttVaKiZ1lLFZUZbs61i3jpl7a6aLmqquGnLaWc/YWqhwIUhSHuz7Hx3ZrNgf4BHrjtEAfYACdyXgxiukxAO1h40C0XULEFTU6PpHfKxRlIYZ7FGSIqnYisdwzmakMC9jepcNrPIT/WkMsTg2imNqOARx8FBRBWBBIrZJPkxZyQQ0VC0+vAHAzYwzluNFtzQAMpmVoY/WZ2LAQuAVYhE9MFA1SWWdH2BWDsn7X5irfKIlUV2nUg5SMviZ+lpxmOBJOPdwh9D0jg2JHfkE0/4VB1WhXfLqhlvW8P7ykdEXtZzHMjQ4QnwgwLe6dYMeFMDXEOYkkg9qNEwX1xZ98DvA5CyuQTve0mHyzNT44Stc8J+AX+KxpgOWUgEYbJ9Htfd4EVdWds0msVQ3bH8vwnUlp61F3zioVAmbhBdMxje6OgI53NloJp15+a2hupILBz5mAU1v3HS/JVpfrtbreg37Vf2V4Po72wn6rHKxoJZp12YZe9lA9DVsOzB3UvrxjK+w0pydEgeVA+Wxrv2DcDFSQ8eHNSiIr8qOWjqZuFXSPzGFaARrbSWcc9kOeBMtnZkM3pdAjWrwrhbd6I2davtBssVd4R9EiAuAb9HM6w7xB8d36ogrV/xJzewKxvjwOnJty1QGlMmxXYfQkqNm0ZcvCY0Wbn2yOX88ocdFydTB9MxW15lgX4UgBMJU0qL7ZDBrhTvhwy2Fy4usJmTd7sT2JeA5JgiQZ7yL512aPOhJG6myy0jr8stw843ET+kumr9LU/ZhuJR0Bo6mFOvW39DzqY4DJku108bzOo3mLb5yh1fx9hL4hc2mHazQHed97HBNHXXcoQoUhn3E65t6eT9OBtLUzcpNfQ++06jPKxF94+VbTV0k/JTKZYEsXctWTYXls4rJcvmCmV1Nu6lasmiG43b3lKeBEv1gqW1oSAKf5Zi7lhjtguWVivf3bv+DEa3UUeCY6y/uvksgqVTmF3vRrDo1uiAiOOTnAc1wMvjXLsDnnw1mpurkB1OQB8W6No/fbF0xxSEvVzELWPAnBPgBwa88Gcx1SGu+6kDGBNxhMZB3VjbrxRjh8Na9zM1kD+9v1Ae11ZBGVaVwWDp1t8DQOQc4wJdHYrt2jOx7v79vRDzhXQXevJV5a2cBydECyN6UXu+Lef8sXk01uW8vdQWNOt3+tTY7jX6Eiz3On0KZ0mlnIuibuiNpp+98b2ieWHlmyhq+m26PFpDW0w/raGzzRdOG+2w6TTElfiGlu4bGufnCfAZXSMvbi0R7Ek7Y+XIxWt4H01mbsTfAaOMw/mAZf6jMrut4jkhmV/q55bqthrpjxyL+3Rnb2RUQsw/BpdunhXHKtFPHxWsw8z+5JPcPw6d7plReAq5wKOt3ueaHN9EVVfBRQz8VKPvxrqUif5GYmgE2MaV4uvExWGYknyevmHiv4UrUMx+bB1Xz36ybl/9Bg== \ No newline at end of file diff --git a/src/main/java/ch/zhaw/pm2/racetrack/InvalidTrackFormatException.java b/src/main/java/ch/zhaw/pm2/racetrack/InvalidTrackFormatException.java index 3cdd04c..fa24c25 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/InvalidTrackFormatException.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/InvalidTrackFormatException.java @@ -7,4 +7,7 @@ public class InvalidTrackFormatException extends Exception { public InvalidTrackFormatException(String errorMessage) { super(errorMessage); } + public InvalidTrackFormatException() { + super(); + } } diff --git a/src/test/java/ch/zhaw/pm2/racetrack/CarTest.java b/src/test/java/ch/zhaw/pm2/racetrack/CarTest.java index 89b1965..6325c9b 100644 --- a/src/test/java/ch/zhaw/pm2/racetrack/CarTest.java +++ b/src/test/java/ch/zhaw/pm2/racetrack/CarTest.java @@ -2,9 +2,11 @@ package ch.zhaw.pm2.racetrack; import ch.zhaw.pm2.racetrack.strategy.*; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -176,7 +178,11 @@ class CarTest { car.setMoveStrategy(moveStrategy); assertEquals(moveStrategy, car.getMoveStrategy()); - moveStrategy = new MoveListStrategy(); + try { + moveStrategy = new MoveListStrategy(".\\moves\\challenge-car-a.txt"); + } catch (FileNotFoundException e) { + Assertions.fail(); + } car.setMoveStrategy(moveStrategy); assertEquals(moveStrategy, car.getMoveStrategy()); @@ -184,8 +190,35 @@ class CarTest { car.setMoveStrategy(moveStrategy); assertEquals(moveStrategy, car.getMoveStrategy()); - moveStrategy = new UserMoveStrategy(); + moveStrategy = new UserMoveStrategy(new UserInterface("Hello"),0,'a'); car.setMoveStrategy(moveStrategy); assertEquals(moveStrategy, car.getMoveStrategy()); } + + /** + * Test for get WinPoints + */ + @Test + void getWinPoints() { + assertEquals(0,car.getWinPoints()); + } + + /** + * Test for increase WinPoints + */ + @Test + void increaseWinPoints() { + car.increaseWinPoints(); + assertEquals(1,car.getWinPoints()); + } + + /** + * Test for deduct WinPoints + */ + @Test + void deductWinPoints() { + car.deductWinPoints(); + assertEquals(-1,car.getWinPoints()); + } + } From 45918124dfc3d8e0b2f234d0e5d6066df86a2291 Mon Sep 17 00:00:00 2001 From: romanschenk37 <84532681+romanschenk37@users.noreply.github.com> Date: Wed, 23 Mar 2022 08:48:46 +0100 Subject: [PATCH 41/45] refactoring and javadoc in PathFollowerMoveStrategy.javaa --- .../strategy/PathFollowerMoveStrategy.java | 96 ++++++++++++------- 1 file changed, 59 insertions(+), 37 deletions(-) diff --git a/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFollowerMoveStrategy.java b/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFollowerMoveStrategy.java index f85a0e9..74f55c0 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFollowerMoveStrategy.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFollowerMoveStrategy.java @@ -13,12 +13,30 @@ import java.util.Scanner; * The PathFollowerMoveStrategy class determines the next move based on a file containing points on a path. */ public class PathFollowerMoveStrategy implements MoveStrategy { + + /** + * The current Position of the car. + */ private PositionVector currentPosition; + /** + * The current Velocity of the car. + */ private PositionVector currentVelocity; + /** + * List of all points on the path. + */ private ArrayList pointList; + /** + * The index of the next point on the path. + */ private int pointer; - + /** + * Constructer to create a new PathFollowerMoveStrategy for a car. + * @param path The location where the file is saved + * @param startPosition The start position of the car + * @throws FileNotFoundException If the file with the given path does not exist. + */ public PathFollowerMoveStrategy(String path, PositionVector startPosition) throws FileNotFoundException { pointList = new ArrayList<>(); pointer = 0; @@ -27,6 +45,11 @@ public class PathFollowerMoveStrategy implements MoveStrategy { currentVelocity = new PositionVector(0, 0); } + /** + * Method to read the given File and add the points to the pointList + * @param trackFile the File Object which should be read + * @throws FileNotFoundException If the file with the given path does not exist. + */ public void readFile(File trackFile) throws FileNotFoundException { Scanner scanner = new Scanner(new FileInputStream(trackFile), "UTF-8"); while (scanner.hasNextLine()) { @@ -36,61 +59,60 @@ public class PathFollowerMoveStrategy implements MoveStrategy { } } + /** + * Method to select the direction for the next move. + * @return The direction for the next move. null if there are no points left in the list. + */ @Override public Direction nextMove() { + // if no more points in the list --> return null if (pointer >= pointList.size()) { return null; } - int accelerationX = 0; - int accelerationY = 0; + + // increase pointer variable if the next point is reached. if (pointList.get(pointer).equals(currentPosition)) { pointer ++; } - - if(currentVelocity.getX() > 0){ - accelerationX = -1; - } else if(currentVelocity.getX() < 0) { - accelerationX = 1; - } - if(currentVelocity.getY() > 0){ - accelerationY = -1; - } else if(currentVelocity.getY() < 0) { - accelerationY = 1; - } - - + // calculate Vector from current Position to next Point PositionVector movementVector = new PositionVector(pointList.get(pointer).getX() - currentPosition.getX(), pointList.get(pointer).getY() - currentPosition.getY()); - if(movementVector.getX() > 0 && movementVector.getX()/2.0 > currentVelocity.getX()) { - accelerationX = 1; + + // select acceleration for X + int accelerationX; + if((movementVector.getX() == 0 && currentVelocity.getX() > 0) || //reduce velocity to 0 if the destination coordinate is reached + (movementVector.getX() > 0 && movementVector.getX()/2.0 <= currentVelocity.getX()) || //increase velocity + (movementVector.getX() < 0 && movementVector.getX()/2.0 < currentVelocity.getX())){ //reduce velocity + accelerationX = -1; + } else if((movementVector.getX() == 0 && currentVelocity.getX() < 0) || //reduce velocity to 0 if the destination coordinate is reached + (movementVector.getX() > 0 && movementVector.getX()/2.0 > currentVelocity.getX()) || //increase velocity + (movementVector.getX() < 0 && movementVector.getX()/2.0 >= currentVelocity.getX())) { //reduce velocity + accelerationX = 1; } - else if(movementVector.getX() > 0 && movementVector.getX()/2.0 <= currentVelocity.getX()) { - accelerationX = -1; - } - else if(movementVector.getX() < 0 && movementVector.getX()/2.0 < currentVelocity.getX()) { - accelerationX = -1; - } - else if(movementVector.getX() < 0 && movementVector.getX()/2.0 >= currentVelocity.getX()) { - accelerationX = 1; + else { //no acceleration + accelerationX = 0; } - if(movementVector.getY() > 0 && movementVector.getY()/2.0 > currentVelocity.getY()) { - accelerationY = 1; + // select acceleration for Y + int accelerationY; + if((movementVector.getY() == 0 && currentVelocity.getY() > 0) || //reduce velocity to 0 if the destination coordinate is reached + (movementVector.getY() > 0 && movementVector.getY()/2.0 <= currentVelocity.getY()) || //increase velocity + (movementVector.getY() < 0 && movementVector.getY()/2.0 < currentVelocity.getY())){ //reduce velocity + accelerationY = -1; + } else if((movementVector.getY() == 0 && currentVelocity.getY() < 0) || //reduce velocity to 0 if the destination coordinate is reached + (movementVector.getY() > 0 && movementVector.getY()/2.0 > currentVelocity.getY()) || //increase velocity + (movementVector.getY() < 0 && movementVector.getY()/2.0 >= currentVelocity.getY())) { //reduce velocity + accelerationY = 1; } - else if(movementVector.getY() > 0 && movementVector.getY()/2.0 <= currentVelocity.getY()) { - accelerationY = -1; - } - else if(movementVector.getY() < 0 && movementVector.getY()/2.0 < currentVelocity.getY()) { - accelerationY = -1; - } - else if(movementVector.getY() < 0 && movementVector.getY()/2.0 >= currentVelocity.getY()) { - accelerationY = 1; + else { //no acceleration + accelerationY = 0; } - + //update current Velocity and current Position with the selected acceleration currentVelocity = new PositionVector(currentVelocity.getX() + accelerationX, currentVelocity.getY() + accelerationY); currentPosition = new PositionVector(currentPosition.getX() + currentVelocity.getX(), currentPosition.getY() + currentVelocity.getY()); + //Find Direction for acceleration PositionVector acceleration = new PositionVector(accelerationX, accelerationY); Direction[] directions = Direction.values(); for (Direction direction : directions) { From b1a4f3b3dbe5c8244e36653055d40155d1dfae4f Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Wed, 23 Mar 2022 08:53:40 +0100 Subject: [PATCH 42/45] -CarTest added winpoint test -added Klassendiagramm -create Constructor InvalidTrackFormatException --- src/main/java/ch/zhaw/pm2/racetrack/Game.java | 2 +- .../java/ch/zhaw/pm2/racetrack/GameTest.java | 164 +++++++++++------- 2 files changed, 98 insertions(+), 68 deletions(-) diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Game.java b/src/main/java/ch/zhaw/pm2/racetrack/Game.java index 413fef3..44bf317 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Game.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Game.java @@ -222,7 +222,7 @@ public class Game implements GameSpecification { direction = track.getCar(currentCarIndex).getMoveStrategy().nextMove(); if (direction == null) { track.getCar(currentCarIndex).setMoveStrategy(new DoNotMoveStrategy()); - direction = track.getCar(currentCarIndex).getMoveStrategy().nextMove(); //TODO: Entfernen? + track.getCar(currentCarIndex).getMoveStrategy().nextMove(); }else { doCarTurn(direction); } diff --git a/src/test/java/ch/zhaw/pm2/racetrack/GameTest.java b/src/test/java/ch/zhaw/pm2/racetrack/GameTest.java index ebfb473..0c1dea3 100644 --- a/src/test/java/ch/zhaw/pm2/racetrack/GameTest.java +++ b/src/test/java/ch/zhaw/pm2/racetrack/GameTest.java @@ -1,39 +1,51 @@ package ch.zhaw.pm2.racetrack; import ch.zhaw.pm2.racetrack.strategy.UserMoveStrategy; -import org.junit.Before; -import org.junit.jupiter.api.*; - +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Assertions; import java.io.File; import java.util.List; - import static ch.zhaw.pm2.racetrack.Game.NO_WINNER; import static ch.zhaw.pm2.racetrack.PositionVector.Direction.*; + +/** + * Test for Class Game + */ class GameTest { private UserInterface userInterface; private Game game; private Track track; + private String TRACK_FILE_PATH = ".\\tracks\\challenge.txt"; + private int CAR_INDEX_ONE = 0; + private int CAR_INDEX_TWO = 1; + + /** + * This nested Class tests if the game gets initiatet correctly + */ @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))); + track = game.selectTrack(new File(TRACK_FILE_PATH)); + game.selectMoveStrategy(track.getCar(CAR_INDEX_ONE), new UserMoveStrategy(new UserInterface("Testing"), CAR_INDEX_ONE, track.getCarId(CAR_INDEX_ONE))); + game.selectMoveStrategy(track.getCar(CAR_INDEX_TWO), new UserMoveStrategy(new UserInterface("Testing"), CAR_INDEX_TWO, track.getCarId(CAR_INDEX_TWO))); } - @Test void getCurrentCarIndex() { - Assertions.assertEquals(0, game.getCurrentCarIndex()); + Assertions.assertEquals(CAR_INDEX_ONE, game.getCurrentCarIndex()); game.switchToNextActiveCar(); - Assertions.assertEquals(1, game.getCurrentCarIndex()); + Assertions.assertEquals(CAR_INDEX_TWO, game.getCurrentCarIndex()); } @Test @@ -70,16 +82,20 @@ class GameTest { } } + /** + * This nested Class makes basic manipulation after Game init. + */ @Nested @DisplayName("Basic manipulation") - class 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))); + track = game.selectTrack(new File(TRACK_FILE_PATH)); + game.selectMoveStrategy(track.getCar(CAR_INDEX_ONE), new UserMoveStrategy(new UserInterface("Testing"), CAR_INDEX_ONE, track.getCarId(CAR_INDEX_ONE))); + game.selectMoveStrategy(track.getCar(CAR_INDEX_TWO), new UserMoveStrategy(new UserInterface("Testing"), CAR_INDEX_TWO, track.getCarId(CAR_INDEX_TWO))); } @@ -104,6 +120,9 @@ class GameTest { } } + /** + * This nested Class tests a Playtrough. And implements a UserInterface interagtion to pretend a real player + */ @Nested @DisplayName("Playtrough") class Play { @@ -111,75 +130,86 @@ class GameTest { @Test void winner() { - game = new Game(new interFace("Test")); + game = new Game(new interFace("Test",new Integer[]{0,2,1},new PositionVector.Direction[]{RIGHT, + RIGHT, + RIGHT, + NONE, + NONE, + NONE, + NONE, + UP, + LEFT, + LEFT, + LEFT, + LEFT, + UP_LEFT, + NONE, + RIGHT, + RIGHT, + RIGHT, + NONE, + LEFT, + DOWN_LEFT, + DOWN_LEFT, + LEFT, + LEFT, + NONE, + RIGHT, + NONE, + DOWN, + DOWN, + RIGHT, + NONE, + RIGHT, + DOWN, + NONE, + UP_RIGHT, + RIGHT, + UP_RIGHT, + UP_RIGHT, + RIGHT, + RIGHT})); try { game.initPhase(); - Assertions.assertEquals('a',game.gamePhase()); + Assertions.assertEquals("a",game.gamePhase()); } catch (InvalidTrackFormatException | PositionVectorNotValid e) { e.printStackTrace(); } } - + @Test + void crashA() { + game = new Game(new interFace("Test",new Integer[]{0,2,2},new PositionVector.Direction[]{UP})); + try { + game.initPhase(); + Assertions.assertEquals("b",game.gamePhase()); + } catch (InvalidTrackFormatException | PositionVectorNotValid e) { + e.printStackTrace(); + } + } } private class interFace extends UserInterface { - private static PositionVector.Direction[] directions = {RIGHT, - RIGHT, - RIGHT, - NONE, - NONE, - NONE, - NONE, - UP, - LEFT, - LEFT, - LEFT, - LEFT, - UP_LEFT, - NONE, - RIGHT, - RIGHT, - RIGHT, - NONE, - LEFT, - DOWN_LEFT, - DOWN_LEFT, - LEFT, - LEFT, - NONE, - RIGHT, - NONE, - DOWN, - DOWN, - RIGHT, - NONE, - RIGHT, - DOWN, - NONE, - UP_RIGHT, - RIGHT, - UP_RIGHT, - UP_RIGHT, - RIGHT, - RIGHT}; + private final PositionVector.Direction[] directions; + private final Integer[] instructions; + private int pointerDir,pointerInstruction; - private static int pointer = -1; - public interFace(String welcometxt) { + public interFace(String welcometxt, Integer[] instructions, PositionVector.Direction[] directions) { super(welcometxt); + pointerDir = -1; + pointerInstruction = -1; + this.instructions = instructions; + this.directions = directions; } @Override public int selectOption(String text, List options) { - if (text.equals("Select Track file")) { - return 1; - } else if (text.contains("Select Strategy for Car")) { - return 2; - } - return 0; + pointerInstruction++; + return instructions[pointerInstruction]; + } public void printInformation(String text) { @@ -192,9 +222,9 @@ class GameTest { } public PositionVector.Direction selectDirection(int playingCarIndex, char playingCarID) { - pointer += 1; - if(pointer < directions.length) { - return directions[pointer]; + pointerDir += 1; + if(pointerDir < directions.length) { + return directions[pointerDir]; } return NONE; } From 6ac4f644561fd61f7e18ee2d03b36bb59c1e519b Mon Sep 17 00:00:00 2001 From: romanschenk37 <84532681+romanschenk37@users.noreply.github.com> Date: Wed, 23 Mar 2022 08:55:32 +0100 Subject: [PATCH 43/45] Javadoc in PathFollowerMoveStrategy.java --- .../zhaw/pm2/racetrack/strategy/PathFollowerMoveStrategy.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFollowerMoveStrategy.java b/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFollowerMoveStrategy.java index 74f55c0..807ddc8 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFollowerMoveStrategy.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/strategy/PathFollowerMoveStrategy.java @@ -32,7 +32,7 @@ public class PathFollowerMoveStrategy implements MoveStrategy { private int pointer; /** - * Constructer to create a new PathFollowerMoveStrategy for a car. + * Constructor to create a new PathFollowerMoveStrategy for a car. * @param path The location where the file is saved * @param startPosition The start position of the car * @throws FileNotFoundException If the file with the given path does not exist. From 826f67d125320c326cfa7d11c65a682dcad0836e Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Wed, 23 Mar 2022 09:10:51 +0100 Subject: [PATCH 44/45] gamePhase() prevent null direction --- src/main/java/ch/zhaw/pm2/racetrack/Game.java | 5 ++--- 1 file changed, 2 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 44bf317..fcfd270 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Game.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Game.java @@ -222,10 +222,9 @@ public class Game implements GameSpecification { direction = track.getCar(currentCarIndex).getMoveStrategy().nextMove(); if (direction == null) { track.getCar(currentCarIndex).setMoveStrategy(new DoNotMoveStrategy()); - track.getCar(currentCarIndex).getMoveStrategy().nextMove(); - }else { - doCarTurn(direction); + direction = track.getCar(currentCarIndex).getMoveStrategy().nextMove(); } + doCarTurn(direction); switchToNextActiveCar(); } userInterface.printTrack(track); From ded9023ff1d3c008d2874f48d7de426790c6aeb6 Mon Sep 17 00:00:00 2001 From: romanschenk37 <84532681+romanschenk37@users.noreply.github.com> Date: Wed, 23 Mar 2022 11:24:58 +0100 Subject: [PATCH 45/45] adapted CarTest.java for PathFolowerMoveStrategy.java --- src/test/java/ch/zhaw/pm2/racetrack/CarTest.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/test/java/ch/zhaw/pm2/racetrack/CarTest.java b/src/test/java/ch/zhaw/pm2/racetrack/CarTest.java index 6325c9b..ebac56c 100644 --- a/src/test/java/ch/zhaw/pm2/racetrack/CarTest.java +++ b/src/test/java/ch/zhaw/pm2/racetrack/CarTest.java @@ -186,7 +186,11 @@ class CarTest { car.setMoveStrategy(moveStrategy); assertEquals(moveStrategy, car.getMoveStrategy()); - moveStrategy = new PathFollowerMoveStrategy(); + try { + moveStrategy = new PathFollowerMoveStrategy(".\\follower\\challenge_points.txt", new PositionVector(0, 0)); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } car.setMoveStrategy(moveStrategy); assertEquals(moveStrategy, car.getMoveStrategy());