forked from hackclub/sprig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsprock_blast.js
977 lines (837 loc) · 21.1 KB
/
sprock_blast.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
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
972
973
974
975
976
977
/*
@title: Sprock Blast
@author: cayleb247
@tags: ['puzzle', 'retro']
@addedOn: 2024-10-03
*/
/*
CONTROLS:
W - Move cursor up
S - Move cursor down
A - Move block one position to the left
D - Move Block one position to the right
J - Selected a block | Place a block
L - Cancel block selection
I - Restart game (at game over)
Glossary for comments:
Brick - An individual bitmap of a brick; makes up blocks
Blocks - A block made of bricks with a unique shape (L, T, Square, etc.)
Tile - A 3x3 sized space within the board
Position - A tile on the board that is able for a block to be placed within
*/
const player = "p"
let blockSelection = []
let blockPreviewStatus = 0; // Status of whether block outline is within the board
let blockPosition = 0; // Index of a current shown block position
let tileList = [] // List of the every single 3x3 tile's configuration on the board
let positionList = []; // Indexes of every position that the block can be on the board
let blockSelectionIndexes = []; // List of every index of the selected block that a brick present
let currentPositionIndex = 0;
let blockChoice = 0; // The block out of the three offered that the player selects
let availableBlocks = []; // List of every block left (starting from top; 1 through 3)
let score = 0; // Player's score
let combo = 0; // Default value for combos
let gameStatus = 1; // Whether the game is active or not
// Block Indexes
const blockCombos = [
[0,3,4,7], // s
[4,5,6,7], // s2
[1,3,4,6], // sInv
[3,4,7,8], // sInv2
[0,3,6,7], // L
[3,4,5,6], // L2
[0,1,4,7], // L3
[5,6,7,8], // L4
[1,4,6,7], // lInv
[3,6,7,8], // lInv2
[0,1,3,6], // lInv3
[3,4,5,8], // lInv4
[0,1,2,3,6], // bigL
[0,1,2,5,8], // bigL2
[2,5,6,7,8], // bigL3
[0,3,6,7,8], // bigL4
[3,6,7], // smallL
[3,4,6], // smallL2
[3,4,7], // smallL3
[4,6,7], // smallL4
[6], // dot
[3,6], // line
[6,7], // line2
[0,3,6], // bigLine
[6,7,8], // bigLine2
[1,3,4,7], // t
[4,6,7,8], // t2
[0,3,4,6], // t3
[3,4,5,7], // t4
[0,1,2,4,7], // bigT
[2,3,4,5,8], // bigT2
[1,4,6,7,8], // bigT3
[0,3,4,5,6], // bigT4
[0,1,3,4,6,7], // rect
[3,4,5,6,7,8], // rect2
[3,4,6,7], // cube
[0,1,2,3,4,5,6,7,8], // bigcube
[1,3,4,5,7] // plus
]
// Block Colors
const blockColors = [
"1", // pink
"2", // yellow
"3", // red
"4", // blue
"5", // green
"6" // orange
]
// Board
const background = "x"
const board_top_left_corner = "a"
const board_top_middle = "b"
const board_top_right_corner = "c"
const board_right_middle = "d"
const board_bottom_right_corner = "e"
const board_bottom_middle = "f"
const board_bottom_left_corner = "g"
const board_left_middle = "h"
// Block Pieces:
const block1 = "1"
const block2 = "2"
const block3 = "3"
const block4 = "4"
const block5 = "5"
const block6 = "6"
// Misc Sprites:
const selection = "s"
const brickDivideLeft = "v"
const brickDivideCenter = "y"
const brickDivideRight = "z"
// Functions:
function getTileValue(x, y) { // Get the information for a select 3x3 tile
let tileData = []; // List for every brick in a 3x3 tile
x += 2; // To adjust x value to start at right value during first iteration
y -= 1;
for (let i=0;i<3;i++) {
x -= 3; // Reset x coordinate every shift down in y
y += 1;
for (let i=0;i<3;i++) {
x += 1;
tileData.push(getTile(x,y));
}
}
return tileData;
}
function getTileList() { // Get information on every 3x3 tile on board
tileList = [];
for (let y=1;y<=8;y++) {
for (let x=1;x<=8;x++) {
tileList.push(getTileValue(x,y));
}
}
}
function makePositionList() { // Iterate through every postion and check if space is available
positionList = []; // Begin with an empty list of positions
for (let i=0;i<=63;i++) {
let isPlaceable = 1;
for (const index of blockSelectionIndexes) {
if (tileList[i][index].length == 0) {
isPlaceable = 0;
break;
} else if (tileList[i][index][0]["_type"] !== "x") {
isPlaceable = 0;
break;
}
}
if (isPlaceable === 1) {
positionList.push(i);
}
}
if (positionList.length == 0) { // Condition to end the game
endGame();
}
}
function getSelectedBlockIndexes() {
for (let i=0;i<=8;i++) { // Get block selection's indexes
if (blockSelection[i].length !== 0) {
blockSelectionIndexes.push(i)
}
}
}
function displayPosition(positionIndex) { // Display selection for a select postion on position list
if (gameStatus == 1) { // Seems redundant but prevents a type error when the game ends
let currentPosition = tileList[positionIndex];
for (const blockIndex of blockSelectionIndexes) {
addSprite(currentPosition[blockIndex][0]["_x"], currentPosition[blockIndex][0]["_y"], "s");
}
}
}
function removeDisplayPosition(positionIndex) { // Remove selection for a select postion on position list
let currentPosition = tileList[positionIndex];
for (const blockIndex of blockSelectionIndexes) {
clearTile(currentPosition[blockIndex][0]["_x"], currentPosition[blockIndex][0]["_y"]);
addSprite(currentPosition[blockIndex][0]["_x"], currentPosition[blockIndex][0]["_y"], "x")
}
}
function placeBlock(positionIndex) { // Places a block given an index for the position on the board
let currentPosition = tileList[positionIndex]; // Declares current position given the position index
for (const blockIndex of blockSelectionIndexes) { // Add corresponding colored bricks
addSprite(currentPosition[blockIndex][0]["_x"], currentPosition[blockIndex][0]["_y"], blockSelection[blockSelectionIndexes[0]][0]["_type"]);
}
}
function clearBlockChoice(block_number) {
if (block_number == 1) {
for (let y=1;y<=3;y++) {
for (let x=12;x<=14;x++) {
clearTile(x,y);
}
}
} else if (block_number == 2) {
for (let y=5;y<=7;y++) {
for (let x=12;x<=14;x++) {
clearTile(x,y);
}
}
} else if (block_number == 3) {
for (let y=9;y<=11;y++) {
for (let x=12;x<=14;x++) {
clearTile(x,y);
}
}
}
}
function getRandomInt(max) { // Returns a random number between 0 and the max (not including the max)
return Math.floor(Math.random()*max);
}
function newBlocks() { // Generates a new set of three blocks
if (availableBlocks.length == 0) {
let threeBlockCoords = [
[[12,1], [13,1], [14,1], [12,2], [13,2], [14,2], [12,3], [13,3], [14,3]],
[[12,5], [13,5], [14,5], [12,6], [13,6], [14,6], [12,7], [13,7], [14,7]],
[[12,9], [13,9], [14,9], [12,10], [13,10], [14,10], [12,11], [13,11], [14,11]]
];
let selectedBlocks = []; // A list of three random blocks
let selectedColors = []; // A list of three random colors
for (let i=1; i<=3; i++) {
selectedBlocks.push(blockCombos[getRandomInt(blockCombos.length)]);
selectedColors.push(blockColors[getRandomInt(blockColors.length)]);
}
for (let i=0;i<=2;i++) {
let block = selectedBlocks[i]; // Iterate through each block and color
let color = selectedColors[i];
let coords = threeBlockCoords[i]
for (const blockIndex of block) {
addSprite(coords[blockIndex][0], coords[blockIndex][1], color);
}
}
availableBlocks = [1,2,3];
}
}
function getRows() {
let rows = [];
let currentRow = [];
for (let y=3;y<=10;y++) {
currentRow = [];
for (let x=1;x<=8;x++) {
currentRow.push(getTile(x,y));
}
rows.push(currentRow);
}
return rows;
}
function getColumns() {
let columns = [];
let currentColumn = [];
for (let x=1;x<=8;x++) {
currentColumn = [];
for (let y=3;y<=10;y++) {
currentColumn.push(getTile(x,y));
}
columns.push(currentColumn);
}
return columns;
}
function blockClear() {
let rowsList = getRows(); // Get list of very row
let columnsList = getColumns(); // Get list of very column
let rowsToClear = []; // List of every full row
let columnsToClear = []; // List of every full column
for (const row of rowsList) { // Push index of every full row (0-7)
let currentRow = [];
for (let i=0;i<=7;i++) {
if (row[i].length == 2) {
currentRow.push(i);
}
}
if (currentRow.length == 8) {
rowsToClear.push(rowsList.indexOf(row));
}
}
for (const column of columnsList) { // Push index of every full column (0-7)
let currentColumn = [];
for (let i=0;i<=7;i++) {
if (column[i].length == 2) {
currentColumn.push(i);
}
}
if (currentColumn.length == 8) {
columnsToClear.push(columnsList.indexOf(column));
}
}
if (rowsToClear.length != 0 && columnsToClear.length != 0) { // Scoring if both a row and column is cleared
for (const rowIndex of rowsToClear) {
score += 10; // Add 10 for a row complete
for (const removedBrick of rowsList[rowIndex]) {
clearTile(removedBrick[0]["_x"], removedBrick[0]["_y"]);
addSprite(removedBrick[0]["_x"], removedBrick[0]["_y"], "x");
}
}
for (const colIndex of columnsToClear) {
score += 10; // Add 10 for a column complete
for (const removedBrick of columnsList[colIndex]) {
clearTile(removedBrick[0]["_x"], removedBrick[0]["_y"]);
addSprite(removedBrick[0]["_x"], removedBrick[0]["_y"], "x");
}
}
score += combo * 10; // Add combo score
combo += 1; // Level up combo
} else if (rowsToClear.length != 0) { // Scoring if a row is cleared
for (const rowIndex of rowsToClear) {
score += 10;
for (const removedBrick of rowsList[rowIndex]) {
clearTile(removedBrick[0]["_x"], removedBrick[0]["_y"]);
addSprite(removedBrick[0]["_x"], removedBrick[0]["_y"], "x");
}
}
score += combo * 10;
combo += 1;
} else if (columnsToClear.length != 0) { // Scoring if a column is cleared
for (const colIndex of columnsToClear) {
score += 10;
for (const removedBrick of columnsList[colIndex]) {
clearTile(removedBrick[0]["_x"], removedBrick[0]["_y"]);
addSprite(removedBrick[0]["_x"], removedBrick[0]["_y"], "x");
}
}
score += combo * 10;
combo += 1;
} else {
combo = 0; // Combo resets if no rows or columns are cleared
}
clearText(); // Remove previous score
addText("SCORE:" + score, {
x: 1,
y: 1,
color: color`2`
})
}
function endGame() {
gameStatus = 0;
setMap(levels[1]);
clearText();
addText("GAME OVER!", {
x: 5,
y: 6,
color: color`2`
})
addText("Score:", {
x: 2,
y: 8,
color: color`2`
})
addText(score.toString(), {
x: 8,
y: 8,
color: color`2`
})
addText("Play again? <i>", {
x: 2,
y: 12,
color: color`2`
})
}
function restartGame() {
gameStatus = 1; // Replay the game
blockPreviewStatus = 0;
setMap(levels[0]);
clearText();
score = 0;
availableBlocks = [];
addText("SCORE:" + score, {
x: 1,
y: 1,
color: color`2`
})
newBlocks();
}
setLegend(
[ selection, bitmap`
..22..22..22..22
..22..22..22..22
22..22..22..22..
22..22..22..22..
..22..22..22..22
..22..22..22..22
22..22..22..22..
22..22..22..22..
..22..22..22..22
..22..22..22..22
22..22..22..22..
22..22..22..22..
..22..22..22..22
..22..22..22..22
22..22..22..22..
22..22..22..22..`],
[ player, bitmap`
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222`],
[ board_top_left_corner, bitmap`
22LLLLLLLLLLLLLL
222LLLLLLLLLLLLL
L221111111111111
LL12111111111111
LL11LLLLLLLLLLLL
LL11LLLLLLLLLLLL
LL11LLL111111111
LL11LL1111111111
LL11LL1111111111
LL11LL1111111111
LL11LL1111111111
LL11LL111111LLLL
LL11LL11111LLLLL
LL11LL11111LLLLL
LL11LL11111LLL11
LL11LL11111LLL11`],
[ board_top_middle, bitmap`
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
1111111111111111
1111111111111111
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
1111111111111111
1111111111111111`],
[ board_top_right_corner, bitmap`
LLLLLLLLLLLLLL22
LLLLLLLLLLLLL222
111111111111122L
11111111111121LL
LLLLLLLLLLLL11LL
LLLLLLLLLLLL11LL
111111111LLL11LL
1111111111LL11LL
1111111111LL11LL
1111111111LL11LL
1111111111LL11LL
LLLL111111LL11LL
LLLLL11111LL11LL
LLLLL11111LL11LL
11LLL11111LL11LL
11LLL11111LL11LL`],
[ board_right_middle, bitmap`
11LLL11111LL11LL
11LLL11111LL11LL
11LLL11111LL11LL
11LLL11111LL11LL
11LLL11111LL11LL
11LLL11111LL11LL
11LLL11111LL11LL
11LLL11111LL11LL
11LLL11111LL11LL
11LLL11111LL11LL
11LLL11111LL11LL
11LLL11111LL11LL
11LLL11111LL11LL
11LLL11111LL11LL
11LLL11111LL11LL
11LLL11111LL11LL`],
[ board_bottom_right_corner,bitmap`
11LLL11111LL11LL
11LLL11111LL11LL
LLLLL11111LL11LL
LLLLL11111LL11LL
LLLL111111LL11LL
1111111111LL11LL
1111111111LL11LL
1111111111LL11LL
1111111111LL11LL
111111111LLL11LL
LLLLLLLLLLLL11LL
LLLLLLLLLLLL11LL
11111111111121LL
111111111111122L
LLLLLLLLLLLLL222
LLLLLLLLLLLLLL22`],
[ board_bottom_middle, bitmap`
1111111111111111
1111111111111111
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
1111111111111111
1111111111111111
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL`],
[ board_bottom_left_corner, bitmap`
LL11LL11111LLL11
LL11LL11111LLL11
LL11LL11111LLLLL
LL11LL11111LLLLL
LL11LL111111LLLL
LL11LL1111111111
LL11LL1111111111
LL11LL1111111111
LL11LL1111111111
LL11LLL111111111
LL11LLLLLLLLLLLL
LL11LLLLLLLLLLLL
LL12111111111111
L221111111111111
222LLLLLLLLLLLLL
22LLLLLLLLLLLLLL`],
[ board_left_middle, bitmap`
LL11LL11111LLL11
LL11LL11111LLL11
LL11LL11111LLL11
LL11LL11111LLL11
LL11LL11111LLL11
LL11LL11111LLL11
LL11LL11111LLL11
LL11LL11111LLL11
LL11LL11111LLL11
LL11LL11111LLL11
LL11LL11111LLL11
LL11LL11111LLL11
LL11LL11111LLL11
LL11LL11111LLL11
LL11LL11111LLL11
LL11LL11111LLL11`],
[ block1, bitmap`
22222222HHHHHHHH
228888888888888H
282888888888888H
H88888888888888H
H88888888888888H
H88888888888888H
H88888888888888H
H88888888888888H
H88888888888888H
H88888888888888H
H88888888888888H
H88888888888888H
H88888888888888H
H888888888888282
H888888888888822
HHHHHHHHH2222222`],
[ block2, bitmap`
22222FFFFFFFFFFF
226666666666666F
262666666666666F
F66666666666666F
F66666666666666F
F66666666666666F
F66666666666666F
F66666666666666F
F66666666666666F
F66666666666666F
F66666666666666F
F66666666666666F
F66666666666666F
F666666666666262
F666666666666622
FFFFFFFFFFF22222`],
[ block3, bitmap`
22222CCCCCCCCCCC
223333333333333C
232333333333333C
C33333333333333C
C33333333333333C
C33333333333333C
C33333333333333C
C33333333333333C
C33333333333333C
C33333333333333C
C33333333333333C
C33333333333333C
C33333333333333C
C333333333333232
C333333333333322
CCCCCCCCCCC22222`],
[ block4, bitmap`
2222255555555555
2277777777777775
2727777777777775
5777777777777775
5777777777777775
5777777777777775
5777777777777775
5777777777777775
5777777777777775
5777777777777775
5777777777777775
5777777777777775
5777777777777775
5777777777777272
5777777777777722
5555555555522222`],
[ block5, bitmap`
22222DDDDDDDDDDD
224444444444444D
242444444444444D
D44444444444444D
D44444444444444D
D44444444444444D
D44444444444444D
D44444444444444D
D44444444444444D
D44444444444444D
D44444444444444D
D44444444444444D
D44444444444444D
D444444444444242
D444444444444422
DDDDDDDDDDD22222`],
[ block6, bitmap`
22222CCCCCCCCCCC
229999999999999C
292999999999999C
C99999999999999C
C99999999999999C
C99999999999999C
C99999999999999C
C99999999999999C
C99999999999999C
C99999999999999C
C99999999999999C
C99999999999999C
C99999999999999C
C999999999999292
C999999999999922
CCCCCCCCCCC22222`],
[ brickDivideLeft, bitmap`
................
................
................
................
................
..LLLLLLLLLLLLLL
.L21111111111111
L221111111111111
L111111111111111
.L11111111111111
..LLLLLLLLLLLLLL
................
................
................
................
................`],
[ brickDivideCenter, bitmap`
................
................
................
................
................
LLLLLLLLLLLLLLLL
1111111111111111
1111111111111111
1111111111111111
1111111111111111
LLLLLLLLLLLLLLLL
................
................
................
................
................`],
[ brickDivideRight, bitmap`
................
................
................
................
................
LLLLLLLLLLLLLL..
11111111111111L.
111111111111111L
111111111111111L
11111111111111L.
LLLLLLLLLLLLLL..
................
................
................
................
................`],
[ background, bitmap`
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000`],
)
setBackground("x")
const levels = [
map`
...............
...............
abbbbbbbbcp....
hxxxxxxxxd.....
hxxxxxxxxd..vyz
hxxxxxxxxd.....
hxxxxxxxxd.....
hxxxxxxxxd.....
hxxxxxxxxd..vyz
hxxxxxxxxd.....
hxxxxxxxxd.....
gffffffffe.....`,
map`
abbbbbbbbbbbbbc
h.............d
h.............d
h.............d
h.............d
h.............d
h.............d
h.............d
h.............d
h.............d
h.............d
gfffffffffffffe`
]
setMap(levels[0])
addText("SCORE:" + score, {
x: 1,
y: 1,
color: color`2`
})
newBlocks(); // Begin with new blocks
onInput("d", () => { // Move the displayed position one position to the left
if (gameStatus == 1) {
if (blockPreviewStatus == 1) {
removeDisplayPosition(positionList[currentPositionIndex]); // Remove the current displayed position
currentPositionIndex += 1; // Move to next position
if (currentPositionIndex > positionList.length - 1) { // Check if position index is out of range, reset if so
currentPositionIndex = 0;
}
displayPosition(positionList[currentPositionIndex]); // Display the new position
}
}
})
onInput("a", () => { // Move the displayed position one position to the left
if (gameStatus == 1) {
if (blockPreviewStatus == 1) {
removeDisplayPosition(positionList[currentPositionIndex]);
currentPositionIndex -= 1;
if (currentPositionIndex < 0) { // Check if position index is out of range
currentPositionIndex = positionList.length - 1;
}
displayPosition(positionList[currentPositionIndex]);
}
}
})
onInput("s", () => {
if (blockPreviewStatus == 0) {
getFirst(player).y += 4
}
})
onInput("w", () => {
if (blockPreviewStatus == 0) {
getFirst(player).y -= 4
}
})
onInput("j", () => {
if (gameStatus == 1) {
if (blockPreviewStatus == 0) {
blockSelectionIndexes = []; // Clear prior selection indexes when new block is chosen
currentPositionIndex = 0; // Begin position indexes at 0
blockSelection = []; // Clear prior block selection when new block is chosen
getTileList(); // Get list of every tile on board
if (getFirst(player).y < 4 && availableBlocks.includes(1)) {
blockChoice = 1; // First block was selected
blockPreviewStatus = 1;
for (let y=1;y<=3;y++) {
for (let x=12;x<=14;x++) {
blockSelection.push(getTile(x,y));
}
}
getSelectedBlockIndexes();
makePositionList();
displayPosition(positionList[currentPositionIndex]);
} else if (getFirst(player).y < 8 && getFirst(player).y > 4 && availableBlocks.includes(2)) {
blockChoice = 2; // Second block was selected
blockPreviewStatus = 1;
for (let y=5;y<=7;y++) {
for (let x=12;x<=14;x++) {
blockSelection.push(getTile(x,y));
}
}
getSelectedBlockIndexes();
makePositionList();
displayPosition(positionList[currentPositionIndex]);
} else if (getFirst(player).y > 8 &&availableBlocks.includes(3)) {
blockChoice = 3; // Third block was selected
blockPreviewStatus = 1;
for (let y=9;y<=11;y++) {
for (let x=12;x<=14;x++) {
blockSelection.push(getTile(x,y));
}
}
getSelectedBlockIndexes();
makePositionList();
displayPosition(positionList[currentPositionIndex]);
}
} else if (blockPreviewStatus == 1) { // If "j" is pressed when block preview is active
availableBlocks.splice(availableBlocks.indexOf(blockChoice), 1); // Remove block from list of available blocks
removeDisplayPosition(positionList[currentPositionIndex]); // Remove the displayed position
placeBlock(positionList[currentPositionIndex]); // Place selected block
blockPreviewStatus = 0; // Turn off block preview
clearBlockChoice(blockChoice); // Delete placed block in viewer
score += blockSelectionIndexes.length // Add the value of the block to the score
newBlocks(); // Generate new blocks
blockClear(); // Clear completed rows and columns
}
}
})
onInput("l", () => {
blockPreviewStatus = 0;
removeDisplayPosition(positionList[currentPositionIndex]);
})
onInput("i", () => {
if (gameStatus == 0) {
restartGame(); // "i" restarts the game
}
})