Win condition and windialog done
This commit is contained in:
@@ -3,7 +3,7 @@ package main;
|
|||||||
public class Board {
|
public class Board {
|
||||||
|
|
||||||
private char[][] board;
|
private char[][] board;
|
||||||
private final int BOARD_SIZE = 8;
|
private final int BOARD_SIZE = 4;
|
||||||
private char[][] symbols;
|
private char[][] symbols;
|
||||||
|
|
||||||
public Board() {
|
public Board() {
|
||||||
@@ -43,4 +43,47 @@ public class Board {
|
|||||||
System.out.println();
|
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 {
|
public class Input {
|
||||||
|
|
||||||
private static Scanner s;
|
private Scanner s;
|
||||||
|
|
||||||
static {
|
public Input() {
|
||||||
s = new Scanner(System.in);
|
s = new Scanner(System.in);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int scanFieldInput() {
|
public int scanFieldInput() {
|
||||||
String input;
|
String input;
|
||||||
int intInput = 0;
|
int intInput = 0;
|
||||||
|
|
||||||
@@ -47,11 +47,21 @@ public class Input {
|
|||||||
return intInput;
|
return intInput;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean inputOnlyNumbers(String input) {
|
private boolean inputOnlyNumbers(String input) {
|
||||||
return input.matches("[0-9]+");
|
return input.matches("[0-9]+");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void close() {
|
public static boolean yesNoDialog() {
|
||||||
s.close();
|
@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 class Launcher {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
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 {
|
public class Player {
|
||||||
|
|
||||||
|
private static Input input;
|
||||||
|
|
||||||
private char symbol;
|
private char symbol;
|
||||||
|
|
||||||
|
static {
|
||||||
|
input = new Input();
|
||||||
|
}
|
||||||
|
|
||||||
public Player(char symbol) {
|
public Player(char symbol) {
|
||||||
this.symbol = symbol;
|
this.symbol = symbol;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void printTurnMessage() {
|
||||||
|
System.out.println(symbol + " ist am Zug!");
|
||||||
|
}
|
||||||
|
|
||||||
public int makeTurn() {
|
public int makeTurn() {
|
||||||
System.out.println(symbol + " ist am Zug!");
|
int in = input.scanFieldInput();
|
||||||
return Input.scanFieldInput();
|
return in;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public char getSymbol() {
|
public char getSymbol() {
|
||||||
|
|||||||
@@ -1,42 +1,58 @@
|
|||||||
package main;
|
package main;
|
||||||
|
|
||||||
public class TicTacToe {
|
public class TicTacToe {
|
||||||
|
|
||||||
Board board;
|
Board board;
|
||||||
Player player1, player2;
|
Player player1, player2;
|
||||||
Player turn;
|
Player turn;
|
||||||
|
|
||||||
private boolean running;
|
private boolean running;
|
||||||
|
|
||||||
public TicTacToe() {
|
public TicTacToe() {
|
||||||
|
|
||||||
board = new Board();
|
board = new Board();
|
||||||
|
|
||||||
player1 = new Player('X');
|
player1 = new Player('X');
|
||||||
player2 = new Player('O');
|
player2 = new Player('O');
|
||||||
turn = player1;
|
turn = player1;
|
||||||
|
|
||||||
running = true;
|
running = true;
|
||||||
while (running) {
|
while (running) {
|
||||||
loop();
|
loop();
|
||||||
}
|
}
|
||||||
close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void loop() {
|
public void loop() {
|
||||||
board.draw();
|
board.draw();
|
||||||
|
turn.printTurnMessage();
|
||||||
int field = turn.makeTurn();
|
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());
|
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)) {
|
if (turn.equals(player1)) {
|
||||||
turn = player2;
|
turn = player2;
|
||||||
}else {
|
} else {
|
||||||
turn = player1;
|
turn = player1;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void close() {
|
|
||||||
Input.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user