#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/*
    Math Practice Game (Beginner Friendly)
    --------------------------------------
    This program demonstrates:
    1) do-while loop -> for menu (Play / Show Score / Exit)
    2) for loop -> ask exactly 5 questions in each game
    3) while loop -> re-ask when input is invalid
    4) break -> quit game early from Play mode

    Tip:
    - Enter -1 as your answer to quit a game early.
*/

int main(void) {
    int menuChoice;
    int totalGamesPlayed = 0;
    int bestScore = 0;
    int lastScore = 0;

    /* Seed random numbers so questions change each run */
    srand((unsigned int)time(NULL));

    do {
        printf("\n====================================\n");
        printf("      MATH PRACTICE GAME\n");
        printf("====================================\n");
        printf("1. Play\n");
        printf("2. Show Score\n");
        printf("3. Exit\n");
        printf("Choose (1-3): ");

        /*
            While loop to re-ask until user gives a valid integer (1-3).
            scanf returns 1 when it successfully reads an integer.
        */
        while (scanf("%d", &menuChoice) != 1 || menuChoice < 1 || menuChoice > 3) {
            int ch;
            printf("Invalid input. Please enter 1, 2, or 3: ");

            /* Clear bad input from buffer */
            while ((ch = getchar()) != '\n' && ch != EOF) {
                /* do nothing, just clean buffer */
            }
        }

        if (menuChoice == 1) {
            int score = 0;
            int quitEarly = 0;

            printf("\nGreat! You will get 5 questions.\n");
            printf("Enter -1 anytime to quit early.\n\n");

            /* for loop -> exactly 5 questions */
            for (int i = 1; i <= 5; i++) {
                int a = rand() % 20 + 1;  /* 1 to 20 */
                int b = rand() % 20 + 1;  /* 1 to 20 */
                int correct = a + b;
                int userAnswer;

                printf("Q%d) %d + %d = ", i, a, b);

                /*
                    while loop to re-ask invalid input.
                    Valid input = an integer.
                */
                while (scanf("%d", &userAnswer) != 1) {
                    int ch;
                    printf("Please enter a NUMBER: ");
                    while ((ch = getchar()) != '\n' && ch != EOF) {
                        /* clear invalid characters */
                    }
                }

                /*
                    break example:
                    If user enters -1, stop asking more questions.
                */
                if (userAnswer == -1) {
                    printf("You chose to quit this game early.\n");
                    quitEarly = 1;
                    break;
                }

                if (userAnswer == correct) {
                    printf("Correct! +1 point\n");
                    score++;
                } else {
                    printf("Oops! Correct answer is %d\n", correct);
                }
            }

            lastScore = score;
            totalGamesPlayed++;
            if (score > bestScore) {
                bestScore = score;
            }

            printf("\nGame Over!\n");
            if (quitEarly) {
                printf("You quit early. ");
            }
            printf("Your score: %d / 5\n", score);
        } else if (menuChoice == 2) {
            printf("\n========= SCORE BOARD =========\n");
            printf("Games played : %d\n", totalGamesPlayed);
            printf("Last score   : %d / 5\n", lastScore);
            printf("Best score   : %d / 5\n", bestScore);
        } else {
            printf("\nThanks for playing. Keep practicing!\n");
        }

    } while (menuChoice != 3); /* do-while -> menu shows at least once */

    return 0;
}
