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

Ken Oliver ksterling at mindspring.com
Sat May 26 00:23:54 CEST 2007


-----Original Message-----
>From: Matt Smith <matt at mattanddawn.orangehome.co.uk>
>Sent: May 25, 2007 4:31 PM
>To: Python Tutor <tutor at python.org>
>Subject: [Tutor] Still having trouble with my game of life algorithm
>
>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


I am NOT an expert, and I have not studied your code very carefully, but I had a thought.  If I understand Conway's rules, if an empty cell has exactly two neighbors, a new "live" cell is spawned.  I don't see that you have done this, but it may be something subtle that I missed.

ken oliver


More information about the Tutor mailing list