2

I am trying to write the code to Conway's game of life but it just keeps dying out. I do not know what i should write so instead of making all the calculations as the code going forward it should just store what the code says and then make the changes. And to clarify i do not want my code to make one decision about one cell and the change it and then move on to the next cell but I want it to make a decision move on to the next cell and in the end of my code i want it to change all the cell att once.

This is the button to create the world:

 private void btnSVActionPerformed(java.awt.event.ActionEvent evt) {                                      
    // TODO add your handling code here:

     for (rad = 0; rad < 15; rad++){
        for (kolumn = 0; kolumn < 15; kolumn++){
            int x = 10 + 20 * kolumn;
            int y = 10 + 20 * rad;
            int diameter = 20;
            int liv = (int) (Math.random()+0.5);
            Cirkel cirkel = new Cirkel(x, y, diameter, liv);
            cirklar.add(cirkel);
        }
    }

    repaint();
}                                     

This is a button to make the next generation:

 private void btnNGActionPerformed(java.awt.event.ActionEvent evt) {                                      
    // TODO add your handling code here:

    for (i=0; i<=225; i++){
 rad=(i/15+1);
 kolumn=(i%15+1);


 if (rad==1 && kolumn==1) {

    int levandeGrannarH1=cirklar.get(i+1).kollaLevande()
                         +cirklar.get(i+15).kollaLevande()
                         +cirklar.get(i+16).kollaLevande();
    if ((levandeGrannarH1!=2 && levandeGrannarH1!=4) && cirklar.get(i).kollaLevande()==1)
        cirklar.get(i).gorDod();
    else if ((levandeGrannarH1==3) && cirklar.get(i).kollaLevande()==0 )
        cirklar.get(i).gorLevande();

 }


 if (rad==1 && kolumn==15 ) {

    int levandeGrannarH2=cirklar.get(i-1).kollaLevande()+cirklar.get(i+14).kollaLevande()+cirklar.get(i+15).kollaLevande();
    if ((levandeGrannarH2!=2 && levandeGrannarH2!=4) && cirklar.get(i).kollaLevande()==1)
        cirklar.get(i).gorDod();
    else if ((levandeGrannarH2==3) && cirklar.get(i).kollaLevande()==0 )
        cirklar.get(i).gorLevande();

 }


 if (rad==15 && kolumn==1 ) {

    int levandeGrannarH3=cirklar.get(i+1).kollaLevande()+cirklar.get(i-14).kollaLevande()+cirklar.get(i-15).kollaLevande();
    if ((levandeGrannarH3!=2 && levandeGrannarH3!=4) && cirklar.get(i).kollaLevande()==1)
        cirklar.get(i).gorDod();
    else if ((levandeGrannarH3==3) && cirklar.get(i).kollaLevande()==0 )
        cirklar.get(i).gorLevande();

 }


 if (rad==15 && kolumn==15 ) {

    int levandeGrannarH4=cirklar.get(i-1).kollaLevande()+cirklar.get(i-15).kollaLevande()+cirklar.get(i-16).kollaLevande();
    if ((levandeGrannarH4!=2 && levandeGrannarH4!=4) && cirklar.get(i).kollaLevande()==1)
        cirklar.get(i).gorDod();
    else if ((levandeGrannarH4==3) && cirklar.get(i).kollaLevande()==0 )
        cirklar.get(i).gorLevande();

 }


 if (rad==1 && kolumn>1  && kolumn<15) {

    int levandeGrannarR1=cirklar.get(i-1).kollaLevande()+cirklar.get(i+1).kollaLevande()+cirklar.get(i+14).kollaLevande()+cirklar.get(i+15).kollaLevande()+cirklar.get(i+16).kollaLevande();
    if ((levandeGrannarR1!=2 && levandeGrannarR1!=4) && cirklar.get(i).kollaLevande()==1)
        cirklar.get(i).gorDod();
    else if ((levandeGrannarR1==3) && cirklar.get(i).kollaLevande()==0 )
        cirklar.get(i).gorLevande();

 }


 if (rad>1 && kolumn==15 && rad<15) {


    int levandeGrannarR2=cirklar.get(i-15).kollaLevande()+cirklar.get(i-16).kollaLevande()+cirklar.get(i-1).kollaLevande()+cirklar.get(i+14).kollaLevande()+cirklar.get(i+15).kollaLevande();
    if ((levandeGrannarR2!=2 && levandeGrannarR2!=4) && cirklar.get(i).kollaLevande()==1)
        cirklar.get(i).gorDod();
    else if ((levandeGrannarR2==3) && cirklar.get(i).kollaLevande()==0 )
        cirklar.get(i).gorLevande();

 }


 if (rad==15 && kolumn>1 && kolumn<15) {

    int levandeGrannarR3=cirklar.get(i-1).kollaLevande()+cirklar.get(i+1).kollaLevande()+cirklar.get(i-14).kollaLevande()+cirklar.get(i-15).kollaLevande()+cirklar.get(i-16).kollaLevande();
    if ((levandeGrannarR3!=2 && levandeGrannarR3!=4) && cirklar.get(i).kollaLevande()==1)
        cirklar.get(i).gorDod();
    else if ((levandeGrannarR3==3) && cirklar.get(i).kollaLevande()==0 )
         cirklar.get(i).gorLevande();

 }


 if (rad>1 && kolumn==1 && rad<15 ) {

    int levandeGrannarR4=cirklar.get(i-15).kollaLevande()+cirklar.get(i-14).kollaLevande()+cirklar.get(i+1).kollaLevande()+cirklar.get(i+15).kollaLevande()+cirklar.get(i+16).kollaLevande();
    if ((levandeGrannarR4!=2 && levandeGrannarR4!=4) && cirklar.get(i).kollaLevande()==1)
        cirklar.get(i).gorDod();
    else if ((levandeGrannarR4==3) && cirklar.get(i).kollaLevande()==0 )
        cirklar.get(i).gorLevande();

 }


 if (rad>1 && kolumn>1 && rad<15 && kolumn<15) {

    int levandeGrannar = cirklar.get(i-16).kollaLevande()+cirklar.get(i-15).kollaLevande()+cirklar.get(i-14).kollaLevande()+cirklar.get(i-1).kollaLevande()+cirklar.get(i+1).kollaLevande()+cirklar.get(i+14).kollaLevande()+cirklar.get(i+15).kollaLevande()+cirklar.get(i+16).kollaLevande();
    if ((levandeGrannar!=2 && levandeGrannar!=4) && cirklar.get(i).kollaLevande()==1)
        cirklar.get(i).gorDod();
    else if ((levandeGrannar==3) && cirklar.get(i).kollaLevande()==0 )
        cirklar.get(i).gorLevande();

 }

      repaint();   

    }

} 

This is the explanation of how the cells should look like and if they are alive or not.

 public class Cirkel {
  private int x = 0;
  private int y = 0;
  private int diameter = 10;
  private int liv = 1;

public Cirkel(int x, int y, int diameter, int liv){
    this.x = x;
    this.y = y;
    this.diameter = diameter;
    this.liv = liv;

}

public void rita(Graphics g){
    if (this.liv==1) 
        g.setColor(Color.green);
    else
        g.setColor(Color.white);

        g.fillOval(this.x, this.y, this.diameter, this.diameter);
    }
public int kollaLevande(){
    return this.liv;
}

public void gorLevande(){
    this.liv=1;
}

public void gorDod(){
    this.liv=0;
}

}

I would be grateful for any kind of help and if there is some problem whit my question please just ask and I will try to explain in an other way.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373

1 Answers1

5

When you calculate your next generation, you appear to be trying to update the state of your current grid cells, in other words, you're setting the state of the cells in your cirklar ArrayList, while you're still using cirklar to calculate the state of the next generation of cells. But doing this before the entire grid has been checked will effect calculations of the grid cell neighbors down-stream. And in fact you already know that this is a problem, since you state:

And to clarify i do not want my code to make one decision about one cell and the change it and then move on to the next cell but I want it to make a decision move on to the next cell and in the end of my code i want it to change all the cell att once.

To fix this, you need to create a new grid, and then when doing your calculations, set the state of the new grid cells as dead or alive. Then when you've completed your iteration through the entire grid, swap the new grid in for the old grid.

Also note that you have a lot of unneeded redundancy in your code, and I think that you're doing this in order to handle the edges of the grid. This can be cleaned up greatly and greatly simplified by thinking through your coding a bit. Do something like:

// pseudo-code:
SomeArray newGrid = new SomeArray....

for (int currentRow = 0; currentRow <= NUMBER_OF_ROWS; currentRow++) {
  for (int currentCol = 0; currentCol <= NUMBER_OF_COLS; currentCol++) {  
    int minumumRow = Math.max(currentRow - 1, 0); // *** check for edge!!!
    int maximumRow = Math.min(currentRow + 1, NUMBER_OF_ROWS); // *** check for edge!!!
    int minumumCol = Math.max(currentCol - 1, 0); // *** check for edge!!!
    int maximumCol = Math.MIN(currentCol + 1, NUMBER_OF_COLS); // *** check for edge!!!

    int neighborCount = 0;
    for (row = minumumRow; row <= maximumRow; row++) {
      for (col = minumumCol; col <= maximumCol; col++) {
        if (row != currentRow || col != currentCol) {
          // check if neighbors alive and increment neighborCount
        }
      }
    }

    // use neighbor count to set the state of the grid cell in a **new** grid
    // silly pseudo-code:
    newGrid.getCell(currentRow, currentCol).setState(....)
  }
}

// now you've checked all the grid and set the new grid states. 
// Time to swap grids
currentGrid = newGrid;
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373