Click to See Complete Forum and Search --> : help help, ... :) "game of life",


sanfor
December 9th, 2009, 06:25 AM
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/teaching/I2J/Ex3/GameOfLife.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)

keang
December 9th, 2009, 08:12 AM
Congratulations on writing a well thought out post.

The exception message says the array index is out of bounds and shows it's actual value is -1. Therefore, the value for either x or y passed into the checkCell() method is incorrect. BTW What is the purpose of this method, as the array already contains boolean values the method does nothing useful - you could just as easily replace the call to this method with cells[x][y]

The best approach to solve your problem is to work through your code in the numberOfNeighbours() method using a pen and paper to note down the values of i, j, x & y at each line of code.

I haven't really looked at the code (no time) but the normal problem associated with this sort of search is what happens when the cell you are looking at is at the edge of the grid ie it has no neighbours on one side (or on two sides if it's in a corner).

ProgramThis
December 9th, 2009, 08:41 AM
i had a home work about whats called "Game of Life"
This wouldn't happen to be for Andre's class would it? ;) This used to be an old favorite assignment he would give every other semester.

public static boolean checkCell(boolean[][] cells, int x, int y) {

if (cells[x][y] == true)
return true;

return false;

}
Just a piece of advice on this function: cells[x][y] IS a boolean value, there is no need to check it against true. The check is a.) redundant and b.) should be (true == cells[x][y]) anyway so that you don't accidentially use a single = and assign it the value instead of checking it.





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++;
}



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;
},

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

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

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

Your problem most likely is happening when you reach the end of the array. What you are doing is calling numberOfNeighbors() when i and j are at the last element of the array. However, if you notice in your while loop you are calling check cell on X+J and just below your IF statements you make J = 1. The next time you call checkCell on X+1 you have now gone past the array length.

guyafe
December 9th, 2009, 08:58 AM
There is a problem with your isInside function.
In order to verify that an index is inside the bounds of an array, it should be both smaller than the length of the array, and larger than 0. Notice that the error was about an index of -1.
In other words - the index should be after the start and before the end of the array.
Also notice that you put wrong conditionals. All conditions should be OR'ed together because it is sufficient that the index should be out of bounds in one direction, and not all of them together, to have the index outside of the array.

Imagine a box that in order to get out of it, you can be above it, or in front of it, but you do not have to be both above it and in front of it.

Your condition should look something like this:

public static boolean isInside(boolean[][] cells, int x, int y) {

if ((cells.length < x) || (x < 0) || (cells[0].length < y) || (y < 0))
return false;

return true;

}


If you want to AND conditions together, you should check if the index is inside all of the boundaries, instead of check if it outside at least one of the boundaries.

This will not solve your problem, because you are still trying to access a place out side of the array, but now this error will be caught in the right place.

Can you post where you tried to access that array?

sanfor
December 9th, 2009, 09:56 AM
................

Your condition should look something like this:

public static boolean isInside(boolean[][] cells, int x, int y) {

if ((cells.length < x) || (x < 0) || (cells[0].length < y) || (y < 0))
return false;

return true;

}


.........
Can you post where you tried to access that array?

i got your point :) 1 outbound enough to get error.

i corrected it

sanfor
December 9th, 2009, 09:58 AM
There is a problem with your isInside function.
In order to verify that an index is inside the bounds of an array, it should be both smaller than the length of the array, and larger than 0. Notice that the error was about an index of -1.
In other words - the index should be after the start and before the end of the array.
Also notice that you put wrong conditionals. All conditions should be OR'ed together because it is sufficient that the index should be out of bounds in one direction, and not all of them together, to have the index outside of the array.

Imagine a box that in order to get out of it, you can be above it, or in front of it, but you do not have to be both above it and in front of it.

Your condition should look something like this:

public static boolean isInside(boolean[][] cells, int x, int y) {

if ((cells.length < x) || (x < 0) || (cells[0].length < y) || (y < 0))
return false;

return true;

}


If you want to AND conditions together, you should check if the index is inside all of the boundaries, instead of check if it outside at least one of the boundaries.

This will not solve your problem, because you are still trying to access a place out side of the array, but now this error will be caught in the right place.

Can you post where you tried to access that array?

This wouldn't happen to be for Andre's class would it? ;) This used to be an old favorite assignment he would give every other semester.

public static boolean checkCell(boolean[][] cells, int x, int y) {

if (cells[x][y] == true)
return true;

return false;

}
Just a piece of advice on this function: cells[x][y] IS a boolean value, there is no need to check it against true. The check is a.) redundant and b.) should be (true == cells[x][y]) anyway so that you don't accidentially use a single = and assign it the value instead of checking it.



Your problem most likely is happening when you reach the end of the array. What you are doing is calling numberOfNeighbors() when i and j are at the last element of the array. However, if you notice in your while loop you are calling check cell on X+J and just below your IF statements you make J = 1. The next time you call checkCell on X+1 you have now gone past the array length.

Congratulations on writing a well thought out post.

The exception message says the array index is out of bounds and shows it's actual value is -1. Therefore, the value for either x or y passed into the checkCell() method is incorrect. BTW What is the purpose of this method, as the array already contains boolean values the method does nothing useful - you could just as easily replace the call to this method with cells[x][y]

The best approach to solve your problem is to work through your code in the numberOfNeighbours() method using a pen and paper to note down the values of i, j, x & y at each line of code.

I haven't really looked at the code (no time) but the normal problem associated with this sort of search is what happens when the cell you are looking at is at the edge of the grid ie it has no neighbours on one side (or on two sides if it's in a corner).


,
,
,
,

thanks guys

i see all of you say that i have the same problem with .. the -1

someone told me to do this:


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;

}

guyafe
December 9th, 2009, 10:13 AM
Yap, but you can also narrow it to the following:

public static boolean checkCell(boolean[][] cells, int x, int y) {

return isInside(cells, x, y) && cells[x][y];

}


If isInside will return false, Java wouldn't check the following conditions, so you wouldn't get any exception. Next cells[x][y] is a boolean variable, and the result of the entire statement is boolean, so you can just return it.
You don't have to check if something is true and then return true. You can just return that something.

sanfor
December 9th, 2009, 10:35 AM
This wouldn't happen to be for Andre's class would it? ;) This used to be an old favorite assignment he would give every other semester.

public static boolean checkCell(boolean[][] cells, int x, int y) {

if (cells[x][y] == true)
return true;

return false;

}
Just a piece of advice on this function: cells[x][y] IS a boolean value, there is no need to check it against true. The check is a.) redundant and b.) should be (true == cells[x][y]) anyway so that you don't accidentially use a single = and assign it the value instead of checking it.



Your problem most likely is happening when you reach the end of the array. What you are doing is calling numberOfNeighbors() when i and j are at the last element of the array. However, if you notice in your while loop you are calling check cell on X+J and just below your IF statements you make J = 1. The next time you call checkCell on X+1 you have now gone past the array length.

look at this for exampe:

http://i50.tinypic.com/2efkokl.jpg

when i start with

CELL[2][2]

it works

sanfor
December 9th, 2009, 10:39 AM
see guys

the problem keeps coming from the "checkcell" or "the calling for checkcell"

after i changed few things:



public class NextGeneration {

public static void main(String[] args) {

}


public static boolean isInside(boolean[][] cells, int x, int y) {

if ((cells.length < x) || (x < 0) || (cells[0].length < y) || (y < 0))
return false;

return true;

}


public static boolean checkCell(boolean[][] cells, int x, int y) {

return isInside(cells, x, y) && cells[x][y];

}



public static int numberOfNeighbors(boolean[][] cells,int x,int y) {

int count=0,j=0,i=1,marker=0;

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;
}


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;
}

}


now i get:


Exception in thread "Thread-10" java.lang.ArrayIndexOutOfBoundsException: 25
at NextGeneration.checkCell(NextGeneration.java:19)
at NextGeneration.numberOfNeighbors(NextGeneration.java:31)
at NextGeneration.nextGeneration(NextGeneration.java:55)
at CellSpace.next(CellSpace.java:102)
at GameOfLife.run(GameOfLife.java:96)
at java.lang.Thread.run(Unknown Source)


i start to get frustrated

ProgramThis
December 9th, 2009, 11:59 AM
see guys

the problem keeps coming from the "checkcell" or "the calling for checkcell"



Exception in thread "Thread-10" java.lang.ArrayIndexOutOfBoundsException: 25
at NextGeneration.checkCell(NextGeneration.java:19)
at NextGeneration.numberOfNeighbors(NextGeneration.java:31)
at NextGeneration.nextGeneration(NextGeneration.java:55)
at CellSpace.next(CellSpace.java:102)
at GameOfLife.run(GameOfLife.java:96)
at java.lang.Thread.run(Unknown Source)
i start to get frustrated

What is the length of your array? Is it 24 by chance? Like I said, you are doing a j = 1 in numberOfNeighbors, and then calling checkCell on (x+j,y). When you do this and you are checking the last row of the array you are going to go out of bounds.

sanfor
December 9th, 2009, 12:56 PM
i finally got it
no more eerrors with the function i did

i had to change:

if ((cells.length < x) || (x < 0) || (cells[0].length < y) || (y < 0))

to

if ((cells.length <= x) || (x < 0) || (cells[0].length <= y) || (y < 0))

guyafe
December 9th, 2009, 02:58 PM
Of course!
I embarrassed that I missed it :)
In Java (And in any other discent programming language) arrays are indexed from 0 to (length - 1), So array[length of array] is the first reference that is out of bounds.

sanfor
December 9th, 2009, 04:46 PM
so far i am doing just great..
i don't have errors.. scripts works fine

but i have a small problem

look..


instead of running in with java applet and stuff
i created array from the main function.. to test it first in "eclipse" .. much easier
and to let you understand itmore :)

the code works alright

but when i enter:

cell[1][2] = true;
cell[1][1] = true;
cell[1][0] = true;


and print it through the main function:


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


and when i run the

cell = nextGeneration(cell);

i get this:


false false false false false
false false false false false
false (true) false false false
false false false false false
false (true) false false false


which is wrong, why?
because i have to get it like this:


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




public class NextGeneration {

public static void main(String[] args) {

boolean[][] cell = new boolean[5][5];

for (int i=0;i<cell.length;i++)
for (int j=0;j<cell[0].length;j++)
cell[i][j] = false;

cell[1][2] = true;
cell[1][1] = true;
cell[1][0] = true;

for (int i=cell.length-1;i>=0;i--)
{
for (int j=0;j<cell[0].length;j++)
{
System.out.print(cell[i][j]+" ");
}
System.out.println();
}

System.out.println(numberOfNeighbors(cell,1,1)+" "+ checkCell(cell,1,1));

cell = nextGeneration(cell);

for (int i=cell.length-1;i>=0;i--)
{
for (int j=0;j<cell[0].length;j++)
{
System.out.print(cell[i][j]+" ");
}
System.out.println();
}

}


public static boolean isInside(boolean[][] cells, int x, int y) {

if ((cells.length <= x) || (x < 0) || (cells[0].length <= y) || (y < 0))
return false;

return true;

}



public static boolean checkCell(boolean[][] cells, int x, int y) {

return isInside(cells, x, y) && cells[x][y];

}

public static int numberOfNeighbors(boolean[][] cells,int x,int y) {

int count=0,j=0,i=1,marker=0;

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;

marker++;

if (marker == 2)
j=-1;

}

return count;
}


public static boolean[][] nextGeneration(boolean[][] cells) {


boolean[][] cell = new boolean[cells.length][cells[0].length];

for (int i=0;i<cells.length;i++)
for (int j=0;j<cells[0].length;j++)
cell[i][j] = false;

for (int i=0;i<cells.length;i++)
for (int j=0;j<cells[0].length;j++)
{
if ((checkCell(cells,i,j)) && (numberOfNeighbors(cells,i,j) > 2))
{
cell[i][j] = true;
}

if (numberOfNeighbors(cells,i,j) == 3)
{
cell[i][j] = true;
}

else
{
cell[i][j] = false;
}
}


return cell;
}

}

guyafe
December 10th, 2009, 12:35 AM
Why not? It works well.
In order for a cell to survive, it must have more than 2 neighbours.
Cell [1][1] has only 2: One at the right and one at the left, so it dies, according to your rules.
As I recall, in the game of life, 2 neighbours are enough for a cell to survive, so you should cahnge your >2 to >=2.

Few more things:
Use documentation. I usually write a remark for each line. You can't imagine how helpful this is.
Keep consistent with your variables: In main function i is used for rows and j is used for columns, and in numberOfNeighbours it is the other way around. It makes your code confusing.
Using redable naming is much better. Instead of i and j use row and column. Variables' names should be long and understandable.
Inside nextGeneration there is an array named cells and an array named cell. This is very confusing. Use names such as oldCells and newCells.
Your numberOfNeighbours function is a bit cumbersome. Try something like this:

public static boolean[][] numberOfNeighbours(boolean[][] cells, int x, int y){
int numberOfNeighbours = -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 isInside(cells, x + deltaX, y + deltaY) && cells[x + deltaX][y + deltaY]{
numberOfNeighbours++; //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 numberOfNeighbours; //returning the result
}


Your code should be understandable and readable. The key word here is KISS (Keep It Simple Stupid).
Always try to program the same way you think. Your mind is much clearer than any programming language, so always first think "Hmmm, how would I count the neighbours?" You will get to the conclusion that it is the simplest to just move around the cell. Now it is easy to program such loop.

sanfor
December 10th, 2009, 01:54 AM
no man
the rules are:

1. live cell - stays alive if it has 2 or 3 LIVE neighbors
2. dead cell - becomes alive if it has "exactly" 3 live neighbors.
3. else it stays "dead" or becomes "dead"



,

and i have to change

if ((checkCell(cells,i,j)) && (numberOfNeighbors(cells,i,j) >= 2))

to


if ((checkCell(cells,i,j)) && ((numberOfNeighbors(cells,i,j) == 2) || (numberOfNeighbors(cells,i,j) == 3)))



,
,

and about the "i and j" in the main function and in "the nextgeneration" function..

i t's the oppositte because in the function it has to start [0][0] from the bottom left
but i changed it inthe main so it can be printed as the "board in the html accept"

(just to see it before uploading the file to the folder of the .html file)
(i hope you got me)

,
,

rows and columns yeah.. ihave to pick easier names for the variables.

,

i am gonna gfive it a try
then i will check your numberOfNeighbors... function

guyafe
December 10th, 2009, 02:03 AM
You don't need the comparison to 3. It is enough to use:

if checkCell(cells,i,j) && (numberOfNeighbors(cells,i,j) == 2)

Because the next if statement will compare to 3.

sanfor
December 10th, 2009, 02:19 AM
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"


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
}


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;
}


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.

sanfor
December 10th, 2009, 02:24 AM
see i just checkd to add [0][0] as an true cell

and i got these "if'z" to be worked.


if (numberOfNeighbors(old_cell,i,j) == 3)
{
new_cell[i][j] = true;
}

else
{
new_cell[i][j] = false;
}

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


if ((checkCell(old_cell,i,j)) && (numberOfNeighbors(old_cell,i,j) == 2))
{
new_cell[i][j] = true;
}

guyafe
December 10th, 2009, 02:37 AM
Put some printouts.
Inside the loop of nextGeneration put something like:
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.

sanfor
December 10th, 2009, 02:47 AM
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

sanfor
December 10th, 2009, 02:49 AM
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]

sanfor
December 10th, 2009, 02:53 AM
i see it's
when the CELL is LIVE it count the correct neighbors
but when it's DEAD it counts -1

guyafe
December 10th, 2009, 02:59 AM
Yap, my mistake
There is no avoidence of starting the counting from 0, and after looping add the following:
if (cells[x][y]){numberOfNeighbours--;}

sanfor
December 10th, 2009, 03:05 AM
i did it ;P


int numberOfNeighbors;

if (checkCell(cells,x,y))
numberOfNeighbors=-1;
else
numberOfNeighbors=0;


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 true false false false
false true false false false
false true false false false

sanfor
December 10th, 2009, 03:07 AM
DONE ;)

thanks mate.
i appreciate it alot

guyafe
December 10th, 2009, 03:52 AM
Great!
Have fun with next assignments....

sanfor
December 10th, 2009, 08:04 AM
haha, you might see alot of me soon ;P

sanfor
December 21st, 2009, 09:47 AM
i am having difficults with another assigment

keang
December 21st, 2009, 12:32 PM
i am having difficults with another assigmentOK, 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.

NOT-BAD
March 5th, 2010, 07:23 AM
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

keang
March 5th, 2010, 08:12 AM
I need this assignment please because I have to solve it.Yes you're quite right you have to solve it.

But instead of False True I need it 0 1Ok but that's easy for you to change.

exactly same this assignment which disccussed here.Lucky you, that'll make things a lot easier for you.

please help cause I have tough coursesOh poor you :rolleyes:

NOT-BAD
March 6th, 2010, 02:25 AM
oh please I wanna help

because really I don't understand JAVA

and there are many seperated codes here I don't know what I have to pick

please help :(

don't worry about zeros one .

just make it done for me.

NOT-BAD
March 6th, 2010, 03:41 AM
I put this cod:


public class Life {
public static void main(String[] args) {

boolean[][] cell = new boolean[5][5];

for (int i=0;i<cell.length;i++)
for (int j=0;j<cell[0].length;j++)
cell[i][j] = false;

cell[1][2] = true;
cell[1][1] = true;
cell[1][0] = true;

for (int i=cell.length-1;i>=0;i--)
{
for (int j=0;j<cell[0].length;j++)
{
System.out.print(cell[i][j]+" ");
}
System.out.println();
}

System.out.println(numberOfNeighbors(cell,1,1)+" "+ checkCell(cell,1,1));

cell = nextGeneration(cell);

for (int i=cell.length-1;i>=0;i--)
{
for (int j=0;j<cell[0].length;j++)
{
System.out.print(cell[i][j]+" ");
}
System.out.println();
}

}
public static boolean isInside(boolean[][] cells, int x, int y) {

if ((cells.length <= x) || (x < 0) || (cells[0].length <= y) || (y < 0))
return false;

return true;

}
public static boolean checkCell(boolean[][] cells, int x, int y) {

return isInside(cells, x, y) && cells[x][y];

}
public static int numberOfNeighbors(boolean[][] cells,int x,int y) {

int count=0,j=0,i=1,marker=0;

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;

marker++;

if (marker == 2)
j=-1;

}

return count;
}
public static boolean[][] nextGeneration(boolean[][] cells) {


boolean[][] cell = new boolean[cells.length][cells[0].length];

for (int i=0;i<cells.length;i++)
for (int j=0;j<cells[0].length;j++)
cell[i][j] = false;

for (int i=0;i<cells.length;i++)
for (int j=0;j<cells[0].length;j++)
{
if ((checkCell(cells,i,j)) && (numberOfNeighbors(cells,i,j) > 2))
{
cell[i][j] = true;
}

if (numberOfNeighbors(cells,i,j) == 3)
{
cell[i][j] = true;
}

else
{
cell[i][j] = false;
}
}


return cell;
}
}

It gives me :

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
2 true
false false false false false
false false false false false
false true false false false
false false false false false
false true false false false

<<< but the correct way is
input:
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

output:

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

keang
March 6th, 2010, 07:51 AM
There's only one cell that is wrong and that is the case where a cell stays alive if it has 2 or 3 neighbours. So you need to look at the code that handles this case - of course it would be eaiser for you to do this if you had documented which bit of code handled which case.

NOT-BAD
March 6th, 2010, 01:07 PM
i did it ;P


int numberOfNeighbors;

if (checkCell(cells,x,y))
numberOfNeighbors=-1;
else
numberOfNeighbors=0;


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 true false false false
false true false false false
false true false false false


I don't know where I have to add this code to fix the problem.

again, please I need help.

dlorde
March 7th, 2010, 03:45 AM
I don't know where I have to add this code to fix the problem.

You claim you already wrote the code that does other checks of the neighbours, so you ought to know where it goes. If not, find code that looks similar, and insert it there (hint: the nextGeneration(..) method). Some things you need to figure out for yourself.

Why do we never have time to do it right, but always have time to do it over?
Anon.

ajhampson
March 8th, 2010, 11:33 AM
oh please I wanna help

because really I don't understand JAVA

and there are many seperated codes here I don't know what I have to pick

please help :(



In case you're wondering why you're getting the responses you see here, this is an assistance forum, not a homework cheating forum.

You say you don't really understand Java, but isn't that the point of the class you are taking? If you don't understand, how is having someone else do your work going to help you understand better? I suggest you do some more reading and/or work with your instructor, because you do need more help if this is the case.

don't worry about zeros one .

just make it done for me. :eek:

Not going to happen. Most of us here are helping out in our spare time in addition to what ever full time jobs we have. We don't have the time or inclination to write your homework for you. We've already put in the time to learn and know the subject. This is where you do the same. If you read this entire thread, you'll see the difference between the amount of effort Sanfor put in and what you're giving. That level of effort is directly reflected in the answers you're getting.