I am wriiting a code about Conway's game of life,and i am a beginner of python,have looked at my code for hours,still couldnt find out the bug...when i run it,it doesnt work....only return the same matrix as the test code given..anybody can help with check it would be very helpful.
here is my code:

``````'''If a cell is dead at time T with exactly three live neighbours, the cell will be alive at T+1
If a cell is alive at time T with less than two living neighbours it dies at T+1
If a cell is alive at time T with more than three live neighbours it dies at T+1
If a cell is alive at time T with exactly two or three live neighbours it remains alive at T+1'''
import numpy

def apply_rules (universe_array):
height, width = universe_array.shape
# create a new array for t+1
evolved_array = numpy.zeros((height, width),numpy.uint8)
for iy in range(1, height-1):
for ix in range(1,width-1):
neighbours=0
if universe_array[iy-1,ix-1]==1:
neighbours+=1
if universe_array[iy,ix-1]==1:
neighbours+=1
if universe_array[iy+1,ix-1]==1:
neighbours+=1
if universe_array[iy-1,ix]==1:
neighbours+=1
if universe_array[iy+1,ix]==1:
neighbours+=1
if universe_array[iy-1,ix+1]==1:
neighbours+=1
if universe_array[iy,ix+1]==1:
neighbours+=1
if universe_array[iy+1,ix+1]==1:
neighbours+=1

if universe_array[iy,ix]==0 and neighbours==3:
evolved_array[iy,ix]==1
if universe_array[iy,ix]==1 and neighbours<2:
evolved_array[iy,ix]==0
if universe_array[iy,ix]==1 and neighbours>3:
evolved_array[iy,ix]==0
if universe_array[iy,ix]==1 and (neighbours==2 or neighbours==3):
evolved_array[iy,ix]=universe_array[iy,ix]

return evolved_array

if __name__ == "__main__":
a = numpy.zeros((4,4),numpy.uint8)
a[1,1]=1
a[1,2]=1
a[2,1]=1
print a
b= apply_rules(a)
print b``````
2
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by Littlerocks

Nice code. Almost there. Here's a hint: `evolved_array[iy,ix]==1` does NOT assign any value to `evolved_array[iy,ix]` . But you seem to be trying to do that. Blame it on your editor...

Nice code. Almost there. Here's a hint: `evolved_array[iy,ix]==1` does NOT assign any value to `evolved_array[iy,ix]` . But you seem to be trying to do that. Blame it on your editor...

Thanks for BearofNH!!!i sort it~~~~~~~~~~~~~~