-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathgameoflife.py
executable file
·109 lines (94 loc) · 2.71 KB
/
gameoflife.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from acabsl import send
from acabsl import update
import acabsl
import colorsys
import random
import time
tick = 2
maxX = acabsl.WALLSIZEX
maxY = acabsl.WALLSIZEY
aliveC = [0,255,0]
deadC = [255,0,0]
class cell(object):
def __init__(self, x, y, state):
"""
track status of cell
state is either:
0 = dead
1 = alive
"""
self.curr_state = state
self.pos = [x, y]
def getState(self):
"""
returns current state of cell
"""
return self.curr_state
def setState(self, state):
"""
manually set the state of a cell
state is either
0 = dead or
1 = alive
"""
self.curr_state = state
def updateState(self,universe):
"""
updates the state of a cell
take a list of the 8 neighbors states as argument
"""
#determine neighborhood sum
x = self.pos[0]
y = self.pos[1]
prev_state = self.curr_state
neighborStates=[]
for i in range(x-1 , x+2):
for j in range(y-1 , y+2):
neighborStates.append( universe[i % maxX][j % maxY].getState() )
n = sum(neighborStates) - prev_state
#cell is alive
if (prev_state == 1 ) :
#less than 2 or more than 3 neighbors: die
if ( n < 2 or n > 3 ):
self.curr_state = 0
#cell is dead
else :
#3 neighnors: become alive
if ( n == 3):
self.curr_state = 1
def randomUniverse():
"""
populates a universe with random state cells
returns following datastructure:
[
[cell_object@0,0 , cell_object@1,0, cell_object@2,0, cell_object@3,0 ...]
[cell_object@0,1 , cell_object@1,1, cell_object@2,1, cell_object@3,1,...]
[cell_object@0,2 , cell_object@1,2, cell_object@2,2, cell_object@3,2 ...]
]
"""
universe = []
for x in range ( 0 , maxX ):
universe.append( [] )
for y in range ( 0 , maxY + 1 ):
universe[x].append( cell( x , y , random.randint(0,1) ))
return universe
u = randomUniverse()
h_dead = 0
update()
while 1:
h_dead += random.gauss(0.02,0.05)
h_dead = h_dead% 1
h_alive = (h_dead + 0.5)%1
rd, gd, bd = colorsys.hsv_to_rgb(h_dead,.3, .2)
ra, ga, ba = colorsys.hsv_to_rgb(h_alive,1., 1.)
for i in range (0, maxX ):
for j in range (0, maxY):
if ( u[i][j].getState() == 0 ):
send(i , j , rd*255, gd*255, bd*255 , tick*0.5)
else:
send(i , j , ra*255, ba*255, ba*255 , tick*0.5)
u[i][j].updateState(u)
update()
time.sleep(tick)