[Tutor] Conway's game of life

Phil phillor1948 at gmail.com
Thu Feb 21 00:41:03 EST 2019


Thank you for reading this.

It's been quite some time since I've attempted any programming and since
I've recently discovered PySimpleGUI I thought I'd have a go at programming
Conway's game of life. Unfortunately, I've been stuck trying to resolve a
logic error for the past couple of weeks using the IDLE debugger and a host
of print statements. I can see where the error occurs but I cannot see why.

The error occurs at the point of the third generation which results in a
fixed pattern of cells rather that a looping pattern. It's always the third
generation no matter what the initial pattern is. The error seems to be in
the area where the rules are applied.

Can a kind person spend a couple of minutes to find what is most likely an
obvious error?

import sys
import PySimpleGUI as sg
import time
import numpy

layout = [[sg.Graph(canvas_size=(400, 400),
                   graph_bottom_left=(0,20),
                   graph_top_right=(20,0),
                   background_color='white',
                   key='graph')],]

window = sg.Window('The game of life',
grab_anywhere=True).Layout(layout).Finalize()

graph = window.FindElement('graph')

board_size = 8#20

dead = 0
live = 1

#board = [[dead] * board_size for i in range(board_size)]
#next_board = [[dead] * board_size for i in range(board_size)]

board = numpy.zeros(board_size * board_size, dtype='i').reshape(board_size,
board_size)
#board = numpy.zeros((board_size, board_size))

#next_board = numpy.zeros((board_size, board_size))
next_board = numpy.zeros(board_size * board_size,
dtype='i').reshape(board_size, board_size)


'''
seed board[y][x] NOT board[x][y]
'''

board [1][1] = dead
board [1][2] = live
board [1][3] = dead

board [2][1] = dead
board [2][2] = dead
board [2][3] = live

board [3][1] = live
board [3][2] = live
board [3][3] = live

def display():
    event, values = window.Read(timeout = 0)

    for y in range(board_size):
        for x in range(board_size):

            if (board[x][y]) == 0:
                graph.DrawCircle((y,x), 5, line_color='white',
fill_color='red')#'white')

            else:
                graph.DrawCircle((y,x), 5, line_color='black',
fill_color='black')

    window.Refresh()

    time.sleep(2)

def count_live_neighbours(x, y):
    live_neighbours = 0

    debug_cell = board[x][y]

    for i in range(-1, 2):
        for j in range(-1, 2):
            live_neighbours += board[x + i][y + j]

    #don't count the cell at x = 0 and y = 0
    live_neighbours -= board[x][y]

    return live_neighbours

#display initial board
display()

while True:
    #clear next_board
    #next_board.fill(0)

    for x in range(1, board_size - 1):
        for y in range(1, board_size - 1):

            live_neighbours = count_live_neighbours(x, y)

            '''
Any live cell with fewer than two live neighbors dies, as if by
underpopulation.
Any live cell with two or three live neighbors lives on to the next
generation.
Any live cell with more than three live neighbors dies, as if by
overpopulation.
Any dead cell with exactly three live neighbors becomes a live cell, as if
by reproduction.

            '''
            if board[x][y] == 1 and live_neighbours <  2:
                next_board[x][y] = 0

            elif board[x][y] == 1 and live_neighbours >  3:
                next_board[x][y] = 0

            elif board[x][y] == 0 and live_neighbours == 3:
                next_board[x][y] = 1

            elif board[x][y] == 1 and (live_neighbours == 2 or
live_neighbours == 3):
                next_board[x][y] = 1

            #else:
                #next_board[x][y] = board[x][y]

    board = next_board

    display()





-- 
Regards,
Phil


More information about the Tutor mailing list