Created
December 2, 2015 19:40
-
-
Save dmitmel/f3d883f17ba96fffaef3 to your computer and use it in GitHub Desktop.
Game of life
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
s1x = 30 | |
s1y = 30 | |
s2 = 20 | |
gen = [] | |
next_gen = [] | |
clicks = [] | |
def grid_filler(sx, sy, grid, item): | |
for x in range(sx): | |
grid.append([]) | |
for y in range(sy): | |
grid[x].append(item) | |
grid_filler(s1x, s1y, gen, 0) | |
grid_filler(s1x, s1y, next_gen, 0) | |
grid_filler(s1x, s1y, clicks, False) | |
zero = 50 | |
one = 250 | |
cell_stroke = 60 | |
earsing = 0 | |
adding = 1 | |
changing = 2 | |
kClick = False | |
population = 0 | |
generation = 0 | |
keyDelay = 0 | |
maxKeyDelay = 40 | |
maxFrames = 40 | |
mouseMode = adding | |
for x in range(s1x): | |
for y in range(s1y): | |
if gen[x][y] == one: | |
population += 1 | |
def cell(x, y): | |
global s1x, s1y, gen | |
c = 0 | |
if x >= 0 and x < s1x: | |
if y >= 0 and y < s1y: | |
c = gen[x][y] | |
return c; | |
def neighbours(x, y): | |
n = 0 | |
n += cell(x - 1, y - 1) | |
n += cell(x, y - 1) | |
n += cell(x + 1, y - 1) | |
n += cell(x - 1, y) | |
n += cell(x + 1, y) | |
n += cell(x - 1, y + 1) | |
n += cell(x, y + 1) | |
n += cell(x + 1, y + 1) | |
return n | |
def show(): | |
global s1x, s1y, s2, gen, next_gen, one, zero | |
for x in range(s1x): | |
for y in range(s1y): | |
if gen[x][y] == 0: | |
fill(zero) | |
stroke(cell_stroke) | |
else: | |
fill(one) | |
stroke(cell_stroke) | |
rect(s2 * x, s2 * y, s2, s2) | |
def mouseDrew(): | |
global s1x, s1y, s2, gen, population, one, zero, next_gen, mouseMode, clicks, adding, earsing, changing | |
if mousePressed: | |
for x in range(s1x): | |
for y in range(s1y): | |
if (mouseX > s2 * x and mouseY > s2 * y and mouseX < s2 * x + s2 and mouseY < s2 * y + s2) and not(clicks[x][y]): | |
clicks[x][y] = True | |
if mouseMode == changing: | |
if gen[x][y] == 0: | |
mouseMode = adding | |
else: | |
mouseMode = earsing | |
if mouseMode == adding: | |
next_gen[x][y] = 1 | |
population += 1 | |
else: | |
next_gen[x][y] = 0 | |
population -= 1 | |
gen = next_gen | |
show() | |
def setup(): | |
global s1x, s1y, s2 | |
size(s1x * s2 + 1, s1y * s2 + 1) | |
show() | |
def draw(): | |
global s1x, s1y, s2, gen, next_gen, zero, one, state,kClick, generation, population, keyDelay, maxKeyDelay, maxFrames, mouseMode, changing | |
mouseDrew() | |
show() | |
if not(mousePressed): | |
for x in range(s1x): | |
for y in range(s1y): | |
if clicks[x][y]: clicks[x][y] = False | |
mouseMode = changing | |
if not(keyPressed): | |
kClick = False | |
keyDelay = 0 | |
if keyPressed: keyDelay += 1 | |
if keyDelay > maxKeyDelay: keyDelay = maxKeyDelay | |
if keyPressed and (not(kClick) or keyDelay > maxKeyDelay - 1): | |
kClick = True | |
if keyDelay > maxKeyDelay - 1: | |
keyDelay = maxFrames | |
generation += 1 | |
print 'current generation: ', generation | |
print 'current population: ', population | |
for x in range(s1x): | |
for y in range(s1y): | |
nCount = neighbours(x, y) | |
if nCount == 3: | |
next_gen[x][y] = 1 | |
elif nCount < 2 or nCount > 3: | |
next_gen[x][y] = 0 | |
if next_gen[x][y] == 0 and nCount == 3: | |
population += 1 | |
elif next_gen[x][y] == 1 and (nCount < 2 or nCount > 3): | |
population -= 1 | |
gen = next_gen |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment