Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

how to check surrounding cells in a 2d array - java

Options
  • 28-01-2015 8:24pm
    #1
    Closed Accounts Posts: 768 ✭✭✭


    Hi,
    im trying to implement the game of life in java and keep getting an out of bounds exception in my code.I've been staring it for ages for ages and as usual cant see where my logic has gone arseways:)

    Have a 16x16 boolean array.Heres the code for the method thats throwing the out of bounds exception
    public int checkSurroundingCells(boolean [][] boardArray , int row , int col ){
    		int count = 0;
    		
    		 if(row - 1 < rowMin){
    			 //do nothing
    		 }else{
    			 if(boardArray[row - 1][col]== true){
    				 count++;
    			 }
    		 }
    		 
    		 if(row + 1 > rowMax){
    			 //do nothing
    		 }else{
    			 if(boardArray[row+1][col]==true){
    				 count++;
    			 }
    		 }
    		 
    		 if(col-1<colMin){
    			 //do nothing
    		 }else{
    			 if(boardArray[row][col-1]== true){
    				 count++;
    			 }
    		 }
    		 
    		 if(col+1>colMax){
    			 //do nothing
    		 }else{
    			 if(boardArray[row][col+1]==true){
    				 count++;
    			 }
    		 }
    		
    		
    		return count;
    	}//end checkSuuroundingCells
    


Comments

  • Closed Accounts Posts: 8,016 ✭✭✭CreepingDeath


    If the board is a 16x16 array, then I would expect rowMax and colMax to the board size - 1, ie. both set to 15.

    Then change your if statements which test whether you've hit the bounds of the board with
    if (row + 1 >= rowMax)
    
    
    if (col + 1 >= colMax )
    


  • Closed Accounts Posts: 768 ✭✭✭SpaceSasqwatch


    If the board is a 16x16 array, then I would expect rowMax and colMax to the board size - 1, ie. both set to 15.

    Then change your if statements which test whether you've hit the bounds of the board with
    if (row + 1 >= rowMax)
    
    
    if (col + 1 >= colMax )
    

    Thanks CreepingDeath..had the max values set to 16.Changed as you suggested and it works fine.The cells are dying and giving birth as they should.

    Am I right in saying that when i had the mx values set to 16 the method was seeing the size of the array as 17x17?


  • Closed Accounts Posts: 8,016 ✭✭✭CreepingDeath


    Am I right in saying that when i had the mx values set to 16 the method was seeing the size of the array as 17x17?

    Sort of...

    A common mistake is to forget that arrays are zero based.
    So a 16x16 array is 0-15, not 0-16.

    On top of that, if you're checking the surrounding cells, then the cells at the edge, ie. row or column 15 do not have cells to their right/bottom.
    So you have to take that into account too, and avoid checking them.


Advertisement