forked from amalkhatib90/Project01
-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.py
166 lines (148 loc) · 5.48 KB
/
board.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
## @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
## @class Board
# @brief Handles board creation and board functionality
class Board:
## Constructor
# @author: Clare
def __init__(self):
## @var boardSize
# stores the size of the board
self.boardSize = 0
## @var mines_num
# stores the number of mines
self.mines_num = 0
## Generates a grid object
# @author: Clare
# @param: size, size of the grid
# @returns: grid
def make_grid(self, size):
size = int(size)
grid = [[0 for x in range(size)] for y in range(size)]
for i in range(0, size):
for j in range(0, size):
grid[i][j] = Square()
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, size, grid):
self.mines_num=mines;
self.boardSize=size;
for i in range(0, self.mines_num):
is_bomb = False
while not is_bomb:
a = randint(0, self.boardSize - 1)
b = randint(0, self.boardSize - 1)
if not grid[a][b].is_mine:
grid[a][b].is_mine = True
is_bomb = True
## Prints the board with minimal formatting
# @author:Clare
# @pre: the grid has been generated
# @param: size, size of the grid
# @param: grid, grid to be printed
# @post: grid printed in a grid-like manner
def just_print(self, size, grid):
for i in range(0, size):
for j in range(0, size):
grid[i][j].print_square()
print('\n', end=' ')
## 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, size, main_grid):
size = int(size)
grid = [[0 for x in range(size + 2)] for y in range(size + 2)]
for i in range(0, size+2):
for j in range(0, size+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[i][j] = " "
elif j == 0:
grid[i][j] = i-2
elif i == 0:
grid[i][j] = j-2
elif j == 1:
grid[i][j] = "|"
elif i == 1:
grid[i][j] = "--"
else:
grid[i][j] = main_grid[i - 2][j - 2]
for i in range(0, size+2):
for j in range(0, size+2):
if i == 0 or i == 1 or j == 0 or j == 1:
if i == 1 and j == 0:
print(grid[i][j], end=' ')
elif i == 0 and j == 0 or i == 0 and j == 1 or i == 1 and j == 1:
print((str(grid[i][j]).ljust(2)), end=' ')
elif i == 0 or j == 0:
print((str(grid[i][j]).zfill(2)), end=' ')
else:
print(str(grid[i][j]).ljust(2), end=' ')
else:
grid[i][j].print_square()
print('\n', end=' ')
## 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, size, grid):
if grid[x][y].is_mine:
return
if 0 < x:
# top left
if 0 < y:
if grid[x - 1][y - 1].is_mine:
grid[x][y].num_adj_mines += 1
# top middle
if grid[x - 1][y].is_mine:
grid[x][y].num_adj_mines += 1
# top right
if y < size:
if grid[x - 1][y + 1].is_mine:
grid[x][y].num_adj_mines += 1
# left
if 0 < y:
if grid[x][y - 1].is_mine:
grid[x][y].num_adj_mines += 1
# right
if y < size:
if grid[x][y + 1].is_mine:
grid[x][y].num_adj_mines += 1
if x < size:
# bottom left
if 0 < y:
if grid[x + 1][y - 1].is_mine:
grid[x][y].num_adj_mines += 1
# bottom middle
if grid[x + 1][y].is_mine:
grid[x][y].num_adj_mines += 1
# bottom right
if y < size:
if grid[x + 1][y + 1].is_mine:
grid[x][y].num_adj_mines += 1
## 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, size, grid):
for w in range(size):
for z in range(size):
self.count_nearby_mines(w, z, size - 1, grid)