You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
149 lines
3.9 KiB
149 lines
3.9 KiB
package Application;
|
|
|
|
import Game.Game;
|
|
import Game.Tictactoe;
|
|
import Game.Chess;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.Scanner;
|
|
|
|
public class App {
|
|
|
|
private boolean isRunning = false;
|
|
private Cli cli;
|
|
|
|
private boolean inMenu = true;
|
|
private MenuManager menuManager;
|
|
|
|
private Game currentGame;
|
|
|
|
public App(Cli cli) {
|
|
this.cli = cli;
|
|
init();
|
|
}
|
|
|
|
private void init() {
|
|
menuManager = initMenuManager();
|
|
|
|
goToMenu();
|
|
}
|
|
|
|
public void start() {
|
|
isRunning = true;
|
|
while (isRunning) {
|
|
String input = cli.getScanner().nextLine();
|
|
if (input.equals("exit")) {
|
|
stop();
|
|
return;
|
|
} else {
|
|
if (inMenu) {
|
|
cli.clearConsole();
|
|
selectMenuItem(input);
|
|
} else {
|
|
if (input.equals("q")) {
|
|
goToMenu();
|
|
continue;
|
|
}
|
|
cli.clearConsole();
|
|
currentGame.update(input);
|
|
currentGame.print(cli);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public void stop() {
|
|
isRunning = false;
|
|
cli.getPrintStream().println("Stopping application...");
|
|
}
|
|
|
|
public boolean isRunning() {
|
|
return isRunning;
|
|
}
|
|
|
|
public Game getCurrentGame() {
|
|
return this.currentGame;
|
|
}
|
|
|
|
private MenuManager initMenuManager() {
|
|
MenuManager mm = new MenuManager();
|
|
ArrayList<Menu> gameList = new ArrayList<>();
|
|
|
|
gameList.add(new Menu("Tic Tac Toe"));
|
|
gameList.add(new Menu("Chess"));
|
|
|
|
Menu gameMenu = new Menu("Games");
|
|
|
|
gameMenu.addMenu(new Menu("Back"));
|
|
for (Menu game : gameList) {
|
|
game.addMenu(new Menu("Back"));
|
|
gameMenu.addMenu(game);
|
|
}
|
|
mm.addMenu(gameMenu);
|
|
|
|
return mm;
|
|
}
|
|
|
|
protected void selectMenuItem(String input) {
|
|
Scanner scanner = new Scanner(input);
|
|
|
|
if (input.equals("q") && !menuManager.inRootMenu()) {
|
|
goToMenu();
|
|
}
|
|
|
|
if (scanner.hasNextInt()) {
|
|
int index = scanner.nextInt() - 1;
|
|
menuManager.select(index);
|
|
if (menuManager.getCurrentMenu() != null) {
|
|
switch (menuManager.getCurrentMenu().getName()) {
|
|
case "Back":
|
|
try {
|
|
menuManager.back();
|
|
menuManager.back();
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
break;
|
|
case "Tic Tac Toe":
|
|
setCurrentGame(new Tictactoe());
|
|
return;
|
|
case "Chess":
|
|
setCurrentGame(new Chess());
|
|
return;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
printMenu();
|
|
}
|
|
|
|
private void setCurrentGame(Game game) {
|
|
inMenu = false;
|
|
currentGame = game;
|
|
cli.clearConsole();
|
|
currentGame.print(cli);
|
|
}
|
|
|
|
private void goToMenu() {
|
|
inMenu = true;
|
|
currentGame = null;
|
|
while (!menuManager.inRootMenu()) {
|
|
try {
|
|
menuManager.back();
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
printMenu();
|
|
}
|
|
|
|
private void printMenu() {
|
|
cli.clearConsole();
|
|
cli.getPrintStream().println("Welcome to the Cli Arcade Service!");
|
|
cli.getPrintStream().println("Type 'exit' at any time to stop the application");
|
|
cli.getPrintStream().println("Select a item by typing the number next to it");
|
|
|
|
cli.getPrintStream().print(menuManager.getFormattedMenuList());
|
|
}
|
|
}
|