-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
700 lines (659 loc) · 21.5 KB
/
script.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
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
// All images are from Genshin Impact
const canvas = document.getElementById("myCanvas")
const ctx = canvas.getContext("2d")
ctx.fillStyle = "#0095DD";
ctx.lineWidth = 10
ctx.strokeStyle = "#0d3678";
var col = 8
var row = 8
var imageW = 40
var imageH = 40
var padding = 5
var gridOffsetLeft = 75
var gridOffsetUp = 75
// Top offset for each piece
// Used for moving animations
pieceOffsetUp = new Array(col)
for (let c = 0; c < col; c++) {
pieceOffsetUp[c] = new Array(row)
for (let r = 0; r < row; r++) {
pieceOffsetUp[c][r] = 0
}
}
var consecutive = false
// Images for the game pieces
var folder = "images/"
var pyroImage = new Image()
pyroImage.src = folder + "pyro.png"
var cryoImage = new Image()
cryoImage.src = folder + "cryo.png"
var hydroImage = new Image()
hydroImage.src = folder + "hydro.png"
var electroImage = new Image()
electroImage.src = folder + "electro.png"
var anemoImage = new Image()
anemoImage.src = folder + "anemo.png"
var geoImage = new Image()
geoImage.src = folder + "geo.png"
var hutaoImage = new Image()
hutaoImage.src = folder + "Hutao.png"
var ayakaImage = new Image()
ayakaImage.src = folder + "Ayaka.png"
var kokomiImage = new Image()
kokomiImage.src = folder + "Kokomi.png"
var ittoImage = new Image()
ittoImage.src = folder + "Itto.png"
var raidenImage = new Image()
raidenImage.src = folder + "Raiden.png"
var kazuhaImage = new Image()
kazuhaImage.src = folder + "Kazuha.png"
var yoimiyaImage = new Image()
yoimiyaImage.src = folder + "Yoimiya.png"
var childeImage = new Image()
childeImage.src = folder + "Childe.png"
var albedoImage = new Image()
albedoImage.src = folder + "Albedo.png"
var yaeImage = new Image()
yaeImage.src = folder + "Yae.png"
var ganyuImage = new Image()
ganyuImage.src = folder + "Ganyu.png"
var ventiImage = new Image()
ventiImage.src = folder + "Venti.png"
images = [pyroImage, cryoImage, hydroImage, electroImage, anemoImage, geoImage, hutaoImage, ayakaImage, kokomiImage, raidenImage, kazuhaImage, ittoImage, yoimiyaImage, ganyuImage, childeImage, yaeImage, ventiImage, albedoImage]
var board = new Array(col)
for (let c = 0; c < col; c++) {
board[c] = new Array(row)
for (let r = 0; r < row; r++) {
board[c][r] = {piece: Math.floor(6 * Math.random()), special:0}
}
}
var clickedPieceC, clickedPieceR
var click = 0
var sleepTime = 20
// Wait until all images loaded
counter = 0
for (let i = 0; i < 18; i++) {
images[i].onload = function () {
counter++
if (counter == 18) {
console.log("all loaded")
initialDraw()
}
}
}
function initialDraw() {
updateBoard(board)
ctx.strokeRect(gridOffsetLeft - 10, gridOffsetUp - 10, col * imageW + (col - 1) * padding + 20, row * imageH + (row - 1) * padding + 20)
document.addEventListener("click", clicked, false);
moveFunc()
}
function clicked(e) {
// Get coordinates of click
const rect = canvas.getBoundingClientRect()
const clickX = e.clientX - rect.left
const clickY = e.clientY - rect.top
for (let c = 0; c < col; c++) {
for (let r = 0; r < row; r++) {
// Calculate boundaries of each piece
var pieceTop = gridOffsetUp + imageH * r + padding * r
var pieceBottom = gridOffsetUp + imageH * (r + 1) + padding * r
var pieceLeft = gridOffsetLeft + imageW * c + padding * c
var pieceRight = gridOffsetLeft + imageW * (c + 1) + padding * c
// Check if click lands on piece
if (pieceLeft <= clickX && clickX <= pieceRight && pieceTop <= clickY && clickY <= pieceBottom) {
click++
// First click
if (click == 1) {
clickedPieceC = c
clickedPieceR = r
highlight(c, r)
}
// Second click
else {
// Check if piece is valid
if ((c == clickedPieceC && r == clickedPieceR + 1) || (c == clickedPieceC && r == clickedPieceR - 1) || (c == clickedPieceC + 1 && r == clickedPieceR) || (c == clickedPieceC - 1 && r == clickedPieceR)) {
swap(c, r, clickedPieceC, clickedPieceR)
updateBoard()
// Check for consecutives, remove them, and move pieces down
consecutive = false
moveFunc()
// Don't swap if same click or click not valid
if (!consecutive) {
swap(c, r, clickedPieceC, clickedPieceR)
updateBoard()
}
}
// Erase highlight if same piece clicked
else if (c == clickedPieceC && r == clickedPieceR) {
updateBoard()
}
// Reset click
click = 0
updateBoard()
}
}
}
}
}
function updateBoard() {
// Clear board
ctx.clearRect(0, 0, 1000, 1000)
for (let c = 0; c < col; c++) {
for (let r = 0; r < row; r++) {
if (board[c][r].piece != -1) {
ctx.beginPath()
ctx.drawImage(images[board[c][r].piece + board[c][r].special], gridOffsetLeft + imageW * c + padding * c, gridOffsetUp + pieceOffsetUp[c][r] + imageH * r + padding * r, imageW, imageH)
ctx.stroke()
ctx.closePath()
}
}
}
// Frame
ctx.lineWidth = 10;
ctx.strokeRect(gridOffsetLeft - 10, gridOffsetUp - 10, col * imageW + (col - 1) * padding + 20, row * imageH + (row - 1) * padding + 20)
}
function swap(c, r, clickedPieceC, clickedPieceR) {
var temp = board[c][r].piece
board[c][r].piece = board[clickedPieceC][clickedPieceR].piece
board[clickedPieceC][clickedPieceR].piece = temp
temp = board[c][r].special
board[c][r].special = board[clickedPieceC][clickedPieceR].special
board[clickedPieceC][clickedPieceR].special = temp
}
function checkConsecutive() {
var temp = new Array(col);
for (let c = 0; c < col; c++) {
temp[c] = new Array(row);
for (let r = 0; r < row; r++) {
temp[c][r] = -2;
}
}
for (let c = 0; c < col; c++) {
for (let r = 0; r < row; r++) {
// Five consecutives up down
if (r + 4 < row && board[c][r].piece == board[c][r + 1].piece && board[c][r].piece == board[c][r + 2].piece && board[c][r].piece == board[c][r + 3].piece && board[c][r].piece == board[c][r + 4].piece) {
temp[c][r] = -1
temp[c][r + 1] = -1
temp[c][r + 2] = -1
temp[c][r + 3] = -1
temp[c][r + 4] = -1
}
// Four consecutives up down
else if (r + 3 < row && board[c][r].piece == board[c][r + 1].piece && board[c][r].piece == board[c][r + 2].piece && board[c][r].piece == board[c][r + 3].piece && (r - 1 < 0 || (r - 1 >= 0 && board[c][r - 1].piece != board[c][r].piece))) {
temp[c][r] = -1
temp[c][r + 1] = -1
temp[c][r + 2] = board[c][r].piece
temp[c][r + 3] = -1
board[c][r].special = 0
board[c][r + 1].special = 0
board[c][r + 2].special = 12
board[c][r + 3].special = 0
}
// Three consecutives up down
else if (r + 2 < row && board[c][r].piece == board[c][r + 1].piece && board[c][r].piece == board[c][r + 2].piece && (r - 1 < 0 || (r - 1 >= 0 && board[c][r - 1].piece != board[c][r].piece))) {
// x x x
// x
// x
if (c + 2 < col && board[c][r].piece == board[c + 1][r].piece && board[c][r].piece == board[c + 2][r].piece) {
temp[c][r] = board[c][r].piece
temp[c][r + 1] = -1
temp[c][r + 2] = -1
temp[c + 1][r] = -1
temp[c + 2][r] = -1
board[c][r].special = 6
board[c][r + 1].special = 0
board[c][r + 2].special = 0
board[c + 1][r].special = 0
board[c + 2][r].special = 0
}
// x x x
// x
// x
else if (c - 2 >= 0 && board[c][r].piece == board[c - 1][r].piece && board[c][r].piece == board[c - 2][r].piece) {
temp[c][r] = board[c][r].piece
temp[c][r + 1] = -1
temp[c][r + 2] = -1
temp[c - 1][r] = -1
temp[c - 2][r] = -1
board[c][r].special = 6
board[c][r + 1].special = 0
board[c][r + 2].special = 0
board[c - 1][r].special = 0
board[c - 2][r].special = 0
}
// x
// x
// x x x
else if (c + 2 < col && board[c][r].piece == board[c + 1][r + 2].piece && board[c][r].piece == board[c + 2][r + 2].piece) {
temp[c][r] = -1
temp[c][r + 1] = -1
temp[c][r + 2] = board[c][r].piece
temp[c + 1][r + 2] = -1
temp[c + 2][r + 2] = -1
board[c][r].special = 0
board[c][r + 1].special = 0
board[c][r + 2].special = 6
board[c + 1][r + 2].special = 0
board[c + 2][r + 2].special = 0
}
// x
// x
// x x x
else if (c - 2 >= 0 && board[c][r].piece == board[c - 1][r + 2].piece && board[c][r].piece == board[c - 2][r + 2].piece) {
temp[c][r] = -1
temp[c][r + 1] = -1
temp[c][r + 2] = board[c][r].piece
temp[c - 1][r + 2] = -1
temp[c - 2][r + 2] = -1
board[c][r].special = 0
board[c][r + 1].special = 0
board[c][r + 2].special = 6
board[c - 1][r + 2].special = 0
board[c - 2][r + 2].special = 0
}
// x
// x x x
// x
else if (c + 2 < col && board[c][r].piece == board[c + 1][r + 1].piece && board[c][r].piece == board[c + 2][r + 1].piece) {
temp[c][r] = -1
temp[c][r + 1] = board[c][r].piece
temp[c][r + 2] = -1
temp[c + 1][r + 1] = -1
temp[c + 2][r + 1] = -1
board[c][r].special = 0
board[c][r + 1].special = 6
board[c][r + 2].special = 0
board[c + 1][r + 1].special = 0
board[c + 2][r + 1].special = 0
}
// x
// x x x
// x
else if (c - 2 >= 0 && board[c][r].piece == board[c - 1][r + 1].piece && board[c][r].piece == board[c - 2][r + 1].piece) {
temp[c][r] = -1
temp[c][r + 1] = board[c][r].piece
temp[c][r + 2] = -1
temp[c - 1][r + 1] = -1
temp[c - 2][r + 1] = -1
board[c][r].special = 0
board[c][r + 1].special = 6
board[c][r + 2].special = 0
board[c - 1][r + 1].special = 0
board[c - 2][r + 1].special = 0
}
// x x x
// x
// x
else if (c + 1 < col && c - 1 >= 0 && board[c][r].piece == board[c + 1][r].piece && board[c - 1][r].piece) {
temp[c][r] = board[c][r].piece
temp[c][r + 1] = -1
temp[c][r + 2] = -1
temp[c - 1][r] = -1
temp[c + 1][r] = -1
board[c][r].special = 6
board[c][r + 1].special = 0
board[c][r + 2].special = 0
board[c - 1][r].special = 0
board[c + 1][r].special = 0
}
else if (c + 1 < col && c - 1 >= 0 && board[c][r].piece == board[c + 1][r + 2].piece && board[c][r].piece == board[c - 1][r + 2].piece) {
temp[c][r] = -1
temp[c][r + 1] = -1
temp[c][r + 2] = board[c][r].piece
temp[c - 1][r + 2] = -1
temp[c + 1][r + 2] = -1
board[c][r].special = 6
board[c][r + 1].special = 0
board[c][r + 2].special = 0
board[c - 1][r + 2].special = 0
board[c + 1][r + 2].special = 0
}
else {
temp[c][r] = -1
temp[c][r + 1] = -1
temp[c][r + 2] = -1
}
}
// Five consecutives left right
if (c + 4 < col && board[c][r].piece == board[c + 1][r].piece && board[c][r].piece == board[c + 2][r].piece && board[c][r].piece == board[c + 3][r].piece && board[c][r].piece == board[c + 4][r].piece) {
temp[c][r] = -1
temp[c + 1][r] = -1
temp[c + 2][r] = -1
temp[c + 3][r] = -1
temp[c + 4][r] = -1
}
// Four consecutives left right
else if (c + 3 < col && board[c][r].piece == board[c + 1][r].piece && board[c][r].piece == board[c + 2][r].piece && board[c][r].piece == board[c + 3][r].piece && (c - 1 < 0 || (c - 1 >= 0 && board[c - 1][r].piece != board[c][r].piece))) {
temp[c][r] = -1
temp[c + 1][r] = -1
temp[c + 2][r] = board[c][r].piece
temp[c + 3][r] = -1
board[c][r].special = 0
board[c + 1][r].special = 0
board[c + 2][r].special = 12
board[c + 3][r].special = 0
}
// Three consecutives left right
else if (c + 2 < col && board[c][r].piece == board[c + 1][r].piece && board[c][r].piece == board[c + 2][r].piece && (c - 1 < 0 || (c - 1 >= 0 && board[c - 1][r].piece != board[c][r].piece))) {
// x x x
// x
// x
if (r + 2 < row && board[c][r].piece == board[c][r + 1].piece && board[c][r].piece == board[c][r + 2].piece) {
temp[c][r] = board[c][r].piece
temp[c + 1][r] = -1
temp[c + 2][r] = -1
temp[c][r + 1] = -1
temp[c][r + 2] = -1
board[c][r].special = 6
board[c + 1][r].special = 0
board[c + 2][r].special = 0
board[c][r + 1].special = 0
board[c][r + 2].special = 0
}
// x
// x
// x x x
else if (r - 2 >= 0 && board[c][r].piece == board[c][r - 1].piece && board[c][r].piece == board[c][r - 2].piece) {
temp[c][r] = board[c][r].piece
temp[c + 1][r] = -1
temp[c + 2][r] = -1
temp[c][r - 1] = -1
temp[c][r - 2] = -1
board[c][r].special = 6
board[c + 1][r].special = 0
board[c + 2][r].special = 0
board[c][r - 1].special = 0
board[c][r - 2].special = 0
}
// x x x
// x
// x
else if (r + 2 < row && board[c][r].piece == board[c + 2][r + 1].piece && board[c][r].piece == board[c + 2][r + 2].piece) {
temp[c][r] = -1
temp[c + 1][r] = -1
temp[c + 2][r] = board[c + 2][r] + 6
temp[c + 2][r + 1] = -1
temp[c + 2][r + 2] = -1
board[c][r].special = 0
board[c + 1][r].special = 0
board[c + 2][r].special = 6
board[c + 2][r + 1].special = 0
board[c + 2][r + 2].special = 0
}
// x
// x
// x x x
else if (r - 2 >= 0 && board[c][r].piece == board[c + 2][r - 1].piece && board[c][r].piece == board[c + 2][r - 2].piece) {
temp[c][r] = -1
temp[c + 1][r] = -1
temp[c + 2][r] = board[c + 2][r]
temp[c + 2][r - 1] = -1
temp[c + 2][r - 2] = -1
board[c][r].special = 0
board[c + 1][r].special = 0
board[c + 2][r].special = 6
board[c + 2][r - 1].special = 0
board[c + 2][r - 2].special = 0
}
// x
// x x x
// x
else if (r - 1 >= 0 && r + 1 < row && board[c][r].piece == board[c][r - 1].piece && board[c][r].piece == board[c][r + 1].piece) {
temp[c][r] = board[c][r].piece
temp[c + 1][r] = -1
temp[c + 2][r] = -1
temp[c][r + 1] = -1
temp[c][r - 1] = -1
board[c][r].special = 6
board[c + 1][r].special = 0
board[c + 2][r].special = 0
board[c][r - 1].special = 0
board[c][r + 1].special = 0
}
// x
// x x x
// x
else if (r - 1 >= 0 && r + 1 < row && board[c][r].piece == board[c + 2][r - 1].piece && board[c][r].piece == board[c + 2][r + 1].piece) {
temp[c][r] = -1
temp[c + 1][r] = -1
temp[c + 2][r] = board[c][r].piece
temp[c + 2][r + 1] = -1
temp[c + 2][r - 1] = -1
board[c][r].special = 0
board[c + 1][r].special = 0
board[c + 2][r].special = 6
board[c + 2][r - 1].special = 0
board[c + 2][r + 1].special = 0
}
// x x x
// x
// x
else if (r + 2 < row && board[c][r].piece == board[c + 1][r + 1].piece && board[c][r].piece == board[c + 1][r + 2].piece) {
temp[c][r] = -1
temp[c + 1][r] = board[c][r].piece
temp[c + 2][r] = -1
temp[c + 1][r + 1] = -1
temp[c + 1][r + 2] = -1
board[c][r].special = 0
board[c + 1][r].special = 6
board[c + 2][r].special = 0
board[c + 1][r + 1].special = 0
board[c + 1][r + 2].special = 0
}
// x
// x
// x x x
else if (r - 2 >= 0 && board[c][r].piece == board[c + 1][r - 1].piece && board[c][r].piece == board[c + 1][r - 2].piece) {
temp[c][r] = -1
temp[c + 1][r] = board[c][r].piece
temp[c + 2][r] = -1
temp[c + 1][r - 1] = -1
temp[c + 1][r - 2] = -1
board[c][r].special = 0
board[c + 1][r].special = 6
board[c + 2][r].special = 0
board[c + 1][r - 1].special = 0
board[c + 1][r - 2].special = 0
}
else {
temp[c][r] = -1
temp[c + 1][r] = -1
temp[c + 2][r] = -1
}
}
}
}
var consecutiveBool = false
for (let c = 0; c < col; c++) {
for (let r = 0; r < row; r++) {
if (temp[c][r] != -2) {
consecutiveBool = true
board[c][r].piece = temp[c][r]
}
}
}
if (consecutiveBool) {
consecutive = true
}
return consecutiveBool
}
// Async function to use await
async function moveFunc() {
// Remove event listener when the pieces are moving
document.removeEventListener("click", clicked)
// While loop checks for chain reactions
while (checkConsecutive()) {
// Move down one row every 500 milliseconds
while (checkMove2()) {
for (let i = 0; i < 8; i++) {
await sleep(sleepTime)
moveAnimation()
updateBoard()
}
for (let c = 0; c < col; c++) {
for (let r = 0; r < row; r++) {
pieceOffsetUp[c][r] = 0
}
}
await sleep(sleepTime)
moveDownOne()
updateBoard()
}
await sleep(sleepTime)
}
document.addEventListener("click", clicked, false);
}
// Not using anymore
// Using moveFunc instead
function moveDown() {
for (let c = 0; c < col; c++) {
var index = 0
var temp = []
for (let r = 0; r < row; r++) {
if (board[c][r].piece != -1) {
temp[index] = board[c][r].piece
index++
}
}
for (let r = 0; r < row; r++) {
board[c][r].piece = -1
}
for (let r = 0; r < index; r++) {
board[c][r + row - index] = temp[r]
}
}
}
// Moves down by once
function moveDownOne() {
for (let c = 0; c < col; c++) {
var empty = false
var temp = new Array(row)
var index = row - 1
for (let r = 0; r < row; r++) {
temp[r] = {piece: 0, special: 0}
}
// Delete one empty element (-1) from each empty range
for (let r = row - 1; r > -1; r--) {
if (!empty && board[c][r].piece == -1) {
empty = true
}
else if (empty && board[c][r].piece == -1) {
temp[index].piece = -1
temp[index].special = 0
index--
}
else if (!empty && board[c][r].piece != -1) {
temp[index].piece = board[c][r].piece
temp[index].special = board[c][r].special
index--
}
else if (empty && board[c][r].piece != -1) {
temp[index].piece = board[c][r].piece
temp[index].special = board[c][r].special
index--
empty = false
}
}
for (let r = row - 1; r > -1; r--) {
board[c][r].piece = -1
board[c][r].special = 0
}
for (let r = row - 1; r > index; r--) {
board[c][r].piece = temp[r].piece
board[c][r].special = temp[r].special
}
// Add random piece to the top
if (board[c][0].piece == -1) {
board[c][0].piece = Math.floor(6 * Math.random())
}
}
}
function moveAnimation() {
for (let c = 0; c < col; c++) {
var index = -1
for (let r = row - 1; r > -1; r--) {
if (board[c][r].piece == -1) {
index = r
break
}
}
for (let r = index; r > -1; r--) {
if (board[c][r].piece != -1) {
pieceOffsetUp[c][r] += 5
}
}
}
}
// Not using anymore
// Using checkMove2 instead
function checkMove1() {
var needMove = false
for (let c = 0; c < col; c++) {
var r = 0
while (board[c][r].piece == -1 && r < row) {
r++;
}
while (r < row) {
if (board[c][r].piece == -1) {
needMove = true
break;
}
r++;
}
}
return needMove
}
// Checks if still need to move pieces
function checkMove2() {
for (let c = 0; c < col; c++) {
for (let r = 0; r < row; r++) {
if (board[c][r].piece == -1) {
return true
}
}
}
return false
}
// Not using anymore
// Using an iterative method instead
function moveRecursive() {
if (checkMove2()) {
setTimeout(function() {
moveRecursive()
updateBoard()
moveDownOne()
}, 500)
}
}
// Draws a box around the piece clicked to highlight it
function highlight(pieceC, pieceR) {
ctx.lineWidth = 2
ctx.strokeRect(gridOffsetLeft + imageW * pieceC + padding * pieceC, gridOffsetUp + imageH * pieceR + padding * pieceR, imageW, imageH)
}
// Not using anymore
// Only used for puzzle version
function checkWin(board) {
var empty = true
for (let c = 0; c < col; c++) {
for (let r = 0; r < row; r++) {
if (board[c][r].piece != -1) {
empty = false
}
}
}
if (empty) {
setTimeout(function() {
alert("Congratulations, You Win!")
}, 100)
document.removeEventListener("click", clicked)
return
}
else {
return
}
}
// https://stackoverflow.com/questions/951021/what-is-the-javascript-version-of-sleep/39914235#39914235
// Alternative to setTimeout
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}