Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: help help, ... :) "game of life"

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default help help, ... :) "game of life"

    i had a home work about whats called "Game of Life"

    i had to write 4 functions as i did below:

    function 1:

    	public static boolean isInside(boolean[][] cells, int x, int y) {
     
    		if ((cells.length < x) && (cells[0].length < y))
    			return false;
     
    		return true;
     
    	}


    function 2:

    	public static boolean checkCell(boolean[][] cells, int x, int y) {
     
    		if (cells[x][y] == true)
    			return true;
     
    		return false;
     
    	}

    function 3:

    	public static int numberOfNeighbors(boolean[][] cells,int x,int y) {
     
    		int count=0,j=0,i=1,marker=0;
     
    		if (isInside(cells,i,j))
    		{
    			while (marker < 3)
    			{
    				if ((checkCell(cells,x+j,y)) && (cells[x+j][y] != cells[x][y]))
    					count++;
    				if (checkCell(cells,x+j,y+i))
    					count++;
    				if (checkCell(cells,x+j,y-i))
    					count++;
     
    				j=1;
     
    				if (marker == 2)
    					j=-1;
     
    				marker++;
    			}
    		}
     
    		return count;
    	}


    and the important function 4:

    	public static boolean[][] nextGeneration(boolean[][] cells) {
     
     
    		for (int i=0;i<cells.length;i++)
    			for (int j=0;j<cells[0].length;j++)
    				if (isInside(cells,i,j))
    				{
    					if (numberOfNeighbors(cells,i,j) == 3)
    					{
    						cells[i][j] = true;
    					}
    					if ((checkCell(cells,i,j)) && ((numberOfNeighbors(cells,i,j) == 3) || (numberOfNeighbors(cells,i,j) == 2)))
    					{
    						cells[i][j] = true;
    					}
    					else
    					{
    						cells[i][j] = false;
    					}
    				}
     
    		return cells;
    	}

    ,
    ,

    i have to save this file and copy the *.class file to a directory with the prepared files:

    GameOfLife.html,CellSpace.class,GameOfLife.class.

    the problem is when i copy the file and i run the GameOfLife.html.

    which opens this window:

    http://www.ariel.ac.il/cs/pf/bmboaz/...ameOfLife.html

    and works just above.

    but when i put on 3 squares and press "start"

    the error i get is:

    Exception in thread "Thread-11" java.lang.ArrayIndexOutOfBoundsException: -1
    	at NextGeneration.checkCell(NextGeneration.java:19)
    	at NextGeneration.numberOfNeighbors(NextGeneration.java:38)
    	at NextGeneration.nextGeneration(NextGeneration.java:60)
    	at CellSpace.next(CellSpace.java:102)
    	at GameOfLife.run(GameOfLife.java:96)
    	at java.lang.Thread.run(Unknown Source)

    line 19 = if (cells[x][y] == true)

    line 38 = if (checkCell(cells,x+j,y-i))

    line 60 = if (numberOfNeighbors(cells,i,j) == 3)


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,104
    Thanks
    169
    Thanked 780 Times in 726 Posts
    Blog Entries
    5

    Default Re: help help, ... :) "game of life"

    You should safeguard against the edges of the grid. For example, if you are at the first cell [0][0]. Then for line 38:
    line 38 = if (checkCell(cells,x+j,y-i))
    you will end up checking cell [0][-1], which is out of bounds.

  3. #3
    Junior Member
    Join Date
    Nov 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help help, ... :) "game of life"

    so you mean i have to check if the x,y already on the board
    everytime i call for this function:

    before:

    	public static boolean checkCell(boolean[][] cells, int x, int y) {
     
    			if (cells[x][y] == true)
    				return true;
     
     
    		return false;
     
    	}


    after:

    	public static boolean checkCell(boolean[][] cells, int x, int y) {
     
     
    		if (isInside(cells,x,y))
    		{
    			if (cells[x][y] == true)
    				return true;
     
    		}
    		return false;
     
    	}

  4. #4
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,104
    Thanks
    169
    Thanked 780 Times in 726 Posts
    Blog Entries
    5

    Default Re: help help, ... :) "game of life"

    Yes, but something even more fundamental: examine the isInside function, what if x = 0 and y = -1? You need to safeguard against values greater than the size of the grid, but also less than the size (eg < 0 ). Also, you should safeguard against just x > cells.length or y > cells[0].length

Similar Threads

  1. Replies: 16
    Last Post: August 27th, 2010, 03:30 PM
  2. Please help! Exception in thread "main" java.lang.NullPointerException
    By Arutha2321 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 18th, 2009, 01:25 AM
  3. Replies: 1
    Last Post: October 25th, 2009, 11:54 AM
  4. Replies: 4
    Last Post: August 13th, 2009, 05:54 AM
  5. [SOLVED] "GridLayout" problem in Java program
    By antitru5t in forum AWT / Java Swing
    Replies: 3
    Last Post: April 16th, 2009, 10:26 AM