This repository has been archived by the owner on Jul 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bad.py
executable file
·376 lines (311 loc) · 12.1 KB
/
bad.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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
class Cell:
'Class describe alived cell if it does exist.'
def __init__(self, x, y, sort=None):
'Simple constructor'
self.__x = x
self.__y = y
self.sort = sort
def get_coords(self):
'Coordinates getter'
return self.__x, self.__y
def get_neighbours(self):
'List of neighbours getter'
x, y = self.__x, self.__y
return [Cell(i[0], i[1]) for i in (
(x-1, y-1), (x, y-1), (x+1, y-1),
(x-1, y), (x+1, y),
(x-1, y+1), (x, y+1), (x+1, y+1))]
def __eq__(self, other):
'Magic method for using this class as operand of `equal` method'
if isinstance(other, type(self)):
return self.get_coords() == other.get_coords()
return False
def __hash__(self):
'Magic method for using this class as set element'
return hash((1+self.__x) ** (1+abs(self.__y) ** (1 + self.__y < 0)))
def __str__(self):
'Magic method for string representation'
return '<{}; {}>'.format(self.__x, self.__y)
def __repr__(self):
'Magic method for pretty printing'
return self.__str__()
from modules.cell import Cell
from modules.world import World
import cv2 as cv
import numpy as np
def nothing(x):
pass
class Gui:
def __init__(self, grid_size=50, width=600, height=600):
cv.namedWindow('field')
self.__grid_size = grid_size
self.__width = width
self.__height = height
# cv.createTrackbar('shift_x', 'params', 0, 500, nothing)
# cv.createTrackbar('shift_y', 'params', 0, 500, nothing)
# cv.createTrackbar('max_x', 'params', 0, 500, nothing)
# cv.createTrackbar('max_y', 'params', 0, 500, nothing)
# cv.createTrackbar('grid_size', 'params', 0, 50, nothing)
def set_args(self,
shift_x,
shift_y,
max_x,
max_y,
root_x=0,
root_y=0):
self.user_grid_size = 10
self.shift_x = shift_x
self.shift_y = shift_y
self.max_x = max_x
self.max_y = max_y
self.root_x = root_x
self.root_y = root_y
def draw(self, world):
assert isinstance(world, World)
alived = world.get_cells()
user_grid_size = 10
shift_x = self.shift_x
shift_y = self.shift_y
max_x = self.max_x
max_y = self.max_y
self.__grid_size = user_grid_size
self.__height = self.__grid_size * max_y
self.__width = self.__grid_size * max_x
field = np.ones(
(self.__height, self.__width, 3), dtype=np.uint8) * 255
print(self.__height)
for y in range(0, self.__height, self.__grid_size):
for x in range(0, self.__width, self.__grid_size):
x += shift_x
y += shift_y
field = cv.line(field, (x, 0), (x, self.__height), 0)
color = (0, 0, 0)
if Cell(x/self.__grid_size+self.root_x, y/self.__grid_size+self.root_y) in alived:
for tmp_cell in alived:
if tmp_cell == Cell(x/self.__grid_size+self.root_x, y/self.__grid_size+self.root_y):
print(tmp_cell.sort)
if tmp_cell.sort == 'A':
color = (255, 0, 0)
break
if tmp_cell.sort == 'B':
color = (0, 0, 255)
break
if tmp_cell.sort == 'C':
color = (0, 255, 0)
break
if tmp_cell.sort == 'D':
color = (0, 0, 0)
break
field = cv.rectangle(
field,
(x-self.root_x*self.__grid_size, y-self.root_y*self.__grid_size),
(x+self.__grid_size-self.root_x*self.__grid_size, y+self.__grid_size - self.__grid_size*self.root_y),
color, cv.FILLED)
field = cv.line(field, (0, y), (self.__width, y), 0)
cv.imshow('field', field)
return cv.waitKey(100)
from modules.cell import Cell
class World():
'Class of the field of the game. Collect all alived cells.'
def __init__(self, cells=[]):
'Simple constructor of initial state'
assert all(isinstance(cell, Cell) for cell in cells)
self.__cells = cells
World.step = 0
def set_cells(self, cells):
'Setter of alived cells'
assert all(isinstance(cell, Cell) for cell in cells)
self.__cells = set(cells)
def get_cells(self):
'Getter of alived cells'
return self.__cells
@staticmethod
def update_stete(cells):
'Static method of updating the state of the world'
assert all(isinstance(cell, Cell) for cell in cells)
# create containers of alived cells and possible alived cells
alive_cells = cells
neighbours = []
# fill container of possible alived cells
for cell in cells:
neighbours += cell.get_neighbours()
neighbours += [cell for cell in alive_cells]
neighbours = {cell for cell in neighbours}
# create container of really alived cells
result = []
def get_sort_neighbours_count(sort, alive_cells, neighbours):
return sum([
cell in filter(lambda c: c.sort == sort, alive_cells) for\
cell in neighbour.get_neighbours()])
# check every possible alived cells using Game of life rules
for neighbour in neighbours:
for tmp_n in alive_cells:
if tmp_n == neighbour: neighbour = tmp_n
local_neighbours_count_D = get_sort_neighbours_count(
'D', alive_cells, neighbours)
local_neighbours_count_C = get_sort_neighbours_count(
'C', alive_cells, neighbours)
local_neighbours_count_B = get_sort_neighbours_count(
'B', alive_cells, neighbours)
local_neighbours_count_A = get_sort_neighbours_count(
'A', alive_cells, neighbours)
if neighbour.sort is None:
if local_neighbours_count_D == 3:
neighbour.sort = 'D'
result.append(neighbour)
continue
if local_neighbours_count_C == 3:
neighbour.sort = 'C'
result.append(neighbour)
continue
if local_neighbours_count_B == 3:
neighbour.sort = 'B'
result.append(neighbour)
continue
if local_neighbours_count_A == 3:
neighbour.sort = 'A'
result.append(neighbour)
continue
elif neighbour.sort == 'D':
if \
local_neighbours_count_D >= 2 or \
local_neighbours_count_C >= 2 or \
local_neighbours_count_B >= 2 or \
local_neighbours_count_A >= 2:
neighbour.sort = 'D'
result.append(neighbour)
continue
elif neighbour.sort == 'C':
if local_neighbours_count_D == 3:
neighbour.sort = 'D'
result.append(neighbour)
continue
if local_neighbours_count_C > 1:
result.append(neighbour)
continue
elif neighbour.sort == 'B':
if local_neighbours_count_D == 3:
neighbour.sort = 'D'
result.append(neighbour)
continue
if local_neighbours_count_B == 3:
result.append(neighbour)
continue
elif neighbour in alive_cells and local_neighbours_count_B==2:
result.append(neighbour)
continue
elif neighbour.sort == 'A':
if local_neighbours_count_D == 3:
neighbour.sort = 'D'
result.append(neighbour)
continue
if World.step % 2 == 0:
result.append(neighbour)
continue
if local_neighbours_count_A == 3:
result.append(neighbour)
continue
elif neighbour in alive_cells and local_neighbours_count_A==2:
result.append(neighbour)
continue
# return alived cells as a set
return {cell for cell in result}
def next_state(self):
'Method of evaluating the game\'s state'
print('\nPrev. state: {}'.format(self.__cells))
World.step += 1
self.__cells = self.update_stete(self.__cells)
print('Current state: {}\n'.format(self.__cells))
#!/usr/bin/python3
from modules.cell import Cell
from modules.world import World
from modules.gui import Gui
import numpy as np
import tkinter as tk
import PIL
import PIL.Image, PIL.ImageTk
import cv2 as cv
world = World(
{
Cell(0, 0, 'B'),
Cell(1, 1, 'B'),
Cell(1, 2, 'B'),
Cell(0, 2, 'B'),
Cell(-1, 2, 'B'),
Cell(4, 5, 'C'),
Cell(4, 4, 'C'),
Cell(2, 4, 'C'),
Cell(3, 4, 'C'),
Cell(-1, -1, 'A'),
Cell(-2, -1, 'A'),
Cell(0, -1, 'A'),
Cell(-3, -1, 'D'),
Cell(-4, -1, 'D'),
Cell(-5, -1, 'D'),
Cell(-6, -1, 'D'),
# Cell(-0, -5, 'A'),
# Cell(-7, -1, 'A'),
})
gui = Gui(20)
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.pack()
self.create_widgets()
def create_widgets(self):
# self.hi_there = tk.Button(self)
# self.hi_there["text"] = "Hello World\n(click me)"
# self.hi_there["command"] = self.say_hi
# self.hi_there.pack(side="top")
self.field = np.zeros((400, 400, 3), dtype=np.uint8)
height, width, channels = self.field.shape
# interface
self.frame2=tk.Frame(self,width=200,height=400)
self.frame2.grid(row=0, column=1)
# ширина поляв клетках
self.textX1=tk.Entry(self.frame2,width=10,font='Arial 14')
self.textX1.place(x='0', y='0')
self.labelX1=tk.Label(self.frame2,text='X1',font='Arial 14')
self.labelX1.place(x='170', y='0')
# высота поляв клетках
self.textY1=tk.Entry(self.frame2,width=10,font='Arial 14')
self.textY1.place(x='0', y='50')
self.labelY1=tk.Label(self.frame2,text='Y1',font='Arial 14')
self.labelY1.place(x='170', y='50')
# смещение по горизонтали
self.textX2=tk.Entry(self.frame2,width=10,font='Arial 14')
self.textX2.place(x='0', y='100')
self.labelX2=tk.Label(self.frame2,text='X2',font='Arial 14')
self.labelX2.place(x='170', y='100')
# смещение по вертикали
self.textY2=tk.Entry(self.frame2,width=10,font='Arial 14')
self.textY2.place(x='0', y='150')
self.labelY2=tk.Label(self.frame2,text='Y2',font='Arial 14')
self.labelY2.place(x='170', y='150')
self.quit = tk.Button(
self.frame2, text='next', fg="red", width='22',
command=self.say_hi)
self.quit.place(x='0', y='350')
def say_hi(self):
print("hi there, everyone!")
root.update()
max_x = int(self.textX1.get())
max_y = int(self.textY1.get())
root_x = int(self.textX2.get())
root_y = int(self.textY2.get())
gui.set_args(10, 10, max_x, max_y, root_x, root_y)
gui.draw(world)
world.next_state()
root = tk.Tk()
app = Application(master=root)
app.mainloop()
i = 0
#while True:
# print('step {}'.format(i))
# key = gui.draw(world)
#
# if key == ord('q'):
# exit()
#
# world.next_state()