Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- __author__ = 'rebecca'
- # Literal version of the game of life.
- import itertools as it
- def next_round(the_world, updated_world):
- the_world.update(updated_world)
- def togglecell(x, y):
- if the_world[x, y] == False:
- the_world[x, y] = True
- else:
- the_world[x, y] = False
- def neighbors(x, y):
- neighbors = []
- for i, j in it.product(range(x - 1, x + 2), range(y - 1, y + 2)):
- if i >= 0 & i <= (wide - 1):
- if j >= 0 & j <= (high - 1):
- if (i, j) != (x, y):
- try:
- neighbors.append(the_world[(i,j)])
- except KeyError:
- pass # if the_world[(i, j)] does not exist, skip it.
- return neighbors
- def checkneighbors(x, y):
- live_neighbors = neighbors(x, y).count(True)
- if live_neighbors < 2:
- updated_world.update({(x, y): False})
- elif live_neighbors > 3:
- updated_world.update({(x, y): False})
- elif live_neighbors == 2:
- updated_world.update({(x, y): True})
- elif live_neighbors == 3:
- updated_world.update({(x, y): True})
- else:
- print "Something went wrong!"
- # Initialize.
- wide = int(raw_input("How wide?\n>"))
- high = int(raw_input("How high?\n>"))
- the_world = {}
- updated_world = {}
- for i, j in it.product(range(wide), range(high)):
- the_world.update({(i,j): False})
- for i, j in it.product(range(wide), range(high)):
- checkneighbors(i, j)
- print the_world
- next_round(the_world, updated_world)
- print the_world
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement