-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminesweeper_board.py
66 lines (44 loc) · 1.54 KB
/
minesweeper_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
import random
#
# Given board size: width x height and number of bombs
# return minesweeper's board filled with randomly distributed bombs
# with neighbourhood cells containing numbers of bombs in adjustment cells
#
def show_board(a):
for row in a:
for cell in row:
print cell,
print ""
def create_board(width, height, num_bombs):
if width <= 0 or height <= 0:
print("Size of board should be bigger than 0")
if num_bombs <= 0:
print("Number of bombs should be bigger than 0")
max_cell_idx = width * height
if num_bombs > max_cell_idx:
return [['X' for _ in xrange(width)] for _ in xrange(height)]
bombs_idx = set()
while len(bombs_idx) != num_bombs:
bombs_idx.add(random.randint(0, max_cell_idx))
neighbours = {}
for idx in bombs_idx:
y, x = idx / width, idx % width
for y_idx in xrange(max(0, y-1), 1 + min(y+1, height-1)):
for x_idx in xrange(max(0, x - 1), 1 + min(x + 1, width - 1)):
if y_idx == y and x_idx == x:
continue
neighbours[(y_idx, x_idx)] = neighbours.get((y_idx, x_idx), 0) + 1
a = []
for y in xrange(height):
row = []
for x in xrange(width):
if y * width + x in bombs_idx:
row.append('X')
elif (y, x) in neighbours:
row.append(neighbours[(y, x)])
else:
row.append(0)
a.append(row)
return a
w = create_board(10, 10, 30)
show_board(w)