Problem with java and conditional (game of life)
- by Muad'Dib
Hello everybody,
I'm trying to implement The Game of Life in java, as an exercise to learn this language.
Unfortunately I have a problem, as I don't seem able to make this program run correctly.
I implemented a torodial sum (the plane is a donut) with no problem:
int SumNeighbours (int i, int j) {
int value = 0;
value = world[( i - 1 + row ) % row][( j - 1 + column ) % column]+world[( i - 1 + row ) % row][j]+world[( i - 1 + row ) % row][( j + 1 ) % column];
value = value + world[i][( j - 1 + column ) % column] + world[i][( j + 1 ) % column];
value = value + world[( i + 1 ) % row][( j - 1 + column ) % column] + world[( i + 1 ) % row][j]+world[ ( i+1 ) % row ][( j + 1 ) % column];
return value;
}
And it sums correctly when I test it:
void NextWorldTest () {
int count;
int [][] nextWorld = new int[row][row];
nextWorld = world;
for (int i=0; i<row; i++) {
for (int j=0; j<column; j++) {
count = SumNeighbours(i,j);
System.out.print(" " + count + " ");
}
System.out.println();
}
world=nextWorld;
}
Unfortunately when I add the conditions of game of life (born/death) the program stop working correctly, as it seems not able anymore to count correctly the alive cells in the neighborhood. It counts where there are none, and it doesn't count when there are some. E.g.: it doesn't count the one below some living cells.
It's a very odd behaviour, and it's been giving me a headache for 3 days now... maybe I'm missing something basic about variables?
Here you can find the class.
void NextWorld () {
int count;
int [][] nextWorld = new int[row][column];
nextWorld = world;
for (int i=0; i<row; i++) {
for (int j=0; j<column; j++) {
count = SumNeighbours(i,j);
System.out.print(" " + count + " ");
if ( ( world[i][j] == 0) && ( count == 3 ) ) {
nextWorld[i][j] = 1;
} else if ( ( world[i][j] == 1 ) && ( (count == 3) || (count == 2) )) {
nextWorld[i][j] = 1;
} else {
nextWorld[i][j]=0;
}
}
System.out.println();
}
world=nextWorld;
}
}
Am I doing something wrong?
Below you can find the full package.
package com.GaOL;
public class GameWorld {
int [][] world;
int row;
int column;
public int GetRow() {
return row;
}
public int GetColumn() {
return column;
}
public int GetWorld (int i, int j) {
return world[i][j];
}
void RandomGen (int size, double p1) {
double randomCell;
row = size;
column = size;
world = new int[row][column];
for (int i = 0; i<row; i++ ) {
for (int j = 0; j<column; j++ ) {
randomCell=Math.random();
if (randomCell < 1-p1) {
world[i][j] = 0;
} else {
world[i][j] = 1;
}
}
}
}
void printToConsole() {
double test = 0;
for (int i=0; i<row; i++) {
for (int j=0; j<column; j++) {
if ( world[i][j] == 0 ) {
System.out.print(" ");
} else {
System.out.print(" * ");
test++;
}
}
System.out.println("");
}
System.out.println("ratio is " + test/(row*column));
}
int SumNeighbours (int i, int j) {
int value = 0;
value = world[( i - 1 + row ) % row][( j - 1 + column ) % column]+world[( i - 1 + row ) % row][j]+world[( i - 1 + row ) % row][( j + 1 ) % column];
value = value + world[i][( j - 1 + column ) % column] + world[i][( j + 1 ) % column];
value = value + world[( i + 1 ) % row][( j - 1 + column ) % column] + world[( i + 1 ) % row][j]+world[ ( i+1 ) % row ][( j + 1 ) % column];
return value;
}
void NextWorldTest () {
int count;
int [][] nextWorld = new int[row][row];
nextWorld = world;
for (int i=0; i<row; i++) {
for (int j=0; j<column; j++) {
count = SumNeighbours(i,j);
System.out.print(" " + count + " ");
}
System.out.println();
}
world=nextWorld;
}
void NextWorld () {
int count;
int [][] nextWorld = new int[row][column];
nextWorld = world;
for (int i=0; i<row; i++) {
for (int j=0; j<column; j++) {
count = SumNeighbours(i,j);
System.out.print(" " + count + " ");
if ( ( world[i][j] == 0) && ( count == 3 ) ) {
nextWorld[i][j] = 1;
} else if ( ( world[i][j] == 1 ) && ( (count == 3) || (count == 2) )) {
nextWorld[i][j] = 1;
} else {
nextWorld[i][j]=0;
}
}
System.out.println();
}
world=nextWorld;
}
}
and here the test class:
package com.GaOL;
public class GameTestClass {
public static void main(String[] args) {
GameWorld prova = new GameWorld();
prova.RandomGen(10, 0.02);
for (int i=0; i<3; i++) {
prova.printToConsole();
prova.NextWorld();
}
}
}