2016-04-04 18 views
0

私は自分のConwayのGame of LifeをJavaで作成しました。最初の "人生"を出力するプログラムを手に入れることができますが、3回の反復では更新されません。最初の人生は3回。私はcopyLife()runTheLife()メソッドを書き直そうとしましたが、何も動作していないようです。私の質問はなぜ私の "lifes"が3回更新されないのかということです。Conwaysゲームの更新が更新されない

ご協力いただきますようお願い申し上げます。

public class Life { 

     private static final int ROW = 20; 
     private static final int COL = 20; 

public static void main(String[] args) { 


     // sets up a 20x20 array with a 2 row x column. adds 2 for remaining edges so no overflow in array. 

     boolean[][] nextLife = new boolean[ROW+2][COL+2]; 
     boolean[][] currLife = new boolean[ROW+2][COL+2]; 


     //sets whole life equal to false 
     for(int i = 1; i <= currLife.length - 1; i++) 
     { 

      for(int j = 1; j <= currLife.length - 1; j++) 
      { 
      currLife[i][j] = false; 
      nextLife[i][j] = false; 
      } 
     } 

     // life game 
     for (int i = 1; i < currLife.length - 1; i++) 
     { 
      for (int j = 1; j < currLife.length - 1; j++) 
       { 

        currLife[8][8]= true; 
        currLife[9][9]=true; 
        currLife[9][10] = true; 
        currLife[10][9] = true; 
        currLife[10][10] = true; 

        } 

     } 

     runTheLife(currLife, nextLife); 
} 

private static void runTheLife (boolean[][] currLife, boolean[][] nextLife) 
{ 

    for (int i = 0; i < 3; i++) 
     { 

     copyLife(currLife, nextLife); 
     runThroughRules(currLife, nextLife); 
     printLife(nextLife); 
     System.out.println(); 
     copyLife(nextLife, currLife); 
     runThroughRules(currLife, nextLife); 
     printLife(nextLife); 

     } 

} 
private static int countLiveNeighbors(int row, int col, boolean[][] currLife) 
{ 
     //counts all the live neighbors around a specific cell (checks every cell)   
    int count = 0; 
    if(currLife[row-1][col]){ 
     count++; 
    } 
    if(currLife[row+1][col]){ 
     count++; 
    } 
    if(currLife[row][col-1]){ 
     count++; 
    } 
    if(currLife[row][col+1]){ 
     count++; 
    } 
    if(currLife[row-1][col-1]){ 
     count++; 
    } 
    if(currLife[row-1][col+1]){ 
     count++; 
    } 
    if(currLife[row+1][col-1]){ 
     count++; 
    } 
    if(currLife[row+1][col+1]){ 
     count++; 
    } 
return count; 
} 

private static void runThroughRules(boolean[][] currLife, boolean[][] nextLife) 
{ 
     //determines if the surrounding cells, and the cell itself lives or dies for each iteration   
    for (int i = 1; i < currLife.length - 1; i++) 
    { 

     for (int j = 1; j < currLife.length - 1; j++) 
     { 
      if(countLiveNeighbors(i, j, currLife) == 3) 
       nextLife[i][j] = true; 
      if(countLiveNeighbors(i, j, currLife) < 2) 
       nextLife[i][j] = false; 
      if(countLiveNeighbors(i, j, currLife) > 3) 
       nextLife[i][j] = false; 
     } 
    } 
} 

private static void copyLife(boolean[][] currLife, boolean[][]nextLife) 
{ 
    //copies all the nextLife values to the currLife values   
    for (int i = 1; i < currLife.length - 1; i++) 
    { 

     for (int j = 1; j < currLife.length - 1; j++) 
     { 
      nextLife[i][j] = currLife[i][j]; 


     } 
    } 
} 


private static void printLife(boolean[][] nextLife) 
{ 
    //prints out the program, o being a living cell, a space being a dead cell   
    for (int i = 1; i <= ROW; i++) 
    { 

     for (int j = 1; j <= COL; j++) 
     { 
      if (nextLife[i][j]) 
      { 
       System.out.print("1"); 
      } else { 
       System.out.print("0"); 
      } 
     } 
     System.out.println(""); 

    } 
    //shows when the changes ends and begins 
    System.out.println("--------------------------------------------------------------------------------------"); 
    } 
} 

現在、私の出力は「1」が生存細胞で、「0」が死んだ細胞です。私は、この同じ出力を3回、それが通過するそれぞれの "人生"を更新するゲームのルールなしで得る。

00000000000000000000 
00000000000000000000 
00000000000000000000 
00000000000000000000 
00000000000000000000 
00000000000000000000 
00000000000000000000 
00000000100000000000 
00000001010000000000 
00000000110000000000 
00000000000000000000 
00000000000000000000 
00000000000000000000 
00000000000000000000 
+0

こんにちは、あなたは何を出力したいですか? –

+0

アップデートされた "life" – Eagles11

答えて

1

ライブ/死んだ細胞の異なる初期設定を試してください。

初期設定はそうです。 。 。第一世代の後

00000 
01000 
00110 
00110 
00000 

、あなたはこれがロックシナリオと考えることができ、次の

00000 
00100 
01010 
00110 
00000 

を取得します。死んだ細胞には3つの生きている隣人がいないので、死んだ細胞はすべて死んだままになります。同様に、すべての生きている細胞は2つまたは3つの生きている隣人を持っているので、生きているすべての細胞は生き続ける。したがって、セルに変更がないため、第1世代以降のすべての世代で同じ出力が表示されます。

関連する問題