forked from amalkhatib90/Project01
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboard.py
More file actions
431 lines (323 loc) · 12.7 KB
/
board.py
File metadata and controls
431 lines (323 loc) · 12.7 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
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
## @file board.py
# Source file for the board object
#
# Project: Minesweeper
# Author: Clare Meyer
# Created: 09/06/18
from random import randint
from square import Square
import tkinter as Tk
class Board:
"""
Board class that handles game board creation and functionality
Attributes:
width: Integer for width of the board
height: Integer for height of the board
mines_num: Integer for recording number of mines
num_flags: Integer for recording number of flags
grid: 2D array to store Square instances that make up board
first_selection: Boolean for recording whether or not an interaction is
the user's first interaction
board_window: tk toplevel window instance for board
"""
## Constructor
# @author: Clare
def __init__(self, root, hide_callback):
"""
constructor for board class
Args:
root: root tk widget instance
hide_callback: function to call in order to hide all windows
and return to setup window
"""
self.width = 0
self.height = 0
self.mines_num = 0
self.num_flags = Tk.IntVar(0)
self.grid = [0][0]
self.first_selection = True
# create and center board window
self.board_window = Tk.Toplevel(root)
# configure window
self.board_window.title("Minesweeper 2018")
self.board_window.resizable(width=False, height=False)
self.board_window.protocol("WM_DELETE_WINDOW", hide_callback)
# initially hide window
self.board_window.withdraw()
def gridSquares(self):
"""
place all the squares into the board window
"""
# set size of board window to needed size based on width & height
self.board_window.geometry("{}x{}".format(40*self.width, 40*self.height))
# add squares to grid
for i in range(self.width):
for j in range(self.height):
self.grid[i][j].grid(row=j, column=i, sticky="nesw")
# set size of columns
for i in range(self.width):
self.board_window.grid_columnconfigure(i, minsize=40)
# set size of rows
for i in range(self.height):
self.board_window.grid_rowconfigure(i, minsize=40)
def ungridSquares(self):
"""
remove all the squares from the board window
"""
for i in range(self.width):
for j in range(self.height):
self.grid[i][j].destroy()
## Generates a grid object
# @author: Clare
# @param: size, size of the grid
# @returns: grid
def make_grid(self, width, height, reveal_callback, flag_callback):
"""
Populates grid to user specification
Args:
width: Integer for width of board
height: Integer for height of board
reveal_callback: function to call when a square is revealed
flag_callback: function to call when a square is flagged
Returns:
grid: Populated 2D array to act as game board
"""
width = int(width)
height = int(height)
grid = [[0 for y in range(height)] for x in range(width)]
for i in range(0, width):
for j in range(0, height):
grid[i][j] = Square(self.board_window, i, j, reveal_callback, flag_callback)
return grid
## Randomly places mines on board
# @author: Ayah
# @pre: a grid has been generated
# @param: num_mines, user-selected number of mines
# @param: size, size of the grid
# @param: grid, grid to be populated
# @post: the grid is populated with mines
def generate_mines(self, mines, width, height):
"""
Randomly places mines on board
Args:
mines: Integer number of mines to be used
width: Integer for width of board
height: Integer for height of board
"""
self.mines_num = mines;
self.width = width;
self.height = height;
for i in range(0, self.mines_num):
is_bomb = False
while not is_bomb:
a = randint(0, self.width - 1)
b = randint(0, self.height - 1)
if not self.grid[a][b].is_mine:
self.grid[a][b].is_mine = True
is_bomb = True
## Prints a formatted game board
# @author: Clare
# @pre: grid does not have formatting
# @param: size, size of the grid
# @param: main_grid, grid to be printed
# @post: grid is printed to look nice for the user
def print_board(self, width, height, main_grid):
"""
Prints a formatted game board, followed by making a call to print a
formatted time from stopwatch
Args:
width: Integer for width of board
height: Integer for height of board
main_grid: 2D array to be printed
"""
width = int(width)
height = int(height)
grid = [[0 for x in range(height + 2)] for y in range(width + 2)]
for i in range(0, height+2):
for j in range(0, width+2):
if(i == 0 and j == 0 or i == 0 and j == 1 or i == 1 and j == 0
or i == 1 and j == 1):
grid[j][i] = " "
elif j == 0:
grid[j][i] = i-2
elif i == 0:
grid[j][i] = j-2
elif j == 1:
grid[j][i] = "|"
elif i == 1:
grid[j][i] = "--"
else:
grid[j][i] = main_grid[j - 2][i - 2]
for i in range(0, height+2):
for j in range(0, width+2):
if i == 0 or i == 1 or j == 0 or j == 1:
if i == 1 and j == 0:
print(grid[j][i], end=' ')
elif i == 0 and j == 0 or i == 0 and j == 1 or i == 1 and j == 1:
print((str(grid[j][i]).ljust(2)), end=' ')
elif i == 0 or j == 0:
print((str(grid[j][i]).zfill(2)), end=' ')
else:
print(str(grid[j][i]).ljust(2), end=' ')
else:
grid[j][i].print_square()
print('\n', end=' ')
self.printCurrentTime()
## Counts number of adjacent mines for a given cell
# @authors: Kyle, Kristi
# @param x, x-coordinate of cell
# @param y, y-coordinate of cell
# @param: size, size of the grid
# @param: grid, grid to be checked
def count_nearby_mines(self, x, y, width, height):
"""
Counts the number of adjacent mines for a given Square
Args:
x: Integer coordinate of Square on the x-axis
y: Integer coordinate of Square on the y-axis
width: Integer for width of board
height: Integer for height of board
"""
if self.grid[x][y].is_mine:
return
for i in range(-1, 2):
if x+i >= 0 and x+i < width:
for j in range(-1, 2):
if y+j >= 0 and y+j < height:
if self.grid[x+i][y+j].is_mine:
self.grid[x][y].num_adj_mines += 1
def checkAdditionalReveals(self):
"""
Ensures Squares that were already revealed but were changed to 0 after
the mines moved are revealed properly
"""
for i in range(self.width):
for j in range(self.height):
if self.grid[i][j].is_revealed:
self.reveal(i, j)
# Simple loop to reset num_adj_mines to 0 before being evaluated again
def resetGridMineCount(self):
"""
Resets number of adjacent mines to 0 before evaluating the board
"""
for x in range(0, self.width):
for y in range(0, self.height):
self.grid[x][y].num_adj_mines = 0;
self.grid[x][y].was_moved = False;
## Counts/labels number of adjacent mines for board
# @author: Kyle
# @param: size, size of the grid
# @param: grid, grid to be checked
# @post: each square is labeled with num_adj_mines
def mine_check(self, width, height):
"""
Assigns number of adjacent mines for each Square to the Square member
variable
Args:
width: Integer width of the board
height: Integer height of the board
"""
for x in range(width):
for y in range(height):
#grid[x][y].num_adj_mines = 0;
#oldCount = grid[x][y].num_adj_mines
self.count_nearby_mines(x, y, width, height)
## Recursively calls reveal_adjacent() to uncover squares
# @authors: Ethan, Kristi
# @param x, x-coordinate of cell
# @param y, y-coordinate of cell
def reveal(self, x, y):
"""
Recursively calls reveal_adajenct to reveal Squares
Args:
x: Integer coordinate on the x-axis
y: Integer coordinate on the y-axis
"""
self.grid[x][y].reveal()
if self.grid[x][y].num_adj_mines == 0:
self.reveal_adjacent(x - 1, y - 1)
self.reveal_adjacent(x - 1, y)
self.reveal_adjacent(x - 1, y + 1)
self.reveal_adjacent(x + 1, y)
self.reveal_adjacent(x, y - 1)
self.reveal_adjacent(x, y + 1)
self.reveal_adjacent(x + 1, y - 1)
self.reveal_adjacent(x + 1, y + 1)
## Reveals cells that aren't mines; Called by reveal()
# @authors: Ethan, Kristi
# @param x, x-coordinate of cell
# @param y, y-coordinate of cell
def reveal_adjacent(self, x, y):
"""
Reveals cells that aren't mines
Args:
x: Integer coordinate on the x-axis
y: Integer coordinate on the y-axis
"""
if not self.is_valid_cell(x, y):
return
if self.grid[x][y].is_revealed or self.grid[x][y].is_flagged:
return
if self.grid[x][y].num_adj_mines == 0:
self.grid[x][y].reveal()
self.reveal(x, y)
else:
if not self.grid[x][y].is_mine:
self.grid[x][y].reveal()
return
## Checks that coordinates are within bounds of board
# @author: Kristi
# @param x, x-coordinate of cell
# @param y, y-coordinate of cell
def is_valid_cell(self, x, y):
"""
Checks that coordinates are within bounds of the board
Args:
x: Integer coordinate on the x-axis
y: Integer coordinate on the y-axis
"""
if 0 <= x < self.width and 0 <= y < self.height:
return True
return False
# ##Trys to move the mine several times verse just once, set the tolerance to increase the probability of a successful moved
# #default tolerance set to 10
def validTransformation(self, tolerance, x, y):
"""
Determines if moved mines are moved properly
Args:
tolerance: Integer weight for propability of successful move
x: Integer coordinate on the x-axis
y: Integer coordinate on the y-axis
Returns:
True if valid transformation
"""
if not tolerance == 0:
i = randint(0, self.width - 1)
j = randint(0, self.height - 1)
if not self.grid[i][j].is_mine and not self.grid[i][j].is_flagged and self.is_valid_cell(i, j) and not self.grid[i][j].is_revealed:
self.grid[x][y].is_mine = False
self.grid[i][j].is_mine = True
self.grid[i][j].was_moved = True;
else:
self.validTransformation(tolerance-1, x, y)
##Iterates through each cell of the 2D array and determines if there is a mine
#If there is a mine the mine is removed and placed somewhere else not revealed or a mine
def moveMines(self):
"""
Moves mines to a location that is not revealed nor is a mine
"""
for i in range(0, self.width):
for j in range(0, self.height):
if self.grid[i][j].is_mine and not self.grid[i][j].is_flagged and not self.grid[i][j].was_moved:
self.validTransformation(10, i, j)
def hide_board_window(self):
"""
performs actions to properly hide the board window
"""
# remove all squares from window
self.ungridSquares()
# update window so squares will disappear before window does
# otherwise, squares will appear for a second when window pops up again
self.board_window.update()
# hide window
self.board_window.withdraw()