forked from benkei-kuruma/CodeNext-CSSI-2018-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtic-tac-toe.js
356 lines (306 loc) · 11.3 KB
/
tic-tac-toe.js
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// Author: FirstName lastName
/******************************************************************************
constant variables
These are global variables that should stay the same throughout the run of the
program. After being initialized, JavaScript won't let you change them ever
again. Great for when you want to "protect" certain variables from accidental
tampering!
*******************************************************************************/
const READLINE = require("readline-sync");
/******************************************************************************
global variables
board
String array. Represents the tic-tac-toe game board. A 3x3 grid. BLank squares
are represented by " - " strings.
turnsLeft
Number. The number of turns remaining. When this is 0, the game is declared a
draw. Since there are only 9 total moves players can make in one game, this
should be initialized to 9 at the start of every game.
activePlayer
Number. Represents the currently active player. Encoded as 0 for Player One and
1 for Player Two. Initialize this in setupGame() by "flipping a coin".
winner
String. Represents the current winner. By default this is "" or an empty
string. After each round of play, the game should check to see if someone has
won, in which case this variable will hold a value of "X" or "O".
quit
Boolean. Represents if the player has chosen to quit the game (true) or not
(false).
*******************************************************************************/
var board, turnsLeft, activePlayer, winner, quit;
/******************************************************************************
printGreeting()
Prints a simple greeting. Be as creative as you want here. Be sure to include
your name as the author!
*******************************************************************************/
function printGreeting() {
console.log();
console.log("--------------------------------------------------------------");
console.log(" Tic-Tac-Toe ");
console.log("--------------------------------------------------------------");
console.log("By: FirstName LastName");
console.log();
}
/******************************************************************************
resetBoard()
Fill the 2D board array with " - " strings (which represent empty spaces).
*******************************************************************************/
function resetBoard() {
for(var i = 0; i < board.length; i++) {
for(var j = 0; j < board.length; j++) {
board[i][j] = " - ";
}
}
}
/******************************************************************************
setupGame()
Initialize global variables as follows:
-initialize board as a 2D array of undefined values, with 3 rows and 3 columns.
-call resetBoard() to fill the board with " - " (which represent empty spaces).
-set activePlayer to either 0 or 1 ("coin flip"): Math.floor(Math.random() * 2)
-set winner to an empty string ""
-set quit to false
*******************************************************************************/
function setupGame() {
board = [
[undefined, undefined, undefined],
[undefined, undefined, undefined],
[undefined, undefined, undefined]
];
resetBoard();
turnsLeft = 9;
activePlayer = Math.floor(Math.random() * 2);
winner = "";
quit = false;
}
/******************************************************************************
printBoard()
Print the tic-tac-toe board to the terminal so the player can see it.
*******************************************************************************/
function printBoard() {
console.log(" " + " 0 " + " 1 " + " 2 ");
for(var i = 0; i < board.length; i++) {
var currentRow = i + " ";
for(var j = 0; j < board[i].length; j++) {
currentRow += board[i][j];
}
console.log(currentRow);
}
}
/******************************************************************************
checkRows()
Check each individual row for a win. If "X" has won, return "X". If "O" has
won, return "O". If no one has won, return an empty string "".
*******************************************************************************/
function checkRows() {
for(var i = 0; i < board.length; i++) {
var xCount = 0;
var oCount = 0;
for(var j = 0; j < board[i].length; j++) {
if(board[i][j] === " X ") {
xCount++;
} else if(board[i][j] === " O ") {
oCount++;
}
}
if(xCount === 3) {
return "X";
} else if(oCount === 3) {
return "O";
}
}
return "";
}
/******************************************************************************
checkColumns()
Check each individual column for a win. If "X" has won, return "X". If "O" has
won, return "O". If no one has won, return an empty string "".
*******************************************************************************/
function checkColumns() {
for(var i = 0; i < board.length; i++) {
var xCount = 0;
var oCount = 0;
for(var j = 0; j < board[i].length; j++) {
if(board[j][i] === " X ") {
xCount++;
} else if(board[j][i] === " O ") {
oCount++;
}
}
if(xCount === 3) {
return "X";
} else if(oCount === 3) {
return "O";
}
}
return "";
}
/******************************************************************************
checkLeftDiag()
Check from the top left square to the bottom right square for a win. If "X"
has won, return "X". If "O" has won, return "O". If no one has won, return an
empty string "".
*******************************************************************************/
function checkLeftDiag() {
var xCount = 0;
var oCount = 0;
for(var i = 0; i < board.length; i++) {
if(board[i][i] === " X ") {
xCount++;
} else if(board[i][i] === " O ") {
oCount++;
}
}
if(xCount === 3) {
return "X";
} else if(oCount === 3) {
return "O";
}
return "";
}
/******************************************************************************
checkRightDiag()
Check from the top right square to the bottom left square for a win. If "X"
has won, return "X". If "O" has won, return "O". If no one has won, return an
empty string "".
*******************************************************************************/
function checkRightDiag() {
var xCount = 0;
var oCount = 0;
for(var i = board.length - 1; i >= 0; i--) {
if(board[i][2 - i] === " X ") {
xCount++;
} else if(board[i][2 - i] === " O ") {
oCount++;
}
}
if(xCount === 3) {
return "X";
} else if(oCount === 3) {
return "O";
}
return "";
}
/******************************************************************************
checkWon()
Call all four "check" functions to determine if a player has won or if the
game should keep going. If "X" has won, return "X". If "O" has won, return
"O". If no one has won, return an empty string "".
*******************************************************************************/
function checkWon() {
var rowWon = checkRows(board);
var colWon = checkColumns(board);
var leftDiagWon = checkLeftDiag(board);
var rightDiagWon = checkRightDiag(board);
if(rowWon != "") {
return rowWon;
} else if(colWon != "") {
return colWon;
} else if(leftDiagWon != "") {
return leftDiagWon;
} else if(rightDiagWon != "") {
return rightDiagWon;
} else {
return "";
}
}
/******************************************************************************
chooseRowColumn()
Ask the currently active player to choose a row and then a column. Validate
their input to make sure they aren't entering bad inputs (only 0, 1, or 2
should be accepted). If a player chooses a square that is not blank (" - "),
they should be made to choose again, until they choose a blank square.
After the player chooses a valid square, set it to " X " or " O " accordingly
(mind the spaces!). Then switch the active player to the other player.
*******************************************************************************/
function chooseRowColumn() {
var playerName = "X";
if(activePlayer === 1) {
playerName = "O";
}
var validSelection = false;
while(!validSelection) {
var row = READLINE.question(playerName + ", choose a row: ");
while(!(row >= 0 && row <= 2)) {
console.log("Please enter 0, 1, or 2.");
row = READLINE.question(playerName + ", choose a row: ");
}
var column = READLINE.question(playerName + ", choose a column: ");
while(!(column >= 0 && column <= 2)) {
console.log("Please enter 0, 1, or 2.");
column = READLINE.question(playerName + ", choose a column: ");
}
if(board[row][column] !== " - ") {
console.log("That square is already taken! Try again!");
} else {
board[row][column] = " " + playerName + " ";
validSelection = true;
}
}
if(activePlayer === 1) {
activePlayer = 0;
} else {
activePlayer = 1;
}
}
/******************************************************************************
playGame()
While turnsleft is greater than 0 and the winner variable is set to an
empty string, do the following in an endless loop:
1) Print the board.
2) If the winner is "X", print "X wins!"
3) Otherwise, if the winner is "O", print "O wins!".
4) Otherwise, no one has won, so call chooseRowColumn() and decrement
turnsLeft.
Outside of the loop, check if turnsLeft is 0. If so, print the board, then
print "DRAW!".
Finally, the game is over at this point. So ask players if they would like to
play again (just as you did in Nim). If they answer anything other than "yes"
or "y" (or whatever else you want), set quit to true.
*******************************************************************************/
function playGame() {
while(turnsLeft > 0 && winner === "") {
printBoard();
console.log();
winner = checkWon();
if(winner === "X") {
console.log("X wins!");
} else if(winner === "O"){
console.log("O wins!");
} else {
chooseRowColumn();
turnsLeft--;
console.log();
}
}
if(turnsLeft === 0){
printBoard();
console.log();
console.log("DRAW!");
}
var keepPlaying = READLINE.question("Play again? (yes or no): ");
if(keepPlaying != "yes" && keepPlaying != "y") {
quit = true;
}
console.log();
}
/******************************************************************************
run()
The "mother function" of the program. Runs the game by doing the following:
1) Print a greeting.
2) While quit is set to false, do the the following:
A) Setup the game.
B) Play the game.
3) Outside the loop above, print a "Goodbye" message.
*******************************************************************************/
function run() {
printGreeting();
while(!quit) {
setupGame();
playGame();
}
console.log("Goodbye!");
}
// Run the program!
run();
READLINE.question("Press Enter key to exit.");