Python Conway's Game of Life not behaving properly for glider gun design

Josh Katofsky

I'm trying to replicate Conway's Game of Life in Python. The rules of this simulation and a properly working version of it can be found here: https://bitstorm.org/gameoflife/

In my version, when I randomly assign cells to being alive at the start, it appears to behave properly, with the classic amourphous blobs of cells expanding over the screen.

However, when I replicate the "glider gun" arrangement (which can also be seen on the linked website), the cells are not updating properly: the structures decay slightly and then the movement of the cells remains stagnant.

This leads me to believe I have a logical error in my code - any help would be appreciated!

This is the section of my code within my Cell class that updates its survival based on its neighbors (the eight cells surrounding it):

def update(self, neighbors):
    numAliveNeighbors = 0
    for neighbor in neighbors:
        if neighbor.isAlive:
            numAliveNeighbors+=1

    if numAliveNeighbors <= 1 or numAliveNeighbors >=4:
        self.isAlive = False
    elif not self.isAlive and numAliveNeighbors is 3:
        self.isAlive = True

This is the section of my code that finds all of the neighbors of every cell and calls the update method on them:

for row in range(len(cells)):
    for column in range(len(cells[row])):
            neighbors = []
            for tempRow in range (row-1, row + 2):
                for tempColumn in range (column-1, column + 2):
                    if tempRow >= 0 and tempRow < len(cells):
                        if tempColumn >= 0 and tempColumn < len(cells[tempRow]):
                            if not(tempRow is row and tempColumn is column):
                                neighbors.append(cells[tempRow][tempColumn])
            cells[row][column].update(neighbors)
hyper-neutrino

In-Place Updates

You're doing in-place updates; that is, you update each cell individually. What you need to do is create a cloned grid and each time you update a cell on an iteration, update the cloned grid, then set the game board to the cloned grid. Otherwise, cells are going to be updated based on the current iteration, which doesn't make sense.

What you could do is something like this:

def isAlive(alive, neighbours):
    return (alive and 2 <= neighbours <= 3) or (not alive and neighbours == 3)

def update(cells):
    grid = [[0] * len(row) for row in cells]
    for row in range(len(cells)):
        for col in range(len(cells[row])):
            neighbours = 0
            for tr in range(row - 1, row + 2):
                for tc in range(col - 1, col + 2):
                    if (tr != row or tr != col) and cells[tr][tc]:
                        neighbours += 1
            grid[row][col] = isAlive(cells[row][col], neighbours)
return grid

Then you can call cells = update(cells) in a loop.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Python Conway's Game of Life

From Dev

Conway's Game Of Life In C

From Dev

Conway's game of life error

From Dev

Copying List Error Python (part of Conway's Game of Life)

From Dev

C# Conway's Game Of Life

From Dev

Conway's Game of Life - beyond the grid

From Dev

Conway's Game of Life New Values

From Dev

building conway's game of life for iPad

From Dev

Debugging Conway's Game of Life Graphics?

From Dev

Conway's Game of Life New Values

From Dev

Conway's game of life list index error

From Dev

Conway's Game of Life Applying Rules of Life Issues Android

From Dev

What's wrong with my (Conway's Game Of) life?

From Dev

Modelling Conway's game of life in integer linear programming?

From Dev

Counting live neighbors in conway's game of life not working

From Dev

A very specific Conway's Game of Life (Ruby beginner)

From Dev

Why is my Conway's Game of Life acting strangely?

From Dev

John Conway's Game of Life - Basic implementation in C

From Dev

Counting neighboring cells for Conway's Game of Life in C++

From Dev

Optimizing neighbor count function for conway's game of life in C

From Dev

ATBS Conway's Game of Life code doesnt seem right

From Dev

JAVA - Conway's Game of Life deletes all alive cells?

From Dev

Why is my Conway's Game of Life acting strangely?

From Dev

Counting live neighbors in conway's game of life not working

From Dev

Conway's Game of Life C++ Code check

From Dev

Conway game of life, Next generation not printing well

From Dev

Why in Conway's game of life the Gilder becomes the following? I just don't understand

From Dev

Drawing smooth lines to a grid [2D array] in Conway's Game of Life

From Dev

Iterating through different-sized grids to play audio for Conway's Game of Life

Related Related

  1. 1

    Python Conway's Game of Life

  2. 2

    Conway's Game Of Life In C

  3. 3

    Conway's game of life error

  4. 4

    Copying List Error Python (part of Conway's Game of Life)

  5. 5

    C# Conway's Game Of Life

  6. 6

    Conway's Game of Life - beyond the grid

  7. 7

    Conway's Game of Life New Values

  8. 8

    building conway's game of life for iPad

  9. 9

    Debugging Conway's Game of Life Graphics?

  10. 10

    Conway's Game of Life New Values

  11. 11

    Conway's game of life list index error

  12. 12

    Conway's Game of Life Applying Rules of Life Issues Android

  13. 13

    What's wrong with my (Conway's Game Of) life?

  14. 14

    Modelling Conway's game of life in integer linear programming?

  15. 15

    Counting live neighbors in conway's game of life not working

  16. 16

    A very specific Conway's Game of Life (Ruby beginner)

  17. 17

    Why is my Conway's Game of Life acting strangely?

  18. 18

    John Conway's Game of Life - Basic implementation in C

  19. 19

    Counting neighboring cells for Conway's Game of Life in C++

  20. 20

    Optimizing neighbor count function for conway's game of life in C

  21. 21

    ATBS Conway's Game of Life code doesnt seem right

  22. 22

    JAVA - Conway's Game of Life deletes all alive cells?

  23. 23

    Why is my Conway's Game of Life acting strangely?

  24. 24

    Counting live neighbors in conway's game of life not working

  25. 25

    Conway's Game of Life C++ Code check

  26. 26

    Conway game of life, Next generation not printing well

  27. 27

    Why in Conway's game of life the Gilder becomes the following? I just don't understand

  28. 28

    Drawing smooth lines to a grid [2D array] in Conway's Game of Life

  29. 29

    Iterating through different-sized grids to play audio for Conway's Game of Life

HotTag

Archive