-
Notifications
You must be signed in to change notification settings - Fork 0
/
tetris.wsf
456 lines (420 loc) · 10.6 KB
/
tetris.wsf
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
# Copyright 2008 Peter De Wachter
# Copyright 2020-2022 Thalia Archibald
# Heap usage:
# 0-113 piece rotations
# 121-352 board
# 360 rand
# 361 read char
# 362 tmp
# 363 tmp
# Labels are assigned in order of usage
# A piece rotation is encoded as a 6-tuple containing the color ID (1-7),
# offsets for the four blocks, and the index of the next rotation for that
# piece.
#
# Piece rotations:
#
# 0
# XX..
# O XX.. (1 red)
# ....
# ....
#
# 1 7
# .... .X..
# I XXXX .X.. (2 green)
# .... .X..
# .... .#..
#
# 2 8 9 10
# XXX. .X.. X... XX..
# J ..X. .X.. XXX. X... (3 yellow)
# .... XX.. .... X...
# .... .... .... ....
#
# 3 11 12 13
# XXX. XX.. ..X. X...
# L X... .X.. XXX. X... (4 blue)
# .... .X.. .... XX..
# .... .... .... ....
#
# 4 14 15 16
# XXX. .X.. .X.. X...
# T .X.. XX.. XXX. XX.. (5 magenta)
# .... .X.. .... X...
# .... .... .... ....
#
# 5 17
# XX.. .X..
# Z .XX. XX.. (6 cyan)
# .... X...
# .... ....
#
# 6 18
# .XX. X...
# S XX.. XX.. (7 white)
# .... .X..
# .... ....
#
# Block offsets in 4x4 grid:
# 0 1 2 4
# 11 12 13 14
# 22 23 24 25
# 33 34 35 36
1 0 1 11 12 0 # O 0
2 11 12 13 14 7 # I 1
3 0 1 2 13 8 # J 2
4 0 1 2 11 11 # L 3
5 0 1 2 12 14 # T 4
6 0 1 12 13 17 # Z 5
7 1 2 11 12 18 # S 6
2 1 12 23 34 1 # I 7
3 1 12 22 23 9 # J 8
3 0 11 12 13 10 # J 9
3 0 1 11 22 2 # J 10
4 0 1 12 23 12 # L 11
4 2 11 12 13 13 # L 12
4 0 11 22 23 3 # L 13
5 1 11 12 23 15 # T 14
5 1 11 12 13 16 # T 15
5 0 11 12 22 4 # T 16
6 1 11 12 22 5 # Z 17
7 0 11 12 23 6 # S 18
# Store piece rotations at addresses 0-113
113
.init_pieces:
swap ^1 swap
store
1-
^ jn .init_pieces__break
jmp .init_pieces
.init_pieces__break:
drop
# Initial seed
360 123456 store
# Initial board:
#
# 121: -2 0 0 0 0 0 0 0 0 0 0
# 132: -2 0 0 0 0 0 0 0 0 0 0
# 143: -2 0 0 0 0 0 0 0 0 0 0
# 154: -2 0 0 0 0 0 0 0 0 0 0
# 165: -2 0 0 0 0 0 0 0 0 0 0
# 176: -2 0 0 0 0 0 0 0 0 0 0
# 187: -2 0 0 0 0 0 0 0 0 0 0
# 198: -2 0 0 0 0 0 0 0 0 0 0
# 209: -2 0 0 0 0 0 0 0 0 0 0
# 220: -2 0 0 0 0 0 0 0 0 0 0
# 231: -2 0 0 0 0 0 0 0 0 0 0
# 242: -2 0 0 0 0 0 0 0 0 0 0
# 253: -2 0 0 0 0 0 0 0 0 0 0
# 264: -2 0 0 0 0 0 0 0 0 0 0
# 275: -2 0 0 0 0 0 0 0 0 0 0
# 286: -2 0 0 0 0 0 0 0 0 0 0
# 297: -2 0 0 0 0 0 0 0 0 0 0
# 308: -2 0 0 0 0 0 0 0 0 0 0
# 319: -2 0 0 0 0 0 0 0 0 0 0
# 330: -2 0 0 0 0 0 0 0 0 0 0
# 341: -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2 -2
#
# Cell values:
# empty: 0
# block: -1
# wall: -2
# Initialize the board at addresses 121-352
121
.init_board:
^ -2 store # Wall
^ 341 j= .init_board__floor
.init_board__field:
1+
^ 0 store # Field
^ 11% jz .init_board
jmp .init_board__field
.init_board__floor:
1+
^ -2 store # Floor
^ 352 j< .init_board__floor
drop
call draw_empty_board
# Spawns the next piece at the top of the board.
# ( -- !)
.game_loop__spawn_piece:
5 # Piece position
call rand_piece
call check_collision jn game_over
call draw_piece
# Reads a key press from the user.
# (pos piece -- !)
.game_loop__read_key:
call move_cursor_bottom_right
361 readc
call erase_piece
2dup
361 retrieve 'j' j= .game_loop__key_left
361 retrieve 'k' j= .game_loop__key_down
361 retrieve 'l' j= .game_loop__key_right
361 retrieve 'i' j= .game_loop__key_rotate
361 retrieve '\e' j= .game_loop__key_quit
jmp .game_loop__no_move
.game_loop__key_left:
swap 1- swap
jmp .game_loop__try_move
.game_loop__key_down:
swap 11+ swap
jmp .game_loop__move_down
.game_loop__key_right:
swap 1+ swap
jmp .game_loop__try_move
.game_loop__key_rotate:
5+ retrieve 6*
jmp .game_loop__try_move
.game_loop__key_quit:
2drop
jmp game_over
# Moves the piece left, right, or rotates it.
# (pos piece pos' piece' -- ! pos'' piece'')
.game_loop__try_move:
call check_collision jn .game_loop__no_move
jmp .game_loop__move
# Moves the piece down or places it.
# (pos piece pos' piece' -- ! pos'' piece'')
.game_loop__move_down:
call check_collision jn .game_loop__place_piece
jmp .game_loop__move
# Moves the piece.
# (pos piece pos' piece' -- ! pos' piece')
.game_loop__move:
call draw_piece # Unnecessary draw
# Discard old position using heap temporaries
362 swap store
363 swap store
2drop
363 retrieve
362 retrieve
call draw_piece
jmp .game_loop__read_key
# Redraws the unmoved piece.
# (pos piece pos' piece' -- ! pos piece)
.game_loop__no_move:
2drop
call draw_piece
jmp .game_loop__read_key
# Places the piece, then spawns the next.
# (pos piece pos' piece' -- !)
.game_loop__place_piece:
2drop
call draw_piece
2drop
call remove_filled_lines
jmp .game_loop__spawn_piece
# Quits the game and cleans up the bottom right corner of the board.
# (pos piece -- !)
game_over:
call draw_piece
2drop
call move_cursor_bottom_right
call reverse_color
" \n" prints # Fill empty bottom-right corner
"\e[G" prints # Move cursor to start of line
call reset_color
end
# Returns the next piece and updates the pseudorandom state.
# ( -- piece)
rand_piece:
# Update the rand state to (rand*279470273)%4294967291
360 retrieve
279470273* 4294967291%
^ 360 swap store
# Return (rand%7)*6
7% 6* ret
# Removes any filled lines.
# ( -- )
remove_filled_lines:
341
# Iterate lines, starting at the bottom
.remove_filled_lines__loop_line:
11/ 11* 1-
^ 122 j< .remove_filled_lines__ret
# Iterate columns, starting at the right
.remove_filled_lines__loop_col:
^ retrieve jz .remove_filled_lines__loop_line
1-
^ 11% jz .remove_filled_lines__remove_line
jmp .remove_filled_lines__loop_col
.remove_filled_lines__remove_line:
^ 11/ 10- call remove_line
^ call shift_board_lines
11+
jmp .remove_filled_lines__loop_line
.remove_filled_lines__ret:
drop ret
# Shifts lines above pos in heap down by one.
# (pos -- )
shift_board_lines:
^ ^ retrieve
swap 11+ swap store
^ 122 j= .shift_board_lines__clear_top
1-
jmp shift_board_lines
# Set top line to 0
.shift_board_lines__clear_top:
^ 0 store
1+
^ 11% jz .shift_board_lines__ret
jmp .shift_board_lines__clear_top
.shift_board_lines__ret:
drop ret
# Checks whether the piece collides with other blocks or a wall. Returns -1 on
# collision and 0 otherwise.
# (pos piece -- pos piece collided?)
check_collision:
2dup
swap 121+ swap
1+ ^ retrieve ^2 + retrieve jn .check_collision__collided
1+ ^ retrieve ^2 + retrieve jn .check_collision__collided
1+ ^ retrieve ^2 + retrieve jn .check_collision__collided
1+ ^ retrieve ^2 + retrieve jn .check_collision__collided
2drop 0 ret
.check_collision__collided:
2drop -1 ret
# Draws the piece on the screen and in the heap (sets blocks to -1).
# (pos piece -- pos piece)
draw_piece:
call draw_piece_screen
swap 121+ swap
1+ ^ retrieve ^2 + -1 store
1+ ^ retrieve ^2 + -1 store
1+ ^ retrieve ^2 + -1 store
1+ ^ retrieve ^2 + -1 store
4-
swap 121- swap
ret
# Erases the piece on the screen and in the heap (sets blocks to 0).
# (pos piece -- pos piece)
erase_piece:
call erase_piece_screen
swap 121+ swap
1+ ^ retrieve ^2 + 0 store
1+ ^ retrieve ^2 + 0 store
1+ ^ retrieve ^2 + 0 store
1+ ^ retrieve ^2 + 0 store
4-
swap 121 - swap
ret
# Draws the piece on the screen.
# (pos piece -- pos piece)
draw_piece_screen:
^ retrieve call set_background
jmp .draw_piece_screen
# Erases the piece on the screen.
# (pos piece -- pos piece)
erase_piece_screen:
0 call set_background # black
.draw_piece_screen:
1+ ^ retrieve ^2 + call draw_block_screen
1+ ^ retrieve ^2 + call draw_block_screen
1+ ^ retrieve ^2 + call draw_block_screen
1+ ^ retrieve ^2 + call draw_block_screen
4-
ret
# Draws a block at the given position on the screen.
# (pos -- )
draw_block_screen:
^
# Move cursor to line (n/11+1), col ((n%11)*2+1)
"\e[" prints
11/ 1+ printi
';' printc
11% 2* 1+ printi
'H' printc
"\e[2X" prints # Erase next 2 characters without moving cursor
" " prints
ret
# Removes a filled line and shifts the above lines down.
# (line -- )
remove_line:
call reset_color
"\e[" prints printi 'H' printc # Move cursor to line
"\e[1M" prints # Delete current line and shift remaining lines up
jmp draw_board_line # Add empty line at top
# Draws an empty board.
# ( -- )
draw_empty_board:
"\e[2J" prints # Clear entire screen
# Draw floor except for bottom-right corner
"\e[H" prints # Move cursor to upper-left corner
call reverse_color
23 call print_spaces
# Draw 20 lines of walls
-20
.draw_empty_board__loop:
call draw_board_line
1+
^ jn .draw_empty_board__loop
drop ret
# Inserts a line and walls at the top of the board.
# ( -- )
draw_board_line:
call reset_color
"\e[H" prints # Move cursor to upper-left corner
"\e[L" prints # Insert line at cursor
call reverse_color
" " prints # Draw left wall
"\e[23G " prints # Draw right wall at column 23
ret
# Moves cursor to the bottom-right corner, where user input is read.
# ( -- )
move_cursor_bottom_right:
"\e[21;24H" prints # Move cursor to line 21, col 24
jmp reset_color
# Sets reverse video on.
# ( -- )
reverse_color:
"\e[0;7m" prints
ret
# Resets attributes and sets the background color, given 0-7.
# 40 black
# 41 red
# 42 green
# 43 yellow
# 44 blue
# 45 magenta
# 46 cyan
# 47 white
# (color -- )
set_background:
"\e[0;4" prints
printi
'm' printc
ret
# Resets all terminal attributes.
# ( -- )
reset_color:
"\e[0m" prints
ret
# Prints n spaces.
# (n -- )
print_spaces:
^ jz .print_spaces__ret
' ' printc
1-
jmp print_spaces
.print_spaces__ret:
drop ret
# Prints "Crash!" and dumps the stack until it underflows.
# (s... -- !)
dump_stack:
"\nCrash!\n" prints
.dump_stack__loop:
printi
'\n' printc
jmp .dump_stack__loop
# Dumps heap values above 121, infinitely.
# ( -- !)
dump_heap:
121
.dump_heap_loop:
^ retrieve printi
';' printc
1+
jmp .dump_heap_loop