-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
554 lines (381 loc) · 9.67 KB
/
Copy pathscript.js
File metadata and controls
554 lines (381 loc) · 9.67 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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
$(document).ready(function(){
console.log("hello");
var turn = 2; //1 is x and 2 is o
var level = 1; // 1 is easy and 2 is hard
//buttons
$oButton= $("#oButton");
$xButton = $("#xButton");
$startButton= $("#startButton");
$hardButton= $("#hardButton");
$easyButton = $("#easyButton");
$button = $(".pickBox");
//screens
$startScreen=$("#startScreen");
$gameScreen= $("#homeScreen");
$endScreen=$("#endScreen");
$xButton.click(function(e){
$xButton.css("border", "solid orange");
$oButton.css("border", "none");
turn= 1;
// console.log(turn);
})
$oButton.click(function(e){
$oButton.css("border", "solid orange");
$xButton.css("border", "none");
turn = 2;
// console.log(turn);
})
$easyButton.click(function(e){
$easyButton.css("border", "solid orange");
$hardButton.css("border", "none");
level= 1;
// console.log(level);
})
$hardButton.click(function(e){
$hardButton.css("border", "solid orange");
$easyButton.css("border", "none");
level= 2;
// console.log(level);
})
$startButton.click(function(e){
$startScreen.fadeOut();
$gameScreen.css("visibility","visible");
$endScreen.css("visibility","hidden");
if(turn==1){
turn=2;// if he picked x make it o and make computer play a move as it is o
computerMove();
numMoves++;
}
console.log("level:"+ level);
})
////////////////////////////////
//Game Logic
//array
//stores whether x (1) or o (2) or nothing(0)
//index o is garbage as using 1 to 9 as id will return 1 to 9
var tttBox = [0,0,0,0,0,0,0,0,0,0];
var numMoves=0; //counts number of moves
//Game Logic for drawing and switching turns
function toggleTurn(){ // 1 is X and 2 is O
if(turn==1){
turn=2;
}
else if(turn==2){
turn=1;
}
}
//game logic to check if someone has won
function ThreeInRow( a, b, c){
if(tttBox[a]!=0){ // if the first box is non zero
if(tttBox[a]==tttBox[b] && tttBox[c]==tttBox[b] ){
return true; // and the rest of the boxs are the same then return
//three in a row;
}
}
return false;
}
function ifWin(){ // checks all possible winning combinations
if(ThreeInRow(1,2,3) || ThreeInRow(1,4,7) || ThreeInRow(1,5,9)
|| ThreeInRow(2,5,8) || ThreeInRow(3,6,9) || ThreeInRow(3,5,7) ||
ThreeInRow(4,5,6) || ThreeInRow(7,8,9)){
return true;
}
return false;
}
function userMove(boxNumber){ // changes array internally
tttBox[boxNumber]=1;
// console.log(tttBox[boxNumber]);
// console.log(boxNumber);
}
////Computer Logic
function computerMove(){
if(level==1){
computerMoveEasy();
}
if(level==2){
ComputerMoveHard();
}
}
//Level Easy
function computerMoveEasy(){//level easy
randomMove();
}
function randomMove(){
while(true){
var num= Math.floor((Math.random() * 9) + 1); //return a number between 1 and 9
// console.log(num);
if(tttBox[num]==0){
makeComputerMove(num);
return;
}
}
}
function makeComputerMove(index){
tttBox[index]=2;
if(turn==1){
$("#"+index).find(".xIcon").css("visibility", "visible");
}
else{
$("#"+index).find(".oIcon").css("visibility", "visible");
}
toggleTurn();
return;
}
////Level hard
function ComputerMoveHard(){
if(numMoves==0){
firstMove();
return;
}
if(numMoves==1){
secondMove();
return;
}
if (numMoves==2) {
thirdMove();
return;
}
if(numMoves==3){
console.log("fourthMove")
fourthMove();
return;
}
if(numMoves > 3){
laterhalfMove();
return;
}
}
function firstMove(){
// Computer making first move should take an open cornerBox
var index = openCornerBoxIndex();
makeComputerMove(index);
}
function secondMove(){ //need to finish
if(tttBox[5]==0){
makeComputerMove(5);
return;
}
var index = openCornerBoxIndex();
makeComputerMove(index); // or else take a corner
}
function thirdMove(){ // pick a random corner box
var index = openCornerBoxIndex();
makeComputerMove(index);
}
function fourthMove(){
var blockingIndex = OneMoveAway(1);
if(blockingIndex!=0){
makeComputerMove(blockingIndex); // if user can win block him
return;
}
var integralBlockIndex = integralCheck(); // checking if he picked two integrals and me center
if(tttBox[5]==2){
var index = openIntegralBoxIndex(); // if have center pick random integral box
if(integralBlockIndex != 0){
index = integralBlockIndex; //block with the index
}
makeComputerMove(index);
return;
}
makeComputerMove(openCornerBoxIndex()); // if don't center take a corner box
}
function laterhalfMove(){
var winningIndex = OneMoveAway(2);
if(winningIndex!=0){
makeComputerMove(winningIndex); // if I can win make the move
return;
}
var blockingIndex = OneMoveAway(1);
if(blockingIndex!=0){
makeComputerMove(blockingIndex); // if user can win block him
return;
}
randomMove(); // make random move
}
function openCornerBoxIndex(){
while(true){
var cornerBoxIndex= Math.floor((Math.random() * 4) + 1);// returns between 1 and 4 to pick one of the corners
if(cornerBoxIndex==1){ // if statements convert into real corner box index
cornerBoxIndex = 1;
}
if(cornerBoxIndex==2){
cornerBoxIndex = 3;
}
if(cornerBoxIndex==3){
cornerBoxIndex = 7;
}
if(cornerBoxIndex==4){
cornerBoxIndex = 9;
}
if(tttBox[cornerBoxIndex]==0){ // check if index is empty or else finds another cornerbox
return cornerBoxIndex;
}
}
}
function openIntegralBoxIndex(){
while(true){
var cornerBoxIndex= Math.floor((Math.random() * 4) + 5);// returns between 1 and 4 to pick one of the corners
if(cornerBoxIndex==5){ // if statements convert into real corner box index
cornerBoxIndex = 2;
}
if(cornerBoxIndex==6){
cornerBoxIndex = 4;
}
if(cornerBoxIndex==7){
cornerBoxIndex =6;
}
if(cornerBoxIndex==8){
cornerBoxIndex = 8;
}
if(tttBox[cornerBoxIndex]==0){ // check if index is empty or else finds another cornerbox
return cornerBoxIndex;
}
}
}
function integralCheck(){
if(tttBox[2]==1 && tttBox[4]==1){
return 1;
}
if(tttBox[4]==1 && tttBox[8]==1){
return 7;
}
if(tttBox[6]==1 && tttBox[8]==1){
return 9;
}
if(tttBox[2]==1 && tttBox[6]==1){
return 3;
}
return 0;
}
function OneMoveAway(mover){ // checks if user or computer one move away based on the parameter passed in
//1 for user and 2 for computer//
//returns the index that is will be three in Row if filled in by player
if(possible3inRow1(mover)){
return indexOneWay(1,2,3);
}
if(possible3inRow2(mover)){
return indexOneWay(4,5,6);
}
if(possible3inRow3(mover)){
return indexOneWay(7,8,9);
}
if(possible3inCollumn1(mover)){
return indexOneWay(1,4,7);
}
if(possible3inCollumn2(mover)){
return indexOneWay(2,5,8);
}
if(possible3inCollumn3(mover)){
return indexOneWay(3,6,9);
}
if(possible3inDiagonal1(mover)){
return indexOneWay(1,5,9);
}
if(possible3inDiagonal2(mover)){
return indexOneWay(3,5,7);
}
return 0;
}
function possibleThreeInRow( a, b, c, mover){ /// return is those three index are one away from threeinRow
if(tttBox[a]==0 && tttBox[b]==mover && tttBox[c]==mover){
return true;
}
if(tttBox[a]==mover && tttBox[b]==0 && tttBox[c]==mover){
return true;
}
if(tttBox[a]==mover && tttBox[b]==mover && tttBox[c]==0){
return true;
}
return false;
}
function indexOneWay(x,y,z){ // input indexs where there is for sure a three in a row and gives you where that spot is
if(tttBox[x]==0){
return x;
}
if(tttBox[y]==0){
return y;
}
if(tttBox[z]==0){
return z;
}
}
function possible3inRow1(mover){ /// takes whose move 1 if user and 2 if computer
/// returnsif row 1 is one moveaway from being three in a row
return possibleThreeInRow(1,2,3, mover);
}
function possible3inRow2(mover){
return possibleThreeInRow(4,5,6, mover);
}
function possible3inRow3(mover){
return possibleThreeInRow(7,8,9, mover);
}
function possible3inCollumn1(mover){
return possibleThreeInRow(1,4,7, mover);
}
function possible3inCollumn2(mover){
return possibleThreeInRow(2,5,8, mover);
}
function possible3inCollumn3(mover){
return possibleThreeInRow(3,6,9, mover);
}
function possible3inDiagonal1(mover){
return possibleThreeInRow(1,5,9, mover);
}
function possible3inDiagonal2(mover){
return possibleThreeInRow(3,5,7, mover);
}
function printBoard(){
for (var i = 1; i < tttBox.length; i++) {
console.log(tttBox[i]);
}
}
$square = $(".box");
//array
//stores whether user (1) or computer (2) or nothing(0)
//index o is garbage as using 1 to 9 as id will return 1 to 9
var tttBox = [0,0,0,0,0,0,0,0,0,0];
$square.click(function(e){
var boxNumber = parseInt(this.id); // get which box it is
if(tttBox[boxNumber]!=0){
return; // return out at there is already an icon there
}
userMove(boxNumber);
numMoves++;
console.log("#moves:" + numMoves);
if(turn==1){
$(this).find(".xIcon").css("visibility", "visible");
}
else{
$(this).find(".oIcon").css("visibility", "visible");
}
console.log("UserWinningMove:" + OneMoveAway(1));
toggleTurn();
if(ifWin()){
alert("you won");
location.reload();
return;
}
;
if(numMoves==9){
alert("Its a tie");
location.reload();
return;
}
setTimeout(function(){
computerMove();
numMoves++;
console.log("#moves:" + numMoves);
console.log("ComputerWinningMove:" + OneMoveAway(2));
if(ifWin()){
alert("computer won");
location.reload();
return;
}
if(numMoves==9){
alert("Its a tie");
location.reload();
return;
}
},1000);
})
})