-4

大学でプログラミング モジュールの Conways ゲーム オブ ライフ シミュレーションを作成する必要があります。プログラムは、反復ごとに近隣の数を正しく計算するという事実で機能します。それがどのように機能するかは次のとおりです。

Current State       Neighbors              Next State
Alive                   2                  Alive
Alive                   3                  Alive
Alive                  <2                  Dead
Alive                  >3                  Dead
Dead                    3                  Alive   

セルの状態が変更されるたびに、その 8 つの周囲のセルの隣接フィールドがインクリメントまたはデクリメントされます。

public static Cell[][] updateGrid(Cell[][] theMatrix){
Cell[][] copy = new Cell[DIMENSIONX][DIMENSIONY];
for(int x = 0; x < DIMENSIONX; x++){
    for(int y = 0; y < DIMENSIONY; y++ ){
        copy[x][y] = theMatrix[x][y];
    }
}
int increment;
for(int x = 0; x < DIMENSIONX; x++){
    for(int y = 0; y < DIMENSIONY; y++ ){
        //Underpopulation
        if((copy[x][y].alive == false)&&(copy[x][y].neighbours == 3)){
            theMatrix[x][y].alive = true;
            increment = 1;
            theMatrix = addNeighbours(theMatrix, increment, x,y);
        }
        //Over Population
        else if((copy[x][y].alive==true)&&(copy[x][y].neighbours > 3)){
            theMatrix[x][y].alive = false;
            increment = -1;
            theMatrix = addNeighbours(theMatrix, increment, x,y);
        }
    }
}
return theMatrix;
}

時間を割いて見てくれてありがとう!〜ポール

4

2 に答える 2

1

生細胞のすべてのチェックを行っているわけではありません。セルの他のパラメータも確認する必要があります

あなたが持っている:

for(int x = 0; x < DIMENSIONX; x++){
    for(int y = 0; y < DIMENSIONY; y++ ){
        //Underpopulation
        if((copy[x][y].alive == false)&&(copy[x][y].neighbours == 3)){
            theMatrix[x][y].alive = true;
            increment = 1;
            theMatrix = addNeighbours(theMatrix, increment, x,y);
        }
        //Over Population
        else if((copy[x][y].alive==true)&&(copy[x][y].neighbours > 3)){
            theMatrix[x][y].alive = false;
            increment = -1;
            theMatrix = addNeighbours(theMatrix, increment, x,y);
        }
    }
}

簡潔に言うと、次のとおりです。

for all cells of the grid:
  if dead and neighbor count is 3, make alive
  if alive and neighbor count is > 3 make dead

そして、あなたが必要です:

for all cells of the grid:
  if dead and neighbor count is 3, make alive
  if alive and neighbor count is > 3 make dead
  if alive and neighbor count is 0 or 1 make dead // ** need this

また、if ブロックでは、次のよう== falseなものを使用しないでください。

if((copy[x][y].alive == false) && .....

代わりに

if((!copy[x][y].alive) && .....
于 2013-11-11T23:22:47.927 に答える
0

あなたのコメントにはいくつかの作業が必要です。「人口不足」の行は、人口不足を実際にテストしているわけではありません。それは創造のためのテストです: 死んだ細胞にちょうど 3 つの生きた隣人がいるとき、新しい生命を産み出します。

人口不足のテストはまったくありません。それは、生きている細胞が死ぬ場所であり、それを維持するために2つ未満の生きた隣人がいます.

これを修正する最も簡単な方法は、「overpopulation」テスト do を「over/under」消去に変更することです。

   //Overpopulation or underpopulation
    else if ( (copy[x][y].alive==true) && 
              ( copy[x][y].neighbours > 3 || copy[x][y].neighbours< < 2) ) {
        theMatrix[x][y].alive = false;
        increment = -1;
        theMatrix = addNeighbours(theMatrix, increment, x,y);
    }

が新しい値でオブジェクトをaddNeighbors作成していることを確認してください。そうしないと、他の問題が発生します。new Cell

于 2013-11-12T00:06:00.890 に答える