/
Game.java
54 lines (43 loc) · 1.66 KB
/
Game.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package tictactoe;
import java.util.InputMismatchException;
import java.util.Scanner;
class Game {
private BoardPrinter map = new BoardPrinter();
private PlayerChanger playerChanger = new PlayerChanger();
private ConditionsChecker conditionsChecker = new ConditionsChecker();
private Scanner input = new Scanner(System.in);
private char[][] board = new char[3][3];
private int row, col;
private boolean isTrue = true;
void StartGame() {
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board.length; j++) {
board[i][j] = '_';
}
}
map.printBoard(board);
while (isTrue) {
try {
if (conditionsChecker.checkIfWinner(board)) {
isTrue = false;
}
while (!conditionsChecker.checkIfWinner(board)) {
row = input.nextInt();
col = input.nextInt();
if (!conditionsChecker.checkIfOutOfBoard(row, col)) {
if (!conditionsChecker.checkIfOccupied(row, col, board)) {
board[row - 1][col - 1] = playerChanger.getTurn();
map.printBoard(board);
playerChanger.changePlayer();
}
}
}
} catch (InputMismatchException e) {
System.err.println("You have inputed something else\nTry again!");
input.nextLine();
}
}
playerChanger.changePlayer();
System.out.println("The winner is " + playerChanger.getTurn());
}
}