-
Notifications
You must be signed in to change notification settings - Fork 0
/
Map.py
60 lines (51 loc) · 2.26 KB
/
Map.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
import random
class Map:
def __init__(self, _table: list):
self.height = len(_table)
self.width = len(_table[0])
self.table = []
self.table.append([0 for _ in range(self.width + 2)])
for item in _table:
temp_row = [0]
temp_row.extend(item)
temp_row.append(0)
self.table.append(temp_row)
self.table.append([0 for _ in range(self.width + 2)])
def Update(self):
count = self.get_neighbor_count_map()
for i in range(self.height):
for j in range(self.width):
if self.get_cell(i, j) == 1:
if count[i][j] < 2 or count[i][j] > 3:
self.set_cell(i, j, 0)
elif count[i][j] == 3:
self.set_cell(i, j, 1)
def flip_cell(self, row: int, col: int):
if 0 <= row <= self.height and 0 <= col <= self.width:
self.table[row + 1][col + 1] = not self.table[row + 1][col + 1]
else:
raise ValueError('Invalid row or col')
def get_neighbor_count_map(self):
count = [[0 for _ in range(self.width)] for _ in range(self.height)]
bias = [(0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0), (-1, 1)]
for i in range(self.height):
for j in range(self.width):
for xx, yy in bias:
count[i][j] = count[i][j] + self.table[i + 1 + xx][j + 1 + yy]
return count
def get_cell(self, row: int, col: int):
if 0 <= row <= self.height and 0 <= col <= self.width:
return self.table[row + 1][col + 1]
else:
raise ValueError('Invalid row or col')
def set_cell(self, row: int, col: int, state: int):
if 0 <= row <= self.height and 0 <= col <= self.width:
if state not in [0, 1]:
raise ValueError('State should be zero or one')
self.table[row + 1][col + 1] = state
else:
raise ValueError('Invalid row or col')
def reset(self, possibility: float):
for i in range(self.height):
for j in range(self.width):
self.table[i + 1][j + 1] = 1 if random.random() <= possibility else 0