0

This code is for a game of life application. My code promts a user for a text file the container rows and columns. The file is read and entered into a 2D array. The array is then pass to my nextGeneration method and this prints the generation.

I need to pass the future array back to the nextGeneration as many times a required per the users input.

I've been struggling all day on how to pass the nextGeneration the "future" array once it has gone through the first generation.

Any help would be greatly appreciated. Thanks.

static void nextGeneration(int grid[][], int M, int N, int NumberofGenerations) 
{ 
    int[][] future = new int[M][N]; 

    // Loop through every cell 


    for (int l = 1; l < M - 1; l++) 
    { 
        for (int m = 1; m < N - 1; m++) 
            { 
        // finding no Of Neighbours that are alive 
        int aliveNeighbours = 0; 
        for (int i = -1; i <= 1; i++) 
            for (int j = -1; j <= 1; j++) 
            aliveNeighbours += grid[l + i][m + j]; 


        // The cell needs to be subtracted from 
        // its neighbours as it was counted before 
        aliveNeighbours -= grid[l][m]; 

        // Implementing the Rules of Life 

        // Cell is lonely and dies 
        if ((grid[l][m] == 1) && (aliveNeighbours < 2)) 
            future[l][m] = 0; 

        // Cell dies due to over population 
        else if ((grid[l][m] == 1) && (aliveNeighbours > 3)) 
            future[l][m] = 0; 

        // A new cell is born 
        else if ((grid[l][m] == 0) && (aliveNeighbours == 3)) 
            future[l][m] = 1; 

        // Remains the same 
        else
            future[l][m] = grid[l][m]; 

        }

    }


    if (NumberofGenerations != 0)
             {
                return   NumberofGenerations -1 *  nextGeneration(future[l][m], 20, 20,NumberofGenerations -1); 
                // recursive call
            else
                return 1;
             }



    System.out.println("Next Generation"); 
    for (int i = 0; i < M; i++) 
    { 
            for (int j = 0; j < N; j++) 
            { 
                if (future[i][j] == 0) 
                    System.out.print(" "); 
                else
                    System.out.print("*"); 
            } 
            System.out.println(); 
    } 

} 
john
  • 1

1 Answers1

0

Generally speaking - Recursion is a good idea but not good for implementation, it can usually be achieved iteratively and takes much more time and memory than an iterative way. Therefore I will show you how to implement your algorithm iteratively.

Pseudo code:

1.    Call nextGeneration
2.    loop 1 to NumberofGenerations
2.1.    init future
2.2.    put next generation in future
2.3.    print future
2.4.    copy future to grid

Since your code creates the next generation regardless of what in future array, it will work.

You already have steps 1, 2.1, 2.2, 2.3, all you need to do is to remove your recursive call, add a loop (stage 2.) and copy future to grid (stage 2.4).

Copy code:

for (int i=0; i < grid.length; i++)
  for (int j=0; j < grid[i].length; j++)
    grid[i][j] = future[i][j];
Yonlif
  • 1,770
  • 1
  • 13
  • 31