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.

Spiel des Lebens

Empfohlene Antworten

Veröffentlicht

Hi, ich mach mir grad ein Game of Life.

Bei mir wird zwar ne ausgabe gemacht, aber ich anscheinend sterben bei mir keine Nachbarn weg ;(

Ich grübbel schon tagelang und find einfach nichts >.<

Hiermal der Source-Code:


#include<stdio.h>

#include<string.h>



void advance  (int x, int y)

{

			int j;

			int i;  

			int star=0; 

			int now[100][100];

			int next[100][100];



	for (i = 0; i < x; i++) // Mein Spielfeld


		{


			for (j = 0; j < y; j++)


			{

			now[i][j]=rand()%2; /* Zufallsgenerator weist dem array 0 oder 1 zu */

			}




		}



	// Regeln und Nachbarn zählen


	system("cls");

	for (i = 0; i < x; i++) 

		{

			for (j = 0; j < y; j++)	/* Spielfeld nochmal */

			{



				if((i-1>=0)&&(j-1>=0))		/* oben links */


				{

					if(now[(i-1)%x][(j-1)%y]==1)


					{

						star++;

					}


				}


				if(i >= 0)				/* oben */


				{

					if(now[(i-1)%x][j]==1)


					{

						star++;

					}


				}


				if((i-1>=0)&&(j+1<=y))		/* oben rechts*/


				{

					if(now[(i-1)%x][(j+1)%y]==1)


					{

						star++;

					}

				}	


				if(j-1>=0)				/* links */


				{

					if(now[i][(j-1)%y]==1)


					{

						star++;

					}

				}	


				if(j+1<=y)				/* rechts */


				{

					if(now[i][(j+1)%y]==1)


					{

						star++;

					}

				}	


				if((i+1<=x)&&(j-1>=0))		/* unten links */


				{

					if(now[(i+1)%x][(j-1)%y]==1)


					{

						star++;

					}

				}	


				if(i+1<x)				/* unten */


				{

					if(now[(i+1)%x][j]==1)


					{

						star++;

					}

				}	


				if((i+1<=x)&&((j+1<=y)))		/* unten rechts */

					{

					if(now[(i+1)%x][(j+1)%y]==1)


					{

						star++;

					}

				}	




				/* Leben oder Tod */


				if((star < 2) || (star > 3) )

					{

					next[i][j]=0;

					}	

				else if((star == 3) && (next[i][j]==0))

					{	

					next[i][j]=1;

					}

				else if(star == 2)

					{

					next[i][j]=now[i][j];

					}


			}



		}



		for(i=0; i<x; i++)

			{

			for(j=0; j<y; j++) 

					{

					next[i][j]=now[i][j]; //Ausgabe

						if (next[i][j] == 1)

						{

							printf("X");

						}

						else 

						printf(" ");

					}

				printf("\n");	//Zeilenumbruch

			}


}


int main ()


{

	int x=0;

	int y=0;

	int gen=0;

	int time; 


//Eingabe


	printf("Breite\n");

	scanf("%d", &x);

	printf("Hoehe\n");

	scanf("%d", &y);



	while(gen<1000)

	{

		advance(x,y);

		gen++; 

	}

}

Hoffe jem. hat ein gutes Auge ^^

Lg

Da sind einige Fehler drin.

Der wichtigste dürfte sein, dass now und next lokale Variablen deiner advance-Funktion sind, deren Inhalte also einen einzelnen Schritt des Games nie überleben. Du fängst praktisch immer wieder bei Schritt eins an.

Die Vorgehensweise innerhalb von advance ist auch etwas seltsam. Du ermittelst den Inhalt des next-Felds aus dem now-Feld (bis auf eine Stelle, da nimmst du fälschlicherweise das next-Feld als Informationsquelle), und wenn du damit fertig bist, kopierst du den Inhalt des now-Felds ins next-Feld, bevor du dieses ausgibst. Du überschreibst deine mühsam ermittelten Inhalte für die nächste Generation also jedesmal wieder mit dem Zufallsfeld.

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

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.