-
Notifications
You must be signed in to change notification settings - Fork 2
/
scripts.js
184 lines (163 loc) · 7.31 KB
/
scripts.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
// Initialize turnCount to 0
let turnCount = 0;
/* Want X's to have 1st turn of every game: */
function startGame() {
// Reset turnCount to 0
turnCount = 0;
// Loops through #square___, set innerText to ''
for(let i = 1; i <= 9; i += 1) {
clearBox(i);
// Set #refreshBtn 'visibility:' to 'hidden;'
document.getElementById('refreshBtn').style.visibility = 'hidden';
}
// Create a global variable on the document object, assign initial value of 'X'
document.turn = 'X';
// Create another global variable on the document object, assign initial value of 'null'
document.winner = null;
// Create another global variable on the document object, assign initial value of false
document.cat = false;
// Display, "X's get to start!", message in #turnMessage via setTurnMessage()
setTurnMessage(document.turn + '\'s start first!');
// Set document.turn 'color:' to '#6c757d;' (#6c757d = 'secondary' (light grey) in Bootstrap 4.0)
document.getElementById('turnMessage').style.color = '#6c757d';
}
/* Want function to change the text in #message: */
function setTurnMessage(message) {
// Find #message, set innerText to message
document.getElementById('turnMessage').innerText = message;
}
/* Want function to switch document.turn from "X's" to "O's": */
function switchTurn() {
// If checkWinner returns true
if (checkWinner(document.turn) && document.cat === false) {
// Sets value of document.winner to document.turn
document.winner = document.turn;
// Displays, "Congratulations ___'s, you win!", message in #turnMessage via setTurnMessage()
setTurnMessage('Congratulations ' + document.turn + '\'s, you win!');
// Set document.turn 'color:' to '#28a745;' (#28a745 = 'success' (green) in Bootstrap 4.0)
document.getElementById('turnMessage').style.color = '#28a745';
// Set #refreshBtn 'visibility:' to 'visible;'
document.getElementById('refreshBtn').style.visibility = 'visible';
// Else if nether X's/O's is winner
} else if (document.cat === true) {
// Display, "Cat! Play again?", message in #turnMessage via setTurnMessage()
setTurnMessage('Cat! Play again?');
// Set document.turn 'color:' to '#16a2b8;' (#16a2b8 = 'info' (yellow) in Bootstrap 4.0)
document.getElementById('turnMessage').style.color = '#16a2b8';
// Set #refreshBtn 'visibility:' to 'visible;'
document.getElementById('refreshBtn').style.visibility = 'visible';
}
// Else if document.turn is 'X'
else if (document.turn == 'X') {
// Set document.turn to 'O'
document.turn = 'O';
// Display, "It is O's turn.", message in #turnMessage via setTurnMessage()
setTurnMessage('It is ' + document.turn + '\'s turn');
// Set document.turn 'color:' to '#0586ff;' (#0586ff = 'primary' (blue) in Bootstrap 4.0)
document.getElementById('turnMessage').style.color = '#0586ff';
// Else...
} else {
// Set document.turn to 'X'
document.turn = 'X';
// Display, "It's X's turn.", message in #turnMessage via setTurnMessage()
setTurnMessage('It is ' + document.turn + '\'s turn.');
// Set document.turn 'color:' to '#de3241;' (#de3241 = 'danger' (red) in Bootstrap 4.0)
document.getElementById('turnMessage').style.color = '#de3241';
}
}
/* Want browser to handle click events, and each .square to handle click events: */
function nextMove(square) {
// If X's/O's is winner
if (document.winner != null) {
// Display, "___'s already won!", message in #turnMessage via setTurnMessage()
setTurnMessage(document.turn + '\'s already won!');
// Set document.turn 'color:' to '#ffc904;' (#ffc904 = 'warning' (yellow) in Bootstrap 4.0)
document.getElementById('turnMessage').style.color = '#ffc904';
// Else if .square is empty
} else if (square.innerText == '') {
// Set value of square.innerText to document.turn
square.innerText = document.turn;
// Call switchTurn()
switchTurn();
// Else (.square isn't empty)
} else {
// Display, "Oops, pick a different square", message in #turnMessage via setTurnMessage()
setTurnMessage('Oops, pick a different square!');
// Set document.turn 'color:' to '#ffc904;' (#ffc904 = 'warning' (yellow) in Bootstrap 4.0)
document.getElementById('turnMessage').style.color = '#ffc904';
}
}
/*
*********************************************************************
* Instead of creating ONE long function, create MANY short functions!
*********************************************************************
*/
/*
* Want to check for winning condition:
* Calls checkRow(), passes in #square__ of each possible winning condition and returns result (true/false)
*/
function checkWinner(move) {
// Add 1 to turnCount each time checkWinner() is called
turnCount += 1;
// Initialize result to false
let result = false;
/*
* | |
* #square1 | #square2 | #square3
* | |
* ------------ -------------- -------------
* | |
* #square4 | #square5 | #square6
* | |
* ------------ -------------- -------------
* | |
* #square7 | #square8 | #square9
* | |
*/
// If...
if (
// ...#square1, #square2 and #square3 = 'X', 'X', 'X'/'O', 'O', 'O'
checkRow(1, 2, 3, move) ||
// ...#square4, #square5 and #square6 = 'X', 'X', 'X'/'O', 'O', 'O'
checkRow(4, 5, 6, move) ||
// ...#square7, #square8 and #square9 = 'X', 'X', 'X'/'O', 'O', 'O'
checkRow(7, 8, 9, move) ||
// ...#square1, #square4 and #square7 = 'X', 'X', 'X'/'O', 'O', 'O'
checkRow(1, 4, 7, move) ||
// ...#square2, #square5 and #square8 = 'X', 'X', 'X'/'O', 'O', 'O'
checkRow(2, 5, 8, move) ||
// ...#square3, #square6 and #square9 = 'X', 'X', 'X'/'O', 'O', 'O'
checkRow(3, 6, 9, move) ||
// ...#square1, #square5 and #square7 = 'X', 'X', 'X'/'O', 'O', 'O'
checkRow(1, 5, 9, move) ||
// ...#square3, #square5 and #square7 = 'X', 'X', 'X'/'O', 'O', 'O'
checkRow(3, 5, 7, move)) {
// Set result = true
result = true;
} else if(turnCount > 8 && result === false) {
document.cat = true;
}
return result;
}
/*
* Want function to check rows:
* Calls getBox(), compares value of each .square ('X' or 'O') of each row that is passed in via checkRow() and returns result (true/false)
*/
function checkRow(a, b, c, move) {
// Initialize result to false
let result = false;
// If getBox(a) and getBox(b) and getBox(c) are all equal to all "X's"/all "O's"
if (getBox(a) == move && getBox(b) == move && getBox(c) == move) {
// Set result = true
result = true;
}
return result;
}
/* Want function to return value of each #square___ (either "X"/"O"): */
function getBox(number) {
return document.getElementById('square' + number).innerText;
}
/* Want function to clear value of each #square___ (either "X"/"O"): */
function clearBox(number) {
document.getElementById('square' + number).innerText = '';
}