-
Notifications
You must be signed in to change notification settings - Fork 1
/
source.py
96 lines (86 loc) · 3.32 KB
/
source.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
import numpy as np
class Array:
def __init__(self, x, y, bombs):
self.x = x
self.y = y
self.board = self.int_board(bombs)
def int_board(self, bmb_qty):
"""
Generating bombs, where 1 means bomb, 0 means safe
"""
self.tmp = np.array(list(-1 for _ in range(bmb_qty)))
self.new = np.array([0])
size = self.x*self.y
while len(self.tmp) < size:
self.tmp = np.append(self.tmp, self.new, axis=0)
self.board = np.ravel(self.tmp)
np.random.shuffle(self.board)
self.board = self.board.reshape((self.x, self.y))
self.touching()
return self.board
# def test_board(self):
# self.bd =
def touching(self):
for row_idx, row in enumerate(self.board, start=0):
for nr_idx, nr in enumerate(row, start=0):
if nr == -1:
try:
#1 before
if nr_idx > 0 and self.board[row_idx][nr_idx-1] != -1:
self.board[row_idx][nr_idx-1] += 1
except:
pass
try:
#1 after
if nr_idx < self.y and self.board[row_idx][nr_idx+1] != -1:
self.board[row_idx][nr_idx+1] += 1
except:
pass
try:
#above 1 before
if row_idx > 0 and nr_idx > 0 and self.board[row_idx-1][nr_idx-1] != -1:
self.board[row_idx-1][nr_idx-1] += 1
except:
pass
try:
#above
if row_idx > 0 and self.board[row_idx-1][nr_idx] != -1:
self.board[row_idx-1][nr_idx] += 1
except:
pass
try:
#above 1 after
if row_idx > 0 and nr_idx < self.y and self.board[row_idx-1][nr_idx+1] != -1:
self.board[row_idx-1][nr_idx+1] += 1
except:
pass
try:
#below 1 before
if row_idx <= self.x and nr_idx > 0 and self.board[row_idx+1][nr_idx-1] != -1:
self.board[row_idx+1][nr_idx-1] += 1
except:
pass
try:
#below
if row_idx <= self.x and self.board[row_idx+1][nr_idx] != -1:
self.board[row_idx+1][nr_idx] += 1
except:
pass
try:
#below 1 after
if row_idx <= self.x and nr_idx < self.y and self.board[row_idx+1][nr_idx+1] != -1:
self.board[row_idx+1][nr_idx+1] += 1
except:
pass
return self.board
@property
def bombs(self):
bomb_qty = 0
for row in self.board:
for cell in row:
if cell == -1:
bomb_qty += 1
return bomb_qty
# if __name__ == "__main__":
# a = Array(31, 20, 50)
# print(a.board)