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)