sekkurocode / Cellular-Automata

Development of a python software, simulating "Cellular Automata".
MIT License
1 stars 0 forks source link

Bugfix for #11 Nearest neighbor check #15

Closed fabrice-eberle closed 5 months ago

fabrice-eberle commented 6 months ago

11 bug fix for neigherst neghbor check

While testing the nearest neighbor check, i found the problem that the loop already overwrites the cell, so the board is simultanously checking neighbors it is alreay party going to the next generation. We need a temporary board where changes will be marked down on.

Create a temporary board where updates for the next generation are stored that will replace the previous board after every cell and its neighbors are checked.

fabrice-eberle commented 6 months ago

import numpy as np import random

np.set_printoptions(threshold=10000) numberOfRows = 7 numberOfColumns = 7

board = np.random.choice([0, 1], size=(7, 7), p=[0.5, 0.5]) temp_board = np.zeros([numberOfRows, numberOfColumns]).astype(int)

numNeighbors = 0 print(board) for _ in range(3): for rows in range(numberOfRows): for columns in range(numberOfColumns):

        if board[rows,columns]:
            numNeighbors = np.sum(board[rows-1:rows+2,columns-1:columns+2])-1
        else:
            numNeighbors = np.sum(board[rows-1:rows+2,columns-1:columns+2])

        if numNeighbors >= 4:
            temp_board[rows,columns] = 0
        if numNeighbors == 3:
            temp_board[rows,columns] = 1
        elif numNeighbors <= 2:
            temp_board[rows,columns] = 0
            # Reset the number of neighbors for checking the next array position

        numNeighbors = 0
board = temp_board
temp_board = np.zeros([numberOfRows, numberOfColumns]).astype(int)
print(board)
fabrice-eberle commented 6 months ago

Can someone test this code and check if the rules are being followed? So if the board behaves from generation to generation in an expected way?

sekkurocode commented 5 months ago

I'll test tomorrow

sekkurocode commented 5 months ago

The evolution is still wrong, at least on the first row