|
@ -1,13 +1,20 @@ |
|
|
package Application; |
|
|
package Application; |
|
|
|
|
|
|
|
|
|
|
|
import Game.Game; |
|
|
import Game.Tictactoe; |
|
|
import Game.Tictactoe; |
|
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
|
import java.util.Scanner; |
|
|
|
|
|
|
|
|
public class App { |
|
|
public class App { |
|
|
|
|
|
|
|
|
private boolean isRunning = false; |
|
|
private boolean isRunning = false; |
|
|
private Cli cli; |
|
|
private Cli cli; |
|
|
|
|
|
|
|
|
private Tictactoe ttt; |
|
|
|
|
|
|
|
|
private boolean inMenu = true; |
|
|
|
|
|
private MenuManager menuManager; |
|
|
|
|
|
|
|
|
|
|
|
private Game currentGame; |
|
|
|
|
|
|
|
|
public App(Cli cli) { |
|
|
public App(Cli cli) { |
|
|
this.cli = cli; |
|
|
this.cli = cli; |
|
@ -15,23 +22,33 @@ public class App { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private void init() { |
|
|
private void init() { |
|
|
ttt = new Tictactoe(); |
|
|
|
|
|
|
|
|
menuManager = initMenuManager(); |
|
|
|
|
|
|
|
|
|
|
|
cli.clearConsole(); |
|
|
cli.getPrintStream().println("Welcome to the Cli Arcade Service!"); |
|
|
cli.getPrintStream().println("Welcome to the Cli Arcade Service!"); |
|
|
cli.getPrintStream().println("Press 'q' at any time to stop the application"); |
|
|
cli.getPrintStream().println("Press 'q' at any time to stop the application"); |
|
|
ttt.print(cli); |
|
|
|
|
|
|
|
|
cli.getPrintStream().println("Select a item by typing the number next to it"); |
|
|
|
|
|
|
|
|
|
|
|
cli.getPrintStream().print(menuManager.getFormattedMenuList()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public void start() { |
|
|
public void start() { |
|
|
isRunning = true; |
|
|
isRunning = true; |
|
|
while (isRunning) { |
|
|
while (isRunning) { |
|
|
String input = cli.getScanner().next(); |
|
|
|
|
|
|
|
|
String input = cli.getScanner().nextLine(); |
|
|
if (input.equals("q")) { |
|
|
if (input.equals("q")) { |
|
|
stop(); |
|
|
stop(); |
|
|
return; |
|
|
return; |
|
|
} else { |
|
|
} else { |
|
|
ttt.update(input); |
|
|
|
|
|
|
|
|
if (inMenu) { |
|
|
|
|
|
cli.clearConsole(); |
|
|
|
|
|
cli.getPrintStream().println("Select a item by typing the number next to it"); |
|
|
|
|
|
selectMenuItem(input); |
|
|
|
|
|
} else { |
|
|
cli.clearConsole(); |
|
|
cli.clearConsole(); |
|
|
ttt.print(cli); |
|
|
|
|
|
|
|
|
currentGame.update(input); |
|
|
|
|
|
currentGame.print(cli); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
@ -44,4 +61,60 @@ public class App { |
|
|
public boolean isRunning() { |
|
|
public boolean isRunning() { |
|
|
return 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("Tic Toe")); |
|
|
|
|
|
|
|
|
|
|
|
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 (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; |
|
|
|
|
|
default: |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
cli.getPrintStream().print(menuManager.getFormattedMenuList()); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void setCurrentGame(Game game) { |
|
|
|
|
|
inMenu = false; |
|
|
|
|
|
currentGame = game; |
|
|
|
|
|
cli.clearConsole(); |
|
|
|
|
|
currentGame.print(cli); |
|
|
|
|
|
} |
|
|
} |
|
|
} |