2012-11-25 52 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