-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board.py
79 lines (64 loc) · 2.61 KB
/
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
66
67
68
69
70
71
72
73
74
75
76
77
78
import pygame
class Board(object):
def __init__(self, size, test = False, init_grid=[]):
super().__init__()
self.size = size
self.state = State(size, initial_grid=init_grid)
## test settings
if test:
self.state.grid[size[0] // 2 - 1][size[1] // 2] = True
self.state.grid[size[0] // 2][size[1] // 2] = True
self.state.grid[size[0] // 2 + 1][size[1] // 2] = True
def do_stuff(self):
neighbours = []
for x in range(self.size[0]):
col = []
for y in range(self.size[1]):
col.append(self.state.neighbours((x,y)))
neighbours.append(col)
for x in range(self.size[0]):
for y in range(self.size[1]):
if self.state.grid[x][y]: ## if this pos alive
if neighbours[x][y] >= 4 or neighbours[x][y] <= 1:
self.state.grid[x][y] = False
elif neighbours[x][y] == 3: ## if dead BUT 3 neighbours. THEN GG, BIRTH
self.state.grid[x][y] = True
def modifyAtScreenPos(self, rect, xy, state):
left,top,w,h = rect
box_w = int(w / self.size[0])
box_h = int(h / self.size[1])
x,y = xy
x -= left
y -= top
self.state.grid[x//box_w][y//box_h] = state
def draw(self, rect, display):
left,top,w,h = rect
box_w = int(w / self.size[0])
box_h = int(h / self.size[1])
for x in range(0,self.size[0]):
for y in range(0,self.size[1]):
colour = (230,230,230)
if self.state.grid[x][y]:
colour = (40,230,40)
draw_rect = (left + (box_w * x),
top + (box_h * y),
box_w, box_h)
pygame.draw.rect(display, colour, draw_rect, 0)
class State(object):
def __init__(self, size, initial_grid = []):
super().__init__()
self.grid = initial_grid.copy()
if self.grid == []:
self.grid = [[False for y in range(size[1])] for x in range(size[0])]
def neighbours(self, xy):
num_neighbours = 0
for x in [-1,0,1]:
for y in [-1,0,1]:
if not (x == 0 and y == 0):
check_x = xy[0] + x
check_y = xy[1] + y
if check_x in range(len(self.grid)):
if check_y in range(len(self.grid[check_x])):
if self.grid[check_x][check_y]:
num_neighbours += 1
return num_neighbours