Win condition and windialog done
This commit is contained in:
@@ -3,7 +3,7 @@ package main;
|
||||
public class Board {
|
||||
|
||||
private char[][] board;
|
||||
private final int BOARD_SIZE = 8;
|
||||
private final int BOARD_SIZE = 4;
|
||||
private char[][] symbols;
|
||||
|
||||
public Board() {
|
||||
@@ -43,4 +43,47 @@ public class Board {
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isFieldFree(int field) {
|
||||
field--;
|
||||
int row = field % 3;
|
||||
int col = field / 3;
|
||||
return symbols[row][col] == 0;
|
||||
}
|
||||
|
||||
public char testWinner() {
|
||||
for (int x = 0; x < symbols.length - 1; x++) {
|
||||
if (symbols[x][0] == symbols[x][1] && symbols[x][0] == symbols[x][2] && symbols[x][0] != 0) {
|
||||
return symbols[x][0];
|
||||
}
|
||||
}
|
||||
for (int y = 0; y < symbols[0].length - 1; y++) {
|
||||
if (symbols[0][y] == symbols[1][y] && symbols[0][y] == symbols[2][y] && symbols[0][y] != 0) {
|
||||
return symbols[0][y];
|
||||
}
|
||||
}
|
||||
if (symbols[0][0] == symbols[1][1] && symbols[0][0] == symbols[2][2] && symbols[0][0] != 0) {
|
||||
return symbols[0][0];
|
||||
}
|
||||
if (symbols[0][2] == symbols[1][1] && symbols[0][2] == symbols[2][0] && symbols[0][2] != 0) {
|
||||
return symbols[0][2];
|
||||
}
|
||||
boolean full = true;
|
||||
fullTest: for (int x = 0; x < symbols.length; x++) {
|
||||
for (int y = 0; y < symbols[0].length; y++) {
|
||||
if (symbols[x][y] == 0) {
|
||||
full = false;
|
||||
break fullTest;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (full) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
symbols = new char[3][3];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,13 +4,13 @@ import java.util.Scanner;
|
||||
|
||||
public class Input {
|
||||
|
||||
private static Scanner s;
|
||||
private Scanner s;
|
||||
|
||||
static {
|
||||
public Input() {
|
||||
s = new Scanner(System.in);
|
||||
}
|
||||
|
||||
public static int scanFieldInput() {
|
||||
public int scanFieldInput() {
|
||||
String input;
|
||||
int intInput = 0;
|
||||
|
||||
@@ -47,11 +47,21 @@ public class Input {
|
||||
return intInput;
|
||||
}
|
||||
|
||||
private static boolean inputOnlyNumbers(String input) {
|
||||
private boolean inputOnlyNumbers(String input) {
|
||||
return input.matches("[0-9]+");
|
||||
}
|
||||
|
||||
public static void close() {
|
||||
s.close();
|
||||
public static boolean yesNoDialog() {
|
||||
@SuppressWarnings("resource")
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
String in = scanner.next().toLowerCase();
|
||||
while (!in.equals("y") && !in.equals("n")) {
|
||||
System.out.println("Bitte nur mit 'y' oder 'n' beantworten.");
|
||||
in = scanner.next().toLowerCase();
|
||||
}
|
||||
if (in.equals("y")) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,6 @@ package main;
|
||||
public class Launcher {
|
||||
|
||||
public static void main(String[] args) {
|
||||
new TicTacToe();
|
||||
new MainMenu();
|
||||
}
|
||||
}
|
||||
|
||||
5
Tic-Tac-Toe/src/main/MainMenu.java
Normal file
5
Tic-Tac-Toe/src/main/MainMenu.java
Normal file
@@ -0,0 +1,5 @@
|
||||
package main;
|
||||
|
||||
public class MainMenu {
|
||||
|
||||
}
|
||||
@@ -2,16 +2,26 @@ package main;
|
||||
|
||||
public class Player {
|
||||
|
||||
private static Input input;
|
||||
|
||||
private char symbol;
|
||||
|
||||
static {
|
||||
input = new Input();
|
||||
}
|
||||
|
||||
public Player(char symbol) {
|
||||
this.symbol = symbol;
|
||||
}
|
||||
|
||||
public void printTurnMessage() {
|
||||
System.out.println(symbol + " ist am Zug!");
|
||||
}
|
||||
|
||||
public int makeTurn() {
|
||||
System.out.println(symbol + " ist am Zug!");
|
||||
return Input.scanFieldInput();
|
||||
|
||||
int in = input.scanFieldInput();
|
||||
return in;
|
||||
|
||||
}
|
||||
|
||||
public char getSymbol() {
|
||||
|
||||
@@ -1,42 +1,58 @@
|
||||
package main;
|
||||
|
||||
public class TicTacToe {
|
||||
|
||||
|
||||
Board board;
|
||||
Player player1, player2;
|
||||
Player turn;
|
||||
|
||||
|
||||
private boolean running;
|
||||
|
||||
|
||||
public TicTacToe() {
|
||||
|
||||
|
||||
board = new Board();
|
||||
|
||||
|
||||
player1 = new Player('X');
|
||||
player2 = new Player('O');
|
||||
turn = player1;
|
||||
|
||||
|
||||
running = true;
|
||||
while (running) {
|
||||
loop();
|
||||
}
|
||||
close();
|
||||
}
|
||||
|
||||
|
||||
public void loop() {
|
||||
board.draw();
|
||||
turn.printTurnMessage();
|
||||
int field = turn.makeTurn();
|
||||
while (!board.isFieldFree(field)) {
|
||||
board.draw();
|
||||
System.out.println("Das Feld ist schon belegt!");
|
||||
field = turn.makeTurn();
|
||||
}
|
||||
board.place(field, turn.getSymbol());
|
||||
char winner = board.testWinner();
|
||||
if (winner > 1) {
|
||||
board.draw();
|
||||
System.out.println(winner + " hat gewonnen!");
|
||||
} else if (winner == 1) {
|
||||
board.draw();
|
||||
System.out.println("Unetschieden!");
|
||||
}
|
||||
if (winner != 0) {
|
||||
System.out.println("Noch eine Runde? [y/n]");
|
||||
if (Input.yesNoDialog()) {
|
||||
board.clear();
|
||||
}else {
|
||||
running = false;
|
||||
}
|
||||
}
|
||||
if (turn.equals(player1)) {
|
||||
turn = player2;
|
||||
}else {
|
||||
} else {
|
||||
turn = player1;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void close() {
|
||||
Input.close();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user