Hi all,

I'm coding Conway's Game of Life for my Java class and I've run into some logic errors.

My problem so far is the fact that I have a section of code that looks similar to this:

if(board[i][j+1].getLife() == 1)
     board[i][j].setNumNeighbors(1); //Where setNumNeighbors(int n) { numNeighbors +=1}
}

etc, etc, so I have 8 different comparisons to check in 8 different directions to see if index [j] has a neighbor or not. Now, what I've noticed through testing is that numNeighbors either doesn't reset, and when it does reset, somehow plots with 0 neighbors are returning their neighbors to be 6, 8, 3, etc - which obviously is incorrect.

I have these statements in a Try/Catch as follows:

try{
  /*comparison statements here*/
}catch(Exception e){ 
  //ignore exception
}

So I assume what I'm doing with the Try/Catch is throwing the comparison statements out of the loop when it hits an ArrayOutOfBoundsException. Not being as familiar with exceptions as I should be, could someone please explain what I need to do with the exception statement? I thought if I ignored the exception with a blank line of code it would continue its comparisons of the 2D array without just throwing out the function and continuing on.

Thanks in advance for any help!

Edited 5 Years Ago by Ezzaral: Added code tags. Please use them to format any code that you post.

Dmiller071,
you left too much to be assumed. try posting your code, or maybe the entirety of the compiler complaint you get so that one can know where to look for an issue. first off you assumed you coded correctly, but turns out you have not. you are assuming your problem is inside your try-catch, but may turn out it is not.

Anyways, in this line:
if(board[j+1].getLife() == 1) you are comparing board[][].getLife() against an int. Are you sure your getLife method returns an int. Hope this is of any help.

Edited 5 Years Ago by bibiki: n/a

1. If you have an empty catch block and an E is thrown execution leaves the statement where the E was thrown, drops through the catch block, and goes on to whatever is after that. So if there is a loop it all depends on whether the catch is inside or outside the loop. If its inside the execution will continue with the next iteration of the loop, but if its outside it will exit the loop immediately.
2. setNumNeighbors(int n) { numNeighbors +=1} There's something horribly wrong there, even if its only the method name.
If I call a set method I expect the attribute to be set to whatever I pass in. In this code I can set it to 1 and its value could be 99.

Sorry I didn't paste all of the code last night since it was on a different computer. Here's what I have so far. I assume my try/catch is in the wrong place since it's inside a loop:

import java.util.*;

   public class ArrayBoard{
   
      private Cell[][] board;
      private int numNeighbors;
   
   // Constructor
      public ArrayBoard(){
      //declare board
         board = new Cell[10][10];
         numNeighbors = 0;
      
      //run loop to fill board with Cells with life = 0
         for(int i = 0; i < 10; i++){
            for(int j = 0; j < 10; j++){
               board[i][j] = new Cell(i,j);
            }
         }
      
      
      }
   
   //Get board
      public void getBoardTest(){
         for(int i = 0; i < 10; i++){
            System.out.println();
            for(int j = 0; j < 10; j++){
               System.out.print(board[i][j].getLife()+ " ");
            }
         }
      
      }
   
      public void setNumNeighbors(int n){
         numNeighbors += n;
      }
   
      public int getNumNeighbors(){
         return numNeighbors;
      }
   
   
      public void updateBoard(){
         for(int i = 0; i < 10; i++){
            for(int j = 0; j < 10; j++){
            
               if(board[i][j].getLife() == 0){
                  try{
                  //Check to see if there's any life by the specific cell in 8 directions
                  //to the right of the cell
                     if(board[i][j+1].getLife() == 1 ){
                        setNumNeighbors(1);
                     }
                     //to the left
                     else if(board[i][j-1].getLife() == 1){
                        setNumNeighbors(1);
                     }
                     //to the top
                     else if(board[i-1][j].getLife() == 1){
                        setNumNeighbors(1);
                     }
                     //to the bottom
                     else if(board[i+1][j].getLife() == 1){
                        setNumNeighbors(1);
                     }
                     //to the bottom right
                     else if(board[i+1][j+1].getLife() == 1){
                        setNumNeighbors(1);
                     }
                     //to the bottom left
                     else if(board[i+1][j-1].getLife() == 1){
                        setNumNeighbors(1);
                     }
                     //to the top left
                     else if(board[i-1][j-1].getLife() == 1){
                        setNumNeighbors(1);
                     }
                     //to the top right
                     else if(board[i-1][j+1].getLife() == 1){
                        setNumNeighbors(1);
                     }
                  
                  }
                     catch(Exception e){
                     //ignore ArrayOutOfBoundsException
                     } 
                  if(numNeighbors == 3){
                     board[i][j].setLife(1);
                  }
               
               //End of if life = 0
					//Print out NumNeighbors for testing
         		System.out.println("numNeighbors for "+ i + j + " is " + getNumNeighbors() + " before setting to 0");
         		setNumNeighbors(0);
         		System.out.println("numNeighbors should now = 0: " + " but it = " + getNumNeighbors());
               }
               
               //Start checking for if life = 1
               else if(board[i][j].getLife() == 1){
                  try{
                  //Check to see if there's any life by the specific cell in 8 directions
                  //to the right of the cell
                     if(board[i][j+1].getLife() == 1 ){
                        setNumNeighbors(1);
                     }
                     //to the left
                     else if(board[i][j-1].getLife() == 1){
                        setNumNeighbors(1);
                     }
                     //to the top
                     else if(board[i-1][j].getLife() == 1){
                        setNumNeighbors(1);
                     }
                     //to the bottom
                     else if(board[i+1][j].getLife() == 1){
                        setNumNeighbors(1);
                     }
                     //to the bottom right
                     else if(board[i+1][j+1].getLife() == 1){
                        setNumNeighbors(1);
                     }
                     //to the bottom left
                     else if(board[i+1][j-1].getLife() == 1){
                        setNumNeighbors(1);
                     }
                     //to the top left
                     else if(board[i-1][j-1].getLife() == 1){
                        setNumNeighbors(1);
                     }
                     //to the top right
                     else if(board[i-1][j+1].getLife() == 1){
                        setNumNeighbors(1);
                     }			
                  }
                     catch(Exception e){
                     //ignore ArrayOutOfBoundsException
                     } 
               
               //Decide what happens
                  if(numNeighbors > 3){
                     board[i][j].setLife(0);
                  }
                  
                  else if(numNeighbors < 2){
                     board[i][j].setLife(0);
                  }
                  
                  else if(numNeighbors == 2 || numNeighbors == 3){
                     board[i][j].setLife(1);
                  }
               
               
               
               }
            
            //End of if life == 1
				//Print out NumNeighbors for testing
         System.out.println("numNeighbors for "+ i + j + " is " + getNumNeighbors() + " before setting to 0");
			setNumNeighbors(0);
         System.out.println("numNeighbors should now = 0: " + " but it = " + getNumNeighbors());
            }
         //End of Loop	
			
         }
		//End of Method	
    }
   
   //Set board for testing purposes. 
      public void setBoardTest(){
         board[0][0].setLife(1);
         board[1][0].setLife(1);
      
      }
   
   
   
   }

Cell Class:

public class Cell{
	
	//Used to describe the X and Y coordinates of the arrayBoard
	//Life describes whether the cell is alive or dead. 1 = alive 0 = dead
	private int x, y, life;


	public Cell(int xCoord, int yCoord){
		x = xCoord;
		y = yCoord;
		life = 0;
	
	}

	public int getX(){
		return x;
	}
	
	public int getY(){
		return y;
	}
	
	public void setX(int xCoord){
		x = xCoord;
	}
	
	public void setY(int yCoord){
		y = yCoord;
	}

	public void setLife(int l){
		life = l;
	}
	
	public int getLife(){
		return life;
	}

		
	
	
}

*I'm thinking it's probably better to create numNeighbors inside of the Cell class so it can't go out of scope, thus I can just reset it; however, I know my looping is wrong but I can't seem to figure out the correct way to loop through, use a try/catch, and continue on to comparing neighbors

Edited 5 Years Ago by Dmiller071: n/a

In each block of code, as soon as one array index is out of bounds it jumps to the catch and never executes any of the following neighbours.
Using try/empty catch to manage array indexes isn't ideal. It's not that hard to check the index expressions before trying to use them (just one test for each +1 or -1 expression) and this avoid exceptions in the first place.

It's not that hard to check the index expressions before trying to use them (just one test for each +1 or -1 expression) and this avoid exceptions in the first place.

I understand where you're going with this, but my brain hasn't fully understood the destination of this statement. If you could post a small bit of pseudocode that would help a lot. In my mind I just feel as though it would be more work and more code to test whether that specific index is out of bounds rather than to just throw an exception but I'm sure you have a trick :)

The only other way I'm thinking I could do this is to set each evaluated index inside a try/catch statement. Therefore, if it tests index [i+1][j] and that index is out of bounds, it will skip out of that if([i+1][j]) statement and continue to the next comparison of indexes. This could also be disastrous as well.

BTW Thank you for all the feedback

OK, here's a completely different tack that may make the code a lot easier:
Give the board one extra row/col on each edge.

board = new Cell[12][12];

(you'll have to adjust your loops a bit...

for(int i = 1; i <= 10; i++){
  for(int j = 1; j <= 10; j++){

but now your attempts to reference an off-the-board cell will still be a valid array index (0 or 11). Moreover you can trivially tell that you have gone off-board because the array element will be null, eg

if(board[i][j+1] != null && board[i][j+1].getLife() == 1 ){

Edited 5 Years Ago by JamesCherrill: n/a

The problem is that you use "numNeighbors" variable as the class's variable, and then you do not reset the value each time in the loop. As a result, the number keep increasing... What you need to do is to reset the numNeighbors variable to 0 right before you check for life (right before if (board[j].getLife()) clause...

Line 149 could be "else" and it should be enough.

Edited 5 Years Ago by Taywin: n/a

This article has been dead for over six months. Start a new discussion instead.