Temporary Array

user1714873 picture user1714873 · Dec 12, 2012 · Viewed 8.7k times · Source

I'm writing a program based on John Conway's Game of Life. I got it to compile and Even run after days of working on it non stop. However, the result it is printing out is wrong...

This is my code (not including the main method)

 //clears the grid
public static void clearGrid ( boolean[][] grid )
{

    for(int row = 0; row < 18; row++){
       for(int col = 0; col < 18; col++){
          grid[row][col]= false;
       }
    }
    //set all index in array to false
}

//generate the next generation
public static void genNextGrid ( boolean[][] grid )
{
    int n; //number of neighbors

    boolean[][] TempGrid = grid;// a temporary array
    for(int row = 0; row < 18; row++)
    {
        for(int col = 0; col < 18; col++)
        {
            TempGrid[row][col] = grid[row][col];
            n = countNeighbors(grid, row, col);

            if(grid[row][col] == true)
            {
                if(n != 2 && n != 3)
                {
                    TempGrid[row][col] = false;
                }
                else
                    TempGrid[row][col] = true;
            }

            else
            {
                if(n == 3)
                {
                    TempGrid[row][col] = true;
                }
                else
                    TempGrid[row][col] = false;
            }
            grid[row][col] = TempGrid[row][col];
        }
    }
}

 //count how many neighbors surrounding any speicific cell
public static int countNeighbors ( final boolean[][] grid, final int row, final int col )
{
    int n = 0;
    for (int TempRow = row - 1; TempRow <= row + 1; TempRow++)
    {
        if (TempRow >= 0 && TempRow < 18)
        {
            for (int TempCol = col - 1; TempCol <= col + 1; TempCol++)
            {
                if (TempCol >= 0 && TempCol < 18 && (TempRow != row || TempCol != col))
                {
                    if (grid[TempRow][TempCol])
                    {
                        n++;
                    }
                }
            }
        }
    }
    return n;
 }

I am pretty sure that the problem is occuring within my genNextGrid method.

The assignment sheet included

public static void genNextGrid (boolean[][] grid);

This method will actually generate the next generation of the simulation. It should use the two-dimensional array grid that is passed to it as the "current" generation. It should create a second, temporary two-dimensional array that will hold the "next" generation. You will need to create this temporary matrix to work with as you can't make changes to the current matrix because you would risk losing all of the information you need in order to successfully create the next generation."

  1. I created a temporary array
  2. I copied the current array to the temporary
  3. I later copied the temporary back to current array like the instruction said.

so I'm not really sure what I did wrong.

It's nearly 3 a.m, been staring at my Vim screen since noon. Any help would be greatly appriciated.

Answer

Amadan picture Amadan · Dec 12, 2012
boolean[][] TempGrid = grid;

will use the same array, just by a different name. You have to allocate some new memory instead. Looking at your code, this should do the trick:

boolean TempGrid = new boolean[18][18];

(It would be much better if you replaced those 18s with a constant though)