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:
Code :
public static boolean isInside(boolean[][] cells, int x, int y) {
if ((cells.length < x) && (cells[0].length < y))
return false;
return true;
}
function 2:
Code :
public static boolean checkCell(boolean[][] cells, int x, int y) {
if (cells[x][y] == true)
return true;
return false;
}
function 3:
Code :
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:
Code :
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:
Code :
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)
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.
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:
Code :
public static boolean checkCell(boolean[][] cells, int x, int y) {
if (cells[x][y] == true)
return true;
return false;
}
after:
Code :
public static boolean checkCell(boolean[][] cells, int x, int y) {
if (isInside(cells,x,y))
{
if (cells[x][y] == true)
return true;
}
return false;
}
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