forked from hackclub/sprig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8-Sprig.js
679 lines (623 loc) · 15.7 KB
/
8-Sprig.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
/*
@title: 8-Sprig
@author: NoozAbooz
@tags: ['puzzle','endless']
@addedOn: 2024-07-04
8-Sprig is inspired by the popular 8-puzzle game, especially the version inside of https://conicgames.github.io/exponentialidle/. There are some
new changes and tweaks to accommodate for the Sprig platform. Music by FructosePear.
*/
const cursor = "p"
const background = "b"
const one = "o"
const two = "t"
const three = "T"
const four = "f"
const five = "F"
const six = "s"
const seven = "S"
const eight = "e"
const blocks = ["o", "t", "T", "f", "F", "s", "S", "e"];
let level = 0;
let gameStarted = false;
let speedrun = false;
let statsShowing = false;
let startTime;
let endTime;
let personalBest = 10000;
setLegend(
[cursor, bitmap`
6666666666666666
66............66
6..............6
6..............6
6..............6
6..............6
6..............6
6..............6
6..............6
6..............6
6..............6
6..............6
6..............6
6..............6
66............66
6666666666666666`],
[background, bitmap`
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000`],
[one, bitmap`
................
..222222222222..
.2............2.
.2......2.....2.
.2....222.....2.
.2......2.....2.
.2......2.....2.
.2......2.....2.
.2......2.....2.
.2......2.....2.
.2......2.....2.
.2......2.....2.
.2....22222...2.
.2............2.
..222222222222..
................`],
[two, bitmap`
................
..222222222222..
.2............2.
.2...222222...2.
.2..22....22..2.
.2.........2..2.
.2.........2..2.
.2........22..2.
.2.......22...2.
.2.....222....2.
.2...222......2.
.2..22........2.
.2..222222222.2.
.2............2.
..222222222222..
................`],
[three, bitmap`
................
..222222222222..
.2............2.
.2..222222....2.
.2.......222..2.
.2.........2..2.
.2.......222..2.
.2....2222....2.
.2..2222222...2.
.2........2...2.
.2........22..2.
.2.......22...2.
.2..222222....2.
.2............2.
..222222222222..
................`],
[four, bitmap`
................
..222222222222..
.2............2.
.2..2....2....2.
.2..2....2....2.
.2..2....2....2.
.2..2....2....2.
.2..22222222..2.
.2.......2....2.
.2.......2....2.
.2.......2....2.
.2.......2....2.
.2.......2....2.
.2............2.
..222222222222..
................`],
[five, bitmap`
................
..222222222222..
.2............2.
.2...2222222..2.
.2..22........2.
.2..2.........2.
.2..22222.....2.
.2......222...2.
.2........22..2.
.2.........2..2.
.2........22..2.
.2......222...2.
.2..22222.....2.
.2............2.
..222222222222..
................`],
[six, bitmap`
................
..222222222222..
.2............2.
.2....222222..2.
.2...2........2.
.2..2.........2.
.2..2.........2.
.2.22222222...2.
.2..22....22..2.
.2..2......22.2.
.2..2.......2.2.
.2..222....22.2.
.2....222222..2.
.2............2.
..222222222222..
................`],
[seven, bitmap`
................
..222222222222..
.2............2.
.2.2222222222.2.
.2..........2.2.
.2.........22.2.
.2........22..2.
.2........2...2.
.2.......22...2.
.2......22....2.
.2....222.....2.
.2...22.......2.
.2..22........2.
.2............2.
..222222222222..
................`],
[eight, bitmap`
................
..222222222222..
.2............2.
.2....2222....2.
.2...22..22...2.
.2...2....2...2.
.2...2....2...2.
.2...222222...2.
.2....2222....2.
.2...2....2...2.
.2...2....2...2.
.2...22..22...2.
.2....2222....2.
.2............2.
..222222222222..
................`]
);
const levels = [
map`
.................
.................
.................
.................
.................
.................
.................
.................
.................
.................
..............otT
..............fFs
..............Se.`,
map`
otT
Fs.
fSe`,
map`
Sot
f.e
TFs`,
map`
FoS
f.T
est`
];
const endScreenLevel = [
map`
.................
.................
.................
.................
.................
.................
.................
.................
.................
.................
..............otT
..............fFs
..............Se.`
];
setSolids(
[one, two, three, four, five, six, seven, eight]
)
setBackground(background)
// game start, show text and instructions
setMap(levels[level])
addSprite(14, 10, cursor)
addText("Welcome to 8-Sprig!", {
x: 0,
y: 1,
color: color`2`
})
addText("-Arrange the blocks", {
x: 0,
y: 3,
color: color`2`
})
addText("in order (see below)", {
x: 0,
y: 4,
color: color`2`
})
addText("-Right D-pad to move", {
x: 0,
y: 6,
color: color`2`
})
addText("your", {
x: 0,
y: 7,
color: color`2`
})
addText("cursor", {
x: 5,
y: 7,
color: color`6`
})
addText(", left", {
x: 11,
y: 7,
color: color`2`
})
addText("D-pad to move the", {
x: 0,
y: 8,
color: color`2`
})
addText("block", {
x: 0,
y: 9,
color: color`7`
})
addText("Press Left-Up for", {
x: 0,
y: 11,
color: color`3`
})
addText("speedrun", {
x: 0,
y: 12,
color: color`3`
})
addText("Press Right-Up", {
x: 0,
y: 14,
color: color`8`
})
addText("for endless", {
x: 0,
y: 15,
color: color`8`
})
// music, define sound effects
const melody = tune`
245.9016393442623: D5^245.9016393442623,
245.9016393442623: E4/245.9016393442623 + C5^245.9016393442623,
245.9016393442623: E4~245.9016393442623 + E5-245.9016393442623,
245.9016393442623: E4/245.9016393442623 + F5-245.9016393442623 + B5~245.9016393442623,
245.9016393442623: D5^245.9016393442623 + B5~245.9016393442623,
245.9016393442623: E4/245.9016393442623 + E5-245.9016393442623 + B5~245.9016393442623,
245.9016393442623: E4~245.9016393442623 + C5^245.9016393442623,
245.9016393442623: E4/245.9016393442623 + D5^245.9016393442623,
245.9016393442623: E5^245.9016393442623,
245.9016393442623: E4/245.9016393442623 + A4^245.9016393442623,
245.9016393442623: E4~245.9016393442623 + E5-245.9016393442623,
245.9016393442623: E4/245.9016393442623 + F5-245.9016393442623 + B5~245.9016393442623,
245.9016393442623: D5^245.9016393442623 + B5~245.9016393442623,
245.9016393442623: E4/245.9016393442623 + E5-245.9016393442623 + B5~245.9016393442623,
245.9016393442623: E4~245.9016393442623 + C5^245.9016393442623,
245.9016393442623: E4/245.9016393442623 + D5^245.9016393442623,
245.9016393442623: C5^245.9016393442623,
245.9016393442623: E4/245.9016393442623,
245.9016393442623: E4~245.9016393442623 + E5-245.9016393442623,
245.9016393442623: E4/245.9016393442623 + F5-245.9016393442623 + B5~245.9016393442623,
245.9016393442623: D5^245.9016393442623 + B5~245.9016393442623,
245.9016393442623: E4/245.9016393442623 + E5-245.9016393442623 + B5~245.9016393442623,
245.9016393442623: C5^245.9016393442623 + E4~245.9016393442623,
245.9016393442623: E4/245.9016393442623 + A4^245.9016393442623,
245.9016393442623,
245.9016393442623: E4/245.9016393442623 + C5^245.9016393442623,
245.9016393442623: E5-245.9016393442623 + E4~245.9016393442623,
245.9016393442623: E4/245.9016393442623 + F5-245.9016393442623 + B5~245.9016393442623,
245.9016393442623: D5^245.9016393442623 + B5~245.9016393442623,
245.9016393442623: E4/245.9016393442623 + E5-245.9016393442623 + A4^245.9016393442623 + B5~245.9016393442623,
245.9016393442623: E4~245.9016393442623,
245.9016393442623: E4/245.9016393442623 + C5^245.9016393442623`
const click = tune`
96.7741935483871,
96.7741935483871: E4^96.7741935483871,
2903.225806451613`
const thock = tune`
312.5,
312.5: C4/312.5,
9375`
const playback = playTune(melody, Infinity)
// handle cursor input movement
onInput("i", () => {
playTune(click)
if (statsShowing === false) {
getFirst(cursor).y -= 1
}
if (gameStarted === false) {
speedrun = false;
startGame();
} else if (statsShowing === true && speedrun === false) {
nextLevel();
}
})
onInput("k", () => {
playTune(click)
getFirst(cursor).y += 1
})
onInput("j", () => {
playTune(click)
getFirst(cursor).x -= 1
})
onInput("l", () => {
playTune(click)
getFirst(cursor).x += 1
})
// handle movements for moving the entire cursor and the block selected under it
onInput("w", () => {
const cursorSprite = getFirst(cursor);
const spritesOnCursor = getTile(cursorSprite.x, cursorSprite.y);
const nextTileSprites = getTile(cursorSprite.x, cursorSprite.y - 1);
let shouldMove = true;
// Check if there is any entity in the path preventing the cursor from moving
if (nextTileSprites.some(sprite => sprite !== cursorSprite && sprite.type !== background)) {
shouldMove = false;
}
// Check if the cursor sprite is on top of another sprite
spritesOnCursor.forEach(sprite => {
if (sprite !== cursorSprite && sprite.type !== background) {
// Move both the cursor sprite and the sprite it is on top of upwards
sprite.y -= 1;
}
});
// Move the cursor sprite if conditions allow
if (shouldMove) {
cursorSprite.y -= 1;
playTune(thock)
}
if (gameStarted === false) {
speedrun = true;
startGame();
}
})
onInput("s", () => {
const cursorSprite = getFirst(cursor);
const spritesOnCursor = getTile(cursorSprite.x, cursorSprite.y);
const nextTileSprites = getTile(cursorSprite.x, cursorSprite.y + 1);
let shouldMove = true;
// Check if there is any entity in the path preventing the cursor from moving
if (nextTileSprites.some(sprite => sprite !== cursorSprite && sprite.type !== background)) {
shouldMove = false;
}
// Check if the cursor sprite is on top of another sprite
spritesOnCursor.forEach(sprite => {
if (sprite !== cursorSprite && sprite.type !== background) {
// Move both the cursor sprite and the sprite it is on top of upwards
sprite.y += 1;
}
});
// Move the cursor sprite if conditions allow
if (shouldMove) {
cursorSprite.y += 1;
playTune(thock)
}
})
onInput("a", () => {
const cursorSprite = getFirst(cursor);
const spritesOnCursor = getTile(cursorSprite.x, cursorSprite.y);
const nextTileSprites = getTile(cursorSprite.x - 1, cursorSprite.y);
let shouldMove = true;
// Check if there is any entity in the path preventing the cursor from moving
if (nextTileSprites.some(sprite => sprite !== cursorSprite && sprite.type !== background)) {
shouldMove = false;
}
// Check if the cursor sprite is on top of another sprite
spritesOnCursor.forEach(sprite => {
if (sprite !== cursorSprite && sprite.type !== background) {
// Move both the cursor sprite and the sprite it is on top of upwards
sprite.x -= 1;
}
});
// Move the cursor sprite if conditions allow
if (shouldMove) {
cursorSprite.x -= 1;
playTune(thock)
}
})
onInput("d", () => {
const cursorSprite = getFirst(cursor);
const spritesOnCursor = getTile(cursorSprite.x, cursorSprite.y);
const nextTileSprites = getTile(cursorSprite.x + 1, cursorSprite.y);
let shouldMove = true;
// Check if there is any entity in the path preventing the cursor from moving
if (nextTileSprites.some(sprite => sprite !== cursorSprite && sprite.type !== background)) {
shouldMove = false;
}
// Check if the cursor sprite is on top of another sprite
spritesOnCursor.forEach(sprite => {
if (sprite !== cursorSprite && sprite.type !== background) {
// Move both the cursor sprite and the sprite it is on top of upwards
sprite.x += 1;
}
});
// Move the cursor sprite if conditions allow
if (shouldMove) {
cursorSprite.x += 1;
playTune(thock)
}
})
// called upon main menu input
function startGame() {
gameStarted = true;
clearText()
nextLevel()
startTime = performance.now()
}
// special level switcher logic
function next level() {
setSolids([]) // allow block collisions temporarily to allow setting random puzzle config
if (speedrun === true) { // actually switch levels since we are in speedrun mode
if (levels.length - 1 == level) {
gameOver();
} else {
//console.log("next level");
level++;
setMap(levels[level])
addSprite(0, 0, cursor)
}
} else { // if in endless mode
clearText()
statsShowing = false;
level = 1; // keep level the same for simplicity
setMap(levels[level])
addSprite(0, 0, cursor)
startTime = performance.now()
// generate new puzzle configuration
let puzzle = generateRandomPuzzle();
for (let i = 0; i < blocks.length; i++) { // loop for every tile and get its position from the configuration
let tile = i + 1;
let position = getTilePosition(puzzle, tile);
let tileSprite = getFirst(blocks[i]);
tileSprite.x = position.x
tileSprite.y = position.y
//console.log(`Tile ${blocks[i]} is at position: (${position.x}, ${position.y})`);
}
printPuzzle(puzzle);
}
setSolids( // bring collisions back now that we've set all positions
[one, two, three, four, five, six, seven, eight]
)
}
function endlessStats() { // stats screen after every solve in endless mode
statsShowing = true;
setMap(endScreenLevel[0])
endTime = performance.now()
var timeDiff = endTime - startTime; //in ms
// strip the ms and convert to seconds
timeDiff /= 1000;
var seconds = +timeDiff.toFixed(2);
if (seconds < personalBest) {
personalBest = seconds;
//console.log(personalBest)
}
addText(`Time: ${seconds} sec`, { x: 3, y: 2, color: color`3` })
addText(`PB: ${personalBest} sec`, { x: 4, y: 4, color: color`3` })
addText("Press Right-Up", {
x: 3,
y: 10,
color: color`3`
})
addText("to continue", {
x: 4,
y: 11,
color: color`3`
})
}
function gameOver() { // only in speedrun mode
setMap(endScreenLevel[0])
endTime = performance.now()
var timeDiff = endTime - startTime; //in ms
// strip the ms and convert to seconds
timeDiff /= 1000;
var seconds = +timeDiff.toFixed(2);
addText("Game Over!", {
x: 5,
y: 2,
color: color`2`
})
addText(`Time: ${seconds} sec`, { x: 4, y: 4, color: color`4` })
addText("Did you beat my PB?", {
x: 1,
y: 7,
color: color`2`
})
addText("It was 135s", {
x: 4,
y: 8,
color: color`2`
})
playback.end()
}
afterInput(() => {
const firstRowAligned = getFirst(one).x < getFirst(two).x && getFirst(two).x < getFirst(three).x && [one, two, three].every(type => getFirst(type).y === 0);
const secondRowAligned = getFirst(four).x < getFirst(five).x && getFirst(five).x < getFirst(six).x && [four, five, six].every(type => getFirst(type).y === 1);
const thirdRowAligned = getFirst(seven).x === 0 && getFirst(eight).x === 1 && [seven, eight].every(type => getFirst(type).y === 2);
if (firstRowAligned && secondRowAligned && thirdRowAligned) {
if (speedrun === true) {
nextLevel();
} else {
endlessStats();
}
} else if (statsShowing === true) {
nextLevel();
}
})
// all of this is dedicated to endless mode random setup generation, geeksforgeeks FTW
function generateRandomPuzzle() {
const puzzle = [...Array(9).keys()];
function shuffle(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 countInversions(array) {
let inversions = 0;
for (let i = 0; i < array.length - 1; i++) {
for (let j = i + 1; j < array.length; j++) {
if (array[i] > array[j] && array[i] !== 0 && array[j] !== 0) {
inversions++;
}
}
}
return inversions;
}
let isSolvable = false;
while (!isSolvable) {
shuffle(puzzle);
const inversions = countInversions(puzzle);
isSolvable = inversions % 2 === 0;
}
return puzzle;
}
function printPuzzle(puzzle) {
for (let i = 0; i < 9; i += 3) {
//console.log(puzzle.slice(i, i + 3).join(" "));
}
}
function getTilePosition(puzzle, tile) {
const index = puzzle.indexOf(tile);
if (index === -1) {
return null;
}
const x = index % 3;
const y = Math.floor(index / 3);
return { x, y };
}