Вопрос

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.

Это было полезно?

Решение 3

In addition to not really creating a new temp array but just getting a reference to old one, as said in other answers: you copy the tempGrid cell into the grid cell in the same loop where you calculated it. So next loop iteration will use the new calculated value, instead of the current "old" value in grid.

You need to first calculate entire tempGrid, and then copy it over grid. Or better than copying, would be to set grid to point to tempGrid's array, but that might be out of scope of your homework (you would have to return tempGrid from genNextGrid() and assign it to original grid reference).

The whole point of needing a tempGrid instead of just single temp cell is, you need the previous generation in grid during calculation of next generation.

Другие советы

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)

The most obvious problem is here:

  boolean[][] TempGrid = grid;// a temporary array

TempGrid just points to grid. You need to create a separate array:

  boolean[][] TempGrid = new boolean[18][18];
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top