[Tutor] Modifying game of life
Kooser, Ara S
askoose at sandia.gov
Mon Jan 10 18:53:37 CET 2005
I was trying to modify the Game of Life to a percolation model (you can
pick what the probability of each point being occupied). I wrote the def
for the percolation however when I print the world all I get is
NONE NONE NONE NONE
Etc...
I know my problem is in the line world[i, j] = percolation(perc). The
original code that Danny wrote had the line
world[i,j]= random.choice([LIVE,DEAD]). I am guessing that Danny's code
give a 50/50 chance of a site being occupied and assigns that to i,j . I
do not have the correct syntax to make the world[i,j] go through the
percolation def. I think i and j are not getting assigned a value.
Does anyone have an suggestions, explanations, websites? Thanks.
Ara
import random
perc = raw_input("Please enter a threshold between 0-1. ")
raw_input("Press return to make a world")
PERSON, EMPTY = '*', '.'
def percolation(perc):
randval = random.random()
PERSON, EMPTY = '*', '.'
if randval > perc:
EMPTY
if randval < perc:
PERSON
def make_random_world(M, N):
world = {}
for j in range(N):
for i in range(M):
world[i, j] = percolation(perc)
world['dimensions'] = (M, N)
return world
def print_world(world):
M, N = world['dimensions']
for j in range(N):
for i in range(M):
print world[i, j],
print
print_world(make_random_world(10, 10))
###
More information about the Tutor
mailing list