|
-
December 10th, 2009, 03:03 AM
#16
Re: help help, ... :) "game of life",
You don't need the comparison to 3. It is enough to use:
Code:
if checkCell(cells,i,j) && (numberOfNeighbors(cells,i,j) == 2)
Because the next if statement will compare to 3.
-
December 10th, 2009, 03:19 AM
#17
Re: help help, ... :) "game of life",
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;
}
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
but when i switch it back to my neighbornumber function.. i get two trues.
wierd.
Last edited by sanfor; December 10th, 2009 at 03:21 AM.
-
December 10th, 2009, 03:24 AM
#18
Re: help help, ... :) "game of life",
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;
}
Code:
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
which means the problem is in
Code:
if ((checkCell(old_cell,i,j)) && (numberOfNeighbors(old_cell,i,j) == 2))
{
new_cell[i][j] = true;
}
-
December 10th, 2009, 03:37 AM
#19
Re: help help, ... :) "game of life",
Put some printouts.
Inside the loop of nextGeneration put something like:
Code:
System.out.println("Number of Neighbours in row " + i + " column " + j + " is: " + numberOfNeighbours(old_cell, i, j)");
This will help you debug.
Post the results here, and it will be easier to find the problem.
-
December 10th, 2009, 03:47 AM
#20
Re: help help, ... :) "game of life",
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
-
December 10th, 2009, 03:49 AM
#21
Re: help help, ... :) "game of life",
Code:
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
first one
in the bottom left corner
[0][0] has two live neighbors
which are [1][0] and [1][1]
-
December 10th, 2009, 03:53 AM
#22
Re: help help, ... :) "game of life",
i see it's
when the CELL is LIVE it count the correct neighbors
but when it's DEAD it counts -1
-
December 10th, 2009, 03:59 AM
#23
Re: help help, ... :) "game of life",
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--;}
-
December 10th, 2009, 04:05 AM
#24
Re: help help, ... :) "game of life",
i did it ;P
Code:
int numberOfNeighbors;
if (checkCell(cells,x,y))
numberOfNeighbors=-1;
else
numberOfNeighbors=0;
before:
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
after:
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
-
December 10th, 2009, 04:07 AM
#25
Re: help help, ... :) "game of life",
DONE 
thanks mate.
i appreciate it alot
-
December 10th, 2009, 04:52 AM
#26
Re: help help, ... :) "game of life",
Great!
Have fun with next assignments....
-
December 10th, 2009, 09:04 AM
#27
Re: help help, ... :) "game of life",
haha, you might see alot of me soon ;P
-
December 21st, 2009, 10:47 AM
#28
Re: help help, ... :) "game of life",
i am having difficults with another assigment
-
December 21st, 2009, 01:32 PM
#29
Re: help help, ... :) "game of life",
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.
-
March 5th, 2010, 08:23 AM
#30
Re: help help, ... :) "game of life",
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|