#include #include #include // Score class to handle score management class Score { private: int score; public: // Constructor to initialize score Score(int initialScore) : score(initialScore) {} // Pre-increment operator overload (Correct guess) int operator++() { return ++score; } // Pre-decrement operator overload (Guess too high) int operator--() { return --score; } // Post-decrement operator overload (Guess too low) int operator--(int) { return score--; } // Function to get the current score int getScore() const { return score; } }; // GuessingGame class to encapsulate game logic class GuessingGame { private: int targetNumber; Score score; public: // Constructor to initialize the game GuessingGame() : score(5) { srand(time(0)); // Seed the random number generator targetNumber = rand() % 10 + 1; // Generate a random number between 1 and 10 } // Function to simulate the game void play() { std::cout << "Virtual Guessing Game Starts!\n"; std::cout << "The virtual player will guess a number between 1 and 10.\n"; std::cout << "Score decreases for incorrect guesses and increases for correct ones.\n"; int attempt = 0; while (score.getScore() > 0) { int guess = rand() % 10 + 1; // Virtual player guesses randomly ++attempt; std::cout << "Attempt " << attempt << ": Virtual player guesses " << guess << ". "; if (guess > targetNumber) { std::cout << "Too high! "; --score; } else if (guess < targetNumber) { std::cout << "Too low! "; score--; } else { std::cout << "Correct! "; ++score; std::cout << "Score increases!\n"; std::cout << "The virtual player guessed the correct number: " << targetNumber << "\n"; std::cout << "Final score: " << score.getScore() << "\n"; return; } std::cout << "Score decreases. Current score: " << score.getScore() << "\n"; } // If the loop ends, it means the score reached 0 std::cout << "Game Over! The virtual player ran out of score.\n"; std::cout << "The correct number was: " << targetNumber << "\n"; } }; int main() { GuessingGame game; game.play(); return 0; }