Code Cleanup

This commit is contained in:
romanschenk37
2022-03-25 20:57:07 +01:00
parent a9366de7bc
commit 29b9daadd4
5 changed files with 50 additions and 29 deletions
+36 -19
View File
@@ -20,11 +20,12 @@ public class Game implements GameSpecification {
public static final int NO_WINNER = -1;
private Track track;
private int currentCarIndex;
private final Config config;
private final UserInterface userInterface;
UserInterface userInterface;
public Game(UserInterface userInterface) {
public Game(UserInterface userInterface, Config config) {
this.userInterface = userInterface;
this.config = config;
}
/**
@@ -32,15 +33,13 @@ public class Game implements GameSpecification {
* @return true if the initialization is completed. Returns false if there is an error.
*/
public boolean initPhase() {
File folder = new File("tracks");
File[] listOfFiles = folder.listFiles();
if (listOfFiles.length > 0) {
if (config.getTrackDirectory().listFiles().length > 0) {
List<String> tracks = new ArrayList<>();
for (File file : listOfFiles) {
tracks.add(file.getName());
for (String file : config.getTrackDirectory().list()) {
tracks.add(file);
}
File selectedTrack = listOfFiles[userInterface.selectOption("Select Track file", tracks)];
File selectedTrack = config.getTrackDirectory().listFiles()[userInterface.selectOption("Select Track file", tracks)];
try {
selectTrack(selectedTrack);
} catch (FileNotFoundException e) {
@@ -60,7 +59,7 @@ public class Game implements GameSpecification {
Car car = track.getCar(i);
MoveStrategy moveStrategy = null;
while (moveStrategy == null) {
String filePath;
File selectedFile = null;
int moveStrategie = userInterface.selectOption(
"Select Strategy for Car " + i + " (" + track.getCarId(i) + ")", moveStrategies);
switch (moveStrategie) {
@@ -71,20 +70,38 @@ public class Game implements GameSpecification {
moveStrategy = new UserMoveStrategy(userInterface, i, track.getCarId(i));
break;
case 2:
filePath = ".\\moves\\" + selectedTrack.getName().split("\\.")[0] + "-car-" + track.getCar(i).getID() + ".txt";
try {
moveStrategy = new MoveListStrategy(filePath);
} catch (FileNotFoundException e) {
for(File file : config.getMoveDirectory().listFiles()){
if(file.toString().equals(config.getMoveDirectory().toString() + "\\" + selectedTrack.getName().split("\\.")[0] + "-car-" + track.getCar(i).getID() + ".txt")){
selectedFile = file;
}
}
if(selectedFile != null){
try {
moveStrategy = new MoveListStrategy(selectedFile);
} catch (FileNotFoundException e) {
userInterface.printInformation("There is no Move-List implemented. Choose another Strategy!");
}
} else{
userInterface.printInformation("There is no Move-List implemented. Choose another Strategy!");
}
break;
case 3:
filePath = ".\\follower\\" + selectedTrack.getName().split("\\.")[0] + "_points.txt";
try {
moveStrategy = new PathFollowerMoveStrategy(filePath, track.getCarPos(i));
} catch (FileNotFoundException e) {
for(File file : config.getFollowerDirectory().listFiles()){
if(file.toString().equals(config.getFollowerDirectory().toString() + "\\" + selectedTrack.getName().split("\\.")[0] + "_points.txt")){
selectedFile = file;
}
}
if(selectedFile != null){
try {
moveStrategy = new PathFollowerMoveStrategy(selectedFile, track.getCarPos(currentCarIndex));
} catch (FileNotFoundException e) {
userInterface.printInformation("There is no Point-List implemented. Choose another Strategy!");
}
} else{
userInterface.printInformation("There is no Point-List implemented. Choose another Strategy!");
}
break;
case 4:
moveStrategy = new PathFinderMoveStrategy(track, i);
@@ -102,7 +119,7 @@ public class Game implements GameSpecification {
/**
* The functionality was taken out of init to automate testing
* @param selectedTrack
* @param selectedTrack the Track which was selected by user
*/
Track selectTrack(File selectedTrack) throws InvalidTrackFormatException,FileNotFoundException {
track = new Track(selectedTrack);
@@ -7,8 +7,9 @@ public class Main {
public static void main(String[] args) {
UserInterface userInterface = new UserInterface("Hello and Welcome to Racetrack by Team02-\"AngryNerds\"");
Config config = new Config();
while (true) {
Game game = new Game(userInterface);
Game game = new Game(userInterface, config);
String winner;
if (game.initPhase()) {
winner = game.gamePhase();