Skip to main content

How to Build an Interactive Quiz or Trivia Game in Batch Script

Batch scripts are typically reserved for server maintenance and file manipulation, but the Windows Command Prompt provides all the necessary I/O tools to create fully interactive, branching programs. Building a terminal-based Quiz or Trivia game involves displaying questions, capturing user keyboard inputs, scoring answers via variables, and establishing control flows (GOTO structures) based on those answers.

In this guide, we will demonstrate how to build a dynamic trivia game using native Batch commands.

The Strategy: set /p and Control Flow

  1. Use echo to paint the trivia terminal and list multiple-choice options.
  2. Use set /p to prompt the user for their answer.
  3. Use if /i (case-insensitive checking) to validate the answer against the correct option.
  4. Increment a variable for the score if they are correct.
  5. Use pause and cls to clear the terminal smoothly and load the next question.

Implementation Script

@echo off
setlocal EnableDelayedExpansion

:: Set terminal title and colors
title IT Trivia Challenge
:: 0A sets a black background with bright green text
color 0A

:: Initialize Score
set "score=0"
set "totalQuestions=3"

:StartScreen
cls
echo ==========================================
echo SYSADMIN TRIVIA CHALLENGE
echo ==========================================
echo.
echo Press any key to begin the quiz...
pause >nul

:: ==========================================
:: QUESTION 1
:: ==========================================
:Q1
cls
echo Question 1 of !totalQuestions!
echo ==========================================
echo Which Command-Line utility is used natively
echo to check basic network connectivity to a host?
echo.
echo A. netstat
echo B. ping
echo C. ipconfig
echo D. nslookup
echo.

set "ans1="
set /p "ans1=Enter your choice (A/B/C/D): "

:: Case-insensitive check
if /i "!ans1!"=="B" (
echo.
echo Correct! The 'ping' command sends ICMP Echo Requests.
set /a "score+=1"
) else (
echo.
echo Incorrect. The correct answer was B ^(ping^).
)
pause
goto Q2

:: ==========================================
:: QUESTION 2
:: ==========================================
:Q2
cls
echo Question 2 of !totalQuestions!
echo ==========================================
echo In Windows Batch, what does 'setlocal' do?
echo.
echo A. Defines local time settings
echo B. Sets location variables for GPS
echo C. Localizes environment variable changes to the script
echo D. Downloads the file to the local directory
echo.

set "ans2="
set /p "ans2=Enter your choice (A/B/C/D): "

if /i "!ans2!"=="C" (
echo.
echo Right! It prevents your script's variables from altering the host system.
set /a "score+=1"
) else (
echo.
echo Wrong. The correct answer was C.
)
pause
goto Q3

:: ==========================================
:: QUESTION 3
:: ==========================================
:Q3
cls
echo Question 3 of !totalQuestions!
echo ==========================================
echo What port does SSH operate on by default?
echo.
echo A. 21
echo B. 22
echo C. 80
echo D. 443
echo.

set "ans3="
set /p "ans3=Type the exact port number: "

if "!ans3!"=="22" (
echo.
echo Spot on! SSH uses Port 22.
set /a "score+=1"
) else (
echo.
echo Incorrect. SSH operates on Port 22.
)
pause
goto FinalScore

:: ==========================================
:: SCORE REPORT
:: ==========================================
:FinalScore
cls
echo ==========================================
echo QUIZ COMPLETED!
echo ==========================================
echo.
echo You scored !score! out of !totalQuestions!.

:: Grade Evaluation logic
if !score! equ 3 (
echo AMAZING! A perfect score. You are a true SysAdmin!
) else if !score! equ 2 (
echo Great job! You know your stuff.
) else (
echo Better luck next time! Back to the man pages!
)
echo.

:: Play Again?
set "retry="
set /p "retry=Would you like to play again? (Y/N): "
if /i "!retry!"=="Y" (
set "score=0"
goto StartScreen
)

echo Thanks for playing!
pause
exit /b

How It Works

  1. set /p Variable Capture: The core mechanism is set /p "ans1=Prompt". This stops the script and waits for the user to type something and press Enter.
  2. Input Validation: Notice we check input defensively with if /i "!ans1!"=="B". Using delayed expansion (!ans1!) ensures the variable is evaluated at execution time rather than parse time. Enclosing both the variable and the literal letter in quotes prevents the script from crashing if the user presses Enter without typing anything. The /i flag ensures that typing a lowercase 'b' evaluates as identical to 'B'.
  3. Screen Control (cls): Calling cls (Clear Screen) at the top of every new Question block ensures that older, trailing questions are erased, creating the illusion of a specialized multi-page application.

Adding a Countdown Timer

You can add a countdown timer to create time pressure for each question using the timeout command.

note

Using a timer that simultaneously accepts keyboard input and counts down requires a significantly more complex setup, often involving two cooperating scripts or external utilities. The example below demonstrates a simple fixed-delay approach where the timer expires before the input prompt appears.

@echo off
setlocal EnableDelayedExpansion

set "answer="
cls
echo You have 5 seconds to think starting NOW!
echo What is the capital of France?
echo.

:: Wait 5 seconds using timeout
timeout /t 5 /nobreak >nul

echo Time is up! Type your answer now:
set /p "answer=Your answer: "

if /i "!answer!"=="Paris" (
echo Correct!
) else (
echo Incorrect. The answer was Paris.
)
pause

Conclusion

Building an interactive trivia game in Batch is an excellent way to practice flow control, user input validation, and layout formatting. It demonstrates that the Windows command prompt isn't limited just to headless, silent data manipulation, it can be transformed effortlessly into dynamic, user-facing interfaces with robust evaluation logic.