2

I'm working on the Conway's game of life program. I have the first two generations of cells printed out, but I can not get anymore printed. So I decided to use recursion so multiple batches of cells can be printed. My NewCells method creates the second generation. I thought that If I were to repeat said method by returning NewCells(c) instead of c, It would print out different results, but it prints out the same batch of cells over and over again.

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

}

}
3
  • 1
    Where is your recursion call? Commented Nov 25, 2012 at 23:36
  • PearsonArtPhoto is right. There is not recursion call. As a rule of thumb, finish with a question the next time. Just to make sure you know what is missing, and to avoid misunderstandings while others read the questions and answer them. Commented Nov 25, 2012 at 23:57
  • whoops. Where it says return c, it's supposed to be return NewCell(c). Commented Nov 26, 2012 at 0:04

2 Answers 2

1

I do not think recursion is a good idea for this application. It leads to a StackOverflowError because each generation pushes another call stack frame. Recursion, as this program uses it, has no advantage over iteration.

Instead, put the main method call to NewCells in a loop. That way, you can run as many iterations as you like, regardless of stack size.

Sign up to request clarification or add additional context in comments.

Comments

1

You are not calling NewCell from within NewCell, which is how recursion works.

I'm assuming it's not a typo in your question, but rather a lack of understanding of what it is and how it works, I recommend some reading on recursion in Java.

After you understand the basics, come back here for more help!

1 Comment

It was a typo. It was supposed to be return NewCell(c) instead of return c.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.