Last active
August 29, 2015 14:23
-
-
Save daedalus/faacf175f74d2b437daa to your computer and use it in GitHub Desktop.
conway 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
#!/usr/bin/env python | |
# Copyright (c) 2009 Dario Clavijo | |
# | |
# -*- coding: utf-8 -*- | |
# This is the game life http://en.wikipedia.org/wiki/The_Game_of_Life | |
import os, time, random | |
# | |
# we need a funcion to load cells in the neighborhood | |
# | |
def LoadCells(rows,cols): | |
grid = [] | |
col = [0]*cols | |
cells = 0 | |
#first we load an empty grid | |
for i in range(rows): | |
col = [0]* cols | |
grid.append(col) | |
#then we load some cells | |
for x in range(rows): | |
for y in range(cols): | |
cell = random.randint(0,random.randint(0,1)) | |
grid[x][y] = cell | |
return grid | |
# | |
# here we draw the grid | |
# | |
def DrawGrid(grid): | |
rows=len(grid) | |
cols=len(grid[1]) | |
for x in range(rows): | |
for y in range(cols): | |
if grid[x][y] == 0: | |
print ".", | |
else: | |
print "o", | |
print "\n", | |
# | |
# this funcion count neighbors arround a single cell | |
# | |
def CountNeighbors(grid,x,y): | |
neighbors = 0 | |
rows = len(grid) | |
cols = len(grid[1]) | |
if x < rows-1 and grid[x+1][y] == 1: | |
neighbors = neighbors + 1 | |
if x > 0 and grid[x-1][y] == 1: | |
neighbors = neighbors + 1 | |
if y < cols-1 and grid[x][y+1] == 1: | |
neighbors = neighbors + 1 | |
if y > 0 and grid[x][y-1] == 1: | |
neighbors = neighbors + 1 | |
if x < rows-1 and y < cols-1 and grid[x+1][y+1] == 1: | |
neighbors = neighbors + 1 | |
if x > 0 and y > 0 and grid[x-1][y-1] == 1: | |
neighbors = neighbors + 1 | |
if x > 0 and y < cols-1 and grid[x-1][y+1] == 1: | |
neighbors = neighbors + 1 | |
if x < rows-1 and y > 0 and grid[x+1][y-1] == 1: | |
neighbors = neighbors + 1 | |
return neighbors | |
# here we define a single iteration | |
# if we have between 3 and 6 neighbors the single cell live | |
# in other case the cell die | |
def Iteration(grid): | |
rows = len(grid) | |
cols = len(grid[1]) | |
neighbors = 0 | |
for x in range(rows): | |
for y in range(cols): | |
if grid[x][y] == 1: | |
neighbors = CountNeighbors(grid,x,y) | |
if neighbors >= 3 and neighbors <= 6: | |
xpos = x + random.randint(-1,1) | |
if xpos <= 0: | |
xpos = 1 | |
if xpos >= rows: | |
xpos = rows-1 | |
ypos = y + random.randint(-1,1) | |
if ypos <= 0: | |
ypos = 1 | |
if ypos >= cols: | |
ypos = cols-1 | |
grid[xpos][ypos] = 1 | |
else: | |
grid[x][y] = 0 | |
# | |
# this funcion iterate n pulses and draws the result of earch one | |
# | |
def Iterator(rows,cols,pulsos): | |
pulse = 1 | |
grid = LoadCells(rows,cols) | |
while pulse <= pulses: | |
os.system('clear') | |
print "Pulse: ",pulse | |
Iteration(grid) | |
DrawGrid(grid) | |
pulse = pulse + 1 | |
time.sleep(0.2) | |
number = input("Please imput the number of rows and cols (unique number):") | |
pulses = input("Please imput the number of pulses:") | |
Iterator(number,number,pulses) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment