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,64 +56,28 @@ public class Game implements GameSpecification {
moveStrategies.add("Move List Strategy");
moveStrategies.add("Path Follow Move Strategy");
moveStrategies.add("Path Finder Move Strategy");
for (int i = 0; i < track.getCarCount(); i++) {
Car car = track.getCar(i);
for (int carIndex = 0; carIndex < track.getCarCount(); carIndex++) {
Car car = track.getCar(carIndex);
MoveStrategy moveStrategy = null;
while (moveStrategy == null) {
File selectedFile = null;
int moveStrategie = userInterface.selectOption(
"Select Strategy for Car " + i + " (" + track.getCarId(i) + ")", moveStrategies);
"Select Strategy for Car " + carIndex + " (" + track.getCarId(carIndex) + ")", moveStrategies);
switch (moveStrategie) {
case 0:
moveStrategy = new DoNotMoveStrategy();
break;
case 1:
moveStrategy = new UserMoveStrategy(userInterface, i, track.getCarId(i));
moveStrategy = new UserMoveStrategy(userInterface, carIndex, track.getCarId(carIndex));
break;
case 2:
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!");
e.printStackTrace();
} catch (InvalidFileFormatException e) {
userInterface.printInformation("Invalid Data in Move-List. Choose another Strategy and clean the File");
e.printStackTrace();
}
} else {
userInterface.printInformation("There is no Move-List implemented. Choose another Strategy!");
}
moveStrategy = getMoveListStrategy(selectedTrack, carIndex, 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!");
}
moveStrategy = getPathFollowerMoveStrategy(selectedTrack, carIndex, moveStrategy);
break;
case 4:
moveStrategy = new PathFinderMoveStrategy(track, i);
moveStrategy = new PathFinderMoveStrategy(track, carIndex);
break;
}
}
@ -126,6 +90,52 @@ public class Game implements GameSpecification {
}
}
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()) {
if (file.toString().equals(config.getMoveDirectory().toString() + "\\" + selectedTrack.getName().split("\\.")[0] + "-car-" + track.getCar(carIndex).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!");
e.printStackTrace();
} catch (InvalidFileFormatException e) {
userInterface.printInformation("Invalid Data in Move-List. Choose another Strategy and clean the File");
e.printStackTrace();
}
} else {
userInterface.printInformation("There is no Move-List implemented. Choose another Strategy!");
}
return moveStrategy;
}
/**
* The functionality was taken out of init to automate testing
*