Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Empfohlene Antworten

Veröffentlicht

Hallo ich versuche gerade das spiel des lebens mit bluej zu programmieren ,aber es funktioniert net

/**

* Trägt attribute Tod oder lebendig

*

* @Jens Heinrich

* @0.1

* */

public class Zelle

{

private int LiveOrDeath;

/**

* Gibt an ob sie Tod ist oder lebt

*/

public Zelle()

{

LiveOrDeath = 0;

}

public void kill()

{

LiveOrDeath=0;

}

public void revive()

{

LiveOrDeath=1;

}

public int AreUAlive()

{

return LiveOrDeath;

}

}

/**

* The real Game

*

* @Jens Heinrich

* @0.1

*/

public class Game

{

private Zelle[][] playing;

private Zelle[][] working;

public int höhe;

public int breite;

public Game(int Höhe,int Breite)

/*min grösse 3mal 3

*/

{

höhe = Höhe;

breite = Breite;

playing = new Zelle[höhe][breite];

working = new Zelle[höhe][breite];

FillWithDead();

}

public void FillWithDead()

{

for(int i=0;i<höhe;i++)

{

for(int j=0;j<breite;j++)

{

playing[j]=new Zelle();

working[j]=new Zelle();

}

};

}

public int CountLivingAround(int x,int y)

{

int z;

if((x==0)&&(y==0))

{

z=playing[x][y+1].AreUAlive();

z=z+playing[x+1][y].AreUAlive();

z=z+playing[x+1][y+1].AreUAlive();

z=z+playing[x][breite-1].AreUAlive();

z=z+playing[höhe-1][y].AreUAlive();

z=z+playing[höhe-1][breite-1].AreUAlive();

z=z+playing[höhe-1][y+1].AreUAlive();

z=z+playing[x+1][breite-1].AreUAlive();

}

/*

* links oben

*/

else if((x==0)&&(y==breite))

{

z=playing[x][y-1].AreUAlive();

z=z+playing[x+1][y].AreUAlive();

z=z+playing[x+1][y-1].AreUAlive();

z=z+playing[0][0].AreUAlive();

z=z+playing[1][0].AreUAlive();

z=z+playing[höhe-1][breite-1].AreUAlive();

z=z+playing[höhe-1][breite-2].AreUAlive();

z=z+playing[höhe-1][0].AreUAlive();

}

/*

* rechts oben

*/

else if(x==0)

{

z=playing[x][y+1].AreUAlive();

z=z+playing[x][y-1].AreUAlive();

z=z+playing[x+1][y+1].AreUAlive();

z=z+playing[x+1][y-1].AreUAlive();

z=z+playing[x+1][y].AreUAlive();

z=z+playing[höhe-1][y].AreUAlive();

z=z+playing[höhe-1][y+1].AreUAlive();

z=z+playing[höhe-1][y-1].AreUAlive();

}

/*

* mitte oben

*/

else if((x==höhe)&&(y==0))

{

z=playing[x][y+1].AreUAlive();

z=z+playing[x-1][y].AreUAlive();

z=z+playing[x-1][y+1].AreUAlive();

z=z+playing[x][breite-1].AreUAlive();

z=z+playing[0][y].AreUAlive();

z=z+playing[0][breite-1].AreUAlive();

z=z+playing[0][y+1].AreUAlive();

z=z+playing[x-1][breite-1].AreUAlive();

}

/*

* links unten

*/

else if((x==höhe)&&(y==breite))

{

z=playing[x][y-1].AreUAlive();

z=z+playing[x-1][y].AreUAlive();

z=z+playing[x-1][y-1].AreUAlive();

z=z+playing[0][0].AreUAlive();

z=z+playing[0][y].AreUAlive();

z=z+playing[0][y-1].AreUAlive();

z=z+playing[x][0].AreUAlive();

z=z+playing[x-1][0].AreUAlive();

}

/*

* rechts unten

*/

else if(x==höhe)

{

z=playing[x][y+1].AreUAlive();

z=z+playing[x][y-1].AreUAlive();

z=z+playing[x-1][y+1].AreUAlive();

z=z+playing[x-1][y-1].AreUAlive();

z=z+playing[x-1][y].AreUAlive();

z=z+playing[0][y].AreUAlive();

z=z+playing[0][y+1].AreUAlive();

z=z+playing[0][y-1].AreUAlive();

}

/*

* mitte unten

*/

else if(y==0)

{

z=playing[x][y+1].AreUAlive();

z=z+playing[x][breite].AreUAlive();

z=z+playing[x-1][y+1].AreUAlive();

z=z+playing[x-1][breite].AreUAlive();

z=z+playing[x-1][y].AreUAlive();

z=z+playing[x+1][y].AreUAlive();

z=z+playing[x+1][y+1].AreUAlive();

z=z+playing[x+1][breite].AreUAlive();

}

/*

* links mitte

*/

else if(y==breite)

{

z=playing[x][y-1].AreUAlive();

z=z+playing[x][0].AreUAlive();

z=z+playing[x-1][0].AreUAlive();

z=z+playing[x-1][y-1].AreUAlive();

z=z+playing[x-1][y].AreUAlive();

z=z+playing[x+1][y].AreUAlive();

z=z+playing[x+1][0].AreUAlive();

z=z+playing[x+1][y-1].AreUAlive();

}

/*

* rechts mitte

*/

else

{

z=playing[x][y-1].AreUAlive();

z=z+playing[x][y+1].AreUAlive();

z=z+playing[x-1][y+1].AreUAlive();

z=z+playing[x-1][y-1].AreUAlive();

z=z+playing[x-1][y].AreUAlive();

z=z+playing[x+1][y].AreUAlive();

z=z+playing[x+1][y+1].AreUAlive();

z=z+playing[x+1][y-1].AreUAlive();

}

/*

* mitte mitte

*/

return z;

}

public int WayOfLive(int z)

{

if((z==2)||(z==3))

{

return 1;

}

else

{

return 0;

}

}

public void nextGeneration()

{

for(int i=0;i<höhe;i++)

{

for(int j=0;j<breite;j++)

{

int C=CountLivingAround(i,j);

if (WayOfLive©==1)

{

working[j].revive();

}

else

{

working[j].kill();

}

}

}

for(int i=0;i<höhe;i++)

{

for(int j=0;j<breite;j++)

{

playing[j]=working[j];

}

};

}

public void Ausgabe()

{

for(int i=0;i<höhe;i++)

{

for(int j=0;j<breite;j++)

{

int q=playing[j].AreUAlive();

System.out.print(" "+q+" ");

}

System.out.println();

};

}

public void SetSomeCellsAlive(int x,int y)

{

playing[x][y].revive();

}

}

und wie kann ich das wieter in ein applet umsetzen

Stelle bitte mal eine konkrete Frage was nicht funktioniert und benutze bitte Codetags und eine vernünftige Codeformatierung.

Sind dir die Regeln in Bezug auf die Nachbarn beim Übergang zur nächsten Generation klar? Wo stecken die im Code?

Hallo ich versuche gerade das spiel des lebens mit bluej zu programmieren ,aber es funktioniert net

Benutze Code Tags und lerne bitte Dein Problem so zu beschreiben, dass es verständlich ist. Niemand wird hier Deinen unstrukturierten Code für Dich versuchen zu verstehen

also es ist so das er mir ne meldung gibt das das er auf etwas ausserhalb des array bereiches zugreifen will

die weiterleben regeln sitzten in der methode WayOfLive(int z)

ist das besser so ich geb mein bestes

else if((x==höhe)&&(y==breite))

An solchen Stellen müsstest Du auf (höhe-1) und (breite-1) testen. Ein Array der Länge n geht nunmal von 0 bis n-1 und nicht von 1 bis n.

danke hoffe das hilft

Erstelle ein Konto oder melde dich an, um einen Kommentar zu schreiben.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.