views:

181

answers:

3

Hi, I have just started learning Python, and I'm trying to write a program for Conway's Game of Life. I'm trying to create a closed universe with boundary conditions(which are the opposite side/corner). I think I have done this, but I'm not iterating over the loop when it runs, and can't work out how to do this. Thanks very much in advance!

def iterate_conway_closed(universe_t0, n_iter=1, display=False):
# This function is similar to your iterate_conway_open function
# but instead of adding a zero guard ring it should add a 
# ‘wrapping ring’ as explained earlier.  Use the same apply_rules
# function as part 1) to actually apply the rules once you have
# padded the universe.  
# Note that unlike the always 0 guard ring for an open universe
# the wrapping ring will need updating after every call to
# apply rules to reflect changes in the universe

height, width=universe_t0.shape
universe_array=numpy.zeros((height+2, width+2), dtype=numpy.uint8)
universe_array[1:-1, 1:-1]=universe_t0

def count(n_iter):
    n=0
    while n<= n_iter:
        yield n
        n+=1

for n in range(0,n_iter):
    universe_array[:1,1:-1]=universe_t0[-1:,:] # Maps the bottom row
    universe_array[0,1:-1]=universe_t0[-1:,0:]# Maps the top row
    universe_array[1:-1,0]=universe_t0[:,0]# Maps the left column
    universe_array[1:-1,-1]=universe_t0[:,-1]# Maps the right column
    universe_array[0,0]=universe_t0[-1,0]# Maps the bottom left corner
    universe_array[0,-1]=universe_t0[-1,-1]# Maps the bottom right corner
    universe_array[-1,0]=universe_t0[0,0]# Maps the top left corner
    universe_array[-1,-1]=universe_t0[0,-1]# Maps the top right corner      

for i in range(0, n_iter):
    universe_array=apply_rules(universe_array)


if display==True:
    b_print(universe_array)

return universe_array[1:-1, 1:-1]
+1  A: 

your problem could be the break statement

Edit: Also, you probably want just 1 loop, first you initialize universe_array n_iter times, doing nothing after the first time. then you apply the rules n_iter times, you most likely want to put them in the same loop so that the universe gets correctly updated after each iteration.

cobbal
why should it be a problem?
Joschua
Your loop is `while:...break`. Since the `break` is always executed, the loop will always stop after exactly one iteration. To make sense, `break` should be guarded by an `if` statement.
S.Lott
A: 

Hello, as I understand you want to create an iterator function. What you need for this is to replace your return with yield.

As example if you wish to make an iterator-function, which returns the range from 0 to x do this:

 def count(x=0):
     n = 0
     while n <= x:
         yield n
         n += 1

Then you can iterate over it by a for-statement:

for i in count(10):
    print(i)

# prints 0,1,2,3,4,5,6,7,8,9,10 (instead of "," there are line breaks)

The break is also a problem, because it will be always executed, when the while-loop walks through.

Joschua
Thanks for both getting back so quickly! So I understand that I have to get rid of the break, and I've added the def count(n_iter) function before the bit where i've mapped the boundary functions, and deleted the n=1 things from my original function. Does this sounds better? Thanks so much!def count(n_iter): n=0 while n<= n_iter: yield n n+=1
could you update the code in the question to reflect these new updates?
cobbal
No pleasure. ;) When you this answers your questions, you could chop it. If your questions is not answered yet, please update your question above to the new code. Then I'll try to answer it.
Joschua
yep, i've updated the code- sorry it took me a while to work out how to do it! does it look better? thanks!
A: 

You should check your mapping of the wrapping ring, i don't think its correct

Anon