Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8902405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:36:18+00:00 2026-06-15T01:36:18+00:00

I’m working on the Conway’s game of life program. I have the first two

  • 0

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

}

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T01:36:19+00:00Added an answer on June 15, 2026 at 1:36 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
This could be a duplicate question, but I have no idea what search terms
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
this is what i have right now Drawing an RSS feed into the php,
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.