2012-11-25 53 views
2

我正在研究康威的人生遊戲計劃。我已經打印出了前兩代的細胞,但我無法再打印。所以我決定使用遞歸來打印多批次的單元格。我的NewCells方法創建第二代。我認爲如果我要通過返回NewCells(c)而不是c來重複所述方法,它會打印出不同的結果,但它會一遍又一遍地打印出同一批次的單元格。在java中使用遞歸

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); 

} 

} 
+1

您的遞歸電話在哪裏? – PearsonArtPhoto

+0

'PearsonArtPhoto'是對的。沒有遞歸調用。作爲一個經驗法則,下一次完成一個問題。只是爲了確保你知道什麼是缺失的,並避免誤解,而其他人閱讀問題並回答它們。 –

+0

哎呦。它表示返回c,它應該返回NewCell(c)。 – iii

回答

1

我不認爲遞歸是這個應用程序的好主意。它會導致StackOverflowError,因爲每一代都會推送另一個調用堆棧幀。遞歸,因爲這個程序使用它,與迭代沒有任何優勢。

相反,將主要方法調用放到一個循環中的NewCells。這樣,無論堆棧大小如何,您都可以根據需要運行儘可能多的迭代。

+0

它的工作原理。非常感謝。 – iii

1

你是不是從NewCell內調用NewCell,這是遞歸是如何工作的。

我認爲這不是你的問題中的錯字,而是缺乏對它是什麼以及它是如何工作的理解,我推薦一些關於recursion in Java的閱讀。

在您瞭解基礎知識後,請回到此處尋求更多幫助!

+0

這是一個錯字。它應該是返回NewCell(c)而不是返回c。 – iii