Created
February 20, 2013 10:30
-
-
Save alvesjnr/4994569 to your computer and use it in GitHub Desktop.
Yesterday I was showing my wife how does a computer program works, so I end up with this code
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
#coding: utf-8 | |
""" | |
Conway's Game of Life implementation with console interface | |
Author: Antonio Ribeiro - [email protected] | |
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) |
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
#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 - [email protected] | |
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment