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 1/2] 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 2/2] 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)); }