-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmdline.py
255 lines (221 loc) · 8.14 KB
/
cmdline.py
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
from adversary import RandomAdversary
from arguments import parser
from board import Board, Direction, Rotation, Action
from constants import BOARD_WIDTH, BOARD_HEIGHT, DEFAULT_SEED, INTERVAL,\
BLOCK_LIMIT
from exceptions import BlockLimitException
from player import SelectedPlayer, Player
from time import sleep
import curses
import curses.ascii
COLOR_WALL = 1
COLOR_BLOCK = 2
COLOR_CELL = 3
COLOR_NOTHING = 4
COLOR_RED = 5
COLOR_ORANGE = 6
COLOR_YELLOW = 7
COLOR_GREEN = 8
COLOR_CYAN = 9
COLOR_BLUE = 10
COLOR_MAGENTA = 11
COLOR_BOMB = 12
COLOR_DISCARD = 13
COLOR_NAMES = {
"red": COLOR_RED,
"orange": COLOR_ORANGE,
"yellow": COLOR_YELLOW,
"green": COLOR_GREEN,
"cyan": COLOR_CYAN,
"blue": COLOR_BLUE,
"magenta": COLOR_MAGENTA,
"white": COLOR_BOMB
}
def paint(window, x, y, color, count=1):
if color == COLOR_BOMB:
window.addstr(y, x*2, '<>' * count, curses.color_pair(color))
else:
window.addstr(y, x*2, ' ' * count, curses.color_pair(color))
def render(window, board):
"""
Write a depiction of the board to standard output.
"""
for y in range(board.height):
# Draw each individual row
for x in range(board.width):
if board.falling is not None and (x, y) in board.falling:
# Location is occupied by falling block
color = COLOR_NAMES[board.falling.color]
elif (x, y) in board:
# Location is occupied by fallen block
color = COLOR_NAMES[board.cellcolor[(x, y)]]
else:
# There is nothing here.
color = COLOR_NOTHING
paint(window, x+1, y, color)
# Draw the score
window.addstr(
0,
(board.width*2)+5,
f'SCORE: {board.score} ',
curses.color_pair(COLOR_NOTHING)
)
# Draw the next piece
window.addstr(2, (board.width*2)+5, 'NEXT',
curses.color_pair(COLOR_NOTHING))
if board.next is not None:
for y in range(6):
for x in range(4):
if (x, y) in board.next:
color = COLOR_NAMES[board.next.color]
else:
color = COLOR_NOTHING
paint(window, board.width+x+3, y+3, color)
# Draw the bombs
window.addstr(7, (board.width*2)+5, 'BOMBS',
curses.color_pair(COLOR_NOTHING))
for bomb in range(1,6):
if board.bombs_remaining >= bomb:
s = '<>'
color = COLOR_BOMB
else:
s = ' '
color = COLOR_NOTHING
window.addstr(8, (board.width*2)+5+(bomb-1)*3, s,
curses.color_pair(color))
# Draw the Discards
window.addstr(10, (board.width*2)+5, 'DISCARDS',
curses.color_pair(COLOR_NOTHING))
discards = board.discards_remaining
if discards >= 5:
s1 = "X X X X X"
s2 = (discards - 5)*" X" + (10-discards)*" "
else:
s1 = discards*"X " + (5-discards)*" "
s2 = " "
window.addstr(11, (board.width*2)+5, s1,
curses.color_pair(COLOR_DISCARD))
window.addstr(12, (board.width*2)+5, s2,
curses.color_pair(COLOR_DISCARD))
# Draw the board frame
window.move(0, 0)
window.vline(curses.ACS_VLINE, board.height+2)
window.move(0, 1)
window.vline(curses.ACS_VLINE, board.height+1)
window.addch(0, 0, curses.ACS_ULCORNER)
window.addch(0, 1, curses.ACS_URCORNER)
window.move(0, board.width*2+2)
window.vline(curses.ACS_VLINE, board.height+1)
window.move(0, board.width*2+3)
window.vline(curses.ACS_VLINE, board.height+2)
window.addch(0, board.width*2+2, curses.ACS_ULCORNER)
window.addch(0, board.width*2+3, curses.ACS_URCORNER)
window.move(board.height+1, 0)
window.hline(curses.ACS_HLINE, board.width*2+3)
window.move(board.height, 1)
window.hline(curses.ACS_HLINE, board.width*2+1)
window.addch(board.height+1, 0, curses.ACS_LLCORNER)
window.addch(board.height, 1, curses.ACS_LLCORNER)
window.addch(board.height+1, board.width*2+3, curses.ACS_LRCORNER)
window.addch(board.height, board.width*2+2, curses.ACS_LRCORNER)
window.move(board.height+2, 0)
window.refresh()
class UserPlayer(Player):
"""
A simple user player that reads moves from the command line.
"""
screen = None
def __init__(self, window):
self.window = window
def choose_action(self, board):
key = self.window.getch()
if key == -1:
return None
elif key == curses.KEY_RIGHT:
return Direction.Right
elif key == curses.KEY_LEFT:
return Direction.Left
elif key == curses.KEY_DOWN:
return Direction.Down
elif key == ord(' '):
return Direction.Drop
elif key == curses.KEY_UP:
return Rotation.Clockwise
elif key == ord('z'):
return Rotation.Anticlockwise
elif key == ord('x'):
return Rotation.Clockwise
elif key == ord('b'):
return Action.Bomb
elif key == ord('d'):
return Action.Discard
elif key == curses.ascii.ESC or key == ord('q'):
raise SystemExit
def run(window):
board = Board(BOARD_WIDTH, BOARD_HEIGHT)
adversary = RandomAdversary(DEFAULT_SEED, BLOCK_LIMIT)
args = parser.parse_args()
if args.manual:
window.timeout(INTERVAL)
player = UserPlayer(window)
else:
window.timeout(0)
player = SelectedPlayer()
try:
for move in board.run(player, adversary):
render(window, board)
if not args.manual:
while True:
key = window.getch()
if key == -1:
break
elif key == curses.ascii.ESC:
raise SystemExit
sleep(0.1)
except BlockLimitException:
window.addstr(BOARD_HEIGHT//2, 2,
"Out of blocks", curses.color_pair(COLOR_NOTHING))
window.addstr(BOARD_HEIGHT//2+1, 2,
"Score="+ str(board.score), curses.color_pair(COLOR_NOTHING))
window.addstr(BOARD_HEIGHT//2+2, 2,
"Press a key to exit", curses.color_pair(COLOR_NOTHING))
window.timeout(-1)
window.getch()
if __name__ == '__main__':
try:
# Initialize terminal settings
curses.initscr()
curses.start_color()
curses.noecho()
curses.cbreak()
window = curses.newwin(
BOARD_HEIGHT + 3,
(BOARD_WIDTH + 2 + 7)*2 + 1
)
window.keypad(True)
# Prepare some colors to use for drawing.
curses.init_pair(COLOR_WALL, curses.COLOR_WHITE, curses.COLOR_CYAN)
curses.init_pair(COLOR_BLOCK, curses.COLOR_WHITE, curses.COLOR_RED)
curses.init_pair(COLOR_CELL, curses.COLOR_WHITE, curses.COLOR_WHITE)
curses.init_pair(COLOR_NOTHING, curses.COLOR_WHITE, curses.COLOR_BLACK)
curses.init_pair(COLOR_DISCARD, curses.COLOR_RED, curses.COLOR_BLACK)
# Orange is not supported.
curses.init_pair(COLOR_ORANGE, curses.COLOR_WHITE, curses.COLOR_WHITE)
curses.init_pair(COLOR_RED, curses.COLOR_WHITE, curses.COLOR_RED)
curses.init_pair(COLOR_YELLOW, curses.COLOR_WHITE, curses.COLOR_YELLOW)
curses.init_pair(COLOR_GREEN, curses.COLOR_WHITE, curses.COLOR_GREEN)
curses.init_pair(COLOR_CYAN, curses.COLOR_WHITE, curses.COLOR_CYAN)
curses.init_pair(COLOR_BLUE, curses.COLOR_WHITE, curses.COLOR_BLUE)
curses.init_pair(
COLOR_MAGENTA,
curses.COLOR_WHITE,
curses.COLOR_MAGENTA
)
curses.init_pair(COLOR_BOMB, curses.COLOR_BLACK, curses.COLOR_WHITE)
run(window)
finally:
# Clean up terminal settings once we are done
window.keypad(False)
curses.nocbreak()
curses.echo()
curses.endwin()