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
#! /usr/bin/env python

import curses

def read_start():
# Read the starting configuration from a text file
    file = open('/home/matt/Python/game_of_life/r-pentomino.txt', 'r')
    matrix = []
    for line in file:
        line = line.rstrip('\n')
        line_list=[]
        for i in range(len(line)):
            line_list.append(int(line[i]))
        matrix.append(line_list)
    return matrix

def draw_board(matrix, stdscr, generation):
# Draw the life board based on the matrix containing the current state
    for x in range(len(matrix[0])):
        for y in range(len(matrix)):
            if matrix[y][x]:
                stdscr.addch(y + 1, x + 1, '*')
            else:
                stdscr.addch(y + 1, x + 1, '.')
    stdscr.addstr(len(matrix) + 1, 0, 'Generation: %s' % (generation))
    stdscr.refresh()

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

def main(stdscr):
# Initialise some variables and put the screen in it's starting configuration
    matrix = read_start()
    generation = 1
    draw_board(matrix, stdscr, generation)
    stdscr.addstr(len(matrix) + 2, 0, 
        'Press <space> to advance a generation, <q> to quit.')
# The main program loop - respont to keyboard input
    while 1:
        key_press = stdscr.getkey()
        if key_press == 'q':
            break
        elif key_press == ' ':
            generation = generation + 1
            matrix = update_matrix(matrix)
            draw_board(matrix, stdscr, generation)

# Run the main program inside the curses wrapper to ensure it leaves the screen in a usable state`
curses.wrapper(main)
000000000000000
000000000000000
000000000000000
000000000000000
000000000000000
000000000000000
000000011000000
000000110000000
000000010000000
000000000000000
000000000000000
000000000000000
000000000000000
000000000000000
000000000000000
_______________________________________________
Tutor maillist  -  [email protected]
http://mail.python.org/mailman/listinfo/tutor

Reply via email to