-
Notifications
You must be signed in to change notification settings - Fork 0
/
sweeper.py
executable file
·542 lines (431 loc) · 15.5 KB
/
sweeper.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
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
#!/usr/bin/env python
from dataclasses import dataclass
import pendulum
import random
import time
from typing import List, Dict, Optional, Tuple, Set, Union
from curtsies import FullscreenWindow, Input, fsarray, FSArray, fmtstr, FmtStr
from curtsies import fmtfuncs as ff
import curtsies.events
# source for box chars:
# http://shapecatcher.com/unicode/block/Box_Drawing
class Field:
"""represents the mine field we're solving"""
CHAR_MAP: Dict[str, str] = {
'm': '💣',
'f': '🏳 ',
'c': '□ '
}
def __init__(self, width: int = 10, height: int = 10, mines: int = 10):
self.width = width
self.height = height
self.mine_count = mines
# game clock tracking
self.started: Optional[pendulum.DateTime] = None
self.ended: Optional[pendulum.DateTime] = None
self.stoppage_time: pendulum.Duration = pendulum.duration()
# initialize cursor position
self.chars: FSArray = fsarray([])
self.row, self.col = 5, 5
self.mines: Set[Tuple[int, int]] = self.init_mines()
self.flagged: Set[Tuple[int, int]] = set()
self.opened: Set[Tuple[int, int]] = set()
self.highlighted: Set[Tuple[int, int]] = set()
self.render()
@property
def game_time(self) -> pendulum.Duration:
"""duration that the Field has been worked"""
if self.started:
end = self.ended if self.ended else pendulum.now()
return end - self.started - self.stoppage_time
else:
return pendulum.duration()
@property
def clock(self) -> FmtStr:
"""game clock as a formatted string"""
clock = f"{self.game_time.minutes:02d}:{(self.game_time.seconds % 60):02d}"
if self.game_time.hours > 0:
clock = f"{self.game_time.hours:02d}:{clock}"
return ff.plain(clock)
def add_stoppage(self, stoppage: pendulum.Duration) -> None:
if self.started and not self.ended:
self.stoppage_time += stoppage
def init_mines(self) -> Set[Tuple[int, int]]:
"""Add mines to spaces"""
mines: Set[Tuple[int, int]] = set()
while len(mines) < self.mine_count:
mine = (random.randint(0, self.height - 1), random.randint(0, self.width - 1))
mines.add(mine)
return mines
def neighbors(self, pos: Tuple[int, int]) -> Set[Tuple[int, int]]:
"""returns all neighbors of given position"""
neighbors = set()
for rdiff in range(-1, 2):
for cdiff in range(-1, 2):
neighbor = (pos[0] + rdiff, pos[1] + cdiff)
if neighbor == pos:
continue
if neighbor[0] < 0 or neighbor[0] >= self.height:
continue
if neighbor[1] < 0 or neighbor[1] >= self.width:
continue
neighbors.add(neighbor)
return neighbors
def neighbor_mines(self, pos: Tuple[int, int]) -> int:
"""number of neighbor mines at given position"""
return len(self.neighbors(pos) & self.mines)
def symbol_at(self, pos: Tuple[int, int]) -> Union[str, int]:
"""Returns symbol at given position"""
if pos in self.opened:
if pos in self.mines:
return 'm'
else:
return self.neighbor_mines(pos)
elif pos in self.flagged:
return 'f'
else:
return 'c'
def char_at(self, pos: Tuple[int, int]) -> FmtStr:
"""returns formatted character at position"""
sym = self.symbol_at(pos)
char = f"{sym} " if isinstance(sym, int) else self.CHAR_MAP.get(sym)
if char == "0 ":
char = ". "
kwargs = {}
if pos[0] == self.row and pos[1] == self.col:
kwargs['bg'] = 'blue'
if pos in self.highlighted:
kwargs['fg'] = 'red'
return fmtstr(char, **kwargs)
def render(self) -> None:
"""representation of the field state"""
rows = []
for row in range(self.height):
row_str = self.char_at((row, 0))
for col in range(1, self.width):
row_str = row_str.append(self.char_at((row, col)))
rows.append(row_str)
self.chars = fsarray(rows)
self.highlighted = set()
def move(self, row: int, col: int) -> None:
"""move the position"""
if row == 0 and col == 0:
return
if abs(row) + abs(col) > 1:
raise ValueError("can only move the cursor by one spot")
self.row = self.row + row if 0 <= self.row + row < self.height else self.row
self.col = self.col + col if 0 <= self.col + col < self.height else self.col
self.render()
def open_at(self, pos: Tuple[int, int]) -> None:
"""actually opens at specified position"""
self.opened.add(pos)
if self.symbol_at(pos) == 0:
neighbors = self.neighbors(pos)
still_closed = neighbors - self.opened
for neighbor in still_closed:
self.open_at(neighbor)
def clear_at(self, pos: Tuple[int, int]) -> None:
"""on an open square, opens remaining unflagged squares or highlights"""
count = self.symbol_at(pos)
neighbors = self.neighbors(pos)
flagged = neighbors & self.flagged
unflagged = neighbors - self.flagged
if len(flagged) == count:
for neighbor in unflagged:
self.open_at(neighbor)
else:
self.highlighted = unflagged - self.opened
def open(self) -> None:
"""Opens mine, or adjecent squares, under cursor"""
if not self.started:
self.started = pendulum.now()
pos = (self.row, self.col)
if pos in self.opened:
self.clear_at(pos)
else:
self.open_at(pos)
self.render()
def flag(self) -> None:
"""flag space at cursor"""
pos = (self.row, self.col)
if pos in self.opened:
self.highlighted = {pos}
elif pos in self.flagged:
self.flagged.remove(pos)
else:
self.flagged.add(pos)
self.render()
@property
def lost(self) -> bool:
"""True if we lost"""
lost = bool(self.mines & self.opened)
if lost and not self.ended:
self.ended = pendulum.now()
return lost
@property
def won(self) -> bool:
"""true if we won"""
won = len(self.opened) + len(self.mines) == self.width * self.height
if won and not self.ended:
self.ended = pendulum.now()
return won
class Tick(curtsies.events.ScheduledEvent):
"""An event that represents a tick of game time"""
pass
class Update(curtsies.events.ScheduledEvent):
"""An event that represents a request to update the screen soon"""
pass
class Game:
"""represents the game board, where game state is displayed"""
LEVELS: Dict[str, Dict[str, int]] = {
"beginner": {"width": 10, "height": 10, "mines": 10},
"intermediate": {"width": 16, "height": 16, "mines": 40},
"advanced": {"width": 16, "height": 30, "mines": 99},
}
def __init__(self, window: FullscreenWindow):
# save game window, plus window size info
self.window = window
# initialize game state
self.field: Optional[Field] = None
self.menu: Optional[str] = None
self.menu_opened_at: Optional[pendulum.DateTime] = None
self.level = list(self.LEVELS.keys())[0]
# initialize reactor system + schedule first tick
self.reactor = Input()
self.schedule_tick = self.reactor.scheduled_event_trigger(Tick)
self.schedule_tick(when=time.time())
self.schedule_update = self.reactor.scheduled_event_trigger(Update)
self.last_event: Optional[str] = None
# draw the window
self.update_window()
def update_window(self) -> None:
"""re-draw the window, possibly reacting to changing window size"""
self.max_rows = self.window.height
self.max_cols = self.window.width
# initialize game display + add borders and header
self.chars = FSArray(self.max_rows, self.max_cols)
self.draw_game_border()
self.draw_header()
def draw_game_border(self) -> None:
"""Draws a border around the whole board"""
self.chars[0, 0] = '┌'
self.chars[0, self.max_cols-1] = '┐'
self.chars[self.max_rows-1, 0] = '└'
self.chars[self.max_rows-1, self.max_cols-1] = '┘'
for row in range(1, self.max_rows - 1):
self.chars[row, 0] = '│'
self.chars[row, self.max_cols-1] = '│'
self.chars[2, 0] = '├'
self.chars[2, self.max_cols-1] = "┤"
for col in range(1, self.max_cols - 1):
self.chars[0, col] = '─'
self.chars[2, col] = '-'
self.chars[self.max_rows - 1, col] = '─'
def draw_header(self) -> None:
"""renders the header into our array"""
title = fmtstr(" No-Guess Sweeper :", fg="blue", underline=True)
self.chars[1, 1:1+title.width] = [title]
clock = ff.plain('┊ ') + ff.green(self.field.clock if self.field else "00:00")
self.chars[1, (self.max_cols - 1 - clock.width):(self.max_cols - 1)] = [clock]
avail = self.max_cols - 2 - title.width - clock.width
instructions: List[FmtStr] = [
ff.yellow(' h: ') + ff.gray("Help "),
ff.yellow(' q: ') + ff.gray("Quit "),
ff.yellow(' n: ') + ff.gray("New "),
]
if self.menu:
instructions.append(
ff.yellow(' c: ') + ff.gray("Close ")
)
# drop instructions until they fit on top line
while sum(i.width for i in instructions) > avail:
instructions.pop()
per = int(avail / len(instructions))
istr = FmtStr().join(i.ljust(per) for i in instructions)
self.chars[1, title.width:(title.width + istr.width)] = [istr]
def draw_menu(self, title: FmtStr, items: List[FmtStr]) -> None:
"""Draws the menu of the specified items"""
height = len(items)
width = max((
title.width,
max(item.width for item in items) if items else 0,
))
header = [
"╭" + "-" * (width + 2) + "╮",
"|" + title.center(width + 2) + "|",
"|" + "-" * (width + 2) + "|",
]
footer = [
"╰" + "-" * (width + 2) + "╯",
]
body = fsarray(header + ["| " + i.ljust(width) + " |" for i in items] + footer)
rows = len(body)
cols = max(row.width for row in body)
min_row = int(self.max_rows/2 - rows/2)
min_col = int(self.max_cols/2 - cols/2)
self.chars[min_row:min_row+rows, min_col:min_col+cols] = body
def draw_help(self) -> None:
"""brings up the help menu"""
self.state = "menu"
items = [
('q', 'Quit Sweeper'),
('c', 'Close menu'),
('l', 'Difficulty level'),
('n', 'New game'),
('←,↑,→,↓', 'Move Cursor'),
('f', 'Flag/unflag'),
('SPACE', 'Clear'),
]
max_key = max(len(item[0]) for item in items)
lines = [
ff.yellow(item[0]).ljust(max_key) + " : " + ff.gray(item[1])
for item in items
]
self.draw_menu(ff.bold("Help"), lines)
def draw_level(self) -> None:
"""brings up the level setting menu"""
self.state = "menu"
lines = []
for level in self.LEVELS.keys():
line = f"{level[0]} : {level}"
if level == self.level:
lines.append(ff.underline(ff.yellow(line)))
else:
lines.append(ff.gray(line))
self.draw_menu(ff.bold("Difficulty"), lines)
def draw_confirm_new(self) -> None:
"""Confirms we want to restart the game"""
items = [
ff.red("A game is already in-progress!"),
ff.plain("Press ") + fmtstr("n", fg="yellow", underline=True) + " again to start a new",
ff.plain("game, or ") + fmtstr("c", fg="yellow", underline=True) + " to cancel",
]
self.draw_menu(ff.plain("Restart?"), items)
def draw_won(self) -> None:
"""Confirms we want to restart the game"""
assert self.field is not None
items = [
ff.plain(f"Congratulations! You won in {self.field.game_time.in_words()}"),
ff.plain(f"Press ") + fmtstr("n", fg="yellow", underline=True) + " to start a new game,",
ff.plain("or ") + fmtstr("c", fg="yellow", underline=True) + " to savor your success.",
]
self.draw_menu(ff.green("Victory!"), items)
def draw_lost(self) -> None:
"""Confirms we want to restart the game"""
assert self.field is not None
items = [
ff.plain(f"Alas, you appear to have ") + fmtstr("exploded", fg="red", bold=True) + ".",
ff.plain(f"Press ") + fmtstr("n", fg="yellow", underline=True) + " to start a new game,",
ff.plain("or ") + fmtstr("c", fg="yellow", underline=True) + " to learn from failure.",
]
self.draw_menu(ff.red("Defeat!"), items)
def draw_field(self) -> None:
"""draws the minefield on the board"""
if not self.field:
return
field = self.field.chars
rows, cols = field.shape
min_row = int(self.max_rows/2 - rows/2)
min_col = int(self.max_cols/2 - cols/2)
self.chars[min_row:min_row+rows, min_col:min_col+cols] = field
def clear_main(self) -> None:
"""Hides the game display"""
height = self.max_rows - 1 - 3 - 1 # subtract 3 rows for header, 1 for footer
width = self.max_cols - 1 - 1 - 1 # subtract left + right border
fill = [" " * width] * height
self.chars[4:4+height, 1:1+width] = fill
def draw_debug(self) -> None:
"""Draws some debug info about game state"""
menu = f"menu: {str(self.menu)}"
self.chars[self.max_rows-2, 1:1+len(menu)] = [menu]
event = f"event: {str(self.last_event)}"
self.chars[self.max_rows-3, 1:1+len(event)] = [event]
def update(self) -> None:
"""Updates display based on game state"""
if (self.window.width, self.window.height) != (self.max_cols, self.max_rows):
self.update_window()
else:
self.clear_main()
if self.menu:
if not self.menu_opened_at:
self.menu_opened_at = pendulum.now()
self.clear_main()
if self.menu == "help":
self.draw_help()
elif self.menu == "level":
self.draw_level()
elif self.menu == "confirm_new":
self.draw_confirm_new()
elif self.menu == "won":
self.draw_won()
elif self.menu == "lost":
self.draw_lost()
elif self.field:
if self.menu_opened_at:
self.field.add_stoppage(pendulum.now() - self.menu_opened_at)
self.menu_opened_at = None
self.draw_field()
if not self.field.ended:
if self.field.won:
self.menu = "won"
elif self.field.lost:
self.menu = "lost"
self.draw_header()
self.draw_debug()
self.window.render_to_terminal(self.chars)
def process_event(self, event: Optional[str]) -> None:
"""process an input event"""
self.last_event = event
# clear open menu
if self.menu and event == "c":
self.menu = None
# pick a difficulty level
if self.menu == "level" and isinstance(event, str):
matching = [level for level in self.LEVELS.keys() if level.startswith(event)]
if matching:
self.level = matching.pop()
# open the help menu
if event == "h":
self.menu = "help"
# open the difficulty selector
if event == "l":
self.menu = "level"
if event == "n":
if not self.field or self.field.ended or self.menu == "confirm_new":
self.menu = None
self.field = Field(**self.LEVELS[self.level])
else:
self.menu = "confirm_new"
if self.field:
if event == "<SPACE>":
self.field.open()
if event == "<UP>":
self.field.move(-1, 0)
if event == "<LEFT>":
self.field.move(0, -1)
if event == "<RIGHT>":
self.field.move(0, 1)
if event == "<DOWN>":
self.field.move(1, 0)
if event == "f":
self.field.flag()
def play(self) -> None:
"""Main loop of the game"""
with self.reactor:
for e in self.reactor:
self.update()
if e == '<ESC>' or e == 'q':
break
elif isinstance(e, Tick):
self.schedule_tick(time.time() + 0.5)
elif isinstance(e, Update):
pass
else:
self.process_event(str(e))
self.schedule_update(time.time() + 0.1)
def main() -> None:
with FullscreenWindow() as window:
game = Game(window)
game.play()
if __name__ == "__main__":
main()