Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
469 lines (455 loc) · 16.2 KB
/
Copy pathapp.js
File metadata and controls
469 lines (455 loc) · 16.2 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
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
//declare array to store every play data
var playsData = ['','','','','','','','',''];
var winnerIndex = [0,0,0];
var playerID = 0;
var gameOver =false;
var counter = 0;
var playerName = ['',''];
var winnerCounter = [0,0];
//get the button objects in dom
var $btn1 = $('#1');
var $btn2 = $('#2');
var $btn3 = $('#3');
var $btn4 = $('#4');
var $btn5 = $('#5');
var $btn6 = $('#6');
var $btn7 = $('#7');
var $btn8 = $('#8');
var $btn9 = $('#9');
//function to initialize the game
var initialGame = function(){
playsData = ['','','','','','','','',''];
playerID = 0;
gameOver = false;
counter = 0;
$('button').prop('disabled',false);
$('button').css('border-color','rgba(0,0,0,.2)');
//clear the buttons
for (var i = 1; i < 10; i++){
$("#"+i).html(' ');
}
//assign wining counts to two boxes
$("#p1Count").html(winnerCounter[0]);
$("#p1Count").css('font-size','30px');
$("#p2Count").html(winnerCounter[1]);
$("#p2Count").css('font-size','30px');
}
//load game initialization when the window loaded
$(window).load(function() {
initialGame();
$('.container button').prop('disabled',true);
});
//handle click event, change button status from blank to occupied
var playGame = function(){
var btnID = $(event.target).data('index');
$(event.target).prop('disabled',true);
//get the id of the button just clicked and extract the number of it
//var btnID = parseInt($(this).prop('id'),10);
//console.log(btnID);
//update the playsData according to the button ID and the play ID
if (playerID === 0) {
playsData [btnID-1] = 'O';
//change the display on button
$(this).html('O');
$(this).css('font-size','40px');
//check the winner
checkWinner(btnID);
//change the playerID
playerID = 1;
//change mock up of next player
// $("#p1Border").css('border-color','#00CC33');
}else if (playerID === 1) {
playsData [btnID-1] = 'X';
//change the display on button
$(this).html('X');
$(this).css('font-size','40px');
//check the winner
checkWinner(btnID);
//change the playerID
playerID = 0;
//change mock up of next player
}
if (gameOver === true){
endTheGame();
}else if (gameOver ===true && counter === 8){
endTheGame();
}
//use counter to check if the game reach a draw
else if (counter === 8 && gameOver !== true){
//end the game as it is draw
endDrawGame();
}else{
counter ++;
}
//console.log(playsData);
}
//pop up window for getting player name when click the 'start game' button
var $startBtn = $('#gameOn');
$startBtn.on('click',function(){
$(".form").fadeToggle();
$(".form").css("display","block");
});
//handle pop up window players name when the submit button is clicked
var $submitBtn = $('#subBtn');
$submitBtn.on("click",function(){
//retrieve the input
playerName[0] = $('#p1Name').val();
playerName[1] = $('#p2Name').val();
//assign player name to two boxes
$("#name1").html(playerName[0]);
$("#name1").css('font-size','30px');
$("#name2").html(playerName[1]);
$("#name2").css('font-size','30px');
//assign wining counts to two boxes
$("#p1Count").html(winnerCounter[0]);
$("#p1Count").css('font-size','30px');
$("#p2Count").html(winnerCounter[1]);
$("#p2Count").css('font-size','30px');
//close the popup
$(".form").fadeToggle();
$(".form").css({"display":"none"});
$('#start').css({"display":"none"});
winnerCounter = [0,0];
//initiate the game
initialGame();
});
//button click event handler
$btn1.on('click',playGame);
$btn2.on('click',playGame);
$btn3.on('click',playGame);
$btn4.on('click',playGame);
$btn5.on('click',playGame);
$btn6.on('click',playGame);
$btn7.on('click',playGame);
$btn8.on('click',playGame);
$btn9.on('click',playGame);
//function to close the game when there is a winner
var endTheGame = function(){
//change the shape and color of buttons
for (var d=0;d<3;d++){
$("#"+winnerIndex[d]).css('border-color','#3366CC ');
}
if (playerID === 1){
$("#p1Border").css('border-color','#3366CC ');
winnerCounter[0] ++;
}else{
$("#p2Border").css('border-color','#3366CC ');
winnerCounter[1] ++;
}
//pop up the winning message, make an reset button
showpopup();
}
// function to check the winner
var checkWinner = function(buttonNo){
//get the current clicked button number
//use if statement to decide whether it fits the winning criteria
console.log(buttonNo);
//check button 1 whether there is a winner
if (buttonNo === 1){
//check which player is making the move
if (playerID === 0 ){
//check whether player1 wins the game
if ((playsData[0] === 'O') && (playsData[1] === 'O')&&(playsData[2] ==='O')){
console.log('Player one is the winner on row 1');
gameOver = true;
winnerIndex = [1,2,3];
// $(this).css('color','red');
}else if ((playsData[0] === 'O') && (playsData[4] === 'O')&&(playsData[8] ==='O')){
console.log('Player one is the winner on cross 1');
gameOver = true;
winnerIndex = [1,5,9];
}else if ((playsData[0] === 'O') && (playsData[3] === 'O')&&(playsData[6] ==='O')){
console.log('Player one is the winner on column 1');
gameOver = true;
winnerIndex = [1,4,7];
}
}else if (playerID === 1){
if ((playsData[0] === 'X') && (playsData[1] === 'X')&&(playsData[2] ==='X')){
console.log('Player two is the winner on row 1');
gameOver = true;
winnerIndex = [1,2,3];
}else if ((playsData[0] === 'X') && (playsData[4] === 'X')&&(playsData[8] ==='X')){
console.log('Player two is the winner on cross 1');
gameOver = true;
winnerIndex = [1,5,9];
}else if ((playsData[0] === 'X') && (playsData[3] === 'X')&&(playsData[6] ==='X')){
console.log('Player two is the winner on column 1');
gameOver = true;
winnerIndex = [1,4,7];
}
}
}
//check button 2 whether there is a winner
if (buttonNo === 2){
if (playerID === 0 ){
if ((playsData[0] === 'O') && (playsData[1] === 'O')&&(playsData[2] ==='O')){
console.log('Player one is the winner on row 1');
gameOver = true;
winnerIndex = [1,2,3];
}else if ((playsData[1] === 'O') && (playsData[4] === 'O')&&(playsData[7] ==='O')){
console.log('Player one is the winner on column 2');
gameOver = true;
winnerIndex = [2,5,8];
}
}else if (playerID === 1){
if ((playsData[0] === 'X') && (playsData[1] === 'X')&&(playsData[2] ==='X')){
console.log('Player two is the winner on row 1');
gameOver = true;
winnerIndex = [1,2,3];
}else if ((playsData[1] === 'X') && (playsData[4] === 'X')&&(playsData[7] ==='X')){
console.log('Player two is the winner on column 2');
gameOver = true;
winnerIndex = [2,5,8];
}
}
}
//check button 3 whether there is a winner
if (buttonNo === 3){
if (playerID === 0 ){
if ((playsData[0] === 'O') && (playsData[1] === 'O')&&(playsData[2] ==='O')){
console.log('Player one is the winner on row 1');
gameOver = true;
winnerIndex = [1,2,3];
}else if ((playsData[2] === 'O') && (playsData[4] === 'O')&&(playsData[6] ==='O')){
console.log('Player one is the winner on cross 2');
gameOver = true;
winnerIndex = [3,5,7];
}else if ((playsData[2] === 'O') && (playsData[5] === 'O')&&(playsData[8] ==='O')){
console.log('Player one is the winner on column 3');
gameOver = true;
winnerIndex = [3,6,9];
}
}else if (playerID === 1){
if ((playsData[0] === 'X') && (playsData[1] === 'X')&&(playsData[2] ==='X')){
console.log('Player two is the winner on row 1');
gameOver = true;
winnerIndex = [1,2,3];
}else if ((playsData[2] === 'X') && (playsData[4] === 'X')&&(playsData[6] ==='X')){
console.log('Player two is the winner on cross 2');
gameOver = true;
winnerIndex = [3,5,7];
}else if ((playsData[2] === 'X') && (playsData[5] === 'X')&&(playsData[8] ==='X')){
console.log('Player two is the winner on column 3');
gameOver = true;
winnerIndex = [3,6,9];
}
}
}
//check button 4 whether there is a winner
if (buttonNo === 4){
if (playerID === 0 ){
if ((playsData[0] === 'O') && (playsData[3] === 'O')&&(playsData[6] ==='O')){
console.log('Player one is the winner on column 1');
gameOver = true;
winnerIndex = [1,4,7];
}else if ((playsData[3] === 'O') && (playsData[4] === 'O')&&(playsData[5] ==='O')){
console.log('Player one is the winner on row 2');
gameOver = true;
winnerIndex = [4,5,6];
}
}else if (playerID === 1){
if ((playsData[0] === 'X') && (playsData[3] === 'X')&&(playsData[6] ==='X')){
console.log('Player two is the winner on column 1');
gameOver = true;
winnerIndex = [1,4,7];
}else if ((playsData[3] === 'X') && (playsData[4] === 'X')&&(playsData[5] ==='X')){
console.log('Player two is the winner on row 2');
gameOver = true;
winnerIndex = [4,5,6];
}
}
}
//check button 5 whether there is a winner
if (buttonNo === 5){
if (playerID === 0 ){
if ((playsData[1] === 'O') && (playsData[4] === 'O')&&(playsData[7] ==='O')){
console.log('Player one is the winner on column 2');
gameOver = true;
winnerIndex = [2,5,8];
}else if ((playsData[3] === 'O') && (playsData[4] === 'O')&&(playsData[5] ==='O')){
console.log('Player one is the winner on row 2');
gameOver = true;
winnerIndex = [4,5,6];
}
}
else if (playerID === 1){
if ((playsData[1] === 'X') && (playsData[4] === 'X')&&(playsData[7] ==='X')){
console.log('Player two is the winner on column 2');
gameOver = true;
winnerIndex = [2,5,8];
}else if ((playsData[3] === 'X') && (playsData[4] === 'X')&&(playsData[5] ==='X')){
console.log('Player two is the winner on row 2');
gameOver = true;
winnerIndex = [4,5,6];
}
}
}
//check button 6 whether there is a winner
if (buttonNo === 6){
if (playerID === 0 ){
if ((playsData[2] === 'O') && (playsData[5] === 'O')&&(playsData[8] ==='O')){
console.log('Player one is the winner on column 3');
gameOver = true;
winnerIndex = [3,6,9];
}else if ((playsData[3] === 'O') && (playsData[4] === 'O')&&(playsData[5] ==='O')){
console.log('Player one is the winner on row 2');
gameOver = true;
winnerIndex = [4,5,6];
}
}else if (playerID === 1){
if ((playsData[2] === 'X') && (playsData[5] === 'X')&&(playsData[8] ==='X')){
console.log('Player two is the winner on column 3');
gameOver = true;
winnerIndex = [3,6,9];
}else if ((playsData[3] === 'X') && (playsData[4] === 'X')&&(playsData[5] ==='X')){
console.log('Player two is the winner on row 2');
gameOver = true;
winnerIndex = [4,5,6];
}
}
}
//check button 7 whether there is a winner
if (buttonNo === 7){
if (playerID === 0 ){
if ((playsData[0] === 'O') && (playsData[3] === 'O')&&(playsData[6] ==='O')){
console.log('Player one is the winner on column 1');
gameOver = true;
winnerIndex = [1,4,7];
}else if ((playsData[2] === 'O') && (playsData[4] === 'O')&&(playsData[6] ==='O')){
console.log('Player one is the winner on cross 2');
gameOver = true;
winnerIndex = [3,5,7];
}else if ((playsData[6] === 'O') && (playsData[7] === 'O')&&(playsData[8] ==='O')){
console.log('Player one is the winner on row 3');
gameOver = true;
winnerIndex = [7,8,9];
}
}else if (playerID === 1){
if ((playsData[0] === 'X') && (playsData[3] === 'X')&&(playsData[6] ==='X')){
console.log('Player two is the winner on column 1');
gameOver = true;
winnerIndex = [1,4,7];
}else if ((playsData[2] === 'X') && (playsData[4] === 'X')&&(playsData[6] ==='X')){
console.log('Player two is the winner on cross 2');
gameOver = true;
winnerIndex = [3,5,7];
}else if ((playsData[6] === 'X') && (playsData[7] === 'X')&&(playsData[8] ==='X')){
console.log('Player two is the winner on row 3');
gameOver = true;
winnerIndex = [7,8,9];
}
}
}
//check button 8 whether there is a winner
if (buttonNo === 8){
if (playerID === 0 ){
if ((playsData[1] === 'O') && (playsData[4] === 'O')&&(playsData[7] ==='O')){
console.log('Player one is the winner on column 2');
gameOver = true;
winnerIndex = [2,5,8];
}else if ((playsData[6] === 'O') && (playsData[7] === 'O')&&(playsData[8] ==='O')){
console.log('Player one is the winner on row 3');
gameOver = true;
winnerIndex = [7,8,9];
}
}else if (playerID === 1){
if ((playsData[1] === 'X') && (playsData[4] === 'X')&&(playsData[7] ==='X')){
console.log('Player two is the winner on column 2');
gameOver = true;
winnerIndex = [2,5,8];
}else if ((playsData[6] === 'X') && (playsData[7] === 'X')&&(playsData[8] ==='X')){
console.log('Player two is the winner on row 3');
gameOver = true;
winnerIndex = [7,8,9];
}
}
}
//check button 9 whether there is a winner
if (buttonNo === 9){
if (playerID === 0 ){
if ((playsData[2] === 'O') && (playsData[5] === 'O')&&(playsData[8] ==='O')){
console.log('Player one is the winner on column 3');
gameOver = true;
winnerIndex = [3,6,9];
}else if ((playsData[0] === 'O') && (playsData[4] === 'O')&&(playsData[8] ==='O')){
console.log('Player one is the winner on cross 1');
gameOver = true;
winnerIndex = [1,5,9];
}else if ((playsData[6] === 'O') && (playsData[7] === 'O')&&(playsData[8] ==='O')){
console.log('Player one is the winner on row 3');
gameOver = true;
winnerIndex = [7,8,9];
}
}else if (playerID === 1){
if ((playsData[2] === 'X') && (playsData[5] === 'X')&&(playsData[8] ==='X')){
console.log('Player two is the winner on column 3');
gameOver = true;
winnerIndex = [3,6,9];
}else if ((playsData[0] === 'X') && (playsData[4] === 'X')&&(playsData[8] ==='X')){
console.log('Player two is the winner on cross 1');
gameOver = true;
winnerIndex = [1,5,9];
}else if ((playsData[6] === 'X') && (playsData[7] === 'X')&&(playsData[8] ==='X')){
console.log('Player two is the winner on row 3');
gameOver = true;
winnerIndex = [7,8,9];
}
}
}
//END OF CHECK WINNER
}
//handle popup windows events,handle cancel button on the right top,handle reset button
$(document).ready(function(){
$("#reset_button").click(function(){
gameReset();
});
});
//show the pop up window by setting display and visibiliy
function showpopup(){
if (playerID === 1){
$("#info_text").html("HOORAY! " + playerName[0] + " IS THE WINNER!!! CLICK 'PLAY AGAIN FOR ANOTHER ROUND!'");
}else{
$("#info_text").html("HOORAY! " + playerName[1] + " IS THE WINNER!!! CLICK 'PLAY AGAIN FOR ANOTHER ROUND!'");
}
$("#popup_box").fadeToggle(); //make the window fade in
$("#popup_box").css({"visibility":"visible","display":"block"});
$('button').prop('disabled',true);
}
//handle cancel button to close brower window when clicked
$(document).ready(function(){
$("#cancel_button").click(function(){
window.close();
});
});
//hide the pop up window and initialize the game
function gameReset(){
$("#popup_box").fadeToggle();
$("#popup_box").css({"visibility":"hidden","display":"none"});
//reset to default
initialGame();
}
//handle draw game poppu window events
$(document).ready(function(){
$("#draw_reset_button").click(function(){
drawGameReset();
});
});
//show pop up window when there is a draw
function endDrawGame(){
$("#draw_popup_box").fadeToggle(); //make the window fade in
$("#draw_popup_box").css({"visibility":"visible","display":"block"});
$('button').prop('disabled',true);
}
//hide the draw gamepop up window and initialize the game
function drawGameReset(){
$("#draw_popup_box").fadeToggle();
$("#draw_popup_box").css({"visibility":"hidden","display":"none"});
//reset to default
initialGame();
}
//handle draw game cancel button to close brower window when clicked
$(document).ready(function(){
$("#draw_cancel_button").click(function(){
window.close();
});
});