refactoring Game.java

This commit is contained in:
romanschenk37 2022-03-25 22:04:38 +01:00
parent 00cb917842
commit a77d02b440
1 changed files with 53 additions and 43 deletions

View File

@ -56,23 +56,67 @@ public class Game implements GameSpecification {
moveStrategies.add("Move List Strategy"); moveStrategies.add("Move List Strategy");
moveStrategies.add("Path Follow Move Strategy"); moveStrategies.add("Path Follow Move Strategy");
moveStrategies.add("Path Finder Move Strategy"); moveStrategies.add("Path Finder Move Strategy");
for (int i = 0; i < track.getCarCount(); i++) { for (int carIndex = 0; carIndex < track.getCarCount(); carIndex++) {
Car car = track.getCar(i); Car car = track.getCar(carIndex);
MoveStrategy moveStrategy = null; MoveStrategy moveStrategy = null;
while (moveStrategy == null) { while (moveStrategy == null) {
File selectedFile = null;
int moveStrategie = userInterface.selectOption( int moveStrategie = userInterface.selectOption(
"Select Strategy for Car " + i + " (" + track.getCarId(i) + ")", moveStrategies); "Select Strategy for Car " + carIndex + " (" + track.getCarId(carIndex) + ")", moveStrategies);
switch (moveStrategie) { switch (moveStrategie) {
case 0: case 0:
moveStrategy = new DoNotMoveStrategy(); moveStrategy = new DoNotMoveStrategy();
break; break;
case 1: case 1:
moveStrategy = new UserMoveStrategy(userInterface, i, track.getCarId(i)); moveStrategy = new UserMoveStrategy(userInterface, carIndex, track.getCarId(carIndex));
break; break;
case 2: case 2:
moveStrategy = getMoveListStrategy(selectedTrack, carIndex, moveStrategy);
break;
case 3:
moveStrategy = getPathFollowerMoveStrategy(selectedTrack, carIndex, moveStrategy);
break;
case 4:
moveStrategy = new PathFinderMoveStrategy(track, carIndex);
break;
}
}
selectMoveStrategy(car, moveStrategy);
}
return true;
} else {
userInterface.printInformation("No Trackfile found!");
return false;
}
}
private MoveStrategy getPathFollowerMoveStrategy(File selectedTrack, int carIndex, MoveStrategy moveStrategy) {
File selectedFile = null;
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(carIndex));
} catch (FileNotFoundException e) {
e.printStackTrace();
userInterface.printInformation("There is no Point-List implemented. Choose another Strategy!");
} catch (InvalidFileFormatException e) {
e.printStackTrace();
userInterface.printInformation("Invalid Point-List format. Change Strategy and clean Point-List");
}
} else {
userInterface.printInformation("There is no Point-List implemented. Choose another Strategy!");
}
return moveStrategy;
}
private MoveStrategy getMoveListStrategy(File selectedTrack, int carIndex, MoveStrategy moveStrategy) {
File selectedFile = null;
for (File file : config.getMoveDirectory().listFiles()) { for (File file : config.getMoveDirectory().listFiles()) {
if (file.toString().equals(config.getMoveDirectory().toString() + "\\" + selectedTrack.getName().split("\\.")[0] + "-car-" + track.getCar(i).getID() + ".txt")) { if (file.toString().equals(config.getMoveDirectory().toString() + "\\" + selectedTrack.getName().split("\\.")[0] + "-car-" + track.getCar(carIndex).getID() + ".txt")) {
selectedFile = file; selectedFile = file;
} }
} }
@ -89,41 +133,7 @@ public class Game implements GameSpecification {
} else { } else {
userInterface.printInformation("There is no Move-List implemented. Choose another Strategy!"); userInterface.printInformation("There is no Move-List implemented. Choose another Strategy!");
} }
return moveStrategy;
break;
case 3:
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(i));
} catch (FileNotFoundException e) {
e.printStackTrace();
userInterface.printInformation("There is no Point-List implemented. Choose another Strategy!");
} catch (InvalidFileFormatException e) {
e.printStackTrace();
userInterface.printInformation("Invalid Point-List format. Change Strategy and clean Point-List");
}
} else {
userInterface.printInformation("There is no Point-List implemented. Choose another Strategy!");
}
break;
case 4:
moveStrategy = new PathFinderMoveStrategy(track, i);
break;
}
}
selectMoveStrategy(car, moveStrategy);
}
return true;
} else {
userInterface.printInformation("No Trackfile found!");
return false;
}
} }
/** /**