-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtic-tac-toe.java
211 lines (175 loc) · 5.38 KB
/
tic-tac-toe.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// This program plays the game of Tic-Tac-Toe with the user.
import java.util.Scanner;
public class TicTacToe
{
public static Scanner sc = new Scanner(System.in);
public static void main(String[] args)
{
final int SIZE = 3;
char[][] board = new char[SIZE][SIZE]; // game board
resetBoard(board); // initialize the board (with ' ' for all cells)
// First, welcome message and display the board.
System.out.println("===== WELCOME TO THE TIC-TAC-TOE GAME!! =====\n");
showBoard(board);
// Then ask the user which symbol (x or o) he/she wants to play.
System.out.print(" Which symbol do you want to play, \"x\" or \"o\"? ");
char userSymbol = sc.next().toLowerCase().charAt(0);
char compSymbol = (userSymbol == 'x') ? 'o' : 'x';
// Also ask whether or not the user wants to go first.
System.out.println();
System.out.print(" Do you want to go first (y/n)? ");
char ans = sc.next().toLowerCase().charAt(0);
int turn; // 0 -- the user, 1 -- the computer
int remainCount = SIZE * SIZE; // empty cell count
// THE VERY FIRST MOVE.
if (ans == 'y') {
turn = 0;
userPlay(board, userSymbol); // user puts his/her first tic
}
else {
turn = 1;
compPlay(board, compSymbol); // computer puts its first tic
}
// Show the board, and decrement the count of remaining cells.
showBoard(board);
remainCount--;
// Play the game until either one wins.
boolean done = false;
int winner = -1; // 0 -- the user, 1 -- the computer, -1 -- draw
while (!done && remainCount > 0) {
// If there is a winner at this time, set the winner and the done flag to true.
done = isGameWon(board, turn, userSymbol, compSymbol); // Did the turn won?
if (done)
winner = turn; // the one who made the last move won the game
else {
// No winner yet. Find the next turn and play.
turn = (turn + 1 ) % 2;
if (turn == 0)
userPlay(board, userSymbol);
else
compPlay(board, compSymbol);
// Show the board after one tic, and decrement the rem count.
showBoard(board);
remainCount--;
}
}
// Winner is found. Declare the winner.
if (winner == 0)
System.out.println("\n** YOU WON. CONGRATULATIONS!! **");
else if (winner == 1)
System.out.println("\n** YOU LOST.. Maybe next time :) **");
else
System.out.println("\n** DRAW... **");
}
public static void resetBoard(char[][] brd)
{
for (int i = 0; i < brd.length; i++)
for (int j = 0; j < brd[0].length; j++)
brd[i][j] = ' ';
}
public static void showBoard(char[][] brd)
{
int numRow = brd.length;
int numCol = brd[0].length;
System.out.println();
// First write the column header
System.out.print(" ");
for (int i = 0; i < numCol; i++)
System.out.print(i + " ");
System.out.print('\n');
System.out.println(); // blank line after the header
// The write the table
for (int i = 0; i < numRow; i++) {
System.out.print(i + " ");
for (int j = 0; j < numCol; j++) {
if (j != 0)
System.out.print("|");
System.out.print(" " + brd[i][j] + " ");
}
System.out.println();
if (i != (numRow - 1)) {
// separator line
System.out.print(" ");
for (int j = 0; j < numCol; j++) {
if (j != 0)
System.out.print("+");
System.out.print("---");
}
System.out.println();
}
}
System.out.println();
}
public static void userPlay(char[][] brd, char usym)
{
System.out.print("\nEnter the row and column indices: ");
int rowIndex = sc.nextInt();
int colIndex = sc.nextInt();
while (brd[rowIndex][colIndex] != ' ') {
System.out.print("\n!! The cell is already taken.\nEnter the row and column indices: ");
rowIndex = sc.nextInt();
colIndex = sc.nextInt();
}
brd[rowIndex][colIndex] = usym;
}
public static void compPlay(char[][] brd, char csym)
{
// Find the first empty cell and put a tic there.
for (int i = 0; i < brd.length; i++) {
for (int j = 0; j < brd[0].length; j++) {
if (brd[i][j] == ' ') { // empty cell
brd[i][j] = csym;
return;
}
}
}
}
public static boolean isGameWon(char[][] brd, int turn, char usym, char csym)
{
char sym;
if (turn == 0)
sym = usym;
else
sym = csym;
int i, j;
boolean win = false;
// Check win by a row
for (i = 0; i < brd.length && !win; i++) {
for (j = 0; j < brd[0].length; j++) {
if (brd[i][j] != sym)
break;
}
if (j == brd[0].length)
win = true;
}
// Check win by a column
for (j = 0; j < brd[0].length && !win; j++) {
for (i = 0; i < brd.length; i++) {
if (brd[i][j] != sym)
break;
}
if (i == brd.length)
win = true;
}
// Check win by a diagonal (1)
if (!win) {
for (i = 0; i < brd.length; i++) {
if (brd[i][i] != sym)
break;
}
if (i == brd.length)
win = true;
}
// Check win by a diagonal (2)
if (!win) {
for (i = 0; i < brd.length; i++) {
if (brd[i][brd.length - 1 - i] != sym)
break;
}
if (i == brd.length)
win = true;
}
// Finally return win
return win;
}
}