Game of life käsurealt

Allikas: Lambda
public class Life {                
           
  public static void main(String[] args) {  
    int steps;
    int[][] cells;
    int[][] newcells;  
    int[][] tmpcells; // temporary array pointer, not allocated
    int count;
      
    cells=new int[20][20];
    newcells=new int[20][20];  
    initCells(cells);
      
    System.out.println("Life starts!");
    for(steps=0; steps<100; steps++) {
      System.out.println("Step nr: "+steps);
      printCells(cells);
      count=countCells(cells);
      if (count==0) {
        System.out.println("Everybody dead at step nr: "+steps);
        break;            
      }   
      compNextGen(cells,newcells);      
      // swap two arrays: cells will become newcells, and vice versa  
      tmpcells=cells;  
      cells=newcells;
      newcells=tmpcells;        
    }
  }
  
  
  // compute next generation of cells:
  // old: cells as they were before, will not be changed
  // next: next generation of cells, written anew
  
  static void compNextGen(int[][] old,int[][] next) {
    int row,col;
    int n;  
    for(row=0;row<old.length;row++) {
      for(col=0;col<old[row].length;col++) {
        // compute nr of neighbours of one cell 
        n=cval(old,row-1,col-1)+cval(old,row-1,col)+
            cval(old,row-1,col+1);
        n=n+cval(old,row,col-1)+cval(old,row,col+1);
        n=n+cval(old,row+1,col-1)+cval(old,row+1,col)+
            cval(old,row+1,col+1);
        // compute next gen cell: dead or alive  
        if (old[row][col]==1 && n<2) next[row][col]=0;
        else if (old[row][col]==1 && n>3) next[row][col]=0;
        else if (old[row][col]==1 && (n==2 || n==3)) next[row][col]=1;          
        else if (old[row][col]==0 && n==3) next[row][col]=1;
        else next[row][col]=0;  
      }          
    }           
  }    
  
  // allows to calc neigbour values for cells on borders
  
  static int cval(int[][] cells,int r, int c) {
    if (r<0 || r>=cells.length) return 0;
    if (c<0 || c>=cells[0].length) return 0;  
    return cells[r][c];                  
  }    
  
  // give initial position
  
  static void initCells(int[][] cells) {
    initCells2(cells);        
  }  
  
  static void initCells1(int[][] cells) {
    cells[1][1]=1;
    cells[3][1]=1;  
    cells[3][2]=1;
    cells[4][1]=1;  
    cells[4][2]=1;  
    cells[5][1]=1;          
  }    
  
  static void initCells2(int[][] cells) {
    cells[1][1]=1;
    cells[1][2]=1;
    cells[1][3]=1;       
  }  
  
  // just print current cells
  
  static void printCells(int[][] cells) {
    int row,col;
    for(row=0;row<cells.length;row++) {
      for(col=0;col<cells[row].length;col++) {
        if (cells[row][col]==1) {
          System.out.print("*");
        } else {
          System.out.print(" ");  
        }             
      }
      System.out.println();      
    }              
  }    
  
  // just count current cells
  
  static int countCells(int[][] cells) {
    int row,col,count=0;
    for(row=0;row<cells.length;row++) {
      for(col=0;col<cells[row].length;col++) {
        count=count+cells[row][col];        
      }
    }  
    return count;    
  }  
}