Datasets:
File size: 1,352 Bytes
b263752 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | #include <iostream>
#include <cstdlib>
#include <ctime>
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;
}
|