-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicTacToeGame.java
More file actions
133 lines (114 loc) · 3.51 KB
/
TicTacToeGame.java
File metadata and controls
133 lines (114 loc) · 3.51 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package gr.aueb.cf.ch10.projects;
import java.util.Scanner;
public class TicTacToeGame {
public static final int X = 1, O = -1;
public static final int EMPTY = 0;
public static int player = X;
public static int[][] board = new int[3][3];
public static int winner = 0;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
initBoard();
while (winner == 0) {
printBoard();
int row, col;
do {
System.out.println("Player " + player + ", enter your move (row column): ");
row = scan.nextInt() - 1;
col = scan.nextInt() - 1;
} while (!isValidMove(row, col));
board[row][col] = player;
player = -player;
winner = checkWinner();
}
if (winner == X) {
System.out.println("X wins!");
} else if (winner == O) {
System.out.println("O wins!");
} else {
System.out.println("Tie game.");
}
}
public static boolean isValidMove(int row, int col) {
if (row < 0 || row > 2 || col < 0 || col > 2) {
return false;
}
return board[row][col] == EMPTY;
}
public static int checkWinner() {
// check rows
for (int i = 0; i < 3; i++) {
if (board[i][0] + board[i][1] + board[i][2] == 3 * X) {
return X;
}
if (board[i][0] + board[i][1] + board[i][2] == 3 * O) {
return O;
}
}
// check columns
for (int j = 0; j < 3; j++) {
if (board[0][j] + board[1][j] + board[2][j] == 3 * X) {
return X;
}
if (board[0][j] + board[1][j] + board[2][j] == 3 * O) {
return O;
}
}
// check diagonals
if (board[0][0] + board[1][1] + board[2][2] == 3 * X ||
board[0][2] + board[1][1] + board[2][0] == 3 * X) {
return X;
}
if (board[0][0] + board[1][1] + board[2][2] == 3 * O ||
board[0][2] + board[1][1] + board[2][0] == 3 * O) {
return O;
}
// check if the board is full
if (isBoardFull()) {
return -1;
}
// game is not over yet
return 0;
}
public static boolean isBoardFull() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[i][j] == EMPTY) {
return false;
}
}
}
return true;
}
public static void initBoard() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
board[i][j] = EMPTY;
}
}
}
public static void printBoard() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
switch (board[i][j]) {
case X:
System.out.print("X");
break;
case O:
System.out.print("O");
break;
case EMPTY:
System.out.print(" ");
break;
}
if (j < 2) {
System.out.print("|");
}
}
if (i < 2) {
System.out.println("\n-+-+-");
}
}
System.out.println();
}
}