-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboard.py
More file actions
273 lines (252 loc) · 10.4 KB
/
board.py
File metadata and controls
273 lines (252 loc) · 10.4 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
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
###################################
# Board #
###################################
"""
Abstraction function:
int a * int b * int c -> An a x b minesweeper board with c mines.
"""
import random
class Board():
"""
Representation Invariants:
self.boardVals contains only lists of equal length
self.boardVals[n] are lists of integers (-1 <= int <= 8),
for all n = [0, len(self.boardVals))
Each integer in boardVals[n] (n = [0, len(self.boardVals)])
represents a square on the board, with the following
definition:
-1 -> A mine
0-8 -> 0-8 adjacent mines, respectively. Not a mine.
self.boardDiscovered matches the size of self.boardVals exactly.
self.boardDiscovered[n] contains integers (similarly to self.boardVals)
representing the state of a square on the board, defined:
-2 -> A flagged, undiscovered square.
-1 -> A flagged, previously discovered square.
0 -> An undiscovered square
1 -> A discovered square
"""
boardVals = []
boardDiscovered = []
"""
Constructs a randomized minesweeper board. Matches the dimensions of board.
It is recommended that board consists only of lists of zeroes, if
this is not the case then this function will simply add mines on top
of any existing mines.
:param board: the board on which this construction will be overlaid.
must be a list of lists of integers, with all sublists
being the same length.
:param mines: A positive integer, the number of mines to be put on the
board.
:returns: a list of lists of integers representing an updated board
with mines mines aded. Returns a shallow copy of the
original board if wrong types are provided.
"""
@staticmethod
def constructBoard(board, mines):
try:
newBoard = board.copy()
for i in range(mines):
x = random.randint(0,len(board)-1)
y = random.randint(0,len(board[0])-1)
while(newBoard[x][y] == -1):
x = random.randint(0,len(board)-1)
y = random.randint(0,len(board[0])-1)
newBoard[x][y] = -1
for addX in range(x-1,x+2):
for addY in range(y-1, y+2):
if(addX >= 0 and
addY >= 0 and
addX < len(board) and
addY < len(board[0])):
if(newBoard[addX][addY] != -1):
newBoard[addX][addY] = newBoard[addX][addY] + 1
return newBoard
except TypeError:
return board.copy()
"""
Constructor for board. If wrong types are given, constructs a
board with no mines of length 1 and width 3.
:param length: A positive integer, the vertical length of the board.
Minimum 1.
:param width: A positive integer, the horizontal width of the board.
Minimum 3.
:param mines: A positive integer, the number of mines to be placed
on the board.
"""
def __init__(self, length, width, mines):
board = []
assert(length >= 1), ""
assert(width >= 3), ""
try:
for i in range(width):
col = []
for ii in range(length):
col.append(0)
board.append(col.copy())
self.boardDiscovered.append(col.copy())
self.boardVals = self.constructBoard(board, mines)
except assertionError:
for i in range(3):
col = []
for ii in range(1):
col.append(0)
board.append(col.copy())
self.boardDiscovered.append(col.copy())
self.boardVals = board
except TypeError:
for i in range(3):
col = []
for ii in range(1):
col.append(0)
board.append(col.copy())
self.boardDiscovered.append(col.copy())
self.boardVals = board
"""
Gets a character representation of a minesweeper square.
:param square: An integer in the range -1 <= square <= 8.
The square to be conerted.
:return: A character conversion of square conforming to
the minesweeper display conventions used here.
Returns None iff an invalid parameter was given.
"""
@staticmethod
def printSquare(square):
try:
if(square == -1):
return 'M'
elif(square == 0):
return ' '
else:
return str(square)[0]
except TypeError:
return None
"""
Prepares a row of the board for printout.
:param rowY: 0 <= rowY < len(self.boardVals[0]), the
y-value of the row to prepare.
:returns: A formatted string of the rowY row to print.
"""
def printRow(self, rowY):
try:
assert(0 <= rowY and rowY < len(self.boardVals[0])), ""
returnStr = ""
for x in range(len(self.boardVals)):
returnStr += " "
if(self.boardDiscovered[x][rowY] == 0):
returnStr += "`"
elif(self.boardDiscovered[x][rowY] < 0):
returnStr += "F"
else:
returnStr += str(Board.printSquare(self.boardVals[x][rowY]))
returnStr += " "
return returnStr
except TypeError:
return ""
except AssertionError:
return ""
"""
Marks the given square at (x,y) as discovered.
If (x,y) has no adjacent mines then other adjacent
squares are discovered, this is performed repeatedly
until all nearby squares are discovered. In regular
games of minesweeper, this is likely to be all
relevant squares, however in large, empty boards,
some squares will be missed to prevent stack overflows.
:param x: 0 <= x < len(self.boardVals)
:param y: 0 <= y < len(self.boardVals)
:returns: The value of the square (x,y) that was
discovered, None if the spec was broken.
"""
def discoverSquare(self, x, y):
try:
depth = 0
if(x >= 0 and x < len(self.boardVals) and
y >= 0 and y < len(self.boardVals[0])):
if(self.boardDiscovered[x][y] < 0):
return 0
if(self.boardDiscovered[x][y] == 1):
return self.boardVals[x][y]
self.boardDiscovered[x][y] = 1
if(self.boardVals[x][y] == 0):
for newX in range(x-1,x+2):
for newY in range(y-1, y+2):
if(newX >= 0 and newY >= 0 and
newX < len(self.boardDiscovered) and
newY < len(self.boardDiscovered[0])):
if(self.boardDiscovered[newX][newY] == 0):
try:
if(self.discoverSquareRec(newX,newY, depth + 1) is None):
self.boardDiscovered[newX][newY] = 1
except RecursionError:
return self.boardVals[x][y]
return self.boardVals[x][y]
else:
return None
except TypeError:
return None
"""
Identical to Board.discoverSquare, with the once exception
that this includes an additional parameter to help with recursive
calls.
:param x: 0 <= x < len(self.boardVals)
:param y: 0 <= y < len(self.boardVals)
:param depth: The depth of this call in the recursion chain.
:returns: The value of the square (x,y) that was
discovered, None if the spec was broken.
"""
def discoverSquareRec(self, x, y, depth):
try:
if(x >= 0 and x < len(self.boardVals) and
y >= 0 and y < len(self.boardVals[0])):
if(self.boardDiscovered[x][y] < 0):
return 0
if(self.boardDiscovered[x][y] == 1):
return self.boardVals[x][y]
self.boardDiscovered[x][y] = 1
if(self.boardVals[x][y] == 0):
for newX in range(x-1,x+2):
for newY in range(y-1, y+2):
if(newX >= 0 and newY >= 0 and
newX < len(self.boardDiscovered) and
newY < len(self.boardDiscovered[0])):
if(self.boardDiscovered[newX][newY] == 0):
if(self.discoverSquareRec(newX,newY, depth+1) is None):
self.boardDiscovered[newX][newY] = 1
return self.boardVals[x][y]
else:
return None
except TypeError:
return None
except RecursionError:
if(depth < 800):
return self.boardVals[x][y]
else:
raise RecursionError
"""
Either flags the given square at (x,y), or
if a flag is already present, returns it to
the unflagged discovery state it was at before
being flagged.
:param x: 0 <= x < len(self.boardVals)
:param y: 0 <= y < len(self.boardVals)
:returns: 0 if a mine was not present, 1,
if a mine was just unflagged,
-1 if a mine was just flagged,
None if the spec was broken.
"""
def flagSquare(self, x, y):
try:
if(x >= 0 and x < len(self.boardVals) and
y >= 0 and y < len(self.boardVals[0])):
if(self.boardDiscovered[x][y] < 0):
self.boardDiscovered[x][y] += 2
if(self.boardVals[x][y] == -1):
return 1
else:
self.boardDiscovered[x][y] -= 2
if(self.boardVals[x][y] == -1):
return -1
return 0
return None
except TypeError:
return None