-
Notifications
You must be signed in to change notification settings - Fork 1
/
t.fiv
899 lines (884 loc) · 17.6 KB
/
t.fiv
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
T
!00000573
<scoreboard>
<random>
<clock>
: t
['] tickbeat drop \ Include this man
titlescreen \ put up a title screen
init_rand \ initialize the random number generator
cls
begin
5 10 gotoxy ." Play level (0-9) or ESC? "
0 begin
drop key
dup 27 = if
drop cls
[ TURNKEYING -1 = ] IF{
vbname 1 open if
." Writing out high scores " cr
vboard slotwidth 10 * stack abc|abca write drop drop
close if else drop endif
else
." error " . ." writing out high scores."
endif
exit
}ELSE{ [ TURNKEYING 1 = ] IF{
snapshot
bye
}ELSE{
exit
}ENDIF
}ENDIF
endif
` 0 - dup 10 u<
until 10 swap - speed ! \ Figure speed
0 score ! 0 rows ! \ New game
title \ Show off
0 score+ \ Show score
stats 7 4 * 0 fill \ erase the statistics area
0 svoffset gotoxy
7 0 do i . ." : 0" cr loop \ put up the block numbers
0 svoffset 7 + gotoxy ." # dropped:"
play \ Play game
cls
0 score+ \ Show score
sethighscore \ set new high score, if any
5 10 gotoxy ." Press ESC..."
begin key 27 = until
0 until
;
-*-
v
TURNKEYING
!00000062
\ -1 turnkey
\ 0 memory
\ 1 snapshot
0 constant turnkeying
-*-
>
.R
!0000004B
: .r ( n w -- )
swap <# #s #> stack abc|bcac - 0 max spaces type
;
-*-
>
<TIME>
!00000020
\ Time package
package <time>
-*-
v
MONTH
!000000F1
\ Table of days per month
create month
0 w,
31 w, \ Jan
59 w, \ Feb
90 w, \ Mar
120 w, \ Apr
151 w, \ May
181 w, \ Jun
212 w, \ Jul
243 w, \ Aug
273 w, \ Sep
304 w, \ Oct
334 w, \ Nov
365 w, \ Dec
-*-
>
DAY
!0000001E
\ Day of month
variable day
-*-
>
MO
!00000016
\ Month
variable mo
-*-
>
YR
!00000021
\ Years since 1980
variable yr
-*-
>
GETIME
!000003DF
\ ( -> time ) Return the time in seconds since Jan 1, 1980
: getime
0 0 0 h# 2A00 h# 21 int stack abcde|c \ Get the date
dup h# FFFF and 1980 - yr ! \ Figure years
16 shr dup h# FF and day ! 8 shr 1 - mo ! \ Figure day of month, month
-1 day +! \ Zero base days
mo @ 1 shl month + w@ day +! \ Add days for months to total
yr @ dup 365 * over 2/ 2/ + 1+ day +! \ Add days for years to total
3 and 0= if \ Leap year?
mo @ 2 <= if \ Before Feburary?
-1 day +! \ Yes, fix overkill for leap year
endif
endif
0 0 0 h# 2C00 h# 21 int stack abcde|c \ Get the time
dup h# FF and 60 * mo ! \ Store minutes as seconds
8 shr dup h# FF and 3600 * mo +! \ Add hours as minutes
16 shr mo +! \ Add seconds
day @ 86400 * mo @ + \ Add seconds for days to total
;
-*-
>
DELAY
!00000090
\ ( time -> ) Delay given number of seconds
: delay
getime +
begin
?term if drop exit endif
getime over >
until
drop
;
-*-
^
>
<SCOREBOARD>
!00000016
package <scoreboard>
-*-
v
ROWS
!00000029
\ Number of rows cleared
variable rows
-*-
>
SPEED
!0000001E
\ Game speed
variable speed
-*-
>
SCORE
!00000028
\ Your worthless score
variable score
-*-
>
VBNAME
!00000021
: vbname
" vanity.brd" 1+
;
-*-
>
SLOTWIDTH
!00000017
17 constant SLOTWIDTH
-*-
>
VBOARD
!00000042
create vboard 10 SLOTWIDTH * allot
vboard 10 SLOTWIDTH * 0 fill
-*-
>
SVOFFSET
!00000016
15 constant svoffset
-*-
>
SHOWSCORE
!0000013B
: showscore ( indx -- )
indx !
20 12 indx @ + gotoxy indx @ 1 + 2 .r 32 emit \ print out score #
vboard indx @ SLOTWIDTH * + dup count type \ print out name
dup 12 + @ 40 12 indx @ + gotoxy 4 .r \ print out score
16 + c@ 47 12 indx @ + gotoxy 2 .r \ print out level
;
-*-
v
INDX
!0000000F
variable indx
-*-
^
>
SHOWSCORES
!00000035
: showscores
10 0 do
i showscore
loop
;
-*-
>
TITLESCREEN
!00000131
: titlescreen
cls
[ TURNKEYING ] IF{
vbname 2 open
if
vboard [ 10 SLOTWIDTH * ] literal stack abc|abca read drop drop
close if else drop endif
else
drop vbname 0 createfile drop drop
endif
}ENDIF
30 10 gotoxy ." Tetris "
showscores cr ." Press a key..." key drop
;
-*-
>
SETHIGHSCORE
!0000049D
: sethighscore
score @ vboard
10 0 do \ search for a slot
i SLOTWIDTH * over + 12 + @
stack abc|abca
< if \ found one
dup i SLOTWIDTH * + dup \ source for move
dup SLOTWIDTH + \ dest for move
stack ab|baab \ kludge around fifth 2.6 bug
10 i - 1- SLOTWIDTH * \ length for move
dup 0= if stack abcde| else
move \ make room in table for new score
c@ swap c! \ rest of kludge
endif
dup 12 0 fill \ erase the name
dup 12 + score @ swap ! \ stuff the score in
dup 16 + speed @ 10 swap - swap c! \ stuff the level in
showscores \ put up the scoreboard
23 12 i + gotoxy \ move to the right place
1+ 11 expect \ get name
dup -1 = if drop 0 endif \ handle esc's and fkeys
swap 1- c! \ stuff in length
leave
endif
loop drop drop
showscores
;
-*-
>
SCORE+
!00000115
\ ( num -> ) Add to score, redisplay
<scoreboard>
: score+
11 speed @ - * score +! \ Scale score by speed
5 5 gotoxy
." Score: " score @ 6 .r \ Show score
5 6 gotoxy
." Rows: " rows @ 6 .r
5 7 gotoxy
." Level: " 10 speed @ - 6 .r
;
-*-
^
>
<RANDOM>
!00000012
package <random>
-*-
v
STATS
!0000002E
create stats 7 4 * allot
stats 7 4 * 0 fill
-*-
>
_R_TABLESIZE
!0000001A
55 constant _R_TABLESIZE
-*-
>
_R_TABLE
!00000028
create _r_table _R_TABLESIZE 4 * allot
-*-
>
_R_IDX
!00000011
variable _r_idx
-*-
>
SEED
!0000000F
variable seed
-*-
>
IRAND
!0000004C
\ ( -> rand )
<scoreboard>
: irand
seed @ 47421 * 917 + dup seed !
;
-*-
>
NRAND
!00000252
\ ( n -> rand ) Generate random number 0 -- n using
\ the additive congruential method
<scoreboard>
: nrand
_r_table dup _r_idx @ 2 shl + @ \ get a[i]
_r_idx @ 1+ _R_TABLESIZE mod stack abc|acbac
2 shl + @ \ get a[(i+1)%tablesize]
xor >r \ xor the two nums
swap _r_idx @ 2 shl + r@ 1+ swap ! \ update the seed_table
_r_idx ! \ update the table ptr
r> irand + 8 shr swap mod \ fixup the rand num
;
-*-
>
INIT_RAND
!000001B7
\ initialize the random number generator
<scoreboard> <time>
: init_rand ( -- )
getime seed ! \ base our random # on our clock
_R_TABLESIZE 0 do
irand _r_table i 2 shl + ! \ stuff the seed table
loop
0 _r_idx ! \ initialize the index
stats [ 7 4 * ] literal 0 fill \ initialize the statistics table
1000 0 do
31415 nrand drop
loop
;
-*-
>
RAND
!000000DE
\ ( -> rand ) Generate random number 0-6
<scoreboard>
: rand
7 nrand \ get random num
dup 2 shl stats +
dup @ 1+ stack abc|acacb
! 5 swap svoffset + gotoxy 4 .r
;
-*-
>
TEST_RAND
!00000044
: test_rand
init_rand
begin
chisquare
?term until
;
-*-
v
CHISQUARE
!00000139
: chisquare ( -- )
stats [ 7 4 * ] literal 0 fill
NUMREPS 0 do \ collect random numbers
7 nrand \ get a random number
2 shl stats + 1 swap +! \ and update the table
loop
0
7 0 do
i 2 shl stats + @ dup * +
loop
7 * NUMREPS / NUMREPS - 7 - abs .
;
-*-
v
NUMREPS
!00000016
300 constant NUMREPS
-*-
^
^
^
>
<CLOCK>
!00000011
package <clock>
-*-
v
TICKER
!00000027
\ Current clock tick
variable ticker
-*-
>
TICKBEAT
!00000062
\ Interrupt to bump the ticker counter 18.2 times a second
28 :int tickbeat
1 ticker +!
;
-*-
>
TICKER@
!00000024
: ticker@
ticker int[ @ ]int
;
-*-
^
>
<SCREEN>
!00000012
package <screen>
-*-
v
XXX
!0000006C
: xxx
cls
256 0 do
i screen i 10 * + c!
i 5 * 1+ 80 /mod gotoxy i .
loop
key drop
;
-*-
>
PS
!0000005C
( x y char -> ) \ Put char on screen
: ps
>r 160 * swap 1 shl + screen + r> swap c!
;
-*-
>
GS
!00000053
( x y -> char ) \ Get char from screen
: gs
160 * swap 1 shl + screen + c@
;
-*-
>
WIDX
!00000028
\ Width of play area
20 constant widx
-*-
>
COL
!00000024
\ Starting column
40 constant col
-*-
>
BCOLOR
!00000028
\ Background color
32 constant bcolor
-*-
^
>
TITLE
!00000195
\ Useless title
<screen>
: title
cls
24 0 do
col 1- i 186 ps
col 2- i 186 ps
col widx + i 186 ps
col widx + 1+ i 186 ps
loop
24 0 do
col widx + col do
i j bcolor ps
2 +loop
loop
col 2- 24 200 ps
col 1- 24 202 ps
col widx + 24 202 ps
col widx + 1+ 24 188 ps
widx 0 do
col i + 24 205 ps
loop
;
-*-
>
PLAY
!0000013E
\ Play game till dead
: play
0 #dropped ! \ so far, we haven't dropped any
0 next ! launch \ Set new next piece
launch draw-me \ Somebody new
begin dokey if exit endif \ Handle keys until ESC or dead
fall until \ Fall until dead
;
-*-
v
PIECES
!00000495
\ A list of the pieces, 4 rotations of 7 varieties
create pieces
o o o o o x o o o o o o o x o o
x x x x o x o o x x x x o x o o
o o o o o x o o o o o o o x o o
o o o o o x o o o o o o o x o o
o x o o o o o o o x o o o x o o
o x x o x x x o x x o o x x x o
o x o o o x o o o x o o o o o o
o o o o o o o o o o o o o o o o
o x o o o o o o o o o o o o o o
o x o o o x x x o x x o o o x o
o x x o o x o o o o x o x x x o
o o o o o o o o o o x o o o o o
o o x o o o o o o o o o o o o o
o o x o o x o o o x x o x x x o
o x x o o x x x o x o o o o x o
o o o o o o o o o x o o o o o o
o x o o o o o o o o o o o o o o
o x x o o o x x o x o o o x x o
o o x o o x x o o x x o x x o o
o o o o o o o o o o x o o o o o
o o x o o o o o o o o o o o o o
o x x o o x x o o o x o x x o o
o x o o o o x x o x x o o x x o
o o o o o o o o o x o o o o o o
o o o o o o o o o o o o o o o o
o x x o o x x o o x x o o x x o
o x x o o x x o o x x o o x x o
o o o o o o o o o o o o o o o o
-*-
v
O
!0000000C
: o 0 c, ;
-*-
>
X
!0000000C
: x 1 c, ;
-*-
^
>
CUR-PIECE
!0000004E
\ The current piece - as an offset into the pieces array
variable cur-piece
-*-
>
X
!00000025
\ Upper-left X of piece
variable x
-*-
>
Y
!00000025
\ Upper-left Y of piece
variable y
-*-
>
PIECE-NUM
!00000024
\ Piece number
variable piece-num
-*-
>
DRAW-ME
!000000EF
\ ( -> ) Draw a piece at X,Y
<screen>
: draw-me
4 0 do
4 0 do
cur-piece @ i + j 4 shl + c@ if
i i + x @ + j y @ + over 1+ over piece-num @ color-tab + c@
>r r@ ps r> ps
endif
loop
loop
;
-*-
v
COLOR-TAB
!00000044
create color-tab
176 c, 177 c, 178 c, 219 c, 176 c, 177 c, 178 c,
-*-
^
>
ERASE-ME
!000000CD
\ ( -> ) Draw a piece at X,Y
<screen>
: erase-me
4 0 do
4 0 do
cur-piece @ i + j 4 shl + c@ if
i i + x @ + j y @ + over 1+ over 32 ps bcolor ps
endif
loop
loop
;
-*-
>
TEST-ME
!000000F0
\ ( -> test-me) Test for ok
<screen>
: test-me
4 0 do
4 0 do
cur-piece @ i + j 4 shl + c@ if
i i + x @ + j y @ + gs bcolor = not if 1 exit endif
endif
loop
loop
0 \ All OK
;
-*-
>
NEXT
!00000021
\ The next piece
variable next
-*-
>
#DROPPED
!0000003F
\ # of blocks dropped so far
variable #dropped
0 #dropped !
-*-
>
LAUNCH
!0000025D
\ ( -> ) Launch a new piece
<scoreboard>
<random>
<screen>
: launch
next @ dup setpiece \ Set old next piece & save
70 x ! 18 y ! erase-me \ Erase old next piece
rand dup next ! setpiece \ Figure new next piece
draw-me \ Draw new next piece
setpiece \ Set old next as current again
col widx 2/ + 2- x ! \ Middle of screen
0 y ! \ At top
1 #dropped +! \ we have dropped one more piece
12 svoffset 7 + gotoxy #dropped @ 4 .r \ print out total cnt
;
-*-
v
SETPIECE
!00000063
\ ( n -> ) Set a piece as current
: setpiece
dup piece-num !
6 shl pieces + cur-piece !
;
-*-
^
>
FALL
!000002E9
\ ( -> dead ) Dead flag
: fall
erase-me \ Nuke old
1 y +! \ Drop down one
test-me if \ Check for clear
-1 y +! \ Not clear, fix Y
draw-me \ Show in old position
y @ dup 4 + 24 min swap do \ For all rows just touched
i chop-row \ See if can remove row
loop \
launch \ And launch a new piece
test-me \ New piece fit? (1 if no)
else \
0 \ Otherwise always work
endif \ Clear
draw-me \ Draw new position
;
-*-
v
CHOP-ROW
!00000280
\ ( y -> ) Check given row to see if can remove
<scoreboard>
<screen>
: chop-row
y ! \ Save row
col widx + col do \ Loop thru row
i y @ gs bcolor = if exit endif \ All clear?
loop
0 y @ 1- do \ Loop thru, dropping all rows
i 160 * [ col dup + ] literal + screen + dup 160 +
[ widx dup + ] literal move \ Copy it down
-1 +loop
1 rows +! \ Another row
1 score+ 7 emit \ Tell 'em good
rows @ 10 mod 0= if \ Every ten rows
speed @ 1- 1 max speed ! \ Speed up till max speed
endif
;
-*-
v
Y
!0000000C
variable y
-*-
^
^
>
DOKEY
!00000362
\ ( -> ESC-flag) Handle keys until time-out or null
<scoreboard>
<clock>
: dokey
ticker@ speed @ + time ! \ Get starting value+speed
begin \ Loop for time
?term if key \ Handle keys
dup 32 = if drop'em endif \ Space bar - drop 'em
dup ` 7 = if left endif \ Left
dup ` 8 = if rotate endif \ Rotate
dup ` 9 = if right endif \ Right
dup ` 6 = if speedup endif \ speed up
dup ` 4 = if drop'em endif \ drop block
dup ` p = over ` p 32 + = or if pause endif \ hold game
27 = if 1 exit endif \ ESC ends game
endif \ Loop for keys
ticker@ time @ >= until \ Loop until reach time
0 \ Return null
;
-*-
v
TIME
!0000001C
\ Drop-time
variable time
-*-
>
DROP'EM
!00000227
\ ( -> ) Drop 'em in place
<scoreboard>
<clock>
: drop'em
erase-me \ Clear it
y @ \ remember old pos
begin \ Loop
1 y +! \ Drop one
test-me until \ Loop until won't go
-1 y +! \ Back up one
draw-me \ And show me
y @ stack ab|baba - 0 max 1 shr score+ \ get points
- 2 > if dokey drop endif
ticker int[ @ ]int time ! \ Out of time; next piece please
;
-*-
>
LEFT
!000000DD
\ ( -> ) Move block left
: left
erase-me \ Clear old
-2 x +! \ Go left
test-me if \ Check for walls
2 x +! \ Oops, back up
endif \
draw-me \ And draw new position
;
-*-
>
RIGHT
!000000E0
\ ( -> ) Move block right
: right
erase-me \ Clear old
2 x +! \ Go right
test-me if \ Check for walls
-2 x +! \ Oops, back up
endif \
draw-me \ And draw new position
;
-*-
>
ROTATE
!00000101
\ ( -> ) Rotate block
: rotate
erase-me \ Clear old
cur-piece @ 12 + h# FFFFFFCC and cur-piece !
test-me if \ Check for walls
cur-piece @ 4 + h# FFFFFFCC and cur-piece !
endif \
draw-me \ And draw new position
;
-*-
>
SPEEDUP
!00000054
<scoreboard>
: speedup
speed @ 1- 0 max speed ! \ adjust speed
;
-*-
>
PAUSE
!000001E9
<scoreboard>
<clock>
: pause
0 0 gotoxy ." 100:"
ticker@ firsttick ! \ get our starting time
begin
firsttick @ if
ticker@ firsttick @ - [ 100 18 * ] literal > if
4 0 gotoxy ." ok "
0 firsttick !
endif
endif
?term if
key dup ` p = swap ` p 32 + = or
else
0
endif
until
ticker@ firsttick @ - dup 100 18 * < if
10 * negate score+
else
drop
endif
0 0 gotoxy ." "
;
-*-
v
FIRSTTICK
!00000014
variable firsttick
-*-
^
^
^
>
TETRIS
!0000002A
: tetris
['] bye i->d error !
t
;
-*-
v
BYEBYE
!00000014
: byebye
bye
;
-*-
^
^