MCPcopy Index your code
hub / github.com/ndleah/python-mini-project / Board

Class Board

Minesweeper_game/minesweeper.py:4–109  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2import re
3
4class Board:
5 def __init__(self, dim_size, num_bombs):
6
7 self.dim_size = dim_size
8 self.num_bombs = num_bombs
9
10 self.board = self.make_new_board()
11 self.assign_values_to_board()
12
13 self.dug = set()
14
15 def make_new_board(self):
16 board = [[None for _ in range(self.dim_size)] for _ in range(self.dim_size)]
17 bombs_planted = 0
18 while bombs_planted < self.num_bombs:
19 loc = random.randint(0, self.dim_size**2 - 1)
20 row = loc // self.dim_size
21 col = loc % self.dim_size
22
23 if board[row][col] == '*':
24 continue
25
26 board[row][col] = '*'
27 bombs_planted += 1
28
29 return board
30
31 def assign_values_to_board(self):
32
33 for r in range(self.dim_size):
34 for c in range(self.dim_size):
35 if self.board[r][c] == '*':
36 continue
37 self.board[r][c] = self.get_num_neighboring_bombs(r, c)
38
39 def get_num_neighboring_bombs(self, row, col):
40 num_neighboring_bombs = 0
41 for r in range(max(0, row-1), min(self.dim_size-1, row+1)+1):
42 for c in range(max(0, col-1), min(self.dim_size-1, col+1)+1):
43 if r == row and c == col:
44 continue
45 if self.board[r][c] == '*':
46 num_neighboring_bombs += 1
47
48 return num_neighboring_bombs
49
50 def dig(self, row, col):
51 self.dug.add((row, col))
52
53 if self.board[row][col] == '*':
54 return False
55 elif self.board[row][col] > 0:
56 return True
57
58
59 for r in range(max(0, row-1), min(self.dim_size-1, row+1)+1):
60 for c in range(max(0, col-1), min(self.dim_size-1, col+1)+1):
61 if (r, c) in self.dug:

Callers 1

playFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected