-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminesweeper.py
More file actions
72 lines (55 loc) · 2.15 KB
/
Copy pathminesweeper.py
File metadata and controls
72 lines (55 loc) · 2.15 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
import random
class Board():
def __init__(self, size, num_mines):
self.size = size
self.num_mines = num_mines
self.board = [[0 for x in range(size)] for y in range(size)]
def add_mines(self):
point_list = self.get_mines()
for x, y in point_list:
self.board[x][y] = 'X'
self.add_numbers(point_list)
def get_mines(self):
point_list = []
for x in range(self.num_mines):
point = get_random_point(self.size)
while point in point_list:
point = get_random_point(self.size)
point_list.append(point)
return point_list
def print_board(self):
for row in self.board:
print ' '.join(str(val) for val in row)
def print_partial_board(self, show_list):
for x, row in enumerate(self.board):
print_row = []
for y, val in enumerate(row):
if (x, y) in show_list:
print_row.append(str(val))
else:
print_row.append(' ')
print ' | '.join(print_row)
def add_numbers(self, point_list):
for i in range(self.size):
for j in range(self.size):
if not self.board[i][j] == 'X':
adjacent_points = self.get_adjacent_point_list(i, j)
count = len(adjacent_points.intersection(set(point_list)))
self.board[i][j] = count
def get_adjacent_point_list(self, x, y):
adjacent_offsets = [(i, j) for i in (0, 1, -1) for j in (0, 1, -1)
if not (j == i == 0)]
adjacent_points = set((x+i, y+j) for i, j in adjacent_offsets
if not (x+i < 0 and x+i >= self.size
and y+i < 0 and y+i >= self.size))
return adjacent_points
def main():
board = Board(size=6, num_mines=9)
board.add_mines()
board.print_partial_board([(0, 0), (1, 1), (3, 2), (5, 4)])
def get_random_point(size):
x = random.randint(0, size - 1)
y = random.randint(0, size - 1)
return (x, y)
if __name__ == "__main__":
main()