Ich habe ein ziemlich einfaches Problem, aber ich kann nicht scheinen, es herauszufinden. Ich glaube, dass es ein logischer Fehler ist, der mit der Überprüfung von Nachbarn in zellulären Automaten zu tun hat. Hier ist mein Code, der einmal läuft eine zweite für den Anbau und die Überprüfung Nachbarn:Zelluläre Automaten, die Nachbarn überprüfen
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 ein einfaches 12345/3 (Überleben/Geburt) Regel in meiner Cellular Automata verwenden.
Das Problem ist derzeit Ich habe ein 100x100 Gitter mit einem 10x10 Raum von ALIVE/ON Zellen in der Mitte. Nachdem mein Code einmal ausgeführt wurde, sterben alle Zellen.
Wenn jemand mehr Informationen benötigt, zögern Sie nicht zu fragen. Danke im Voraus!
Entschuldigung für die späte Antwort. Also habe ich einige Änderungen implementiert, die die Probleme, über die Sie gesprochen haben, gelöst haben. Ein anderes Problem ist aufgetaucht, das ist jetzt eine massive Pyramide rechts von den Startblöcken und langsam wächst mit scheinbar keinen Reim oder Grund. Iv hat meinen Code aktualisiert –