' Interactive Menu System — INPUT, SELECT CASE, DO/LOOP menu framework
#COMPILE EXE
#DIM ALL

FUNCTION ShowMenu() AS LONG
    LOCAL choice AS LONG
    PRINT "===== MAIN MENU ====="
    PRINT "1. Display Date/Time Info"
    PRINT "2. Count to N"
    PRINT "3. Show Squares and Cubes"
    PRINT "4. Number Guessing Game"
    PRINT "5. Fibonacci Sequence"
    PRINT "6. Prime Number Check"
    PRINT "7. Exit"
    PRINT STRING$(40, 61)
    INPUT "Enter your choice (1-7): ", choice
    PRINT
    FUNCTION = choice
END FUNCTION

SUB ShowDateTimeInfo()
    LOCAL t AS LONG
    t = TIMER
    PRINT "Timer value: " & STR$(t) & " ms"
    PRINT "Minutes today: " & STR$(INT(t / 60000))
    PRINT "Seconds today: " & STR$(INT(t / 1000))
    PRINT
END SUB

SUB CountToN()
    LOCAL n AS LONG
    LOCAL i AS LONG
    LOCAL line AS STRING
    INPUT "Count up to: ", n
    IF n <= 0 THEN
        PRINT "Enter a positive number."
        EXIT SUB
    END IF
    IF n > 1000 THEN
        PRINT "Too large, limiting to 1000."
        n = 1000
    END IF
    line = ""
    FOR i = 1 TO n
        line = line & STR$(i) & " "
        IF LEN(line) > 70 THEN
            PRINT line
            line = ""
        END IF
    NEXT i
    IF LEN(line) > 0 THEN PRINT line
    PRINT "Counted to " & STR$(n)
    PRINT
END SUB

SUB ShowSquaresCubes()
    LOCAL i AS LONG
    LOCAL n AS LONG
    INPUT "Show squares and cubes up to: ", n
    IF n > 50 THEN n = 50
    PRINT " N    N^2    N^3"
    PRINT STRING$(20, 61)
    FOR i = 1 TO n
        PRINT FORMAT$(i, "##") & "  " & FORMAT$(i * i, "#####") & "  " & FORMAT$(i * i * i, "######")
    NEXT i
    PRINT
END SUB

SUB GuessingGame()
    LOCAL target AS LONG
    LOCAL guess AS LONG
    LOCAL attempts AS LONG
    target = (TIMER MOD 97) + 1
    attempts = 0
    PRINT "Guess the number (1-97):"
    DO
        INPUT "", guess
        attempts = attempts + 1
        IF guess < target THEN
            PRINT "Too low, try again:"
        ELSEIF guess > target THEN
            PRINT "Too high, try again:"
        END IF
    LOOP UNTIL guess = target
    PRINT "Correct in " & STR$(attempts) & " attempts!"
    PRINT
END SUB

SUB FibonacciSeq()
    LOCAL n AS LONG
    LOCAL i AS LONG
    LOCAL a AS QUAD
    LOCAL b AS QUAD
    LOCAL c AS QUAD
    INPUT "How many Fibonacci numbers: ", n
    IF n < 1 THEN n = 1
    IF n > 90 THEN n = 90
    a = 0
    b = 1
    PRINT "Fibonacci (" & STR$(n) & " terms):"
    FOR i = 1 TO n
        PRINT STR$(a)
        c = a + b
        a = b
        b = c
        IF i MOD 10 = 0 THEN PRINT
    NEXT i
    PRINT
    PRINT
END SUB

SUB PrimeCheck()
    LOCAL n AS LONG
    LOCAL i AS LONG
    LOCAL isPrime AS LONG
    INPUT "Check if prime: ", n
    IF n < 2 THEN
        PRINT STR$(n) & " is not prime."
        EXIT SUB
    END IF
    isPrime = 1
    FOR i = 2 TO INT(SQR(n))
        IF n MOD i = 0 THEN
            isPrime = 0
            EXIT FOR
        END IF
    NEXT i
    IF isPrime THEN
        PRINT STR$(n) & " is PRIME"
    ELSE
        PRINT STR$(n) & " is not prime (by " & STR$(i) & ")"
    END IF
    PRINT
END SUB

FUNCTION PBMAIN() AS LONG
    LOCAL choice AS LONG
    PRINT "========================="
    PRINT " Interactive Menu System "
    PRINT "========================="
    PRINT
    DO
        choice = ShowMenu()
        SELECT CASE choice
            CASE 1
                CALL ShowDateTimeInfo()
            CASE 2
                CALL CountToN()
            CASE 3
                CALL ShowSquaresCubes()
            CASE 4
                CALL GuessingGame()
            CASE 5
                CALL FibonacciSeq()
            CASE 6
                CALL PrimeCheck()
            CASE 7
                PRINT "Goodbye!"
                EXIT LOOP
            CASE ELSE
                PRINT "Invalid choice. Try again."
                PRINT
        END SELECT
    LOOP
    FUNCTION = 0
END FUNCTION
