// trivia_quiz.ks — Interactive trivia quiz with shuffled questions and score tracking // Usage: minks trivia_quiz.ks // // Five questions, each with three choices. Enter 1, 2, or 3 to answer. // Questions are presented in a random order each run via a Fisher-Yates shuffle. // // Modules: Math, Konsol, List, Array // ── Questions and answers ───────────────────────────────────────────────────── // Five parallel Lists: one for question text, one per answer option, one for the // correct choice (1 = A, 2 = B, 3 = C). Index i across all lists = question i. List:String questions; List:Push("What is the capital of France?", questions); List:Push("How many sides does a hexagon have?", questions); List:Push("Which planet is closest to the Sun?", questions); List:Push("What gas do plants absorb during photosynthesis?", questions); List:Push("Who wrote Romeo and Juliet?", questions); List:String optA; List:Push("London", optA); List:Push("4", optA); List:Push("Earth", optA); List:Push("Oxygen", optA); List:Push("Dickens", optA); List:String optB; List:Push("Berlin", optB); List:Push("6", optB); List:Push("Venus", optB); List:Push("Carbon dioxide", optB); List:Push("Shakespeare", optB); List:String optC; List:Push("Paris", optC); List:Push("8", optC); List:Push("Mercury", optC); List:Push("Nitrogen", optC); List:Push("Chaucer", optC); // Correct answer: 1 = A, 2 = B, 3 = C List:Number answers; List:Push(3, answers); // Paris = C List:Push(2, answers); // 6 = B List:Push(3, answers); // Mercury = C List:Push(2, answers); // CO₂ = B List:Push(2, answers); // Shakespeare = B // ── Shuffle the question order (Fisher-Yates) ───────────────────────────────── // Build an index array [0, 1, 2, 3, 4] then shuffle it in-place. // Using a separate index array means the five parallel lists stay untouched; // we just look up order[q] at quiz time instead of q directly. Array:New order[5]:Number; for (Number k = 0; k < 5; k++) { order[k] = k; } Var:Number swapI = 4; Var:Number swapJ; Var:Number swapTemp; Var:Number rnd; while (swapI > 0) { // Pick a random index in [0, swapI] by taking a float in [0, swapI+1) and flooring it. Math:Random(swapI + 1, rnd); Math:Floor(rnd, swapJ); // Swap order[swapI] ↔ order[swapJ] swapTemp = order[swapI]; order[swapI] = order[swapJ]; order[swapJ] = swapTemp; swapI = swapI - 1; } // ── Pre-declared working variables ──────────────────────────────────────────── Var:Number score = 0; Var:Number qIdx; Var:String qText; Var:String a; Var:String b; Var:String c; Var:Number correct; Var:Number answer; Var:Number qNum; // ── Quiz loop ───────────────────────────────────────────────────────────────── Konsol:Print("TRIVIA QUIZ"); Konsol:Print("Five questions. Enter 1, 2, or 3 for your answer."); Konsol:Print(""); for (Number q = 0; q < 5; q++) { // Use the shuffled index to look up question data. qIdx = order[q]; List:Get(qIdx, questions, qText); List:Get(qIdx, optA, a); List:Get(qIdx, optB, b); List:Get(qIdx, optC, c); List:Get(qIdx, answers, correct); qNum = q + 1; Konsol:Print("Question ${qNum} of 5: ${qText}"); Konsol:Print(" 1) ${a}"); Konsol:Print(" 2) ${b}"); Konsol:Print(" 3) ${c}"); // Konsol:Input stores the value as a Number when the input looks like one. Konsol:Input("Answer: ", answer); if (answer == correct) { Konsol:Print("Correct!"); score = score + 1; } else { // Tell the player what the right answer was. if (correct == 1) { Konsol:Print("Wrong! The answer was: " + a); } if (correct == 2) { Konsol:Print("Wrong! The answer was: " + b); } if (correct == 3) { Konsol:Print("Wrong! The answer was: " + c); } } Konsol:Print(""); } // ── Final score ─────────────────────────────────────────────────────────────── Konsol:Print("You scored ${score} out of 5."); if (score == 5) { Konsol:Print("Perfect score! Outstanding!"); } else if (score >= 3) { Konsol:Print("Good effort!"); } else { Konsol:Print("Better luck next time."); }