dorbird

Game Of Life

Dec 13th, 2013
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.68 KB | None | 0 0
  1. class GameOfLife
  2. {
  3.     public int feld [][];
  4.     public int tempFeld[][];
  5.     public int tempAnzahl;
  6.     public int feldHoehe=20;
  7.     public int feldBreite=20;
  8.     public char choise='j';
  9.    
  10.     void ersteGenerationSetzen(){
  11.         feld[1][2]=1;
  12.         feld[2][3]=1;
  13.         feld[3][1]=1;
  14.         feld[3][2]=1;
  15.         feld[3][3]=1;
  16.        
  17.     }
  18.     void pruefe(){
  19.         for(int i=1;i<=feldHoehe;i++){
  20.             for (int j=1;j<=feldBreite;j++){
  21.                 tempAnzahl=feld[i-1][j-1]+feld[i][j-1]+feld[i+1][j-1]+feld[i-1][j]+feld[i+1][j]+feld[i-1][j+1]+feld[i][j+1]+feld[i+1][j+1];
  22.                 //IO.print("s"+tempAnzahl);
  23.                 if(feld[i][j]==1&&(tempAnzahl==2||tempAnzahl==3)){
  24.                     tempFeld[i][j]=1;
  25.                 }
  26.                 if(tempAnzahl!=2&&tempAnzahl!=3&&feld[i][j]==1){
  27.                     tempFeld[i][j]=0;
  28.                 }
  29.                 if(tempAnzahl==3&&feld[i][j]==0){
  30.                     tempFeld[i][j]=1;
  31.                 }
  32.                
  33.             }
  34.         }
  35.         for(int i=1;i<=feldHoehe;i++){
  36.             for (int j=1;j<=feldBreite;j++){
  37.                 feld[i][j]=tempFeld[i][j];
  38.             }
  39.         }
  40.         IO.println("");
  41.     }
  42.     void zeichne()
  43.     {
  44.         for(int i=1;i<=feldHoehe;i++){
  45.             for (int j=1;j<=feldBreite;j++){
  46.                 IO.print(""+feld[i][j]);
  47.             }
  48.             IO.println("");
  49.         }
  50.         IO.println("");
  51.     }
  52.     public static void main (String[] args)
  53.     {
  54.         GameOfLife GoL=new GameOfLife();
  55.         IO.println("Geben Sie die hoehe des Feldes ein: ");
  56.         GoL.feldHoehe=IO.readInt();
  57.         IO.println("Geben Sie die breite des Feldes ein: ");
  58.         GoL.feldBreite=IO.readInt();
  59.         GoL.feld=new int [GoL.feldHoehe+2][GoL.feldBreite+2];
  60.         GoL.tempFeld=new int [GoL.feldHoehe+2][GoL.feldBreite+2];
  61.         GoL.ersteGenerationSetzen();
  62.         GoL.zeichne();
  63.         while(GoL.choise=='j'){
  64.             GoL.pruefe();
  65.             GoL.zeichne();
  66.             IO.println("Wollen Sie noch ein Zyklus durchlaufen? (j/n): ");
  67.             GoL.choise=IO.readChar();
  68.         }
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment