Instantly share code, notes, and snippets.

# alvesjnr/gol.py Created Feb 20, 2013

Yesterday I was showing my wife how does a computer program works, so I end up with this code
 #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)
 #coding: utf-8 #!/usr/bin/env pytHEIGHTon """ A Pygame interface for Conway's Game of Life Keys: - Pause/resume running / - increase/decrease speed / - 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()