Created
February 29, 2012 17:56
-
-
Save jithusunny/1942940 to your computer and use it in GitHub Desktop.
Conway's game of life
This file contains hidden or 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/python | |
import time, sys | |
SIZE = int(raw_input('Enter the size of universe: ')) | |
stable = False | |
def read_coord(live): | |
num = int(raw_input('Enter the number of live cells:')) | |
print('Now, please enter the live cell co-ordniates') | |
for i in range(num): | |
input = raw_input('-').split() | |
x = int(input[0]) | |
y = int(input[1]) | |
live.append([x, y]) | |
def num_of_neighbours(live, cell): | |
x,y = cell | |
neighbours = 0 | |
for i in range(3): | |
if [x - 1, y - 1 + i] in live: | |
neighbours += 1 | |
if [x + 1, y - 1 + i] in live: | |
neighbours += 1 | |
if [x, y - 1] in live: | |
neighbours += 1 | |
if [x, y + 1] in live: | |
neighbours += 1 | |
return neighbours | |
def update(live): | |
global stable | |
dead_cells = [] | |
new_cells = [] | |
for i in range(SIZE): | |
for j in range(SIZE): | |
n = num_of_neighbours(live, [i, j]) | |
if [i, j] in live: | |
if n < 2 or n > 3: | |
dead_cells.append([i, j]) | |
else: | |
if n == 3: | |
new_cells.append([i, j]) | |
if dead_cells or new_cells: | |
for cell in dead_cells: | |
live.remove(cell) | |
for cell in new_cells: | |
live.append(cell) | |
else: | |
stable = True | |
def show(live): | |
for i in range(SIZE): | |
for j in range(SIZE): | |
if [i, j] in live: | |
print '* ', | |
else: | |
print ' ', | |
def main(): | |
day = 0 | |
live = [] | |
read_coord(live) | |
while live and not stable: | |
day += 1 | |
print 'day', day | |
show(live) | |
print '\n' | |
update(live) | |
time.sleep(.7) | |
day += 1 | |
print 'day', day | |
show(live) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment