-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
971 lines (914 loc) · 29.2 KB
/
Copy pathscript.js
File metadata and controls
971 lines (914 loc) · 29.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
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
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
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]];
}
}
// Function to the displayed update array
/*function updateGrid() {
// Clear existing grid items
gridContainer1.innerHTML = '';
// Display the Player updated array
for (let i = 0; i < 5; i++) {
for (let j = 0; j < 5; j++) {
const gridItem1 = document.createElement('div');
gridItem1.classList.add('grid-item1');
gridItem1.textContent = PlayerSolbd[i][j];
gridContainer1.appendChild(gridItem1);
}
}
// Display the Bot updated array
for (let i = 0; i < 5; i++) {
for (let j = 0; j < 5; j++) {
const gridItem2 = document.createElement('div');
gridItem2.classList.add('grid-item1');
gridItem2.textContent = BotSolbd[i][j];
gridContainer1.appendChild(gridItem2);
}
}
}
function resetgame(){
playAgain.addEventListener('click', () => {
location.reload();
Options.style.display = 'flex';
Gamecontent.style.display = 'none';
PlayerCont.style.display = 'none';
BotContent.style.display = 'none';
fonthide1.style.display = 'none';
fonthide2.style.display = 'none';
playAgain.style.display = 'none';
});
}*/
function checkWinner1(turn){
let TotalSum = 0;
let bothboard = [
[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]
];
if(turn == 1){
for(let i = 0; i < 5; i++){
for (let j = 0; j < 5; j++) {
bothboard[i][j] = PlayerSolbd[i][j];
TotalSum = TotalPlayerSum;
}
}
}
else if(turn == 2){
for(let i = 0; i < 5; i++){
for (let j = 0; j < 5; j++) {
bothboard[i][j] = BotSolbd[i][j];
TotalSum = TotalBotSum;
}
}
}
if(row1){
PlayerSum=0;
for(let j = 0;j < 5; j++){
PlayerSum += bothboard[0][j];
}
if(PlayerSum == 5){
TotalSum += 1;
row1 = false;
}
}
if(row2){
PlayerSum=0;
for(let j = 0;j < 5; j++){
PlayerSum += bothboard[1][j];
}
if(PlayerSum == 5){
TotalSum += 1;
row2 = false;
}
}
if(row3){
PlayerSum=0;
for(let j = 0;j < 5; j++){
PlayerSum += bothboard[2][j];
}
if(PlayerSum == 5){
TotalSum += 1;
row3 = false;
}
}
if(row4){
PlayerSum=0;
for(let j = 0;j < 5; j++){
PlayerSum += bothboard[3][j];
}
if(PlayerSum == 5){
TotalSum += 1;
row4 = false;
}
}
if(row5){
PlayerSum=0;
for(let j = 0;j < 5; j++){
PlayerSum += bothboard[4][j];
}
if(PlayerSum == 5){
TotalSum += 1;
row5 = false;
}
}
if(col1){
PlayerSum=0;
for(let j = 0;j < 5; j++){
PlayerSum += bothboard[j][0];
}
if(PlayerSum == 5){
TotalSum += 1;
col1 = false;
}
}
if(col2){
PlayerSum=0;
for(let j = 0;j < 5; j++){
PlayerSum += bothboard[j][1];
}
if(PlayerSum == 5){
TotalSum += 1;
col2 = false;
}
}
if(col3){
PlayerSum=0;
for(let j = 0;j < 5; j++){
PlayerSum += bothboard[j][2];
}
if(PlayerSum == 5){
TotalSum += 1;
col3 = false;
}
}
if(col4){
PlayerSum=0;
for(let j = 0;j < 5; j++){
PlayerSum += bothboard[j][3];
}
if(PlayerSum == 5){
TotalSum += 1;
col4 = false;
}
}
if(col5){
PlayerSum=0;
for(let j = 0;j < 5; j++){
PlayerSum += bothboard[j][4];
}
if(PlayerSum == 5){
TotalSum += 1;
col5 = false;
}
}
if(diag1){
PlayerSum=0;
for(let j = 0;j < 5; j++){
PlayerSum += bothboard[j][j];
}
if(PlayerSum == 5){
TotalSum += 1;
diag1 = false;
}
}
if(diag2){
PlayerSum=0;
for(let j = 0;j < 5; j++){
PlayerSum += bothboard[j][4-j];
}
if(PlayerSum == 5){
TotalSum += 1;
diag2 = false;
}
}
if(turn == 1){
TotalPlayerSum = TotalSum;
}
else if (turn == 2){
TotalBotSum = TotalSum;
}
if(TotalPlayerSum == 5){
wordContainer.innerHTML = 'You win!';
return true;
}
if (TotalBotSum == 5){
wordContainer.innerHTML = 'Bot win!';
return true;
}
return false;
}
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;
}
}
}
if( levelOption == 3){
win = checkWinner1(1);
}
else{
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;
}
}
}
if(levelOption == 3){
win = checkWinner1(2);
}
else{
win = checkWinner();
}
//updateGrid();
}
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 || 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 == 4 && ( 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 == 4 && ( 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 == 4 && ( 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 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 || levelOption == 2 )){
if(BotSolbd[2][2] == 0){
BotSolbd[2][2] = 1;
randomNumber = BotBoard[12];
UsedNumber.push(randomNumber);
document.getElementById("BotNum").innerHTML = randomNumber;
addValueB(randomNumber);
exits = false;
}
}
// for taking random number
if(exits && 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 = [];
//const UsedNumber = Array(25).fill(0); //filled with zero element
//const BotSolbd = Array(25).fill(0); //filled with zero element
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 TotalPlayerSum = 0;
let TotalBotSum = 0;
let win = false, row1 = true, row2 = true, row3 = true, row4 = true, row5 = true;
let diag1 = true, diag2 = true, col1 = true, col2 = true, col3 = true, col4 = true, col5 = true;
let turn;
var wordContainer = document.getElementById("wordContainer");
var wordContainer1 = document.getElementById("wordContainer1");
let levelOption;
// Shuffle the element randomly
shuffleArray(playerBoard);
shuffleArray(BotBoard);
// Get the grid container element
const gridContainer = document.getElementById('grid-container');
// Display player board by Creating and appending grid items with shuffled numbers
//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 = BotBoard[i];
gridItem.style.backgroundImage = `url(images/${number}.png)`;
gridContainer1.appendChild(gridItem);
}
// Display Bot board by Creating and appending grid items with shuffled numbers
/*for (let i = 0; i < 25; i++) {
const gridItem3 = document.createElement('div');
gridItem3.classList.add('grid-item');
const number = BotBoard[i];
gridItem3.style.backgroundImage = `url(images/${number}.png)`;
gridContainer.appendChild(gridItem3);
}
const gridContainer1 = document.getElementById('grid-container1');
*/
const crossLine = document.getElementById('cross-line');
let starter = Math.floor(Math.random() * 2) + 1;
// Display bot board by Creating and appending grid items with shuffled numbers
//updateGrid();
// Get all the grid items
const gridItems = document.querySelectorAll('.grid-item');
// Get the clicked-number element
const clickedNumber = document.getElementById('clicked-number');
// Add a click event listener to each grid item
gridItems.forEach((item) => {
item.addEventListener('click', (event) => {
// Get the clicked number
if(!win){
wordContainer.innerHTML = ' ';
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
//const crossLine = document.querySelector('.cross-line');
//crossLine.style.display = 'block';
// Position the cross-line over the clicked grid item
//crossLine.style.top = item.offsetTop + 'px';
//crossLine.style.left = item.offsetLeft + 'px';
// Display the cross-line
displayImageAtPosition('images/Cut.png', clickX, clickY);
//crossLine.style.display = 'block';
//document.getElementById("clickXValue").textContent = `Click X: ${clickX}px`;
//document.getElementById("clickYValue").textContent = `Click Y: ${clickY}px`;
// Display the clicked number below the grid
//clickedNumber.textContent = `You clicked on number ${clickedNum}`;
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;
}
}
}
/*let check = true;
for(let i = 0; i < UsedNumber.length; i++){
if(UsedNumber[i] == clickedNum)
{
check = false;
break;
}
}*/
if(checkValue){
UsedNumber.push(clickedNum);
UsedPyNumber.push(clickedNum);
document.getElementById("PlayerNum").innerHTML = clickedNum;
addValueP(clickedNum);
addValueB(clickedNum);
botturn();
}
}
});
});
// Get the grid container and cross-line element
//const gridContainer = document.getElementById('grid-container');
document.addEventListener('DOMContentLoaded', function(){
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');
//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;
if(starter == 2){
botturn();
wordContainer.innerHTML = 'Bot started the Match';
} else {
wordContainer.innerHTML = 'Click on any number to start the match!';
}
});
//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;
if(starter == 2){
botturn();
wordContainer.innerHTML = 'Bot started the Match';
} else {
wordContainer.innerHTML = 'Click on any number to start the match!';
}
});
//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;
if(starter == 2){
botturn();
wordContainer.innerHTML = 'Bot started the Match';
} else {
wordContainer.innerHTML = 'Click on any number to start the match!';
}
});
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';
});
});