14 Replies - 702 Views - Last Post: 12 October 2012 - 11:45 AM Rate Topic: -----

#1 flashgordon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 12-October 12

Game of Life Error

Posted 12 October 2012 - 06:34 AM

I been getting this error i run my code for game of life.
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at Life.countNeighbors(Life.java:34)
at Life.update(Life.java:77)
at Life.main(Life.java:148)
I know why its giving me the error but i can't for the life of me figure out how to fix it. Any suggestions?
public static int  countNeighbors(boolean[][] board, int i, int j)
        {
        	
        	
        		
	        int count = 0;
	           
	   
	            
	        if (board[i+1][j] == true){
	           count = count + 1;
	        }
	            
	        if (board[i][j+1] == true){
	            	count = count + 1;
	        }
	            
	        if (board[i][j-1] == true){
	            	count = count + 1;
	        }
	            
	        if (board[i-1][j] == true){
	            	count = count + 1;
	        }
	            
	        if (board[i+1][j+1] == true){
	            	count = count + 1;
	        }
	            
	        if (board[i+1][j-1] == true){
	            	count = count + 1;
	        }
	            
	        if (board[i-1][j+1] == true){
	            	count = count + 1;
	        }
	            
	        if (board[i-1][j-1] == true){
	            	count = count + 1;
	        }
	        
	        return count;
	        
	            
        }   
            
     
        	
        	
               
        
        
        
        public static void update(boolean[][] board){
                boolean[][] original = Arrays.copyOf(board,board.length);
                
                for (int i = 0; i < original.length ;  i++){
                    for ( int j = 0; j <= original[0].length ; j++){
                    	
                    	
	                    int neighbor = countNeighbors(board,i,j);
	                    	
	                    	
	                    if (original[i][j] == false && neighbor == 3){
	                             original[i][j] = true;
	                    }
	                            
	                    if (original[i][j] == true && neighbor <= 1){
	                             original[i][j] = false;
	                        }
	                            
	                    if (original[i][j] == true && neighbor > 3){
	                              original[i][j] = false;
	                        }
	                        
	                       
                            
                    	
                    	
                    	
                    	
                    	
                    }
                    	


Is This A Good Question/Topic? 0
  • +

Replies To: Game of Life Error

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Game of Life Error

Posted 12 October 2012 - 06:39 AM

Without seing all your code not easy to match the line number of your stack with your actual code but I'll bet that you are testing

board[i-1][j] or board[i][j-1] with a i or a j == 0

Making a Life game it is a lot easier to make an array of Cell object and have all the neighbors of each of the Cell defined at creation time

or make an horrible test if i == 0 or j == 0 so not check i-1 and j-1
Was This Post Helpful? 1
  • +
  • -

#3 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7746
  • View blog
  • Posts: 13,104
  • Joined: 19-March 11

Re: Game of Life Error

Posted 12 October 2012 - 06:46 AM

So obviously you're checking a cell where i == 0.

One way to fix this would be to guard each of those checks:

  if ((i>0) && (board[i-1][j+1]) == true){


Because of short-circuit evaluation, this will skip the check for [i-1][j+1] if that cell is off the board.

Another way to guard against this would be to leave a border around the play area, whose cells are set to false and are not updated. That is, if your board is an nXn grid, your actual arrays are of length n+2, and you iterate from 1 to n, not zero to n-1. Now i-1, i+1, j-1, and j+1 are all safe operations.

It might be nice to think about how you'd approach this problem in an object-oriented fashion, considering that you're writing this in Java. How would you go about that?
Was This Post Helpful? 1
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Game of Life Error

Posted 12 October 2012 - 07:13 AM

Another solution, if you want to play with a board[N][M] is to make your board[N+2][M+2]
and just play from 1 to N-2 and 1 to M-2
assuming the border will always be filled with 0

000000000000000
0.............0
0.............0
0.............0
000000000000000


play only when the . are

This post has been edited by pbl: 12 October 2012 - 07:14 AM
Reason for edit:: Oups... Jon beat me on that one

Was This Post Helpful? 1
  • +
  • -

#5 flashgordon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 12-October 12

Re: Game of Life Error

Posted 12 October 2012 - 07:24 AM

Here is the complete code

import java.util.Arrays;


public class Life 
{
        /*
        public static boolean[][]life(boolean[][]world)
        {
                int N = world.length;
                boolean [][] a  = new boolean [N][N];
                
                
                return a;
        }
        */
        public static int  countNeighbors(boolean[][] board, int i, int j)
        {
        	
        	
        		
	        int count = 0;
	           
	        
	       

	            
	        if (board[i+1][j] == true){
	           count = count + 1;
	        }
	            
	        if (board[i][j+1] == true){
	            	count = count + 1;
	        }
	            
	        if (board[i][j-1] == true){
	            	count = count + 1;
	        }
	            
	        if (board[i-1][j] == true){
	            	count = count + 1;
	        }
	            
	        if (board[i+1][j+1] == true){
	            	count = count + 1;
	        }
	            
	        if (board[i+1][j-1] == true){
	            	count = count + 1;
	        }
	            
	        if (board[i-1][j+1] == true){
	            	count = count + 1;
	        }
	            
	        if (board[i-1][j-1] == true){
	            	count = count + 1;
	        }
	        
	        return count;
	        
	            
        }   
            
     
        	
        	
               
        
        
        
        public static void update(boolean[][] board){
                boolean[][] original = Arrays.copyOf(board,board.length);
                
                for (int i = 1; i < original.length-2 ;  i++){
                    for ( int j = 1; j < original[0].length-2 ; j++){
                    	
                    	
	                    int neighbor = countNeighbors(board,i,j);
	                    
	                    
	                    	
	                    	
	                   
	                        
	                       
	                        if (original[i][j] == true && neighbor == 2){
	                        original[i][j] = true;
	                        }
	                       
	                        if (original[i][j] == true && neighbor <= 1){
	                        original[i][j] = false;
	                        }
	                       
	                        if (original[i][j] == true && neighbor >= 3){
	                        original[i][j] = false;
	                        }
	                        
	                       
                            
                    	
                    	
                    	
                    	
                    	
                    }
                    	
                    
                    
                }
                
            
            
            
            
                
        }
        
        
        public static void drawBoard(boolean[][] board){
                StdDraw.clear();
                StdDraw.show(0);
                for (int i = 0; i < board.length; i++)
                        for (int j = 0; j < board[0].length; j++)
                        { 
                                if (board[i][j] == true)
                                        StdDraw.filledSquare(i, j, .5);
                                else
                                        StdDraw.square(i,j,.5);
                        }
                StdDraw.show(0);
                
        }
        
        public static  boolean[][] initBoard(int numOfRows, int numOfColumns){
                boolean [][] a = new boolean[numOfRows][numOfColumns];
                
                for (int i = 0; i < a.length+2; i++)
                        for ( int j = 0; j < a[0].length+2; j++)
                                a[i][j] = StdRandom.bernoulli();
                
                return a;
        }
        
        
        public static void main(String[] args)
        {
                StdDraw.setScale(0, 10);
                boolean[][] b = initBoard(10,10);
                
                while(true){
                	 	
                        drawBoard(B)/>;
                        StdDraw.show(100);
                        update(B)/>;
                        
                        
                } 

Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Game of Life Error

Posted 12 October 2012 - 07:34 AM

Did you fix something ?
What is StdDraw ?
Why are your method static ? main() should be the only static method which should create a Life object which has the two arrays in its instance variable
Was This Post Helpful? 0
  • +
  • -

#7 flashgordon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 12-October 12

Re: Game of Life Error

Posted 12 October 2012 - 07:41 AM

StdDraw is what is being used to draw it in Eclipse.
Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7746
  • View blog
  • Posts: 13,104
  • Joined: 19-March 11

Re: Game of Life Error

Posted 12 October 2012 - 07:47 AM

View Postpbl, on 12 October 2012 - 09:13 AM, said:

Oups... Jon beat me on that one


Just want to savor that moment... it happens so rarely. :)
Was This Post Helpful? 0
  • +
  • -

#9 flashgordon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 12-October 12

Re: Game of Life Error

Posted 12 October 2012 - 07:55 AM

Thank you both for the help I realize what i need to do now :)
Was This Post Helpful? 0
  • +
  • -

#10 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5831
  • View blog
  • Posts: 12,683
  • Joined: 16-October 07

Re: Game of Life Error

Posted 12 October 2012 - 09:39 AM

Get rid of all the bloody static. It's an object oriented language. Use objects. And use more methods.

e.g.

public class Life {
	private final boolean[][] board;
	public final int ROWS, COLS;
	
	public Life(int rows, int cols) { /* your code here */ }
	public Life(Life other) { /* your code here */ }
	private boolean randCell() { /* your code here */ }
	public void randomFill() { /* your code here */ }
	private boolean isValidPos(int row, int col) {  /* your code here */ }
	private int isAlive(int row, int col) { /* your code here */ }

	private int countNeighbors(int row, int col) {
		int count = 0;
		count += isAlive(row+1, col+1);
		count += isAlive(row+1, col);
		count += isAlive(row+1, col-1);
		count += isAlive(row, col+1);
		count += isAlive(row, col-1);
		count += isAlive(row-1, col+1);
		count += isAlive(row-1, col);
		count += isAlive(row-1, col-1);
		return count;
	}
	
	private boolean isStillAlive(Life other, int row, int col) {  /* your code here */ }

	public void update() {
		Life original = new Life(this);
		for (int row = 0; row < ROWS; row++) {
			for (int col = 0; col < COLS; col++) {
				this.board[row][col] = isStillAlive(original, row, col);
			}
		}
	}

	public void drawBoard() {  /* your code here */ }

	public static void run() {
		StdDraw.setScale(0, 10);
		Life game = new Life(10, 10);
		while (true) {
			game.drawBoard();
			StdDraw.show(100);
			game.update();
		}
	}
}



Don't use the border of doom model; it's confusing. You're already confused with it. Rather, check for boundaries before the check the life of the cell; if it's out of bounds just return false.

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#11 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7746
  • View blog
  • Posts: 13,104
  • Joined: 19-March 11

Re: Game of Life Error

Posted 12 October 2012 - 09:53 AM

View Postbaavgai, on 12 October 2012 - 11:39 AM, said:

Don't use the border of doom model; it's confusing. You're already confused with it. Rather, check for boundaries before the check the life of the cell; if it's out of bounds just return false.


Oh, don't listen to him, he uses K&R braces... ;)

Seriously, this is a good example of reasonable people apparently disagreeing, but not really. Ultimately, I think we'd all agree that your best bet is to move to an object-oriented solution. That being said, you have to make your own decisions about what you want to do, and what ideas work best for you. Obviously, there's a lot of room for discussion about the details, and a number of ways to get this to work.
Play's to you, I think.
Was This Post Helpful? 0
  • +
  • -

#12 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5831
  • View blog
  • Posts: 12,683
  • Joined: 16-October 07

Re: Game of Life Error

Posted 12 October 2012 - 11:17 AM

Here we call those Java standard braces. :P

I agree, do as you like. However, try for OO and not static. The cool thing about making a class is that you can use any type implementation and only the class knows. Indeed, you can even abstract how you use the class enough than only a few methods know.

While learning, your only static method should be main. It might seem like a waste to create an object for something simple, but you really want to be thinking in that direction.
Was This Post Helpful? 1
  • +
  • -

#13 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7746
  • View blog
  • Posts: 13,104
  • Joined: 19-March 11

Re: Game of Life Error

Posted 12 October 2012 - 11:37 AM

Quote

While learning, your only static method should be main. It might seem like a waste to create an object for something simple, but you really want to be thinking in that direction.


Awfully sensible for a K&R man...


(Me, I've never understood the notion that it's somehow wasteful to create an object. What exactly are we supposed to be preserving here? All of the bits can be recycled, right?)
Was This Post Helpful? 0
  • +
  • -

#14 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5831
  • View blog
  • Posts: 12,683
  • Joined: 16-October 07

Re: Game of Life Error

Posted 12 October 2012 - 11:43 AM

Newbie programmers believe the word "new" gives them cooties, apparently.
Was This Post Helpful? 0
  • +
  • -

#15 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7746
  • View blog
  • Posts: 13,104
  • Joined: 19-March 11

Re: Game of Life Error

Posted 12 October 2012 - 11:45 AM

What? Everyone knows you get cooties from girls.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1