-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript2.js
More file actions
720 lines (664 loc) · 21.1 KB
/
Copy pathscript2.js
File metadata and controls
720 lines (664 loc) · 21.1 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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
/************* this script is to make a manipulation
in same webpage without loading the script to reset
the variables ***************************/
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
gridContainer.innerHTML = '';
for (let i = 0; i < 25; i++) {
const gridItem = document.createElement('div');
gridItem.classList.add('grid-item');
const number = playerBoard[i];
gridItem.style.backgroundImage = `url(images/${number}.png)`;
gridContainer.appendChild(gridItem);
}
gridContainer1.innerHTML = '';
for (let i = 0; i < 25; i++) {
const gridItem = document.createElement('div');
gridItem.classList.add('grid-item');
const number = playerBoard[i];
gridItem.style.backgroundImage = `url(images/${number}.png)`;
gridContainer1.appendChild(gridItem);
}
}
function checkWinner(){
// check player board
for(let i = 0; i < 5; i++){
PlayerSum=0;
for(let j =0; j < 5; j++){
PlayerSum += PlayerSolbd[i][j];
if(PlayerSum == 5){
var word = "You win!";
wordContainer.innerHTML = word;
return true;
}
}
}
for(let i = 0; i < 5; i++){
PlayerSum=0;
for(let j =0; j < 5; j++){
PlayerSum += PlayerSolbd[j][i];
if(PlayerSum == 5){
var word = "You win!";
wordContainer.innerHTML = word;
return true;
}
}
}
PlayerSum=0;
for(let i = 0; i < 5; i++){
PlayerSum += PlayerSolbd[i][i];
if(PlayerSum == 5){
var word = "You win!";
wordContainer.innerHTML = word;
return true;
}
}
PlayerSum=0;
for(let i = 0; i < 5; i++){
PlayerSum += PlayerSolbd[i][4-i];
if(PlayerSum == 5){
var word = "You win!";
wordContainer.innerHTML = word;
return true;
}
}
// check bot board
for(let i = 0; i < 5; i++){
BotSum=0;
for(let j =0; j < 5; j++){
BotSum += BotSolbd[i][j];
if(BotSum == 5){
var word = "Bot win!";
wordContainer.innerHTML = word;
return true;
}
}
}
for(let i = 0; i < 5; i++){
BotSum=0;
for(let j =0; j < 5; j++){
BotSum += BotSolbd[j][i];
if(BotSum == 5){
var word = "Bot win!";
wordContainer.innerHTML = word;
return true;
}
}
}
BotSum=0;
for(let i = 0; i < 5; i++){
BotSum += BotSolbd[i][i];
if(BotSum == 5){
var word = "Bot win!";
wordContainer.innerHTML = word;
return true;
}
}
BotSum=0;
for(let i = 0; i < 5; i++){
BotSum += BotSolbd[i][4-i];
if(BotSum == 5){
var word = "Bot win!";
wordContainer.innerHTML = word;
return true;
}
}
}
// add value in both board solution
function addValueP(value){
let k = 0, j = 0;
for(let i = 0; i < 25; i++){
if(playerBoard[i] == value){
if(i >= 20 && i <= 24){
k = 4;
j = i - 20;
PlayerSolbd[k][j] = 1;
}
else if(i >= 15 && i <= 19){
k = 3;
j = i - 15;
PlayerSolbd[k][j] = 1;
}
else if(i >= 10 && i <= 14) {
k = 2;
j = i - 10;
PlayerSolbd[k][j] = 1;
}
else if(i >= 5 && i <= 9){
k = 1;
j = i - 5;
PlayerSolbd[k][j] = 1;
}
else if(i >= 0 && i <= 4){
k = 0;
j = i;
PlayerSolbd[k][j] = 1;
}
}
}
win = checkWinner();
}
function addValueB(value){
let k = 0, j = 0;
for(let i = 0; i < 25; i++){
if(BotBoard[i] == value){
if(i >= 20 && i <= 24){
k = 4;
j = i - 20;
BotSolbd[k][j] = 1;
}
else if(i >= 15 && i <= 19){
k = 3;
j = i - 15;
BotSolbd[k][j] = 1;
}
else if(i >= 10 && i <= 14) {
k = 2;
j = i - 10;
BotSolbd[k][j] = 1;
}
else if(i >= 5 && i <= 9){
k = 1;
j = i - 5;
BotSolbd[k][j] = 1;
}
else if(i >= 0 && i <= 4){
k = 0;
j = i;
BotSolbd[k][j] = 1;
}
}
}
win = checkWinner();
}
function MatchBotNum(x,y){
let k, value;
k= (x * 5)+ y;
value = BotBoard[k];
return value;
}
//bot brain
function botturn(){
let exits = true;
let RowSum = 0,ColSum = 0,DiagnolSum = 0,RevDiaSum = 0,randomNumber = 0;
// for checking if it is making 4
for(let i = 0; i < 5; i++){
RowSum = 0;
for(let j =0; j < 5; j++){
RowSum += BotSolbd[i][j];
}
if(RowSum == 4 && levelOption == 3){
for(let j = 0; j < 5; j++){
if(BotSolbd[i][j] == 0){
BotSolbd[i][j] = 1;
exits = false;
randomNumber= MatchBotNum(i,j);
break;
}
}
}
if(!exits){
break;
}
ColSum = 0;
for(let j =0; j < 5; j++){
ColSum += BotSolbd[j][i];
}
if(ColSum == 4 && levelOption == 3){
for(let j = 0; j < 5; j++){
if(BotSolbd[j][i] == 0){
BotSolbd[j][i] = 1;
exits = false;
randomNumber= MatchBotNum(j,i);
break;
}
}
}
if(!exits){ break; }
}
if(exits){
DiagnolSum = 0;
for(let j =0; j < 5; j++){
DiagnolSum += BotSolbd[j][j];
}
if(DiagnolSum == 4 && levelOption == 3){
for(let j = 0; j < 5; j++){
if(BotSolbd[j][j] == 0){
BotSolbd[j][j] = 1;
exits = false;
randomNumber= MatchBotNum(j,j);
break;
}
}
}
RevDiaSum = 0;
for(let j =0; j < 5; j++){
RevDiaSum += BotSolbd[j][4-j];
}
if(RevDiaSum == 4 && levelOption == 3){
for(let j = 0; j < 5; j++){
if(BotSolbd[j][4-j] == 0){
BotSolbd[j][4-j] = 1;
exits = false;
randomNumber= MatchBotNum(j,4-j);
break;
}
}
}
}
// for checking if it is making 3
if(exits){
for(let i = 0; i < 5; i++){
RowSum = 0;
for(let j =0; j < 5; j++){
RowSum += BotSolbd[i][j];
}
if(RowSum == 3 && ( levelOption == 3 || levelOption == 2 ) ){
for(let j = 0; j < 5; j++){
if(BotSolbd[i][j] == 0){
BotSolbd[i][j] = 1;
exits = false;
randomNumber= MatchBotNum(i,j);
break;
}
}
}
if(!exits){ break; }
ColSum = 0;
for(let j =0; j < 5; j++){
ColSum += BotSolbd[j][i];
}
if(ColSum == 3 && (levelOption == 3 || levelOption == 2 )){
for(let j = 0; j < 5; j++){
if(BotSolbd[j][i] == 0){
BotSolbd[j][i] = 1;
exits = false;
randomNumber= MatchBotNum(j,i);
break;
}
}
}
if(!exits){ break; }
}
}
if(exits){
DiagnolSum = 0;
for(let j =0; j < 5; j++){
DiagnolSum += BotSolbd[j][j];
}
if(DiagnolSum == 3 && (levelOption == 3 || levelOption == 2 ) ){
for(let j = 0; j < 5; j++){
if(BotSolbd[j][j] == 0){
BotSolbd[j][j] = 1;
exits = false;
randomNumber= MatchBotNum(j,j);
break;
}
}
}
RevDiaSum = 0;
for(let j =0; j < 5; j++){
RevDiaSum += BotSolbd[j][4-j];
}
if(RevDiaSum == 3 && (levelOption == 3 || levelOption == 2 )){
for(let j = 0; j < 5; j++){
if(BotSolbd[j][4-j] == 0){
BotSolbd[j][4-j] = 1;
exits = false;
randomNumber= MatchBotNum(j,4-j);
break;
}
}
}
}
// for checking if it is making 2
if(exits){
for(let i = 0; i < 5; i++){
RowSum = 0;
for(let j =0; j < 5; j++){
RowSum += BotSolbd[i][j];
}
if(RowSum == 2 && (levelOption == 3 || levelOption == 2 || levelOption == 1) ){
for(let j = 0; j < 5; j++){
if(BotSolbd[i][j] == 0){
BotSolbd[i][j] = 1;
exits = false;
randomNumber= MatchBotNum(i,j);
break;
}
}
}
if(!exits){ break; }
ColSum = 0;
for(let j =0; j < 5; j++){
ColSum += BotSolbd[j][i];
}
if(ColSum == 2 && (levelOption == 3 || levelOption == 2 || levelOption == 1) ){
for(let j = 0; j < 5; j++){
if(BotSolbd[j][i] == 0){
BotSolbd[j][i] = 1;
exits = false;
randomNumber= MatchBotNum(j,i);
break;
}
}
}
if(!exits){ break; }
}
}
if(exits){
DiagnolSum = 0;
for(let j =0; j < 5; j++){
DiagnolSum += BotSolbd[j][j];
}
if(DiagnolSum == 2 && (levelOption == 3 || levelOption == 2 || levelOption == 1) ){
for(let j = 0; j < 5; j++){
if(BotSolbd[j][j] == 0){
BotSolbd[j][j] = 1;
exits = false;
randomNumber= MatchBotNum(j,j);
break;
}
}
}
RevDiaSum = 0;
for(let j =0; j < 5; j++){
RevDiaSum += BotSolbd[j][4-j];
}
if(RevDiaSum == 2 && (levelOption == 3 || levelOption == 2 || levelOption == 1) ){
for(let j = 0; j < 5; j++){
if(BotSolbd[j][4-j] == 0){
BotSolbd[j][4-j] = 1;
exits = false;
randomNumber= MatchBotNum(j,4-j);
break;
}
}
}
}
// for checking if it is making 1
if(exits){
for(let i = 0; i < 5; i++){
RowSum = 0;
for(let j =0; j < 5; j++){
RowSum += BotSolbd[i][j];
}
if(RowSum == 1 && (levelOption == 3 || levelOption == 2 || levelOption == 1) ){
for(let j = 0; j < 5; j++){
if(BotSolbd[i][j] == 0){
BotSolbd[i][j] = 1;
exits = false;
randomNumber= MatchBotNum(i,j);
break;
}
}
}
if(!exits){ break; }
ColSum = 0;
for(let j =0; j < 5; j++){
ColSum += BotSolbd[j][i];
}
if(ColSum == 1 && (levelOption == 3 || levelOption == 2 || levelOption == 1) ){
for(let j = 0; j < 5; j++){
if(BotSolbd[j][i] == 0){
BotSolbd[j][i] = 1;
exits = false;
randomNumber= MatchBotNum(j,i);
break;
}
}
}
if(!exits){ break; }
}
}
if(exits){
DiagnolSum = 0;
for(let j =0; j < 5; j++){
DiagnolSum += BotSolbd[j][j];
}
if(DiagnolSum == 1 && (levelOption == 3 || levelOption == 2 || levelOption == 1) ){
for(let j = 0; j < 5; j++){
if(BotSolbd[j][j] == 0){
BotSolbd[j][j] = 1;
exits = false;
randomNumber= MatchBotNum(j,j);
break;
}
}
}
RevDiaSum = 0;
for(let j =0; j < 5; j++){
RevDiaSum += BotSolbd[j][4-j];
}
if(RevDiaSum == 1 && (levelOption == 3 || levelOption == 2 || levelOption == 1) ){
for(let j = 0; j < 5; j++){
if(BotSolbd[j][4-j] == 0){
BotSolbd[j][4-j] = 1;
exits = false;
randomNumber= MatchBotNum(j,4-j);
break;
}
}
}
}
if(!exits){
UsedNumber.push(randomNumber);
document.getElementById("BotNum").innerHTML = randomNumber;
addValueB(randomNumber);
}
if(exits && levelOption == 3){
if(BotSolbd[2][2] == 0){
BotSolbd[2][2] == 1;
UsedNumber.push(randomNumber);
document.getElementById("BotNum").innerHTML = randomNumber;
addValueB(randomNumber);
exits = false;
}
}
// for taking random number
if(exits && (levelOption == 2 || levelOption == 1) ){
let whilecheck = true;
while(whilecheck){
let check= true;
const randomNumber1 = Math.floor(Math.random() * 25) + 1;
for(let i = 0; i < UsedNumber.length; i++){
if(UsedNumber[i] == randomNumber1)
{
check = false;
break;
}
}
if(check){
UsedNumber.push(randomNumber1);
document.getElementById("BotNum").innerHTML = randomNumber1;
addValueB(randomNumber1);
whilecheck = false;
}
}
}
}
//display cut image
function displayImageAtPosition(src, x, y) {
const image = new Image();
image.src = src;
image.style.left = x - 20 + 'px';
image.style.top = y - 20 + 'px';
// Append the image to the container
const container = document.getElementById('imageContainer');
container.appendChild(image);
}
/********************* VARIABLES *********************/
// Create an board for player and bot
const playerBoard = Array.from({ length: 25 }, (_, i) => i + 1); // filled with 1 to 25 number in ascending order
const BotBoard = Array.from({ length: 25 }, (_, i) => i + 1); // filled with 1 to 25 number in ascending order
let UsedNumber = [];
let UsedPyNumber = [];
let PlayerSolbd = [
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]
];
let BotSolbd = [
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]
];
let PlayerSum;
let BotSum;
let win = false;
var wordContainer = document.getElementById("wordContainer");
let levelOption;
// Get the grid container element
const gridContainer = document.getElementById('grid-container');
const gridContainer1 = document.getElementById('grid-container1');
for (let i = 0; i < 25; i++) {
const gridItem = document.createElement('div');
gridItem.classList.add('grid-item');
const number = playerBoard[i];
gridItem.style.backgroundImage = `url(images/${number}.png)`;
gridContainer.appendChild(gridItem);
}
for (let i = 0; i < 25; i++) {
const gridItem = document.createElement('div');
gridItem.classList.add('grid-item');
const number = playerBoard[i];
gridItem.style.backgroundImage = `url(images/${number}.png)`;
gridContainer1.appendChild(gridItem);
}
// Display player board by Creating and appending grid items with shuffled numbers
// Get all the grid items
const gridItems = document.querySelectorAll('.grid-item');
// Add a click event listener to each grid item
// Get the grid container and cross-line element
//const gridContainer = document.getElementById('grid-container');
const startGame = document.getElementById('start-button');
const playAgain = document.getElementById('reset-button');
const rulesButton = document.getElementById('rule-button');
const gamerules = document.getElementById('GameRules');
const rulegoback = document.getElementById('RGoBack-button');
const Optiongoback = document.getElementById('OGoBack-button');
const Options = document.getElementById('level');
const Gamecontent = document.getElementById('grid-container');
const PlayerCont = document.getElementById('PlayerNum');
const BotContent = document.getElementById('BotNum');
const fonthide1 = document.getElementById('fonthide1');
const fonthide2 = document.getElementById('fonthide2');
const levelEasy = document.getElementById('Easy');
const levelMedium = document.getElementById('Medium');
const levelHard = document.getElementById('Hard');
document.addEventListener('DOMContentLoaded', function(){
gridItems.forEach((item) => {
item.addEventListener('click', (event) => {
// Get the clicked number
if(!win){
const backgroundImage = item.style.backgroundImage;
const clickedNum = parseInt(backgroundImage.match(/\d+/)[0]);
const clickX = item.offsetLeft + item.offsetWidth / 2;
const clickY = item.offsetTop + item.offsetHeight / 2;
// Display the cross-line
displayImageAtPosition('images/Cut.png', clickX, clickY);
let checkValue = true;
for(let i = 0; i < UsedPyNumber.length; i++){
if(UsedPyNumber[i] == clickedNum){
checkValue = false;
break;
}
}
if(checkValue){
for(let i = 0; i < UsedNumber.length; i++){
if(UsedNumber[i] == clickedNum){
UsedPyNumber.push(clickedNum);
addValueP(clickedNum);
checkValue = false;
break;
}
}
}
if(checkValue){
UsedNumber.push(clickedNum);
UsedPyNumber.push(clickedNum);
document.getElementById("PlayerNum").innerHTML = clickedNum;
addValueP(clickedNum);
addValueB(clickedNum);
botturn();
}
}
});
});
//for easy level
levelEasy.addEventListener('click', () => {
//console.log('Button clicked');
Options.style.display = 'none';
Gamecontent.style.display = 'grid';
PlayerCont.style.display = 'flex';
BotContent.style.display = 'flex';
fonthide1.style.display = 'block';
fonthide2.style.display = 'block';
playAgain.style.display = 'block';
levelOption = 1;
});
//for medium level
levelMedium.addEventListener('click', () => {
//console.log('Button clicked');
Options.style.display = 'none';
Gamecontent.style.display = 'grid';
PlayerCont.style.display = 'flex';
BotContent.style.display = 'flex';
fonthide1.style.display = 'block';
fonthide2.style.display = 'block';
playAgain.style.display = 'block';
levelOption = 2;
});
//for hard level
levelHard.addEventListener('click', () => {
//console.log('Button clicked');
Options.style.display = 'none';
Gamecontent.style.display = 'grid';
PlayerCont.style.display = 'flex';
BotContent.style.display = 'flex';
fonthide1.style.display = 'block';
fonthide2.style.display = 'block';
playAgain.style.display = 'block';
levelOption = 3;
});
playAgain.addEventListener('click', () => {
location.reload();
});
// for going back
Optiongoback.addEventListener('click', () => {
//console.log('Button clicked');
startGame.style.display = 'flex';
rulesButton.style.display = 'flex';
Options.style.display = 'none';
});
rulegoback.addEventListener('click', () => {
//console.log('Button clicked');
startGame.style.display = 'flex';
rulesButton.style.display = 'flex';
gamerules.style.display = 'none';
});
//for game rules
rulesButton.addEventListener('click', () => {
//console.log('Button clicked');
startGame.style.display = 'none';
rulesButton.style.display = 'none';
gamerules.style.display = 'flex';
});
// start to level page
startGame.addEventListener('click', () => {
//console.log('Button clicked');
startGame.style.display = 'none';
rulesButton.style.display = 'none';
Options.style.display = 'flex';
// Shuffle the element randomly
shuffleArray(playerBoard);
shuffleArray(BotBoard);
});
});