From fc78b103650da7c884bdc06634527801174147bd Mon Sep 17 00:00:00 2001 From: romanschenk37 <84532681+romanschenk37@users.noreply.github.com> Date: Fri, 18 Mar 2022 21:48:27 +0100 Subject: [PATCH] optimized Main Class. added Method quit to UserInterface.java --- src/main/java/ch/zhaw/pm2/racetrack/Game.java | 8 +++-- src/main/java/ch/zhaw/pm2/racetrack/Main.java | 33 ++++++++++++------- .../ch/zhaw/pm2/racetrack/UserInterface.java | 9 +++++ 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Game.java b/src/main/java/ch/zhaw/pm2/racetrack/Game.java index 2ae1957..0b46109 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Game.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Game.java @@ -190,7 +190,7 @@ public class Game implements GameSpecification { } } - public int gamePhase() throws PositionVectorNotValid { + public String gamePhase() throws PositionVectorNotValid { while (CarsMoving() && getWinner() == NO_WINNER) { userInterface.printTrack(track); Direction direction = null; @@ -203,7 +203,11 @@ public class Game implements GameSpecification { switchToNextActiveCar(); } userInterface.printTrack(track); - return getWinner(); + int indexWinner = getWinner(); + if(indexWinner == NO_WINNER){ + return null; + } + return String.valueOf(track.getCar(indexWinner).getID()); } /** diff --git a/src/main/java/ch/zhaw/pm2/racetrack/Main.java b/src/main/java/ch/zhaw/pm2/racetrack/Main.java index 1a7c197..4013616 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/Main.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/Main.java @@ -7,23 +7,34 @@ import java.util.List; public class Main { public static void main(String[] args) throws InvalidTrackFormatException, FileNotFoundException, PositionVectorNotValid { - boolean exit = false; - UserInterface userInterface = new UserInterface("Hello and Welcome"); - while (!exit) { + UserInterface userInterface = new UserInterface("Hello and Welcome to Racetrack by Team02-\"AngryNerds\""); + while (true) { Game game = new Game(userInterface); - int winner = 0; + String winner; if (game.initPhase()) { winner = game.gamePhase(); + List 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 optionsNewGame = new ArrayList<>(); - optionsNewGame.add("exit"); - optionsNewGame.add("new game"); - int selectedOption = userInterface.selectOption("The Winner was Car : " + winner + "\nStart new Game?", optionsNewGame); - if(selectedOption == 0) { - exit = true; + else { + userInterface.quit("The initialisation of the game failed. Press enter to close the application."); + break; } } - userInterface.printInformation("Thank you and goodbye"); + } } diff --git a/src/main/java/ch/zhaw/pm2/racetrack/UserInterface.java b/src/main/java/ch/zhaw/pm2/racetrack/UserInterface.java index c19e7ca..96a8c04 100644 --- a/src/main/java/ch/zhaw/pm2/racetrack/UserInterface.java +++ b/src/main/java/ch/zhaw/pm2/racetrack/UserInterface.java @@ -97,6 +97,15 @@ public class UserInterface { 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(); + } + }