0

I have a pretty simple problem but I cant seem to figure it out. I believe it is a logic error having to do with the checking of neighbors in cellular automata. Here is my code that runs once a second for growing and checking neighbors:

public void grow(){
    Cell[][] next = new Cell[100][100];
    for(int row = 0; row < (SIZE_X/SIZE); row++){
        for(int col = 0; col < (SIZE_Y/SIZE); col++){
            Cell cell = grid[row][col]; 
            Cell nCell = grid[row][col]; // gets 

            if(cell != null){
                int amount = neighbors(row, col); // find out how many neighbors are ALIVE/ON

                if(cell.isOn() == true && amount != 3) // if the current cell is on but doesnt have 3 alive neighbors, it gets turned off
                    nCell.onOff(false);
                else if(cell.isOn() == false && (amount >= 1 && amount <= 4)) // if it is off and has 1-5 alive neighbors it gets turned on
                    nCell.onOff(true);

                next[row][col] = nCell;
            }
        }
    }
    grid = next;
}

public int neighbors(int row, int col){ // checks the amount of neighbors that are ALIVE/ON
    int amount = 0;

    for(int r = row-1; r <= row+1; r++){ // stepping through a 3x3 area of the grid, which surrounds the selected block
        for(int c = col-1; c <= col+1; c++){

            // clamp
            if((r > 0 && r < 99) && (c > 0 && c < 99)){
                if(grid[r][c].isOn() == true && (r != row && c != col)) // checks if the current neighbor is ALIVE/ON 
                    amount++; // if it is then add one to the count
            }
        }
    }
    return amount;
}

Im using a simple 12345/3(Survival/Birth) rule in my Cellular Automata.

The problem currently is I have a 100x100 grid with a 10x10 space of ALIVE/ON cells in the center. After my code runs once, all the cells die.

If anyone needs more information, feel free to ask. Thanks in advance!

3 Answers3

0

There are some problems but I'm not sure how everything is dead as result.

First problem:

if(cell.isOn() == true && amount != 3) // if the current cell is on but doesnt have 3 alive neighbors, it gets turned off
  cell.onOff(false);
if(cell.isOn() == false && (amount >=1 && amount <= 5)) // if it is off and has 1-5 alive neighbors it gets turned on
  cell.onOff(true);

Let's assume the cell have 1 live neighbour. Then first clause turns it off and then second one turns it back on. So "death" rule doesn't work. Solution: use else if.

Second problem:

You check everything in the same place. For example the field is:

**.
*.*

We check cell (0,0) and then field is: .*. .

Then after all first row is checked the field is: ... . Then everyone dies. :) Solution: first check neighbour number for each cell and store it in each cell. Only after that switch them on and off by the rules.

And third problem: on field edges some neighbours are checked twice. For example cell (0,0) is on and we check neighbours for cell (0,1). First we try (-1, 0) that is changed to (0,0) and added to amount. Later (0,0) is checked again as left neihbour and added to amount again.

AlexZam
  • 1,147
  • 7
  • 18
  • Sorry for the late reply. So I implemented some changed which solved the problems you talked about. Another problem has popped up though, which is now it makes a massive pyramid to the right of the starting blocks and slowly grows out with seemingly no rhyme or reason. Iv updated my code –  Nov 03 '16 at 19:56
0
if(grid[r][c].isOn() == true && (r != row && c != col))

Here you will only consider neighbors that aren't on the same row AND column than your center cell. As a result, youŕe considering 4 cells instead of 8. You probably meant this:

if(grid[r][c].isOn() == true && (r != row || c != col)
OB1
  • 377
  • 1
  • 13
-1

So I implemented some changed which solved the problems you talked about. Another problem has popped up though, which is now it makes a massive pyramid to the right of the starting blocks and slowly grows out with seemingly no rhyme or reason. Iv updated my code

Is Cell a class? Because you assign nCell directly from the grid. If you do this by reference, you also change the value of the cell in the old gridview. Doing this will create patterns which have a tendency to spread to the bottom-right corner of the grid.

EDIT: Just realized this is Java, and the above may not be true there. Disregard this if it is the case.

EDIT2: Also:

                 if(cell.isOn() == true && amount != 3) // if the current cell is on but doesnt have 3 alive neighbors, it gets turned
 off
                     nCell.onOff(false);
                 else if(cell.isOn() == false && (amount >= 1 && amount <= 4)) // if it is off and has 1-5 alive neighbors it gets turned on
                    nCell.onOff(true);

This doesn't mesh with 1-5 survival 3 birth rule.

IRBosman
  • 96
  • 5