-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_learntris.py
More file actions
125 lines (114 loc) · 4.02 KB
/
test_learntris.py
File metadata and controls
125 lines (114 loc) · 4.02 KB
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
# Copyright 2025 wpdevelopment11
#
# Use of this source code is governed by an MIT-style
# license that can be found in the LICENSE.txt file or at
# https://opensource.org/licenses/MIT.
from enum import StrEnum
from textwrap import dedent as d
from blocks import HEIGHT, WIDTH, Game, Grid, Tetromino, TetrominoShape, Screen
import re
import sys
CMD_REGEX = re.compile(r"(?:\?)?.")
class NotSupportedCommand(Exception): pass
class Command(StrEnum):
QUIT = "q"
PRINT = "p"
GIVEN = "g"
CLEAR = "c"
STEP = "s"
TETROMINO = "t"
ROTATE = ")"
ROTATE_COUNTERCLOCK = "("
NEWLINE = ";"
PRINT_STATE = "P"
QUERY_SCORE = "?s"
QUERY_CLEARED = "?n"
LEFT = "<"
RIGHT = ">"
UP = "^"
DOWN = "v"
DROP = "V"
PLAY_PAUSE = "!"
TITLE = "@"
def run_learntris_cmd(cmd_line, game):
"""Process one line of Learntris input. Execute appropriate command.
Single line of input may contain multiple commands.
"""
commands = [cmd for word in cmd_line.split() for cmd in CMD_REGEX.findall(word)]
for cmd in commands:
if cmd == Command.QUIT:
sys.exit(0)
elif cmd == Command.PRINT:
if game.screen == Screen.TITLE:
print(d("""\
Learntris (c) 1992 Tetraminex, Inc.
Press start button to begin."""))
elif game.screen == Screen.PAUSE:
print(d("""\
Paused
Press start button to continue."""))
elif game.screen == Screen.GAME:
print(game.grid)
else:
raise AssertionError("Never happens")
elif cmd == Command.GIVEN:
lines = [input() for _ in range(HEIGHT)]
game.grid = Grid.from_str(lines)
elif cmd == Command.CLEAR:
game.clear_grid()
elif cmd == Command.QUERY_SCORE:
print(game.score)
elif cmd == Command.QUERY_CLEARED:
print(game.cleared)
elif cmd == Command.STEP:
game.clear_rows()
elif cmd == Command.TETROMINO:
print(game.active_tetromino)
elif cmd == Command.ROTATE:
game.active_tetromino.rotate()
elif cmd == Command.ROTATE_COUNTERCLOCK:
game.active_tetromino.rotate(counterclock=True)
elif cmd == Command.NEWLINE:
print()
elif cmd == Command.PRINT_STATE:
assert game.screen == Screen.GAME or game.screen == Screen.GAMEOVER
print(game)
if game.screen == Screen.GAMEOVER:
print("Game Over")
elif cmd == Command.LEFT:
game.active_tetromino.move_left()
elif cmd == Command.RIGHT:
game.active_tetromino.move_right()
elif cmd == Command.UP:
game.active_tetromino.move_up()
elif cmd == Command.DOWN:
game.active_tetromino.move_down()
elif cmd == Command.DROP:
while game.active_tetromino.move_down():
pass
game.active_tetromino.finalize()
game.active_tetromino = None
if game.is_game_over():
game.screen = Screen.GAMEOVER
elif cmd == Command.PLAY_PAUSE:
if game.screen == Screen.GAME:
game.screen = Screen.PAUSE
elif game.screen == Screen.PAUSE or game.screen == Screen.TITLE:
game.screen = Screen.GAME
else:
raise AssertionError("Never happens")
elif cmd == Command.TITLE:
game.screen = Screen.TITLE
elif cmd in [shape.name for shape in TetrominoShape]:
assert game.active_tetromino is None
game.active_tetromino = Tetromino(TetrominoShape[cmd], game.grid)
else:
raise NotSupportedCommand("Command '{}' is not supported!".format(cmd))
def main():
"""Read Learntris commands from stdin."""
grid = Grid(WIDTH, HEIGHT)
game = Game(grid)
for line in sys.stdin:
run_learntris_cmd(line, game)
if __name__ == "__main__":
main()