我不知道我是否以正確的方式開展工作,我已經創建了適用於所有「靜物」單元安排的代碼,即。蜂巢,塊,船和麪包。然而,對於那些被認爲是被操縱並涉及細胞死亡和分娩的安排,我有問題。生命遊戲的問題
現在,我先創建單元格,然後殺掉任何屬於規則的人(如果周圍的鄰居總數小於或等於1,等於或大於4)。
我現在走的是什麼,不應該如何應對。我包括我的代碼下面一些圖片:
public class Untitled {
public static void main(String[] params){
Life life = new Life();
String[][] list = life.readGame();
life.print(list);
System.out.println();
for(int i = 1; i <= 5; i++){
list = life.run(list);
life.print(list);
System.out.println();
}
}
}
class Life {
public String[][] readGame(){
String[][] array = {
{ "-","-", "-", "-", "-", "-","-"},
{ "-","-", "-", "-", "-", "-","-"},
{ "-","-", "-", "x", "-", "-","-"},
{ "-","-", "-", "x", "-", "-","-"},
{ "-","-", "-", "x", "-", "-","-"},
{ "-","-", "-", "-", "-", "-","-"},
{ "-","-", "-", "-", "-", "-","-"}
};
return array;
}
public void print(String[][] array){
for(int x = 0; x < array.length; x++){
for(int y = 0; y < array[x].length; y++){
System.out.print(array[x][y]);
}
System.out.println();
}
}
public String[][] run(String[][] array){
populateCells(array);
System.out.println("Populated:\n");
print(array);
killCells(array);
System.out.println("Killed:\n");
print(array);
return array;
}
public int amountOfNeighbors(String[][] array, int x, int y){
int amount = 0;
for(int x1 = -1; x1 <= 1; x1++){
if(x + x1 >= 0 && x + x1 < array.length){
for(int y1 = -1; y1 <= 1; y1++){
if(y + y1 >= 0 && y + y1 < array[x1 + x].length){
if(!(x1 == 0 && y1 == 0) && !array[x1 + x][y1 + y].equals("-")){
amount++;
}
}
}
}
}
return amount;
}
public void populateCells(String[][] array){
int neighbours;
for(int x = 0; x < array.length; x++){
for(int y = 0; y < array[x].length; y++){
neighbours = amountOfNeighbors(array,x,y);
if(neighbours == 3){
array[x][y] = "x";
}
}
}
}
public void killCells(String[][] array){
int neighbours;
for(int x = 0; x < array.length; x++){
for(int y = 0; y < array[x].length; y++){
neighbours = amountOfNeighbors(array,x,y);
if(neighbours <= 1 || neighbours >= 4){
array[x][y] = "-";
}
}
}
}
}
至於圖像,這裏有一個簡單的信號燈細胞的結構和人口和刪除後會發生什麼:
的[遊戲人生(康威的遊戲)可能重複 - 如何檢查細胞鄰居](http://stackoverflow.com/questions/8364301/the-game-of-lifeconways-game-how-to-check-for-cell-neighbours) – 2014-11-23 01:02:10