[Tutor] Still having trouble with my game of life algorithm

Matt Smith matt at mattanddawn.orangehome.co.uk
Fri May 25 22:31:45 CEST 2007


Hi,

First of all, thanks to everyone who helped with my last post
(http://mail.python.org/pipermail/tutor/2007-May/054360.html). I have
re-written the function that applies the rules but it still doesn't
return the expected result. I have been through it and corrected a
couple of bugs bet as far as I can tell it should return a matrix that
has had the rules of Conway's game of life
(http://en.wikipedia.org/wiki/Conway%27s_game_of_life) applied. Here is
my function:

def update_matrix(matrix):
    matrix_updated = matrix
# Perform check for each value in the matrix
    for x in range(len(matrix[0])):
        for y in range(len(matrix)):
            neighbour_count = 0
            for n in (x-1, x, x+1):
                for m in (y-1, y, y+1):
                    try:
                        if matrix[m][n]:
                            if (n,m) != (x,y):
                                neighbour_count = neighbour_count + 1
                    except IndexError:
                        pass
# Apply game of life rules to each item in the matrix
            if neighbour_count < 2:
                matrix_updated[y][x] = 0
            elif neighbour_count > 3:
                matrix_updated[y][x] = 0
            elif neighbour_count == 3:
                matrix_updated[y][x] = 1
# No need to change value if neighbour count == 2
    return matrix_updated

I have also attached the full program and the input file that I am using
for testing in case anyone is interested. The program does use curses to
display the board so I guess it won't be any good for Windows users.

I hope someone can see where I am going wrong here.

Thanks,

Matt
-------------- next part --------------
A non-text attachment was scrubbed...
Name: life.py
Type: text/x-python
Size: 2444 bytes
Desc: not available
Url : http://mail.python.org/pipermail/tutor/attachments/20070525/db07ca9b/attachment.py 
-------------- next part --------------
000000000000000
000000000000000
000000000000000
000000000000000
000000000000000
000000000000000
000000011000000
000000110000000
000000010000000
000000000000000
000000000000000
000000000000000
000000000000000
000000000000000
000000000000000


More information about the Tutor mailing list