Hi,

I've got my program working correctly (or so it seems) with my original
data file (r-pentomino.txt - attached) but when I run it with a larger
(30*30) file (big-r-pentomino - also attached) in an attempt to make it
work with out so many edge effects it returns the following error
message:

Traceback (most recent call last):
  File "Python/game_of_life/life.py", line 75, in <module>
    curses.wrapper(main)
  File "curses/wrapper.py", line 44, in wrapper
  File "Python/game_of_life/life.py", line 60, in main
    draw_board(matrix, stdscr, generation)
  File "Python/game_of_life/life.py", line 28, in draw_board
    stdscr.addch(y + 1, x + 1, ' ')
_curses.error: addch() returned ERR

I thought I had designed the program to work with any text file as long
as the lines are all the same length so I cannot understand why I get
this error message. When I read through the code I cannot see a reason
why the program should work for one size file and not another. The part
of the program that is failing is just drawing a space character at a
particular location on the screen.

Here is the listing of the program that I have also attached:

#! /usr/bin/env python

# Curses based Game of Life program
# Written by Matt Smith

import curses
from copy import deepcopy

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 = deepcopy(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 - respond 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)

Can anyone come up with the reason why one input file works and the
other one doesn't??

Thanks,

Matt
00000000000000000000000000000
00000000000000000000000000000
00000000000000000000000000000
00000000000000000000000000000
00000000000000000000000000000
00000000000000000000000000000
00000000000000000000000000000
00000000000000000000000000000
00000000000000000000000000000
00000000000000000000000000000
00000000000000000000000000000
00000000000000000000000000000
00000000000000000000000000000
00000000000000110000000000000
00000000000001100000000000000
00000000000000100000000000000
00000000000000000000000000000
00000000000000000000000000000
00000000000000000000000000000
00000000000000000000000000000
00000000000000000000000000000
00000000000000000000000000000
00000000000000000000000000000
00000000000000000000000000000
00000000000000000000000000000
00000000000000000000000000000
00000000000000000000000000000
00000000000000000000000000000
00000000000000000000000000000
00000000000000000000000000000
000000000000000
000000000000000
000000000000000
000000000000000
000000000000000
000000110000000
000001100000000
000000100000000
000000000000000
000000000000000
000000000000000
000000000000000
000000000000000
000000000000000
000000000000000
#! /usr/bin/env python

# Curses based Game of Life program
# Written by Matt Smith

import curses
from copy import deepcopy

def read_start():
    # Read the starting configuration from a text file
    file = open('/home/matt/Python/game_of_life/big-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 = deepcopy(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 - respond 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)
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to