0

For my first project using classes I decided to write "The game of Life". I made a class called cell and its work is to check the nearby cells whether they are alive(variable state is true) and than decide whether the cell will be alive next frame or not. Here is my code

public cell Cell[][] = new cell[10][10];
public boolean state[][] = new boolean[10][10];
void setup(){
size(200,200);
for(int x = 0;x > 10;x++){
  for(int y = 0; y > 10;y++){
    state[x][y] = false;
  }
}
state[1][1] = true;
state[1][2] = true;
state[2][1] = true;
state[2][2] = true;
for(int x = 0;x > 10;x++){
    for(int y = 0; y > 10;y++){
      Cell[x][y] = new cell(x,y,state[x][y]);
    }
  }
}
void draw(){
for(int x = 0;x > 10;x++){
    for(int y = 0; y > 10;y++){
      Cell[x][y].update();
    }
  }
}
class cell{
  boolean state; int ngbs,posx,posy;
  cell(int gridX,int gridY,boolean State){
    posx = gridX;
    posy = gridY;
    state = State;
  }
  void update(){
    if(Cell[posx-1][posy].state == true){ngbs++;}
    if(Cell[posx+1][posy].state == true){ngbs++;}
    if(Cell[posx][posy-1].state == true){ngbs++;}
    if(Cell[posx][posy+1].state == true){ngbs++;}
    if(Cell[posx+1][posy-1].state == true){ngbs++;}
    if(Cell[posx+1][posy+1].state == true){ngbs++;}
    if(Cell[posx-1][posy+1].state == true){ngbs++;}
    if(Cell[posx-1][posy-1].state == true){ngbs++;}
    if(ngbs == 3){state = true;}
    if((ngbs != 2) && (ngbs != 3)){state = false;fill(0);}
    if(state){fill(255);}else{fill(0);}
    rect(posx*10,posy*10,10,10);
  }
}
user4676310
  • 383
  • 1
  • 3
  • 12

1 Answers1

0

Look at your for loops:

for(int x = 0;x > 10;x++){
    for(int y = 0; y > 10;y++){

Neither of these loops will ever be entered.

As a side note, you really should use proper naming conventions: variables start with lower-case letters, classes start with upper-case letters.

Also, the Game of Life works in "generations". You can't update one cell at a time, otherwise it throws off all of that cell's neighbors.

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
  • About the loops: Oh my god I am so silly! About the generations: so i should use two arrays one for checking the neighbours from the prev. generation and one for setting the states of the next generation? P.S.:I love the irony of your profile pic XD – user4676310 Mar 28 '15 at 07:46
  • @Sipi Yeah, you need two arrays: one for the current generation, and another for the next generation. Try it with one to see what I'm talking about. – Kevin Workman Mar 29 '15 at 20:04