You don't need the comparison to 3. It is enough to use:
Because the next if statement will compare to 3.Code:if checkCell(cells,i,j) && (numberOfNeighbors(cells,i,j) == 2)
Printable View
You don't need the comparison to 3. It is enough to use:
Because the next if statement will compare to 3.Code:if checkCell(cells,i,j) && (numberOfNeighbors(cells,i,j) == 2)
see i change it to your code
(which by theway is much easier and more unserstandble)
but the thing is that the next generation become all "false"
Code:public static int numberOfNeighbors(boolean[][] cells, int x, int y){
int numberOfNeighbors = -1;
//This is because we will count the current cell as a neighbour, so we should start from -1
//looping over all cells arround current
for(int deltaX = -1 ; deltaX <= 1 ; deltaX++){
//We are starting from one row above, moving to current row and then to the row below
for(int deltaY = -1 ; deltaY <= 1 ; deltaY++){
//We are starting from one column to the left, moving to furren and then to the right
if (checkCell(cells, x + deltaX, y + deltaY)) {
numberOfNeighbors++;
//Adding one to the number of neighbours if the neighbour is alive. We WILL check the index [0][0] and obviously get true. This is the reason we started the counting from -1.
}
}
}
return numberOfNeighbors; //returning the result
}
Code:public static boolean[][] nextGeneration(boolean[][] old_cell) {
boolean[][] new_cell = new boolean[old_cell.length][old_cell[0].length];
for (int i=0;i<old_cell.length;i++)
for (int j=0;j<old_cell[0].length;j++)
new_cell[i][j] = false;
for (int i=0;i<old_cell.length;i++)
for (int j=0;j<old_cell[0].length;j++)
{
if ((checkCell(old_cell,i,j)) && (numberOfNeighbors(old_cell,i,j) >= 2))
{
new_cell[i][j] = true;
}
if (numberOfNeighbors(old_cell,i,j) == 3)
{
new_cell[i][j] = true;
}
else
{
new_cell[i][j] = false;
}
}
return new_cell;
}
but when i switch it back to my neighbornumber function.. i get two trues.Code:before:
false false false false false
false false false false false
false false false false false
true true true false false
false false false false false
after:
false false false false false
false false false false false
false false false false false
false false false false false
false false false false false
wierd.
see i just checkd to add [0][0] as an true cell
and i got these "if'z" to be worked.
Code:if (numberOfNeighbors(old_cell,i,j) == 3)
{
new_cell[i][j] = true;
}
else
{
new_cell[i][j] = false;
}
which means the problem is inCode:false false false false false
false false false false false
false false false false false
true true true false false
true false false false false
false false false false false
false false false false false
false false false false false
false true false false false
false true false false false
Code:if ((checkCell(old_cell,i,j)) && (numberOfNeighbors(old_cell,i,j) == 2))
{
new_cell[i][j] = true;
}
Put some printouts.
Inside the loop of nextGeneration put something like:
This will help you debug.Code:System.out.println("Number of Neighbours in row " + i + " column " + j + " is: " + numberOfNeighbours(old_cell, i, j)");
Post the results here, and it will be easier to find the problem.
see on the paper when i start to write it down
the cell [2][1] has to have 3 true neibghbors.
while starting with -1
first numberOfNeighbors++ gives me > 0
then > 1
then > 2
it suppose to be 3
first oneCode:Number of Neighbours in row 0 column 0 is: 1
Number of Neighbours in row 0 column 1 is: 2
Number of Neighbours in row 0 column 2 is: 1
Number of Neighbours in row 0 column 3 is: 0
Number of Neighbours in row 0 column 4 is: 0
Number of Neighbours in row 1 column 0 is: 1
Number of Neighbours in row 1 column 1 is: 2
Number of Neighbours in row 1 column 2 is: 1
Number of Neighbours in row 1 column 3 is: 0
Number of Neighbours in row 1 column 4 is: 0
Number of Neighbours in row 2 column 0 is: 1
Number of Neighbours in row 2 column 1 is: 2
Number of Neighbours in row 2 column 2 is: 1
Number of Neighbours in row 2 column 3 is: 0
Number of Neighbours in row 2 column 4 is: 0
Number of Neighbours in row 3 column 0 is: 0
Number of Neighbours in row 3 column 1 is: 0
Number of Neighbours in row 3 column 2 is: 0
Number of Neighbours in row 3 column 3 is: 0
Number of Neighbours in row 3 column 4 is: 0
Number of Neighbours in row 4 column 0 is: 0
Number of Neighbours in row 4 column 1 is: 0
Number of Neighbours in row 4 column 2 is: 0
Number of Neighbours in row 4 column 3 is: 0
Number of Neighbours in row 4 column 4 is: 0
in the bottom left corner
[0][0] has two live neighbors
which are [1][0] and [1][1]
i see it's
when the CELL is LIVE it count the correct neighbors
but when it's DEAD it counts -1
Yap, my mistake
There is no avoidence of starting the counting from 0, and after looping add the following:
Code:if (cells[x][y]){numberOfNeighbours--;}
i did it ;P
before:Code:int numberOfNeighbors;
if (checkCell(cells,x,y))
numberOfNeighbors=-1;
else
numberOfNeighbors=0;
after:Code:false false false false false
false false false false false
false false false false false
true true true false false
false false false false false
Code:false false false false false
false false false false false
false true false false false
false true false false false
false true false false false
DONE ;)
thanks mate.
i appreciate it alot
Great!
Have fun with next assignments....
haha, you might see alot of me soon ;P
i am having difficults with another assigment
OK, well start a new thread and in it explain exactly what the problem is and show what you have done to try to solve it.Quote:
i am having difficults with another assigment
Hi there,
I need this assignment please because I have to solve it.
But instead of False True I need it 0 1
input
00000
01110
00000
00000
00000
output
00100
00100
00100
00000
00000
exactly same this assignment which disccussed here.
please help cause I have tough courses