2

私はコンウェイのライフゲームプログラムに取り組んでいます。最初の2世代のセルを印刷しましたが、もう印刷できません。そこで、セルの複数のバッチを印刷できるように再帰を使用することにしました。私のNewCellsメソッドは第2世代を作成します。cの代わりにNewCells(c)を返すことでこのメソッドを繰り返すと、異なる結果が出力されると思いましたが、同じバッチのセルが何度も出力されます。

public class Life {

public static boolean[][] NewCells(boolean[][] c)
{
    int N = 5;
    int o=0;
    int p=0;
    int livecnt = 0; //keeps track of the alive cells surrounding cell
    int store = 0; //amount of surrounding cells for each individual cell
    int livestore[] = new int[N*N];


     System.out.println("Next Generation");
     // Checks for the amount of "*" surrounding (o,p)

      for (o=0; o < N; o++)
      { 
         for (p=0; p<N; p++)
         {
             for (int k=(o-1); k <= o+1; k++)
             {

                 for (int l =(p-1); l <=p+1; l++)
                 {
                     if ( k >= 0 && k < N && l >= 0 && l < N) //for the border indexes.
                     { 

                         if (!(k== o && l==p)) //so livecnt won't include the index being checked.
                         {
                             if (c[k][l] == true)
                             {
                                livecnt++;
                             }
                    }

                 }

                 }
             }
             livestore[store]= livecnt;
             livecnt = 0;
             store++;
         }
      }


      //Prints the next batch of cells
      int counter= 0;
      for (int i2 = 0; i2 <N; i2++)
      {
          for (int j2 = 0; j2 < N; j2++)
          {

          if (c[i2][j2] == false)
                 {
                     if (livestore[counter] ==3)
                     {
                        c[i2][j2]=true;
                         System.out.print("* ");
                     }
                     else
                    System.out.print("- ");
                 }

              else if (c[i2][j2] == true)
                 {
                     if (livestore[counter] ==1)
                     {
                        c[i2][j2]= false;
                        System.out.print("- ");
                     }
                     else if (livestore[counter] >3)
                     {
                         c[i2][j2]= false;
                         System.out.print("- ");
                     } 

                     else
                         System.out.print("* ");
                 }
                 counter++;
          }
          System.out.println();
      }

    return NewCell(c);  
}
/*************************************************************************************************************************************************/
public static void main(String[] args)
{
    int N = 5;
    boolean[][] b = new boolean[N][N];
    double cellmaker = Math.random();

    int i = 0;
    int j = 0;


    int o=0;
    int p=0;
    int livecnt = 0; //keeps track of the alive cells surrounding cell
    int store = 0; //amount of surrounding cells for each individual cell
    int livestore[] = new int[N*N];


     System.out.println("First Generation:");
     // Makes the first batch of cells
     for ( i = 0; i < N ; i++)
     {

         for ( j = 0; j< N; j++)
         {
              cellmaker = Math.random();


             if (cellmaker > 0.5) // * = alive; - = dead
             {
                 b[i][j]=true;

                 System.out.print( "* ");

             }


             if (cellmaker < 0.5)
            { b[i][j] = false;


             System.out.print("- ");

            }

         }
         System.out.println();

     }       


     boolean[][] newcells = new boolean[N][N];
     newcells = NewCells(b);

}

}
4

2 に答える 2

1

このアプリケーションでは再帰は良い考えではないと思います。各世代が別の呼び出しスタックフレームをプッシュするため、StackOverflowErrorが発生します。このプログラムは再帰を使用するため、再帰に勝る利点はありません。

代わりに、NewCellsへのメインメソッド呼び出しをループに入れます。そうすれば、スタックサイズに関係なく、好きなだけ反復を実行できます。

于 2012-11-26T00:56:03.963 に答える
1

NewCell内から呼び出しているわけではありませんNewCell。これが再帰の仕組みです。

私はそれがあなたの質問のタイプミスではなく、それが何であるか、そしてそれがどのように機能するかについての理解の欠如であると思います、私はJavaでの再帰について読むことをお勧めします。

基本を理解したら、ここに戻って助けを求めてください!

于 2012-11-26T00:01:11.097 に答える