#!/usr/bin/env python3
# quiz - tiny quiz game with built-in questions and a score
# Usage: quiz [-n N] [--seed N] [--auto]  (answer by letter, or --auto to auto-answer)
import argparse, random, sys

QUESTIONS = [
 ("What is the capital of France?", ["London", "Paris", "Berlin", "Madrid"], 1),
 ("Which planet is known as the Red Planet?", ["Venus", "Jupiter", "Mars", "Saturn"], 2),
 ("What does CPU stand for?", ["Central Processing Unit", "Computer Personal Unit", "Central Program Utility", "Core Processing Unit"], 0),
 ("How many bits are in a byte?", ["4", "8", "16", "32"], 1),
 ("What is 7 x 8?", ["54", "56", "64", "48"], 1),
 ("Which language is this quiz written in?", ["C", "Java", "Python", "Rust"], 2),
 ("What is the chemical symbol for water?", ["CO2", "O2", "H2O", "NaCl"], 2),
 ("How many days are in a leap year?", ["365", "366", "364", "367"], 1),
 ("Which data structure is FIFO?", ["Stack", "Queue", "Tree", "Graph"], 1),
 ("What year did the first Moon landing happen?", ["1959", "1969", "1979", "1989"], 1),
 ("What is the largest ocean on Earth?", ["Atlantic", "Indian", "Arctic", "Pacific"], 3),
 ("Which file extension is a Python script?", [".py", ".js", ".rb", ".pl"], 0),
]

def run(questions, answers=None):
    score = 0
    for i, (q, opts, correct) in enumerate(questions):
        print(f"\nQ{i+1}: {q}")
        for j, opt in enumerate(opts):
            print(f"  {chr(97+j)}) {opt}")
        if answers is not None:
            ans = answers[i]
            print(f"  (auto answer: {ans})")
        else:
            try:
                ans = input("Your answer (a-d): ").strip().lower()
            except EOFError:
                ans = ""
        idx = ord(ans) - 97 if len(ans) == 1 and ans in "abcd" else -1
        if idx == correct:
            print("  Correct!")
            score += 1
        else:
            print(f"  Wrong — the answer was {chr(97+correct)}) {opts[correct]}")
    print(f"\nScore: {score}/{len(questions)}")
    return score

def selftest():
    qs = QUESTIONS[:4]
    correct_answers = [chr(97 + c) for _, _, c in qs]
    s = run(qs, answers=correct_answers)
    assert s == 4, s
    wrong = ["a"] * 4
    s2 = run(qs, answers=wrong)
    assert s2 < 4
    print("quiz selftest OK")

def main():
    p = argparse.ArgumentParser(description="Tiny quiz game with built-in questions and a score.")
    p.add_argument("-n", "--num", type=int, default=5, help="number of questions (default 5)")
    p.add_argument("--seed", type=int, default=None, help="seed for question order")
    p.add_argument("--auto", action="store_true", help="non-interactive: answer every question correctly")
    p.add_argument("--selftest", action="store_true", help="run a built-in self-test and exit")
    a = p.parse_args()
    if a.selftest:
        selftest(); return 0
    qs = QUESTIONS[:]
    random.Random(a.seed).shuffle(qs)
    qs = qs[:max(1, min(a.num, len(qs)))]
    answers = [chr(97 + c) for _, _, c in qs] if a.auto else None
    run(qs, answers=answers)
    return 0

if __name__ == "__main__":
    sys.exit(main())
