0

Decided to try my hand at implementing Conway's Game of Life in Python. Wasn't going for a particularly efficient implementation or anything, just wanted something that works. My board is a 2 dimensional list, each cell has a 1 or 0 to denote alive/dead. I'm randomly generating seeds right now; I'll work on feeding in seeds later.

My full code is available on GitHub here.

The problem is, my Game of Life sometimes generates long lines of alive cells in places that should just be dead.

Example: Looking normal so far, but then, the very next generation, sudden long line.

Here's the code where I check for alive cells (far from optimal and could be improved with mod, I know):

def alive(board, y, x):
    neighbors = 0
    left = x - 1 if x > 0 else 9
    right = x + 1 if x < 9 else 0
    up = y - 1 if y > 0 else 9
    down = y + 1 if y < 9 else 0
    neighbors += board[up][left] + board[y][left] + board[down][left] + board[up][x] + board[down][x] + board[up][right] + board[y][right] + board[down][right]
    return 1 if (board[y][x] == 0 and neighbors == 3) or (board[y][x] == 1 and (neighbors == 2 or neighbors == 3)) else 0

My logic makes sense to me, but I'm clearly missing something. I thought it might be due to running the main loop too quickly, but I tried increasing the sleep time, and that changed nothing.

Shivam Roy
  • 1,961
  • 3
  • 10
  • 23

1 Answers1

1

Forgot that I switched from hardcoded width and height to variables; instead of 9, should be width-1 and height-1. Whoops.