Datasets:
using namespace std; | |
class NumberGame { | |
private: | |
int secretNumber; | |
public: | |
NumberGame() { | |
srand(time(0)); | |
secretNumber = rand() % 11; | |
} | |
int guessNumber() { | |
int guess; | |
cout << "Rate eine Zahl zwischen 0 und 10: "; | |
cin >> guess; | |
return guess; | |
} | |
bool checkGuess(int guess) { | |
if (guess == secretNumber) { | |
cout << "Glückwunsch! Du hast die richtige Zahl erraten." << endl; | |
return true; | |
} else if (guess < secretNumber) { | |
cout << "Die gesuchte Zahl ist größer als deine Eingabe." << endl; | |
} else { | |
cout << "Die gesuchte Zahl ist kleiner als deine Eingabe." << endl; | |
} | |
return false; | |
} | |
void startGame() { | |
int attempts = 0; | |
bool correctGuess = false; | |
cout << "Willkommen beim Zahlenspiel!" << endl; | |
while (!correctGuess && attempts < 3) { | |
int guess = guessNumber(); | |
correctGuess = checkGuess(guess); | |
attempts++; | |
} | |
if (!correctGuess) { | |
cout << "Du hast alle Versuche aufgebraucht. Die gesuchte Zahl war: " << secretNumber << endl; | |
} | |
} | |
}; | |
int main() { | |
NumberGame game; | |
game.startGame(); | |
return 0; | |
} | |