public
Created

Yesterday I was showing my wife how does a computer program works, so I end up with this code

  • Download Gist
gol.py
Python
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
#coding: utf-8
 
"""
Conway's Game of Life implementation with console interface
Author: Antonio Ribeiro - alvesjunior.antonio@gmail.com
license: Do What You Want to
"""
 
from time import sleep
from random import randint
 
 
def ppp(p):
"""Print the board on the screen"""
l = len(p)
print '--'*(l+1) + '-'
for i in xrange(l):
print '|',
for j in xrange(l):
if p[i][j]:
print '■',
else:
print ' ',
print '|'
print '--'*(l+1) + '-'
 
 
def get_board(l=10, fill=lambda : 0):
p = []
for i in range(l):
line = [fill() for i in xrange(l)]
p.append(line)
return p
 
def get_neighbours(p, i, j):
n = 0
l = len(p)-1
for a in [-1, 0, 1]:
for b in [-1, 0, 1]:
if a+i < 0 or b+j < 0 or a+i > l or b+j > l or a == b == 0:
continue
if p[i+a][j+b]:
n += 1
if p[i][j] and n < 2 or n > 3:
return 0
elif n == 3:
return 1
else:
return p[i][j]
 
 
def step(p):
l = len(p)
new_p = get_board(l)
for i in xrange(l):
for j in xrange(l):
new_p[i][j] = get_neighbours(p, i, j)
 
return new_p
 
 
if __name__ == '__main__':
l = 80
p = get_board(l, fill=lambda : randint(0,1))
while True:
ppp(p)
p = step(p)
sleep(0.2)
gol_interface.py
Python
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
#coding: utf-8
#!/usr/bin/env pytHEIGHTon
"""
A Pygame interface for Conway's Game of Life
Keys:
<SPACE> - Pause/resume running
<KEY_UP>/<KEY_DOWN> - increase/decrease speed
<KEY_LEFT>/<KEY_RIGHT> - advance/return one single step
Author: Antonio Ribeiro - alvesjunior.antonio@gmail.com
license: Do What You Want to
"""
 
import pygame
from time import sleep
from random import randint
import gol
 
MAGNIFY = 5
WEIGHT = HEIGHT = 100
 
class Buffer(object):
""" Just a simple FILO stack"""
 
def __init__(self, max_len=150):
self.max_len = max_len
self._buffer = []
 
def put(self, frame):
if len(self._buffer) == self.max_len:
self._buffer.pop(-1)
 
self._buffer.insert(0,frame)
 
def pop(self):
return self._buffer.pop(0)
 
 
def put_point(screen, i, j, color=(255, 255, 255)):
i *= MAGNIFY
j *= MAGNIFY
pygame.draw.rect(screen, color, (i, j, MAGNIFY, MAGNIFY), 0)
 
 
def clear_screen(screen):
pygame.draw.rect(screen, (0,0,0), (0,0,WEIGHT*MAGNIFY,HEIGHT*MAGNIFY), 0)
 
 
def print_board(screen, p):
l = len(p)
for i in range(l):
for j in range(l):
if p[i][j]:
put_point(screen, i, j)
 
 
def main():
running = True
paused = False
delay = 0.1
screen = pygame.display.set_mode((WEIGHT*MAGNIFY, HEIGHT*MAGNIFY))
bfr = Buffer()
p = gol.get_board(WEIGHT, fill=lambda : randint(0,1))
bfr.put(p)
while running:
 
if not paused:
clear_screen(screen)
print_board(screen, p)
pygame.display.flip()
p = gol.step(p)
bfr.put(p)
if delay > 0.0:
sleep(delay)
 
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_DOWN:
if delay < 1.0:
delay += 0.1
elif event.key == pygame.K_UP:
if delay > 0.0:
delay -= 0.1
elif event.key == pygame.K_SPACE:
paused = not paused
elif event.key == pygame.K_LEFT:
paused = True
try:
p = bfr.pop()
except IndexError:
continue
clear_screen(screen)
print_board(screen, p)
pygame.display.flip()
 
elif event.key == pygame.K_RIGHT:
paused = True
bfr.put(p)
p = gol.step(p)
clear_screen(screen)
print_board(screen, p)
pygame.display.flip()
 
if __name__ == '__main__':
main()

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.