-4

I have to write a Conways game of life simulation for a programming module in college. The program works in the fact that in correctly calculates number of neighbors each iteration. How it should work is:

Current State       Neighbors              Next State
Alive                   2                  Alive
Alive                   3                  Alive
Alive                  <2                  Dead
Alive                  >3                  Dead
Dead                    3                  Alive   

Every time a cells state is changed its 8 surrounding cells neighbor field is increment or decremented.

public static Cell[][] updateGrid(Cell[][] theMatrix){
Cell[][] copy = new Cell[DIMENSIONX][DIMENSIONY];
for(int x = 0; x < DIMENSIONX; x++){
    for(int y = 0; y < DIMENSIONY; y++ ){
        copy[x][y] = theMatrix[x][y];
    }
}
int increment;
for(int x = 0; x < DIMENSIONX; x++){
    for(int y = 0; y < DIMENSIONY; y++ ){
        //Underpopulation
        if((copy[x][y].alive == false)&&(copy[x][y].neighbours == 3)){
            theMatrix[x][y].alive = true;
            increment = 1;
            theMatrix = addNeighbours(theMatrix, increment, x,y);
        }
        //Over Population
        else if((copy[x][y].alive==true)&&(copy[x][y].neighbours > 3)){
            theMatrix[x][y].alive = false;
            increment = -1;
            theMatrix = addNeighbours(theMatrix, increment, x,y);
        }
    }
}
return theMatrix;
}

Thanks for taking the time to have a look guys! ~Paul

PM 77-1
  • 12,933
  • 21
  • 68
  • 111
  • 3
    What is your question here? – PM 77-1 Nov 11 '13 at 23:20
  • 1
    You said it works. Fantastic! Why did you post a question here? – Dawood ibn Kareem Nov 11 '13 at 23:21
  • @DavidWallace The title hints that it does not produce the expected result. – PM 77-1 Nov 11 '13 at 23:23
  • Hmm, good point. So, @Paul, in what way does your actual output differ from what you expect? Can you give an example of a starting matrix, the "expected" next matrix, and the "actual" next matrix? And what did you learn by running this with a debugger? – Dawood ibn Kareem Nov 11 '13 at 23:24
  • There are a number of possible problems here, depending on the definition of `Cell` and the `addNeighbors` method. If `Cell` is mutable (has any "setter" methods) then you could have problems with your `copy[][]` array, since the copy is shallow. You may be using updated cell values in your live-or-die decisions withotu knowing it. Either way, the first thing for you do to is to find a small failing example. You shouldn't need more than a 3x3 board for immutable `Cell` values, or 5x5 if the `Cell` objects are updated rather than replaced in `addNeighbors`. – Mike Housky Nov 11 '13 at 23:49
  • Sorry guys I didnt give output and what is wrong with the program... I just spent 2 hours with the lecturer trying to solve this problem but he is flummouxed... Thanks tho sorry if i wasted your time – Paul Meagher Nov 12 '13 at 14:48

2 Answers2

1

You're not doing all the checks for live cells. You need to check cells other parameters as well

You have:

for(int x = 0; x < DIMENSIONX; x++){
    for(int y = 0; y < DIMENSIONY; y++ ){
        //Underpopulation
        if((copy[x][y].alive == false)&&(copy[x][y].neighbours == 3)){
            theMatrix[x][y].alive = true;
            increment = 1;
            theMatrix = addNeighbours(theMatrix, increment, x,y);
        }
        //Over Population
        else if((copy[x][y].alive==true)&&(copy[x][y].neighbours > 3)){
            theMatrix[x][y].alive = false;
            increment = -1;
            theMatrix = addNeighbours(theMatrix, increment, x,y);
        }
    }
}

Which in brevity is:

for all cells of the grid:
  if dead and neighbor count is 3, make alive
  if alive and neighbor count is > 3 make dead

And you need:

for all cells of the grid:
  if dead and neighbor count is 3, make alive
  if alive and neighbor count is > 3 make dead
  if alive and neighbor count is 0 or 1 make dead // ** need this

Also note that in if blocks, don't use == false such as,

if((copy[x][y].alive == false) && .....

Instead do

if((!copy[x][y].alive) && .....
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
0

Your comments need some work. The "Underpopulation" line isn't really testing for underpopulation. It's testing for creation: spawning new life when a dead cell has exactly three live neighbors.

You don't have an underpopulation test at all. That's where a living cell dies with fewer than 2 live neighbors to sustain it.

The easiest fix to that is modify the "overpopulation" test do to "over/under" extinction:

   //Overpopulation or underpopulation
    else if ( (copy[x][y].alive==true) && 
              ( copy[x][y].neighbours > 3 || copy[x][y].neighbours< < 2) ) {
        theMatrix[x][y].alive = false;
        increment = -1;
        theMatrix = addNeighbours(theMatrix, increment, x,y);
    }

Make sure that addNeighbors is creating a new Cell object with the new values, or you'll have other problems.

Mike Housky
  • 3,959
  • 1
  • 17
  • 31