I am trying to create a gameoflife in java for a homework.
First of all, i have to create an array from an interface class.
Then, i fill this array with alive cells and empty cells such as :
public World(int n, int p) {
numberOfRow = n;
numberOfCol = p;
myWorld = new Cell[numberOfRow][numberOfCol];
AliveCell cellNew = new AliveCell(1, "X");
DeadCell cellDead = new DeadCell(0,"O");
EmptyCell emptyCell = new EmptyCell("");
int gameTurn = 0;
int countAgeCell = 0;
boolean isOccupied = false;
Random numberRand = new Random();
Random numberXRand = new Random();
Random numberYRand = new Random();
numberRandomCellAlive = numberRand.nextInt((2500 - 1 + 1) +1);
compteurCellAliveGenerated = 0;
for(int i = 0; i < this.myWorld.length; i++) {
for(int j = 0; j < this.myWorld.length; j ++) {
myWorld[i][j] = emptyCell;
}
}
for(int i =0; i < this.myWorld.length -1; i ++) {
for(int j = 0; j < this.myWorld.length -1; j++) {
if(compteurCellAliveGenerated < numberRandomCellAlive) {
int posXRandomCellAlive = numberXRand.nextInt((50 - 1));
int posYRandomCellAlive = numberYRand.nextInt((50 - 1));
myWorld[posXRandomCellAlive][posYRandomCellAlive] = cellNew;
isOccupied = true;
compteurCellAliveGenerated++;
}
Here, i can print my array and it all works. I mean, the array is filled with aliveCells with randomly position of array[i][j].
Then, i have made this function in order to count each alive cell around one cell.
public void afficherUpdate(){
AliveCell cellNew = new AliveCell(1,"X");
AliveCell cellBorn = new AliveCell(1, "B");
EmptyCell emptyCell = new EmptyCell("");
int count = 0;
for(int i = 0; i < this.myWorld.length -1; i++) {
for(int j = 0; j < this.myWorld.length -1; j++) {
if(this.myWorld[i][j] == cellNew && i > 0 && i < 50 && j > 0 && j < 50) {
if(this.myWorld[i][j] == cellNew){
count++;
}
if(this.myWorld[i][j-1] == cellNew) {
count++;
}
if(this.myWorld[i-1][j] == cellNew) {
count++;
}
if(this.myWorld[i-1][j+1] == cellNew) {
count++;
}
if(this.myWorld[i-1][j-1] == cellNew) {
count++;
}
if(this.myWorld[i+1][j+1] == cellNew) {
count++;
}
if(this.myWorld[i+1][j-1] == cellNew) {
count++;
}
if(this.myWorld[i+1][j] == cellNew) {
count++;
}
}
System.out.println(count);
if(this.myWorld[i][j] == emptyCell && i > 0 && i < 50 && j > 0 && j < 50) {
if(this.myWorld[i][j+1] == cellNew){
count++;
}
if(this.myWorld[i][j-1] == cellNew) {
count++;
}
if(this.myWorld[i-1][j] == cellNew) {
count++;
}
if(this.myWorld[i-1][j+1] == cellNew) {
count++;
}
if(this.myWorld[i-1][j-1] == cellNew) {
count++;
}
if(this.myWorld[i+1][j+1] == cellNew) {
count++;
}
if(this.myWorld[i+1][j-1] == cellNew) {
count++;
}
if(this.myWorld[i+1][j] == cellNew) {
count++;
}
System.out.println(count);
}
if(myWorld[i][j] == cellNew && count == 2 || count == 3){
this.myWorld[i][j] = cellBorn;
}
if(myWorld[i][j] == emptyCell && count== 3) {
this.myWorld[i][j] = cellBorn;
}
}
}
However, the count variable returns only 0; It cannot count the alive cells around. I really don't understand why this is not working.
Thank you for your replies.
what you want to check is myWorld[i][j] instanceOf AliveCell
== in java is checking if the objects are same instance not equal