-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.ts
587 lines (586 loc) · 16.1 KB
/
main.ts
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
// Funkcja ustawia na odpowiednim miejscu robota i rozbitego astronautę oraz ile maksymalnie paliwa posiada robot
// Function sets x,y coordinates of robot and crashed astronaut as well as max fuel available for robot
function doStart (robotx: number, roboty: number, crashx: number, crashy: number, fuelmax: number) {
listX = []
listY = []
// 0-Puste/robot run
// 1-Brakło paliwa / empty fuel
// 2-Wygrałeś / Winner
efekt = 0
paliwo = fuelmax
score = 0
rx = robotx
ry = roboty
robot.set(LedSpriteProperty.X, rx)
robot.set(LedSpriteProperty.Y, ry)
crashedastronaut.set(LedSpriteProperty.X, crashx)
crashedastronaut.set(LedSpriteProperty.Y, crashy)
}
// Robot goes down for "ile" steps
function doFDown (ile: number) {
for (let index = 0; index < ile; index++) {
doDown()
}
}
// Przemek - snail, slimak
function doPrzemek () {
kierunek = doRadar()
// slimak idzie w prawo i w górę
if (kierunek == 1) {
// slimak idzie w gore i prawo
doFUp(4)
doFRight(4)
doFDown(4)
doFLeft(3)
doFUp(3)
doFRight(2)
doFDown(2)
doLeft()
doUp()
} else if (kierunek == 2) {
// slimak idzie w prawo i w gore
doFRight(4)
doFUp(4)
doFLeft(4)
doFDown(3)
doFRight(3)
doFUp(2)
doFLeft(2)
doDown()
doRight()
} else if (kierunek == 3 || kierunek == 4) {
// slimak idzie w dol i w prawo
doFDown(4)
doFRight(4)
doFUp(4)
doFLeft(3)
doFDown(3)
doFRight(2)
doFUp(2)
doLeft()
doDown()
} else if (kierunek == 5 || kierunek == 6) {
// slimak idzie w dol i w lewo
doFDown(4)
doFLeft(4)
doFUp(4)
doFRight(3)
doFDown(3)
doFLeft(2)
doFUp(2)
doRight()
doDown()
} else if (kierunek == 7 || kierunek == 8) {
// slimak idzie w gore i w lewo
doFUp(4)
doFLeft(4)
doFDown(4)
doFRight(3)
doFUp(3)
doFLeft(2)
doFDown(2)
doRight()
doUp()
}
}
// Robot goes right for "ile" steps
function doFRight (ile: number) {
for (let index = 0; index < ile; index++) {
doRight()
}
}
// Funkcja ustawia wszystkie możliwe pozycje robota oraz miejsce rozbicia sie astronauty, nalewa też paliwa do baku robota
// Function sets all possible positions of robot & astronaut, refuels robot as well
function doWszystkieOpcje () {
// four loops/cztery pętle
for (let x1 = 0; x1 <= 4; x1++) {
for (let x2 = 0; x2 <= 4; x2++) {
for (let y1 = 0; y1 <= 4; y1++) {
for (let y2 = 0; y2 <= 4; y2++) {
if (x1 == x2 && y1 == y2) {
} else {
// tylko jeśli astronauta nie rozbił się w bazie robota/avoid crashing astronaut in robot base
doStart(x1, y1, x2, y2, rozmiarBaku)
// tutaj wstawiamy funkcję poruszającą robotem
// here put robot steering function
doPrzemek()
if (efekt == 1) {
// braklo paliwa/ empty fuel
porazka += 1
} else if (efekt == 2) {
// odnaleziony!!!/success - found!!!
sukces += 1
}
}
}
}
}
}
}
// Robot goes up for "ile" steps
function doFUp (ile: number) {
for (let index = 0; index < ile; index++) {
doUp()
}
}
// Funkcja RadarX() - na wejściu otrzymuje współrzędne astronauty sx,sy oraz robota rx,ry
// pokazuje kierunek w którym znajduje się rozbity astronauta
// Function RadarX() - presents clockwise potential astronaut crash place.
//
// Zwracane wartości - robot umiejscowiony w środku planszy (0) - w zależności w której części planszy jest astronauta
// - zwracana jest odpowiednia cyfra
// Returnig values - robot place id the middle of the screen (0) - depending where astronaut is crashed
// - proper number is returned
// 88122
// 88122
// 77033
// 66544
// 66544
//
// // Jak działa funkcja radar:
// // what are the calculations?
// // "sx=rx" "sy>ry" 5
// // "sx=rx" "sy<ry" 1
// // "sy=ry" "sx<rx" 7
// // "sy=ry" "sx>ry" 3
// // "sy<ry" "sx>rx" 2
// // "sy>ry" "sx>rx" 4
// // "sy>ry" "sx<rx" 6
// // "sy<ry" "sx<rx" 8
function doRadarX (sx: number, sy: number, rx: number, ry: number) {
if (sx == rx && sy > ry) {
return 5
}
if (sx == rx && sy < ry) {
return 1
}
if (sy == ry && sx < rx) {
return 7
}
if (sy == ry && sx > rx) {
return 3
}
if (sy < ry && sx > rx) {
return 2
}
if (sy > ry && sx > rx) {
return 4
}
if (sy > ry && sx < rx) {
return 6
}
if (sy < ry && sx < rx) {
return 8
}
return 0
}
// Maks
function doMaks () {
for (let index = 0; index < 100; index++) {
kierunek = doRadar()
if (kierunek == 2 || kierunek == 3 || kierunek == 4) {
for (let index = 0; index < 2; index++) {
doFDown(1)
doFRight(1)
doFUp(1)
doFRight(1)
}
} else {
for (let index = 0; index < 2; index++) {
doFLeft(1)
doFUp(1)
doFLeft(1)
doFDown(1)
doFLeft(1)
doFUp(1)
doFLeft(1)
}
}
doFDown(4)
}
}
// Robot goes 1 step right, checks if astronaut found
function doRight () {
if (efekt == 0) {
oldry = ry
oldrx = rx
rx += 1
if (rx > 4) {
if (krawedzie == 0) {
rx = 0
} else {
rx = 4
}
}
robot.set(LedSpriteProperty.X, rx)
doCheckSkarb()
}
}
// Michalina - line & down, linijka i w dol
function doMichalina () {
for (let index = 0; index < 100; index++) {
kierunek = doRadar()
if (kierunek == 3) {
doFRight(4)
} else if (kierunek == 7) {
doFLeft(4)
} else if (kierunek == 1) {
doFUp(4)
} else if (kierunek == 5) {
doFDown(4)
} else if (kierunek == 2) {
doUp()
doRight()
} else if (kierunek == 8) {
doUp()
doLeft()
} else if (kierunek == 4) {
doDown()
doRight()
} else if (kierunek == 6) {
doDown()
doLeft()
}
}
}
// Wstawka na święta
function dosilentNight () {
basic.showLeds(`
. . # . .
. # # # .
. # # # .
# # # # #
. . # . .
`)
basic.pause(1000)
music.setTempo(55)
for (let index = 0; index < 2; index++) {
music.playTone(392, music.beat(BeatFraction.Whole))
music.playTone(440, music.beat(BeatFraction.Half))
music.playTone(392, music.beat(BeatFraction.Half))
music.playTone(330, music.beat(BeatFraction.Whole))
}
music.playTone(587, music.beat(BeatFraction.Half))
music.playTone(587, music.beat(BeatFraction.Half))
music.playTone(494, music.beat(BeatFraction.Whole))
music.playTone(523, music.beat(BeatFraction.Half))
music.playTone(523, music.beat(BeatFraction.Half))
music.playTone(392, music.beat(BeatFraction.Half))
}
// Funkcja sprawdza czy robot znalazł rozbitego astronautę, zużywa paliwo robota na wykonanie kroku, prezentuje wynik
// jeśli znalazł astronautę - serduszko, jeśli brakło paliwa - czaszkę (w zależności od parametru wizual = true)
// Function checks if robot found crashed astronaut, deduct robot fuel, show results - hear for success, skull for emty Melodies.Fuel
// (depending on the "wizual" flag = true)
function doCheckSkarb () {
// one more step
score += 1
// less fuel
paliwo += -1
if (wizual) {
// wait - for better presentation
basic.pause(pausa)
}
if (paliwo <= 0) {
// empty fuel
// set flag - empty fuel - robot will not move anymore
efekt = 1
if (wizual) {
basic.showIcon(IconNames.Skull)
}
}
if (rx == crashedastronaut.get(LedSpriteProperty.X) && ry == crashedastronaut.get(LedSpriteProperty.Y)) {
// astronaut found! Success! set flag - success - robot will not move anymore
efekt = 2
if (wizual) {
basic.showIcon(IconNames.Heart)
if (randint(0, 5) == 0) {
dosilentNight()
}
}
}
if (doSprawdz(robot.get(LedSpriteProperty.X), robot.get(LedSpriteProperty.Y)) == 0) {
listX.push(robot.get(LedSpriteProperty.X))
listY.push(robot.get(LedSpriteProperty.Y))
}
}
function doSprawdz (X: number, Y: number) {
if (listX.length > 0) {
for (let index82 = 0; index82 <= listX.length; index82++) {
if (listX[index82] == X && listY[index82] == Y) {
return 1
}
}
}
return 0
}
// Robot goes left for "ile" steps
function doFLeft (ile: number) {
for (let index = 0; index < ile; index++) {
doLeft()
}
}
// Funkcja wywołuje funkcję radarX() podając współrzędne robota oraz rozbitego astronauty - tak aby nie trzeba było
// każdorazowo zastanawiać się gdzie jest robot i game.addLife(0)
// Function calls RadarX() providing coordinates of robot and astronaut
function doRadar () {
return doRadarX(robot.get(LedSpriteProperty.X), robot.get(LedSpriteProperty.Y), crashedastronaut.get(LedSpriteProperty.X), crashedastronaut.get(LedSpriteProperty.Y))
}
// Robot goes 1 step down, checks if astronaut found
function doDown () {
if (efekt == 0) {
oldry = ry
oldrx = rx
ry += 1
if (ry > 4) {
if (krawedzie == 0) {
ry = 0
} else {
ry = 4
}
}
robot.set(LedSpriteProperty.Y, ry)
doCheckSkarb()
}
}
// Adam - total randomized, calkiem losowo + Radar
function doAdam () {
for (let index = 0; index < 100; index++) {
kierunek = randint(1, 15)
if (kierunek == 1) {
nX = robot.get(LedSpriteProperty.X)
nY = robot.get(LedSpriteProperty.Y) + 1
if (nY > 4) {
nY = 0
}
if (doSprawdz(nX, nY) == 0) {
doDown()
} else {
kierunek = randint(2, 15)
}
} else if (kierunek == 2) {
doRight()
} else if (kierunek == 3) {
doLeft()
} else if (kierunek == 4) {
doUp()
} else if (kierunek == 5) {
doFRight(randint(1, 3))
} else if (kierunek == 6) {
doFLeft(randint(1, 3))
} else if (kierunek == 7) {
doFUp(randint(1, 3))
} else if (kierunek == 8) {
doFDown(randint(1, 3))
} else if (kierunek >= 9) {
kierunek = doRadar()
if (kierunek == 1) {
doFUp(4)
}
if (kierunek == 3) {
doFRight(4)
}
if (kierunek == 5) {
doFDown(4)
}
if (kierunek == 7) {
doFLeft(4)
}
if (kierunek == 2) {
doUp()
doRight()
}
if (kierunek == 4) {
doDown()
doRight()
}
if (kierunek == 6) {
doDown()
doLeft()
}
if (kierunek == 8) {
doUp()
doLeft()
}
}
}
}
// Funkcja losuje pozycję robota oraz miejsce rozbicia sie astronauty, nalewa też paliwa do baku robota
// Function rendomize position of robot & astronaut, refuels robot as well
function doLosujRobotiRozbitek () {
// randomize
x12 = randint(0, 4)
x22 = randint(0, 4)
y12 = randint(0, 4)
y22 = randint(0, 4)
while (x12 == x22 && y12 == y22) {
// avoid crashing astronaut in the robot base
if (x12 == x22) {
x12 = randint(0, 4)
} else if (y12 == y22) {
y12 = randint(0, 4)
}
}
// sets the starting positions
doStart(x12, y12, x22, y22, rozmiarBaku)
// tutaj wstawiamy naszą funkcję sterującą robotem
// place here robot steering function
doMaks()
if (efekt == 1) {
// jesli braklo paliwa / emptu fuel
porazka += 1
} else if (efekt == 2) {
// uratowany! / astronaut found!
sukces += 1
} else {
// jesli braklo paliwa / emptu fuel
porazka += 1
}
}
// Rafal - losowo ale pojedynczo
function doRafal () {
for (let index = 0; index < 100; index++) {
kierunek = randint(1, 2)
if (kierunek == 1) {
doFDown(1)
} else if (kierunek == 2) {
doFRight(1)
} else if (kierunek == 3) {
doFLeft(1)
} else if (kierunek == 4) {
doFUp(1)
}
}
}
// Robot goes 1 step left, checks if astronaut found
function doLeft () {
if (efekt == 0) {
oldry = ry
oldrx = rx
rx += -1
if (rx < 0) {
if (krawedzie == 0) {
rx = 4
} else {
rx = 0
}
}
robot.set(LedSpriteProperty.X, rx)
doCheckSkarb()
}
}
// Michal - szlaczki
function doMichal () {
for (let index = 0; index < 100; index++) {
doFRight(2)
doFDown(2)
doFLeft(2)
doFUp(2)
doFLeft(2)
doFDown(2)
doFUp(2)
doFRight(2)
doFLeft(2)
doFUp(2)
doFDown(2)
doFLeft(2)
doFDown(2)
doFLeft(2)
}
}
// Robot goes 1 step up, checks if astronaut found
function doUp () {
if (efekt == 0) {
oldry = ry
oldrx = rx
ry += -1
if (ry < 0) {
if (krawedzie == 0) {
ry = 4
} else {
ry = 0
}
}
robot.set(LedSpriteProperty.Y, ry)
doCheckSkarb()
}
}
/**
* szybkie poruszanie robotem / very fast calculation
*/
/**
* wizual=0 do wielokrotnych symulacji, wizual=1 porusza wolniej robotem i prezentuje wyniki
*/
/**
* astrunauta/astronaut - uzywamy sprite
*/
/**
* robot - uzywamy sprite
*/
let y22 = 0
let y12 = 0
let x22 = 0
let x12 = 0
let nY = 0
let nX = 0
let oldrx = 0
let oldry = 0
let porazka = 0
let kierunek = 0
let score = 0
let efekt = 0
let crashedastronaut: game.LedSprite = null
let krawedzie = 0
let robot: game.LedSprite = null
let ry = 0
let rx = 0
let paliwo = 0
let pausa = 0
let wizual = 0
let rozmiarBaku = 0
let listY: number[] = []
let listX: number[] = []
let sy = 0
let sx = 0
let sukces = 0
listX = []
listY = []
// tutaj ustaw pojemność baku robota / here put fuel max volume
rozmiarBaku = 15
wizual = 1
if (wizual) {
// jesli flaga ustawiona na "true/1" to pokazuj wolniej / if flag set to "true/1" show robot moves
pausa = 200
}
// uzupełnij paliwo / refuel
paliwo = rozmiarBaku
rx = 0
ry = 0
robot = game.createSprite(rx, ry)
krawedzie = 0
// robot nie miga / does not blink
robot.set(LedSpriteProperty.Blink, 0)
crashedastronaut = game.createSprite(randint(1, 4), randint(1, 4))
crashedastronaut.set(LedSpriteProperty.Blink, 400)
doStart(1, 1, 1, 1, 20)
basic.forever(function () {
// tutaj wstawiasz wywolanie funkcji sprawdzajacej efektywnosc algorytmu
for (let index = 0; index < 1000; index++) {
doLosujRobotiRozbitek()
}
// remove robot from memory
robot.delete()
// remove astronaut from memory
crashedastronaut.delete()
basic.clearScreen()
while (true) {
// present results - pokaz ile razy odnalazł astrunautę i ile razy brakło paliwa
// porazka/failure
basic.showString("F:")
basic.showNumber(porazka)
// sukces/success
basic.showString("S:")
basic.showNumber(sukces)
// efekt/effect
basic.showString("E:" + Math.floor(sukces/10).toString() + "%")
}
})