-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
345 lines (316 loc) · 6.77 KB
/
app.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
let player = 'O';
let gameInProgress = false;
let cels = new Array(9);
let movesPlayed = 0;
let score = {X: 0, O: 0};
let vsComputer = true;
let computerDifficult = 'hard';
//handle start/game in progres/another round button
$('#btn').click(function() {
if(!gameInProgress){ //only do something if the game is not in progress
gameInProgress = true;
$('#btn').text('Game in Progress');
$('button').removeClass('clickable');
$('button').addClass('progress');
$('.cel').text('');
changePlayer();
}
});
//handle the clicks on the cels
$('.cel').click(function() {
if(gameInProgress){//only do something if the game is in progress
let id = parseInt($(this).attr("id")[3]) - 1;
if(!cels[id]){
play(id);
}
}
});
//change players turn
const changePlayer = () => {
if(gameInProgress){
player === 'X' ? player = 'O' : player = 'X';
$('#msg').text('Player ' + player + ' turn!');
}
if(player === 'O' && vsComputer && gameInProgress) {
computerPlays();
}
}
//verify all wining cases
const didWin = () => {
if( win(cels[0], cels[1], cels[2]) ||
win(cels[3], cels[4], cels[5]) ||
win(cels[6], cels[7], cels[8]) ||
win(cels[0], cels[3], cels[6]) ||
win(cels[1], cels[4], cels[7]) ||
win(cels[2], cels[5], cels[8]) ||
win(cels[0], cels[4], cels[8]) ||
win(cels[2], cels[4], cels[6])
){
return true;
}
return false;
}
//verify if the 3 cels passed exist and are the same value
const win = (x, y, z) => {
return (x && x === y && x === z) ? true : false;
}
//resets the board and updates the score msg;
const resetGame = () => {
cels = new Array(9);
movesPlayed = 0;
$('#btn').text('Another Round');
$('button').removeClass('progress');
$('button').addClass('clickable');
$('#scores').text('X: ' + score.X + ' vs O: ' + score.O);
}
//change PvC and PvP
$('.opt').click(function() {
if(!gameInProgress) {
$('.opt').removeClass('opt-selected');
$(this).addClass('opt-selected');
$(this).attr('id') === 'pvc' ? vsComputer = true : vsComputer = false;
resetScore();
}
});
//change Difficult
$('.difficult').click(function() {
if(!gameInProgress) {
computerDifficult = $(this).attr('id');
$('.difficult').removeClass('opt-selected');
$(this).addClass('opt-selected');
resetScore();
}
});
//reset button
$('#reset-score').click(function() {
if(!gameInProgress) resetScore();
})
//reset score
const resetScore = (change) => {
score.X = 0;
score.O = 0;
player = 'X';
$('.cel').text('');
$('#msg').text('Click Below to Start');
$('#scores').text('X: 0 vs O: 0');
$('#btn').text('Start Game');
}
//function to handle a move/play on the board
const play = (numCel) => {
movesPlayed++;
$('#cel' + (numCel+1)).text(player);
cels[numCel] = player;
gameInProgress = !didWin();
if(!gameInProgress){
$('#msg').text('Player ' + player + ' won the game!!!');
score[player] += 1;
resetGame();
} else {
if(movesPlayed === 9){
gameInProgress = false;
$('#msg').text('It is a Draw!!!');
resetGame();
} else {
changePlayer();
}
}
}
//computer makes a move on the board
const computerPlays = () => {
switch(computerDifficult) {
case 'easy':
easyPlay();
break;
case 'normal':
normalPlay();
break;
case 'hard':
hardPlay();
break;
default:
break;
}
}
//Easy mode AI
const easyPlay = () => {
let randomCel = Math.floor(9*Math.random());
cels[randomCel] ? easyPlay() : play(randomCel);
}
//Normal mode AI
const normalPlay = () => {
if(canWin('O') || canWin('X')){
return;
}
easyPlay();
}
//Hard mode AI
const hardPlay = () => {
switch(movesPlayed) {
case 0:
hard0();
break;
case 1:
hard1();
break;
case 2:
hard2();
break;
case 3:
hard3();
break;
case 4:
hard4();
break;
case 5:
hard5();
break;
case 6:
hard6();
break;
case 7:
hard7();
break;
case 8:
hard8();
break;
default:
easyPlay();
break;
}
}
/* ================================================= */
//0, 2, 4, 6, 8 means the Computer started playing
const hard0 = () => {
play(0);
}
const hard2 = () => {
if(cels[4] === 'X'){
play(8);
} else if(cels[1] === 'X' ||
cels[2] === 'X' ||
cels[7] === 'X' ||
cels[8] === 'X'){
play(6);
} else {
play(2);
}
}
const hard4 = () => {
if(canWin('O') || canWin('X')) return;
if(cels[8] === 'O'){
if(cels[2] === 'X'){
play(6);
} else if (cels[6] === 'X'){
play(2);
}
} else if(cels[6] === 'O'){
if(cels[1] === 'X' || cels[2] === 'X'){
play(8);
} else {
play(2);
}
} else {
if(cels[5] === 'X'){
play(6);
} else {
play(8);
}
}
}
const hard6 = () => {
if(canWin('O') || canWin('X')) return;
}
const hard8 = () => {
for(let i=0; i<cels.length; i++){
if(!cels[i]){
play(i);
return;
}
}
}
/* ================================================= */
//1, 3, 4, 7 means the Player started playing
const hard1 = () => {
if( cels[4] === 'X'){
play(0);
} else {
play(4);
}
}
const hard3 = () => {
if(canWin('X')) return;
if(cels[4] === 'X'){
play(2);
} else if(cels[0] === 'X'){//if player did NOT started in the center
if(cels[7] === 'X'){
play(3);
} else {
play(1);
}
} else if(cels[1] === 'X'){
if(cels[8] === 'X'){
play(2);
} else {
play(0);
}
} else if(cels[2] === 'X'){
if(cels[7] === 'X'){
play(3);
} else {
play(1);
}
} else if(cels[3] === 'X'){
if(cels[7] === 'X'){
play(6);
} else {
play(7);
}
} else if(cels[5] === 'X'){
if(cels[6] === 'X'){
play(7);
} else {
play(8);
}
}
}
const hard5 = () => {
if(canWin('O') || canWin('X')) return;
if(cels[4] === 'X' && cels[8] === 'X'){
if(cels[1] === 'X'){
play(6);
} else if(cels[3] === 'X'){
play(2);
}
} else if(cels[3] === 'O'){//if player did NOT started in the center
play(2);
} else if(cels[1] === 'O' && cels[3] === 'X'){
play(8);
} else if(!cels[3] && !cels[5]){
play(5);
} else if(!cels[0]){
play(0);
} else {
play(6);
}
}
const hard7 = () => {
if(canWin('O') || canWin('X')) return;
hard8();
}
/* ================================================= */
//verify if the computer can win the game
const canWin = (p) => {
let willWin;
for(let i=0; i<cels.length; i++){
if(!cels[i]){
cels[i] = p;
willWin = didWin();
cels[i] = undefined;
if(willWin) {
play(i);
return true;
}
}
}
return false;
}