tiicic
This commit is contained in:
46
Tic-Tac-Toe/src/main/Board.java
Normal file
46
Tic-Tac-Toe/src/main/Board.java
Normal file
@@ -0,0 +1,46 @@
|
||||
package main;
|
||||
|
||||
public class Board {
|
||||
|
||||
private char[][] board;
|
||||
private final int BOARD_SIZE = 8;
|
||||
private char[][] symbols;
|
||||
|
||||
public Board() {
|
||||
board = new char[BOARD_SIZE * 3 + 1][BOARD_SIZE * 3 + 1];
|
||||
for (int x = 0; x < board.length; x++) {
|
||||
for (int y = 0; y < board[0].length; y++) {
|
||||
if (x % BOARD_SIZE == 0) {
|
||||
board[x][y] = '-';
|
||||
}
|
||||
if (y % BOARD_SIZE == 0) {
|
||||
board[x][y] = '|';
|
||||
}
|
||||
if (x % BOARD_SIZE == 0 && y % BOARD_SIZE == 0) {
|
||||
board[x][y] = '+';
|
||||
}
|
||||
}
|
||||
}
|
||||
symbols = new char[3][3];
|
||||
}
|
||||
|
||||
public void place(int field, char playerSymbol) {
|
||||
field--;
|
||||
int row = field % 3;
|
||||
int col = field / 3;
|
||||
symbols[row][col] = playerSymbol;
|
||||
}
|
||||
|
||||
public void draw() {
|
||||
for (int x = 0; x < board.length; x++) {
|
||||
for (int y = 0; y < board[0].length; y++) {
|
||||
if (x % BOARD_SIZE == BOARD_SIZE / 2 && y % BOARD_SIZE == BOARD_SIZE / 2) {
|
||||
System.out.print(symbols[y / BOARD_SIZE][x / BOARD_SIZE]);
|
||||
} else {
|
||||
System.out.print(board[x][y]);
|
||||
}
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,17 +12,46 @@ public class Input {
|
||||
|
||||
public static int scanFieldInput() {
|
||||
String input;
|
||||
do {
|
||||
input = s.next();
|
||||
if (!input.contains("[0-9]+")) {
|
||||
System.out.println("Bitte eine Zahl eingeben!");
|
||||
}
|
||||
} while (!input.contains("[0-9]+"));
|
||||
return Integer.parseInt(input);
|
||||
int intInput = 0;
|
||||
|
||||
input = s.next();
|
||||
if (!inputOnlyNumbers(input)) {
|
||||
System.out.println("Bitte eine Zahl eingeben!");
|
||||
return scanFieldInput();
|
||||
}
|
||||
intInput = Integer.parseInt(input);
|
||||
if (intInput > 9 || intInput < 1) {
|
||||
System.out.println("Die Zahl muss zwischen 1 und 9 liegen!");
|
||||
return scanFieldInput();
|
||||
}
|
||||
switch (intInput) {
|
||||
case 1:
|
||||
intInput = 7;
|
||||
break;
|
||||
case 2:
|
||||
intInput = 8;
|
||||
break;
|
||||
case 3:
|
||||
intInput = 9;
|
||||
break;
|
||||
case 7:
|
||||
intInput = 1;
|
||||
break;
|
||||
case 8:
|
||||
intInput = 2;
|
||||
break;
|
||||
case 9:
|
||||
intInput = 3;
|
||||
break;
|
||||
}
|
||||
return intInput;
|
||||
}
|
||||
|
||||
private static boolean inputOnlyNumbers(String input) {
|
||||
return input.matches("[0-9]+");
|
||||
}
|
||||
|
||||
public static void close() {
|
||||
s.close();
|
||||
}
|
||||
//gesundheittttttt
|
||||
}
|
||||
|
||||
@@ -8,9 +8,14 @@ public class Player {
|
||||
this.symbol = symbol;
|
||||
}
|
||||
|
||||
public void makeTurn() {
|
||||
public int makeTurn() {
|
||||
System.out.println(symbol + " ist am Zug!");
|
||||
int in = Input.scanFieldInput();
|
||||
return Input.scanFieldInput();
|
||||
|
||||
}
|
||||
|
||||
public char getSymbol() {
|
||||
return symbol;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,12 +2,16 @@ 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;
|
||||
@@ -20,7 +24,9 @@ public class TicTacToe {
|
||||
}
|
||||
|
||||
public void loop() {
|
||||
turn.makeTurn();
|
||||
board.draw();
|
||||
int field = turn.makeTurn();
|
||||
board.place(field, turn.getSymbol());
|
||||
if (turn.equals(player1)) {
|
||||
turn = player2;
|
||||
}else {
|
||||
|
||||
Reference in New Issue
Block a user