Options menü

This commit is contained in:
Asecave
2020-09-05 20:33:06 +02:00
parent 9626725931
commit d9466c3ea5
6 changed files with 181 additions and 22 deletions

View File

@@ -86,4 +86,8 @@ public class Board {
public void clear() {
symbols = new char[3][3];
}
public char[][] getBoard() {
return symbols;
}
}

View File

@@ -1,33 +1,51 @@
package main;
import java.util.Scanner;
public class MainMenu {
private boolean selecting;
public class MainMenu extends MenuScreen {
public MainMenu() {
@SuppressWarnings("resource")
Scanner s = new Scanner(System.in);
selecting = true;
while (selecting) {
printTitleScreen();
String in = s.next();
while (!in.equals("8") && !in.equals("2")) {
in = s.next();
}
}
tabs = new String[4];
tabs[0] = "Spielen";
tabs[1] = "Gegen KI Spielen";
tabs[2] = "Optionen";
tabs[3] = "Beenden";
init();
}
public void printTitleScreen() {
@Override
public void select(int tab) {
switch(tab) {
case 0:
new TicTacToe(TicTacToe.MODE_NORMAL);
break;
case 1:
new TicTacToe(TicTacToe.MODE_AI);
break;
case 2:
new Options();
break;
case 3:
stopLoop();
break;
}
cursorPos = 0;
}
@Override
public void printMenu() {
System.out.println(" _____ _ _____ _____ ");
System.out.println("|_ _|_|___ |_ _|___ ___ |_ _|___ ___ ");
System.out.println(" | | | | _| | | | .'| _| | | | . | -_|");
System.out.println(" |_| |_|___| |_| |__,|___| |_| |___|___|");
System.out.println();
System.out.println("(Use 8 & 2 to move)");
System.out.println("(8 = ^, 2 = v, 5 = enter)");
System.out.println();
for (int i = 0; i < tabs.length; i++) {
if (i == cursorPos) {
System.out.print(" > ");
} else {
System.out.print(" ");
}
System.out.println(tabs[i]);
}
}
}

View File

@@ -0,0 +1,42 @@
package main;
import java.util.Scanner;
public abstract class MenuScreen {
Scanner scanner = new Scanner(System.in);
int cursorPos;
String[] tabs;
private boolean runLoop;
public void init() {
runLoop = true;
while (runLoop) {
printMenu();
String in = scanner.next();
while (!in.equals("8") && !in.equals("2") && !in.equals("5")) {
in = scanner.next();
}
if (in.equals("8")) {
if (cursorPos > 0) {
cursorPos--;
}
} else if (in.equals("2")) {
if (cursorPos < tabs.length - 1) {
cursorPos++;
}
} else {
select(cursorPos);
}
}
}
public abstract void select(int tab);
public abstract void printMenu();
public void stopLoop() {
runLoop = false;
}
}

View File

@@ -0,0 +1,61 @@
package main;
public class Options extends MenuScreen {
public static char player1Symbol;
public static char player2Symbol;
public static int boardSize;
public Options() {
cursorPos = 0;
tabs = new String[4];
tabs[0] = "Player 1 symbol";
tabs[1] = "Player 2 symbol";
tabs[2] = "Spielfeldgröße";
tabs[3] = "Zurück";
player1Symbol = 'X';
player2Symbol = 'O';
boardSize = 4;
init();
}
@Override
public void select(int tab) {
switch (tab) {
case 0:
player1Symbol = requestSymbol();
break;
case 1:
player2Symbol = requestSymbol();
break;
case 2:
break;
case 3:
break;
}
}
private char requestSymbol() {
System.out.println("Bitte neues Symbol eingeben:");
return 0;
}
@Override
public void printMenu() {
System.out.println();
System.out.println("Options");
System.out.println();
for (int i = 0; i < tabs.length; i++) {
if (cursorPos == i) {
System.out.print(" > ");
} else {
System.out.print(" ");
}
System.out.println(tabs[i]);
}
}
}

View File

@@ -0,0 +1,20 @@
package main;
public class PlayerAI extends Player {
private int nextMove;
public PlayerAI(char symbol) {
super(symbol);
}
public void calculateNextMove(char[][] board) {
nextMove = 5;
}
@Override
public int makeTurn() {
return nextMove;
}
}

View File

@@ -8,12 +8,23 @@ public class TicTacToe {
private boolean running;
public TicTacToe() {
public static final int MODE_NORMAL = 0;
public static final int MODE_AI = 1;
public TicTacToe(int mode) {
board = new Board();
player1 = new Player('X');
player2 = new Player('O');
switch(mode) {
case MODE_NORMAL:
player1 = new Player('X');
player2 = new Player('O');
break;
case MODE_AI:
player1 = new Player('X');
player2 = new PlayerAI('O');
break;
}
turn = player1;
running = true;
@@ -25,6 +36,9 @@ public class TicTacToe {
public void loop() {
board.draw();
turn.printTurnMessage();
if (turn instanceof PlayerAI) {
((PlayerAI)turn).calculateNextMove(board.getBoard());
}
int field = turn.makeTurn();
while (!board.isFieldFree(field)) {
board.draw();