-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinesweeper_tdd.py
299 lines (251 loc) · 9.81 KB
/
Minesweeper_tdd.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
# coding=utf-8
import random
from itertools import chain
class Board:
alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u",
"v", "w", "x", "y", "z", "aa", "bb", "cc", "dd"]
class Tile:
def __init__(self, x, y):
self.position = (x, y)
self.nearbyMines = 0
self.isMine = False
self.isHidden = True
self.isFlagged = False
def copy(self):
new = Board.Tile(*self.position)
new.nearbyMines = self.nearbyMines
new.isMine = self.isMine
new.isHidden = self.isHidden
new.isFlagged = self.isFlagged
return new
def __repr__(self):
return str(self.nearbyMines)
def __str__(self):
if not self.isHidden and self.isMine:
if self.isFlagged:
return "⚐ "
else:
return "X "
if self.isFlagged:
return "⚑ "
elif self.isHidden:
return "██"
elif self.nearbyMines == 0:
return " "
else:
return str(self.nearbyMines) + " "
def set_as_mine(self):
self.isMine = True
def set_number_of_nearby_mines(self, number):
self.nearbyMines = number
def reveal(self):
self.isHidden = False
def toggle_flag(self):
self.isFlagged = not self.isFlagged
return self.isFlagged
def __init__(self, width, height):
self.width = width
self.height = height
self.is_mine_triggered = False
self.initialize_grid()
self.number_of_flags = 0
def initialize_grid(self):
self.minePositions = []
self.grid = [
[Board.Tile(x, y) for x in range(self.width)] for y in
range(self.height)]
def copy(self):
new = Board(self.width, self.height)
new.is_mine_triggered = self.is_mine_triggered
new.number_of_flags = self.number_of_flags
new.minePositions = self.minePositions[:]
new.grid = []
for row in self.grid:
newRow = []
for tile in row:
newRow.append(tile.copy())
new.grid.append(newRow)
return new
def place_mines(self, numberOfMinesToPlace, tileToAvoid):
for i in range(numberOfMinesToPlace):
tile = self.pick_random_unused_mine_position(tileToAvoid)
tile.set_as_mine()
self.increment_tiles_around_mine(tile)
self.minePositions.append(tile)
def pick_random_unused_mine_position(self, tileToAvoid):
tile = self.pick_random_tile()
while tile in self.minePositions or tile is tileToAvoid:
tile = self.pick_random_tile()
return tile
def pick_random_tile(self):
x = random.randint(0, self.width - 1)
y = random.randint(0, self.height - 1)
return self.convert_coordinate_to_tile(x, y)
def increment_tiles_around_mine(self, centerTile):
for tile in self.surrounding_tiles(centerTile):
tile.nearbyMines += 1
def toggle_flag(self, x, y):
if self.is_valid_coordinate(x, y):
tile = self.convert_coordinate_to_tile(x, y)
if tile.isHidden:
if tile.toggle_flag():
self.number_of_flags += 1
else:
self.number_of_flags -= 1
def reveal(self, x, y):
tilesRevealed = []
if self.is_valid_coordinate(x, y):
tile = self.convert_coordinate_to_tile(x, y)
if not tile.isHidden and tile.nearbyMines > 0:
numFlags = sum(map(lambda s: s.isFlagged, self.surrounding_tiles(tile)))
if numFlags == tile.nearbyMines:
tilesRevealed = list(chain.from_iterable(
map(self.reveal_recursive, self.surrounding_tiles(tile)))) + self.reveal_recursive(tile)
else:
tilesRevealed = self.reveal_recursive(tile)
self.check_end_game_win()
return tilesRevealed
def reveal_recursive(self, centerTile):
if not centerTile.isFlagged:
centerTile.reveal()
tilesRevealed = [centerTile]
if centerTile.isMine:
self.is_mine_triggered = True
self.reveal_whole_board()
if centerTile.nearbyMines == 0:
for tile in self.surrounding_tiles(centerTile):
if tile.isHidden and not tile.isFlagged:
tilesRevealed.extend(self.reveal_recursive(tile))
return tilesRevealed
else:
return []
def surrounding_tiles(self, tile):
x, y = tile.position
surroundingPositions = [(x - 1, y - 1), (x, y - 1), (x + 1, y - 1),
(x - 1, y), (x + 1, y), (x - 1, y + 1),
(x, y + 1), (x + 1, y + 1)]
for x, y in surroundingPositions:
if self.is_valid_coordinate(x, y):
yield self.convert_coordinate_to_tile(x, y)
def convert_coordinate_to_tile(self, x, y):
return self.grid[y][x]
def is_valid_coordinate(self, x, y):
return x in range(self.width) and y in range(self.height)
def check_end_game_loss(self):
return self.is_mine_triggered
def check_end_game_win(self):
for row in self.grid:
for tile in row:
if (tile.isHidden and not tile.isMine) \
or (tile.isMine and not tile.isFlagged):
return False
self.reveal_whole_board()
return True
def reveal_whole_board(self):
for row in self.grid:
for tile in row:
tile.reveal()
def __repr__(self):
rtnString = self.column_markers()
rtnString += self.inner_board()
rtnString += self.column_markers()
return rtnString
def column_markers(self):
return " " + " ".join(Board.alphabet[:self.width]) + "\n"
def inner_board(self):
rtnString = ""
for rowNumber, row in enumerate(self.grid):
rtnString += self.inner_board_start_row_marker(rowNumber)
rtnString += self.inner_board_row_tiles(row)
rtnString += self.inner_board_end_row_marker(rowNumber)
return rtnString
def inner_board_row_tiles(self, row):
rtnString = ""
for tile in row:
rtnString += str(tile)
return rtnString
def inner_board_start_row_marker(self, rowNumber):
return Board.alphabet[rowNumber] + " "
def inner_board_end_row_marker(self, rowNumber):
return " " + Board.alphabet[rowNumber] + "\n"
class Game:
def __init__(self):
self.reveal_callback = self.start_game
self.errorQueue = []
self.isInFlagMode = False
def show_welcome_message(self):
return (
"Welcome to Minesweeper!\n"
"What size minefield do you wish to play on?\n"
"Say 1 for small, 2 for medium, and 3 for large.\n")
def show_board(self):
return (
"Mines Left: " +
str(self.mines - self.board.number_of_flags) + "\n"
"\n" +
str(self.board) +
"\n")
def generate_board(self, width, height, mines):
self.board = Board(width, height)
self.mines = mines
def toggle_flag_mode(self):
self.isInFlagMode = not self.isInFlagMode
def show_prompt(self):
if self.board.check_end_game_loss():
return "Ouch."
elif self.board.check_end_game_win():
return "Congrats, you've won!"
elif self.isInFlagMode:
return (
"Enter an 'f' to exit flag mode\n"
"Enter a coordinate to toggle the flag\n")
else:
return (
"Enter an 'f' to enter flag mode\n"
"Enter a coordinate to reveal a tile\n")
def process_input(self, userInput):
try:
xAlpha, yAlpha = userInput.split()
if xAlpha.lower() in self.board.alphabet and yAlpha in self.board.alphabet:
x, y = self.alpha_to_coordinates(xAlpha, yAlpha)
if self.isInFlagMode:
self.board.toggle_flag(x, y)
else:
return self.reveal(x, y)
except ValueError:
if len(userInput) == 1 and userInput[0].lower() == 'f':
self.toggle_flag_mode()
return
elif userInput == "show":
return
self.errorQueue.append(
"This isn't valid input. Try something like \"a a\"")
def reveal(self, x, y):
return self.reveal_callback(x, y)
def alpha_to_coordinates(self, a, b):
x = self.board.alphabet.index(a.lower())
y = self.board.alphabet.index(b.lower())
return x, y
def show_errors(self):
for error in self.errorQueue:
yield self.errorQueue.pop()
def start_game(self, x, y):
tile = self.board.convert_coordinate_to_tile(x, y)
self.board.place_mines(self.mines, tile)
revealed = self.board.reveal(x, y)
self.reveal_callback = self.board.reveal
return revealed
if __name__ == '__main__':
game = Game()
print game.show_welcome_message()
size = int(input())
if size == 1:
game.generate_board(5, 5, 3)
elif size == 2:
game.generate_board(10, 5, 6)
elif size == 3:
game.generate_board(30, 16, 99)
while not game.board.check_end_game_win() and not game.board.check_end_game_loss():
print game.show_board()
game.process_input(raw_input(game.show_prompt()))
print game.show_board()