2017-05-09 10 views
1

コードの次のpiceは私の生命のための私のコードです。何らかの理由で、それは非常に奇妙に動作します。ゲームの最初の数ステップは間違っており、出力全体がゼロになります。どの方法が問題を引き起こしているのか分かりませんが、それは数の隣人法だと思います。Conwayのゲームのゲームは働いていない数の隣人の方法は働いていない

ボードはGOLBoardと呼ばれる2D配列で、x座標とy座標はcellRowcellColです。生存細胞は1であり、死細胞は0である。範囲外の問題を回避する方法は、ボードを12x12にすることですが、行と列には1から11しか使用しません。

<code> @Override 
    public int countNeighbours(int cellRow, int cellCol) { 
     int neighbours = 0; 
     for (int i = cellRow-1; i < cellRow + 2; i++) { 
       for (int j = cellCol - 1; j < cellCol + 2; j++) { 
        if (GOLBoard[i][j] == 1) { 
         neighbours += 1; 
        } 
       } 
      } 
     if (GOLBoard[cellRow][cellCol] == 1) { 
      return(neighbours-1); 
     } else { 
      return(neighbours); 
     } 

    } 



    @Override 
    public int applyRules(int cellRow, int cellCol) { 
     int alive = 0; 
     if (GOLBoard[cellRow][cellCol] == 1) { 
      if (countNeighbours(cellRow, cellCol) == 2 || countNeighbours(cellRow, cellCol) == 3) { 
       alive = 1; 
      } else if (countNeighbours(cellRow, cellCol) < 2 || countNeighbours(cellRow, cellCol) > 3) { 
       alive = 0; 
      } 
     } 


     if (GOLBoard[cellRow][cellCol] == 0) { 
      if (countNeighbours(cellRow, cellCol) == 3) { 
       alive = 1; 
      } 
     } 
     return (alive); 
    } 

    @Override 
    public void takeStep() { 
     for (int row = 1; row < 11; row++) { 
      for (int col = 1; col < 11; col++) { 
       GOLBoard[row][col] = applyRules(row, col); 
      } 
     } 
    } 



    @Override 
    public String toString() { 
     for (int row = 1; row < 11; row++) { 
      for (int col = 1; col < 11; col++) { 
       System.out.print("[" + GOLBoard[row][col] + "]"); 
      } 
      System.out.println(""); 
     } 
     return(""); 
    } 
} <code> 

答えて

1

私が正しくGOLのルールを思い出していた場合は、ボードの進化は「新しい」ボード上の各セルの状態は、条件によってのみ決定される、「世代」のシリーズに進むべき「古い」ボード上の対応するセルのあなたのプログラムは、単一のボードを連続的に進化させようとしているので、以前に計算されたセルの変更はまだ計算されていないセルの結果に影響します。

newBoard = new int[12][12]; 
for (int row = 1; row < 11; row++) { 
    for (int col = 1; col < 11; col++) { 
     newBoard[row][col] = applyRules(row, col); 
    } 
} 
GOLBoard = newBoard; 

は、私の知る限り、あなたのcountNeighboursがOKである:あなたのtakeStepルーチンは、このようなより多くの何かをしなければなりません。

関連する問題