optimized Main Class. added Method quit to UserInterface.java

This commit is contained in:
romanschenk37 2022-03-18 21:48:27 +01:00
parent fec3aceaa6
commit fc78b10365
3 changed files with 37 additions and 13 deletions

View File

@ -190,7 +190,7 @@ public class Game implements GameSpecification {
} }
} }
public int gamePhase() throws PositionVectorNotValid { public String gamePhase() throws PositionVectorNotValid {
while (CarsMoving() && getWinner() == NO_WINNER) { while (CarsMoving() && getWinner() == NO_WINNER) {
userInterface.printTrack(track); userInterface.printTrack(track);
Direction direction = null; Direction direction = null;
@ -203,7 +203,11 @@ public class Game implements GameSpecification {
switchToNextActiveCar(); switchToNextActiveCar();
} }
userInterface.printTrack(track); userInterface.printTrack(track);
return getWinner(); int indexWinner = getWinner();
if(indexWinner == NO_WINNER){
return null;
}
return String.valueOf(track.getCar(indexWinner).getID());
} }
/** /**

View File

@ -7,23 +7,34 @@ import java.util.List;
public class Main { public class Main {
public static void main(String[] args) throws InvalidTrackFormatException, FileNotFoundException, PositionVectorNotValid { public static void main(String[] args) throws InvalidTrackFormatException, FileNotFoundException, PositionVectorNotValid {
boolean exit = false; UserInterface userInterface = new UserInterface("Hello and Welcome to Racetrack by Team02-\"AngryNerds\"");
UserInterface userInterface = new UserInterface("Hello and Welcome"); while (true) {
while (!exit) {
Game game = new Game(userInterface); Game game = new Game(userInterface);
int winner = 0; String winner;
if (game.initPhase()) { if (game.initPhase()) {
winner = game.gamePhase(); winner = game.gamePhase();
List<String> 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<String> optionsNewGame = new ArrayList<>(); else {
optionsNewGame.add("exit"); userInterface.quit("The initialisation of the game failed. Press enter to close the application.");
optionsNewGame.add("new game"); break;
int selectedOption = userInterface.selectOption("The Winner was Car : " + winner + "\nStart new Game?", optionsNewGame);
if(selectedOption == 0) {
exit = true;
} }
} }
userInterface.printInformation("Thank you and goodbye");
} }
} }

View File

@ -97,6 +97,15 @@ public class UserInterface {
textTerminal.println(track.toString()); 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();
}
} }