Programm umschreiben?



  • Könntet ihr mir helfen, folgendes C++ Programm ein wenig umzuschreiben, vielleicht sogar zu vereinfachen? Unnötige Sachen rauszunehmen: Es soll ein Stein in der Mitte platziert werden und per Zufall dann ein weiterer ins Spielfeld eingesetzt werden und dieser wandert dann zufällig immer rum, bis er am Setein in der Mitte andockt und so weiter, bis dass Spielfeld voll ist.
    Und wie baue ich ein, dass es aufhört, wenn ein Steinreihe dann den rand berührt (des Spielfeldes)?

    Danke für eure Hilfe! 🙂

    #include <cstdlib>
    #include <ctime>
    #include <iostream>
    #include <math.h>
    
    #include <windows.h>
    #include <conio.h>
    
    using namespace std;
    
    // constante variablen:
    const int x_length = 20;
    const int y_length = 20;
    int schritt=2;
    int delay = 5000;
    
    bool matrix[y_length][x_length];
    int unsigned count = 0;
    
    // funktion gotoxy
    void gotoxy(int x, int y)
    {
    	COORD  coordPos;
    
        coordPos.X = x;
        coordPos.Y = y;
    
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coordPos);
    
      return;
    }
    
    /*
    
      0 1 2
    
      7 X 3
    
      6 5 4
    
    */
    
    void ausgabe()
    {
    	system("cls");
       	for(int i=0; i<y_length; i++)
    	{
    		for(int j=0; j<x_length; j++)
    		{
    			if(matrix[i][j])
    				cout << "X";
    			else
    				cout << ".";
    		}
    		cout << endl;
    	}
    }
    
    void bewege(int xpos,int ypos) // 9568
    {
    	count++;
    	gotoxy(0,40);
    	cout << count;
    
    	// ---------- WENN EIN BENACHBARTES FELD GEFÄRBT IST ---------------
    	if(		matrix[ypos][xpos] == 1
    		||  matrix[(ypos)%y_length][(xpos+1)%x_length] == 1
    		||  matrix[(ypos)%y_length][(xpos-1+x_length)%x_length] == 1
    		||  matrix[(ypos+1)%y_length][(xpos)%x_length] == 1
    		||  matrix[(ypos+1)%y_length][(xpos+1)%x_length] == 1
    		||  matrix[(ypos+1)%y_length][(xpos-1+x_length)%x_length] == 1
    		||  matrix[(ypos-1+y_length)%y_length][(xpos)%x_length] == 1
    		||  matrix[(ypos-1+y_length)%y_length][(xpos+1)%x_length] == 1
    		||  matrix[(ypos-1+y_length)%y_length][(xpos-1+x_length)%x_length] == 1)
    	{
    		// matrix auf 1 setzen
    		matrix[ypos][xpos] = 1;
    		gotoxy(xpos,ypos);
    		cout << "X";
    
    		// neues teilchen
    		bewege(rand()%x_length,rand()%y_length);
    		return;
    	}
    	else
    	{
    
    		// ------------ Teilchen bewegen ---------------
    
    		// alte posi löschen
    		gotoxy(xpos,ypos);
    		cout << ".";
    
    		int richtung = rand()%8;
    
    		switch (richtung)
    		{
    		case 0: xpos = (xpos+schritt)%x_length;
    				ypos = (ypos)%y_length;
    				break;
    		case 1: xpos = (xpos+schritt)%x_length;
    				ypos = (ypos+schritt)%y_length;
    				break;
    		case 2: xpos = (xpos+schritt)%x_length;
    				ypos = (ypos-schritt+y_length)%y_length;
    				break;
    		case 3: xpos = (xpos-schritt+x_length)%x_length;
    				ypos = (ypos)%y_length;
    				break;
    		case 4: xpos = (xpos-schritt+x_length)%x_length;
    				ypos = (ypos+schritt)%y_length;
    				break;
    		case 5: xpos = (xpos-schritt+x_length)%x_length;
    				ypos = (ypos-schritt+y_length)%y_length;
    				break;
    		case 6: xpos = (xpos)%x_length;
    				ypos = (ypos+schritt)%y_length;
    				break;
    		case 7: xpos = (xpos)%x_length;
    				ypos = (ypos-schritt+y_length)%y_length;
    				break;
    		}
    
    		// neu zeichnen
    		gotoxy(xpos,ypos);
    		cout << "X";
    
    		int tmp;
    		for(int k=0;k<=5000;k++)
    			tmp=k*k/9;
    
    		// neu aufrufen
    		bewege(xpos,ypos);
    		return;
    	}
    }
    
    int main()
    {
    	cout << "Bitte beliebige Taste druecken, um Simulation zu starten.\n";
    	system("pause");
    	cout.precision(3);
    	srand(time(0));
    
    	for(int i=0;i<y_length;i++)
    	{
    		for(int j=0;j<y_length;j++)
    		{
    			matrix[x_length][y_length] = 0;
    		}
    	}
    	matrix[x_length/2][y_length/2] = 1;
    
    	ausgabe();
    
    	int xpos = rand()%x_length;
    	int ypos = rand()%x_length;
    	int richtung,k,tmp;
    
    	while(true)
    	{
    		count++;
    		gotoxy(0,40);
    		cout << "Bewegungen:" << count;
    
    		// ---------- WENN EIN BENACHBARTES FELD GEFÄRBT IST ---------------
    		if(		matrix[ypos][xpos] == 1
    			||  matrix[(ypos)%y_length][(xpos+1)%x_length] == 1
    			||  matrix[(ypos)%y_length][(xpos-1+x_length)%x_length] == 1
    			||  matrix[(ypos+1)%y_length][(xpos)%x_length] == 1
    			||  matrix[(ypos+1)%y_length][(xpos+1)%x_length] == 1
    			||  matrix[(ypos+1)%y_length][(xpos-1+x_length)%x_length] == 1
    			||  matrix[(ypos-1+y_length)%y_length][(xpos)%x_length] == 1
    			||  matrix[(ypos-1+y_length)%y_length][(xpos+1)%x_length] == 1
    			||  matrix[(ypos-1+y_length)%y_length][(xpos-1+x_length)%x_length] == 1)
    		{
    			// matrix auf 1 setzen
    			matrix[ypos][xpos] = 1;
    			gotoxy(xpos,ypos);
    			cout << "X";
    
    			// neues teilchen
    			xpos = rand()%x_length;
    			ypos = rand()%x_length;
    		}
    		else
    		{
    
    			// ------------ Teilchen bewegen ---------------
    
    			// alte posi löschen
    			gotoxy(xpos,ypos);
    			cout << ".";
    
    			richtung = rand()%8;
    
    			switch (richtung)
    			{
    			case 0: xpos = (xpos+schritt)%x_length;
    					ypos = (ypos)%y_length;
    					break;
    			case 1: xpos = (xpos+schritt)%x_length;
    					ypos = (ypos+schritt)%y_length;
    					break;
    			case 2: xpos = (xpos+schritt)%x_length;
    					ypos = (ypos-schritt+y_length)%y_length;
    					break;
    			case 3: xpos = (xpos-schritt+x_length)%x_length;
    					ypos = (ypos)%y_length;
    					break;
    			case 4: xpos = (xpos-schritt+x_length)%x_length;
    					ypos = (ypos+schritt)%y_length;
    					break;
    			case 5: xpos = (xpos-schritt+x_length)%x_length;
    					ypos = (ypos-schritt+y_length)%y_length;
    					break;
    			case 6: xpos = (xpos)%x_length;
    					ypos = (ypos+schritt)%y_length;
    					break;
    			case 7: xpos = (xpos)%x_length;
    					ypos = (ypos-schritt+y_length)%y_length;
    					break;
    			}
    
    			// neu zeichnen
    			gotoxy(xpos,ypos);
    			cout << "X";
    
    			for(k=0;k<=delay;k++)
    				tmp=sqrt(sqrt(sqrt(sqrt(k))));
    
    			// neu aufrufen
    		}
    	}
    
    	return 0;
    }
    


  • Bitte 🙂


Anmelden zum Antworten