-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgameoflife.py
executable file
·136 lines (110 loc) · 3.64 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
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
#!/usr/bin/python
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# gameoflife.py
# Copyright (C) 2013 Simon Newton
import array
import math
import numpy
from ola.ClientWrapper import ClientWrapper
TICK_INTERVAL = 250 # in ms
class Pixel(object):
def __init__(self, x, y, data, size=8):
self._data = data
self._offset = y * size
if y % 2:
self._offset += size - 1 - x
else:
self._offset += x
self._offset *= 3
def Red(self, i):
self._data[self._offset] = i
def Green(self, i):
self._data[self._offset + 1] = i
def Blue(self, i):
self._data[self._offset + 2] = i
class Game(object):
def __init__(self, size):
self._size = size
self._zeros = numpy.zeros((size, size))
self.InitBoard(size)
self._wrapper = ClientWrapper()
self._data = array.array('B', [0] * (size * size * 3))
self._pixels = [ self.GenList(y) for y in xrange(size) ]
def InitBoard(self, size):
def BoolToInt(b):
if b:
return 1
else:
return 0
self._counter = 0
func = numpy.vectorize(BoolToInt)
self._board = func(numpy.random.random((size, size)) > 0.75)
def Run(self):
self._wrapper.AddEvent(10, self.SendDMXFrame)
self._wrapper.Run()
def GenList(self, y):
return [ Pixel(x, y, self._data) for x in xrange(self._size) ]
def Evolve(self):
alive = self._board > 0
nbrs_count = sum(numpy.roll(numpy.roll(alive, i, 0), j, 1)
for i in (-1, 0, 1) for j in (-1, 0, 1)
if (i != 0 or j != 0))
survive = (nbrs_count == 3) | (alive & (nbrs_count == 2))
new = self._board * survive + survive
return new
def SetCell(self, pixel, age):
if age >= 8:
r = g = b = 255
else:
r = 255 * (age % 2)
g = 255 * ((age >> 1) % 2)
b = 255 * ((age >> 2) % 2)
pixel.Red(r)
pixel.Green(g)
pixel.Blue(b)
def CapAges(b):
if b >= 8:
return 8
else:
return b
CapAgesF = numpy.vectorize(CapAges)
def SendDMXFrame(self):
for x in xrange(self._size):
for y in xrange(self._size):
self.SetCell(self._pixels[x][y], self._board[x][y])
self._counter += 1
self._wrapper.Client().SendDmx(1, self._data)
if numpy.array_equal(self._board, self._zeros):
print 'reset due to blank, counter was %d' % self._counter
self.InitBoard(self._size)
self._wrapper.AddEvent(2000, self.SendDMXFrame)
return
new_board = self.Evolve()
if numpy.array_equal(self.CapAgesF(self._board),
self.CapAgesF(new_board)):
print 'reset due to static, counter was %d' % self._counter
self.InitBoard(self._size)
self._wrapper.AddEvent(2000, self.SendDMXFrame)
return
if self._counter > 200:
print 'reset due to counter'
self.InitBoard(self._size)
self._wrapper.AddEvent(2000, self.SendDMXFrame)
return
self._board = new_board
self._wrapper.AddEvent(TICK_INTERVAL, self.SendDMXFrame)
game = Game(8)
game.Run()