// © Copyrights 2026 Praajna Pattada

#include <iostream>
#include <ctime>
#include <string>
#include <limits>

int main()
{
    int number{}, attempts{}, guess{};
    bool guessed{ false };
    char status{'1'}; // A variable to check if the player wants to repeat the game or exit.
    struct tm *Time;
    time_t CurrentTime;
    time(&CurrentTime);
    Time = localtime(&CurrentTime);
    int seconds{Time->tm_sec};
    srand(seconds); // Seeding the random number generator with the current second.

    std::cout << "----------------------------------------" << '\n' << "Welcome to the Number-Guessing Game!" << '\n';
    std::cout << '\n' << "The computer will select a random number between 1 to 10. You have three attempts to guess it. If you guess it correctly within them, you win. Otherwise, the system wins." << '\n' << '\n';

    do {
        guessed = false;
        attempts = 0;
        number = (rand() % 10) + 1; // Generate a random number between 1 and 10.
        std::cout << '\n' << "----------------------------------------" << '\n';
        std::cout << "A random number has been chosen between 1 to 10. Enter your guess." << '\n' << '\n';

        while (!guessed && attempts < 3)
        {
            std::cin.sync(); // Clear input buffer
            std::cin.clear();
            std::cout << "Attempt " << (attempts + 1) << ": ";
            std::cin >> guess;
            attempts++;

            if (guess >= 1 && guess <= 10)
            {
                if (guess == number)
                {
                    guessed = true;
                } else if (guess < number)
                {
                    std::cout << "Your guess is low." << '\n';
                } else if (guess > number)
                {
                    std::cout << "Your guess is high." << '\n';
                }
            } else {
                    std::cout << "Invalid input. Please enter a number between 1 and 10." << '\n';
            }
        } // End of the guessing loop

        if (guessed)
        {
            std::cout << "Congratulations! You guessed the number correctly." << '\n';
        } else {
            std::cout << "Sorry, you've used all your attempts. The correct number was " << number << "." << '\n';
        }
        
        std::cout << "Would you like to play again? If yes, enter 1. Enter 0 to exit the game." << '\n';
        do {
            std::cin.get(status);
        } while (status != '1' && status != '0');
    } while (status == '1');

    std::cout << '\n' << "----------------------------------------" << '\n';
    std::cout << "Thank you for playing!" << '\n';

    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    std::cin.get();
    return 0;
}
