Hab ne kleine Frage: Wieso überschreitet die Variable immer den Wert ENDE?



  • Zur Information, wir benutzten eine eigene Bibliothek mit den Namen consoleio.h
    Sie dient dazu, z.b. den Cursor auf einer gewissen Stelle des Programms zu setzten (x/Y): gotoxy(x,y);
    Der Befehl clrscr(); Löscht die Ausgabe.
    Nur meine Frage, wie schaffe ich es, dass es die Schleife verlässt, wenn es über den Wert ENDE (60) kommt?
    Normalerweisse mit break; aber das scheint hier nicht zu Funktionieren

    mfg
    weini

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <math.h>
    #include <time.h>
    #include "consoleio.h"
    
    #define ENDE 60
    
    void flush();
    
    int main() {
    
    	unsigned int playerOneZahl = 0;
    	unsigned int playerOneGesamt = 0;
    	unsigned int playerTwoZahl = 0;
    	unsigned int playerTwoGesamt = 0;
    
    	//PLAYER ONE
    
    	srand((unsigned) time(NULL));
    
    	printf("Pferderennen!\n");
    	gotoxy(1, 2);
    	printf("x");
    	gotoxy(1, 5);
    	printf("u");
    	gotoxy(1, 1);
    
    	while (playerOneGesamt < ENDE || playerTwoGesamt < ENDE) {
    
    		clrscr();
    		//rechnungen
    		playerOneZahl = rand() % 6 + 1;
    		playerOneGesamt += playerOneZahl;
    		playerTwoZahl = ((rand() % 6) + 1);
    		playerTwoGesamt += playerTwoZahl;
    
    		if (playerOneGesamt > ENDE || playerTwoGesamt > ENDE) {
    			break;
    		}
    		printf("|------------||---------------------||------------------|\n\n");
    		printf("\n|------------||---------------------||------------------|\n\n");
    
    		//PLAYER ONE
    
    		gotoxy(playerOneGesamt, 2);
    		printf("x");
    
    		//PLAYER TWO
    
    		gotoxy(playerTwoGesamt, 5);
    		printf("u");
    		msleep(100000);
    	}
    
    	msleep(100000);
    	printf("|------------||---------------------||------------------|\n\n");
    	printf("\n|------------||---------------------||------------------|\n\n");
    	gotoxy(playerOneGesamt, 2);
    	printf("x");
    	gotoxy(playerTwoGesamt, 5);
    	printf("u");
    	printf("\n");
    	system("PAUSE");
    	return 0;
    }
    
    void flush(void) {
    	fflush(stdout);
    	fflush(stdin);
    }
    

    Die Ausgabe schaut dann z.b. so aus (Stimmt jetzt nicht überein):
    |------||------------||---------|
    ___________________________________x

    |------||------------||---------|
    __________________________u

    |------||------------||---------|

    Drücken Sie eine beliebige Taste . . .



  • Weil du das so schreibst.

    Die Bedingung von if (playerOneGesamt > ENDE || playerTwoGesamt > ENDE) wird halt nur wahr wenn eine der Variablen größer als ENDE ist. Also den wert von ENDE überschreitet.

    Evtl. reicht es aus, wenn du beim while das || durch ein && ersetzt.



  • Ohne genau geschaut zu haben... muss das vielleicht lauten:

    if (playerOneGesamt >= ENDE || playerTwoGesamt > ENDE) { 
                break; 
            }
    

    ?

    Weil du verwendest oben in der while-Schleife

    hile (playerOneGesamt < ENDE || playerTwoGesamt < ENDE)
    

    Das passt dann sonst irgendwie nicht.



  • Ok nach längerem überlegen bin ich dann draufgekommen, das das

    printf("|------------||---------------------||------------------|\n\n");
    

    nicht 60 zeichen lang ist
    -.-
    Sorry für den Post


Anmelden zum Antworten