import java.util.Scanner; class MineTerminalGui implements MineGuiInterface { Scanner sc; MineController mc; boolean hasWon = false; public MineTerminalGui(MineController mc) { this.mc = mc; try { sc = new Scanner(System.in); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } @Override public void start() { boolean gotResponse = false; int rows = 0; int cols = 0; int bombs = 0; while (!gotResponse) { System.out.println("Skriv inn størrelse på spillbrettet, samt antall bomber: "); String[] response = sc.nextLine().split(" "); try { if (response.length != 3) throw new Exception(); rows = Integer.parseInt(response[0]); cols = Integer.parseInt(response[1]); bombs = Integer.parseInt(response[2]); if (rows <= 0 || cols <= 0 || bombs < 0) throw new Exception(); gotResponse = true; } catch (Exception e) { System.out.println("Ugyldig input!\n"); } } mc.createModel(rows, cols, bombs); gameLoop(); } @Override public void hasWon(boolean status) { hasWon = status; } public void gameLoop() { int row = 0; int col = 0; String mode = "M"; while (mc.isRunning) { mc.showGame(); System.out.println("Skriv inn koordinater etterfulgt av \"M\" for å (av)markere, eller \"O\" for å åpne rute, eller \"end\" for å avslutte: "); String[] response = sc.nextLine().split(" "); try { if (response.length == 0) throw new Exception(); if (response[0].toLowerCase().equals("end")) { System.out.println("Takk for nå!"); sc.close(); System.exit(0); } if (response.length != 3) throw new Exception(); if (!response[2].toLowerCase().equals("m") && !response[2].toLowerCase().equals("o")); row = Integer.parseInt(response[0]) - 1; col = Integer.parseInt(response[1]) - 1; if (!mc.checkCoords(row, col)) throw new Exception(); mode = response[2]; } catch (Exception e) { System.out.println("Ugyldig input!"); continue; } if (mode.toLowerCase().equals("m")) { mc.toggleSquare(row, col); } else { mc.openSquare(row, col); } } mc.showGame(); sc.close(); System.out.println((hasWon) ? "Gratulerer!" : "Game over."); } }