/
minesweeper.py
178 lines (130 loc) · 4.25 KB
/
minesweeper.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
from __future__ import print_function, division, unicode_literals, absolute_import
from cmd import Cmd
from MinesweeperBoard import MinesweeperBoard, EndState
WELCOME = 'Welcome to Minesweeper!'
NEW_GAME_ADVICE = 'Start a new game with "new WIDTH HEIGHT NUM_MINES".'
PLAY_ADVICE = 'Step, flag, and unflag with "(step|flag|unflag) X Y".'
class MinesweeperCmd(Cmd):
"""Handle commands for a game of Minesweeper."""
def __init__(self):
Cmd.__init__(self)
self.board = None
self.prompt = '(Minesweeper) '
self.intro = '\n'.join(['', WELCOME, NEW_GAME_ADVICE, ''])
# ----- game control -----
def do_new(self, arg_string):
"""new WIDTH HEIGHT NUM_MINES
Start a new game with the specified dimensions and number of mines."""
try:
width, height, num_mines = (int(arg) for arg in arg_string.split())
except ValueError:
return self.onecmd('help new')
self.board = MinesweeperBoard(width, height, num_mines)
print()
print(PLAY_ADVICE)
return False
def _cleanup_game(self):
self.board = None
def do_end(self, __):
"""End the game, but don't exit the program."""
self._cleanup_game()
# ----- game play -----
def do_step(self, arg_string):
"""step X Y
Play a step at space X, Y."""
if not self.board:
return self.onecmd('help new')
try:
x, y = (int(arg) for arg in arg_string.split())
except ValueError:
return self.onecmd('help step')
try:
game_end = self.board.step(x, y)
except IndexError:
print('out of range')
return False
if game_end:
print()
print(self.board)
print()
if game_end == EndState.VICTORY:
print('You win!!')
elif game_end == EndState.DEFEAT:
print('You lose.')
self._cleanup_game()
return False
def do_flag(self, arg_string):
"""flag X Y
Flag space X, Y as containing a mine."""
if not self.board:
return self.onecmd('help new')
try:
x, y = (int(arg) for arg in arg_string.split())
except ValueError:
return self.onecmd('help flag')
self.board.flag(x, y)
def do_mark(self, arg_string):
"""mark X Y
Flag space X, Y as containing a mine."""
if not self.board:
return self.onecmd('help new')
try:
x, y = (int(arg) for arg in arg_string.split())
except ValueError:
return self.onecmd('help mark')
self.board.flag(x, y)
def do_unflag(self, arg_string):
"""flag X Y
Remove flag from space X, Y."""
if not self.board:
return self.onecmd('help new')
try:
x, y = (int(arg) for arg in arg_string.split())
except ValueError:
return self.onecmd('help unflag')
self.board.unflag(x, y)
def do_unmark(self, arg_string):
"""mark X Y
Remove flag from space X, Y."""
if not self.board:
return self.onecmd('help new')
try:
x, y = (int(arg) for arg in arg_string.split())
except ValueError:
return self.onecmd('help unmark')
self.board.unflag(x, y)
# ----- parser handling -----
def _exit(self):
print('Goodbye')
return True
def do_bye(self, __):
"""Exit the game."""
return self._exit()
def do_exit(self, __):
"""Exit the game."""
return self._exit()
def do_quit(self, __):
"""Exit the game."""
return self._exit()
def do_EOF(self, __):
"""Exit the game."""
return self._exit()
# ----- meta -----
def precmd(self, line):
if line != 'EOF':
line = line.lower()
return line
def postcmd(self, stop, line):
if stop:
return True
print()
if self.board:
print(self.board)
else:
print(NEW_GAME_ADVICE)
return False
def main():
interpreter = MinesweeperCmd()
interpreter.cmdloop()
if __name__ == '__main__':
main()