-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrid.py
More file actions
145 lines (120 loc) · 4.6 KB
/
grid.py
File metadata and controls
145 lines (120 loc) · 4.6 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import random
from copy import deepcopy
from collections import OrderedDict
from patterns.spaceships import Glider, LWSS, MWSS, HWSS
class Grid:
def __init__(self, size=12, shadow=False):
self.size = size
self._coo_matrix = {}
self.shadow = shadow
def init_pattern(self, pattern, x_offset=0, y_offset=0, rotation='0', flipped=False):
cells = self.rotate_pattern(pattern, rotation, flipped)
for cell in cells:
self.set(cell[0] + x_offset, cell[1] + y_offset, 1)
@staticmethod
def flipped_cells(cells):
flipped = {}
for cell_x, cell_y in cells:
if cell_x == 0:
flipped[(cell_x, cell_y)] = 1
else:
flipped[(-cell_x, cell_y)] = 1
return flipped
def rotate_pattern(self, pattern, rotation, flipped):
rot_pat = {}
cells = pattern.CELLS if not flipped else self.flipped_cells(pattern.CELLS)
for cell in cells:
if rotation == '0':
rot_pat[(cell[0], cell[1])] = 1
elif rotation == '90':
rot_pat[(cell[0] * -1, cell[1])] = 1
elif rotation == '180':
rot_pat[(cell[0] * -1, cell[1] * -1)] = 1
elif rotation == '270':
rot_pat[(cell[0], cell[1] * -1)] = 1
return rot_pat
def init_glider(self):
self.init_pattern(Glider)
def init_lwss(self):
self.init_pattern(LWSS)
def init_mwss(self):
self.init_pattern(MWSS)
def init_hwss(self):
self.init_pattern(HWSS)
def clear(self):
self._coo_matrix = {}
def set(self, x, y, value=1):
if (x, y) not in self._coo_matrix.keys():
self._coo_matrix[(x, y)] = value
return True
return False
def increment(self, x, y):
if (x, y) in self._coo_matrix.keys():
self.set(x, y, self.get(x, y) + 1)
def decrement(self, x, y):
if (x, y) in self._coo_matrix.keys():
self.set(x, y, self.get(x, y) - 1)
def unset(self, x, y):
if (x, y) not in self._coo_matrix.keys():
raise KeyError("Cannot unset unset key value pair")
else:
return not self._coo_matrix.pop((x, y))
def get(self, x, y):
return self._coo_matrix[(x, y)] if (x, y) in self._coo_matrix.keys() else 0
def birth_cell(self, x, y):
self.set(x, y, 1)
def kill_cell(self, x, y):
self._coo_matrix.pop((x, y))
def is_alive(self, x, y):
return (x, y) in self._coo_matrix.keys()
def is_dead(self, x, y):
return not self.is_alive(x, y)
def flip(self, x, y):
if (x, y) in self._coo_matrix.keys():
return self.unset(x, y)
else:
return self.set(x, y)
def points(self):
return list(self._coo_matrix.keys())
@staticmethod
def count_neighbors(x, y, dict_copy):
neighbors = 0
for i in range(-1, 2):
for j in range(-1, 2):
if i == j == 0:
continue
xi = x + i
yj = y + j
neighbors += dict_copy[(xi, yj)] if (xi, yj) in dict_copy.keys() else 0
return neighbors
def check_neighbors(self, x, y, dict_copy, shadow_world):
for i in range(-1, 2):
for j in range(-1, 2):
xi = x + i
yj = y + j
if i == j == 0:
neighbor_count = self.count_neighbors(x, y, dict_copy)
flip = False
if neighbor_count < 2:
flip = True
elif neighbor_count > 3:
flip = True
if flip:
self.flip(x, y)
# shadow_world.set(x, y, 5)
else:
if (xi, yj) not in dict_copy.keys():
if self.count_neighbors(xi, yj, dict_copy) == 3:
self.set(xi, yj, 1)
def get_next_state(self, shadow_world):
if not shadow_world.shadow:
raise AttributeError("Shadow world is not a shadow grid")
cur_state = OrderedDict(sorted(self._coo_matrix.items(), key=lambda t: t[0]))
# if shadow_world.points():
# print(shadow_world.points())
# for px, py in shadow_world.points():
# shadow_world.decrement(px, py)
# if shadow_world.get(px, py) == 0:
# shadow_world.pop((px, py))
for cell_x, cell_y in cur_state.keys():
self.check_neighbors(cell_x, cell_y, cur_state, shadow_world)