Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing Recursion in java
    text
    copied!<p>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.</p> <pre><code>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 &lt; N; o++) { for (p=0; p&lt;N; p++) { for (int k=(o-1); k &lt;= o+1; k++) { for (int l =(p-1); l &lt;=p+1; l++) { if ( k &gt;= 0 &amp;&amp; k &lt; N &amp;&amp; l &gt;= 0 &amp;&amp; l &lt; N) //for the border indexes. { if (!(k== o &amp;&amp; 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 &lt;N; i2++) { for (int j2 = 0; j2 &lt; 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] &gt;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 &lt; N ; i++) { for ( j = 0; j&lt; N; j++) { cellmaker = Math.random(); if (cellmaker &gt; 0.5) // * = alive; - = dead { b[i][j]=true; System.out.print( "* "); } if (cellmaker &lt; 0.5) { b[i][j] = false; System.out.print("- "); } } System.out.println(); } boolean[][] newcells = new boolean[N][N]; newcells = NewCells(b); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload