dorbird icon

Game Of Life

dorbird | PRO | 12/13/13 03:21:50 PM UTC | 0 ⭐ | 168 👁️ | Never ⏰ | []
Java |

1.68 KB

|

None

|

0 👍

/

0 👎

class GameOfLife
{
    public int feld [][];
    public int tempFeld[][];
    public int tempAnzahl;
    public int feldHoehe=20;
    public int feldBreite=20;
    public char choise='j';
    
    void ersteGenerationSetzen(){
        feld[1][2]=1;
        feld[2][3]=1;
        feld[3][1]=1;
        feld[3][2]=1;
        feld[3][3]=1;
        
    }
    void pruefe(){
        for(int i=1;i<=feldHoehe;i++){
            for (int j=1;j<=feldBreite;j++){
                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];
                //IO.print("s"+tempAnzahl);
                if(feld[i][j]==1&&(tempAnzahl==2||tempAnzahl==3)){
                    tempFeld[i][j]=1;
                }
                if(tempAnzahl!=2&&tempAnzahl!=3&&feld[i][j]==1){
                    tempFeld[i][j]=0;
                }
                if(tempAnzahl==3&&feld[i][j]==0){
                    tempFeld[i][j]=1;
                }
                
            }
        }
        for(int i=1;i<=feldHoehe;i++){
            for (int j=1;j<=feldBreite;j++){
                feld[i][j]=tempFeld[i][j];
            }
        }
        IO.println("");
    }
    void zeichne()
    {
        for(int i=1;i<=feldHoehe;i++){
            for (int j=1;j<=feldBreite;j++){
                IO.print(""+feld[i][j]);
            }
            IO.println("");
        }
        IO.println("");
    }
    public static void main (String[] args)
    {
        GameOfLife GoL=new GameOfLife();
        IO.println("Geben Sie die hoehe des Feldes ein: ");
        GoL.feldHoehe=IO.readInt();
        IO.println("Geben Sie die breite des Feldes ein: ");
        GoL.feldBreite=IO.readInt();
        GoL.feld=new int [GoL.feldHoehe+2][GoL.feldBreite+2];
        GoL.tempFeld=new int [GoL.feldHoehe+2][GoL.feldBreite+2];
        GoL.ersteGenerationSetzen();
        GoL.zeichne();
        while(GoL.choise=='j'){
            GoL.pruefe();
            GoL.zeichne();
            IO.println("Wollen Sie noch ein Zyklus durchlaufen? (j/n): ");
            GoL.choise=IO.readChar();
        }
    }
}

Comments