How to Create a Rock-Paper-Scissors Game in Batch Script
Building a "Rock, Paper, Scissors" game in a Windows Batch script moves beyond simple linear guessing logic and introduces multi-conditional evaluations. Because the computer must generate a random move and then rigorously assess whether the human's input beats, ties, or loses to that specific move, this game heavily utilizes nested IF/ELSE statements and string mapping.
In this guide, we will build a resilient Rock, Paper, Scissors game leveraging the %RANDOM% variable and extensive logic structures.
The Strategy: The %RANDOM% Modulo and String Arrays
- Accept human input for their choice.
- Generate a random computer move. Since
%RANDOM%generates a number between 0 and 32767, we use the modulo operation (%% 3) to reduce that result to a strict range of0, 1, or 2. - Map those integers (0,1,2) to strings (
Rock,Paper,Scissors). - Evaluate all 3 possible outcomes for the human's choice against the computer's choice to declare a Winner, Loser, or Tie.
Implementation Script
@echo off
setlocal
:: Setup the Terminal
title Rock Paper Scissors
color 0E
:: Track Scores
set "wins=0"
set "losses=0"
set "ties=0"
:GameStart
cls
echo ==========================================
echo ROCK, PAPER, SCISSORS
echo ==========================================
echo Score: Wins:%wins% ^| Losses:%losses% ^| Ties:%ties%
echo.
echo Choose your weapon:
echo [1] Rock
echo [2] Paper
echo [3] Scissors
echo [Q] Quit
echo.
:: 1. Gather User Input
set "playerChoice="
set /p "playerChoice=Select (1/2/3/Q): "
:: Handle Quitting
if /i "%playerChoice%"=="Q" goto EndGame
:: Validate Input
if "%playerChoice%"=="1" (
set "playerString=Rock"
) else if "%playerChoice%"=="2" (
set "playerString=Paper"
) else if "%playerChoice%"=="3" (
set "playerString=Scissors"
) else (
echo [!] Invalid selection. Please choose 1, 2, or 3.
pause
goto GameStart
)
:: 2. Generate Computer Move
set /a "compMove=(%RANDOM% %% 3) + 1"
if %compMove% equ 1 set "compString=Rock"
if %compMove% equ 2 set "compString=Paper"
if %compMove% equ 3 set "compString=Scissors"
echo.
echo You chose: %playerString%
echo Computer chose: %compString%
echo.
echo --------------------------------
:: 3. Evaluate Results
if "%playerString%"=="%compString%" (
echo Result: IT'S A TIE!
set /a "ties+=1"
goto ReplayPrompt
)
:: Evaluate Player Win states
if "%playerString%"=="Rock" if "%compString%"=="Scissors" goto PlayerWins
if "%playerString%"=="Paper" if "%compString%"=="Rock" goto PlayerWins
if "%playerString%"=="Scissors" if "%compString%"=="Paper" goto PlayerWins
:: If it wasn't a tie, and the player didn't win, the computer won.
goto ComputerWins
:PlayerWins
echo Result: YOU WIN! %playerString% beats %compString%.
set /a "wins+=1"
goto ReplayPrompt
:ComputerWins
echo Result: YOU LOSE! %compString% beats %playerString%.
set /a "losses+=1"
goto ReplayPrompt
:ReplayPrompt
echo.
pause
goto GameStart
:EndGame
echo.
echo ==========================================
echo FINAL SCOREBOARD
echo ==========================================
echo Wins: %wins%
echo Losses: %losses%
echo Ties: %ties%
echo ==========================================
echo Thanks for playing!
pause
exit /b
:ReplayPrompt
echo.
pause
goto GameStart
:EndGame
cls
echo.
echo ==========================================
echo FINAL SCOREBOARD
echo ==========================================
echo Wins: %wins%
echo Losses: %losses%
echo Ties: %ties%
echo ==========================================
echo.
echo Thanks for playing!
echo.
pause
exit /b
How It Works
- Mapping Indices: Batch does not possess objects or hash maps. By assigning input
1precisely toRock, and mapping the computer's modulo math+ 1precisely toRock, we bridge numeric randomization to human-readable strings flawlessly. - Simplified Evaluation Layout: Rock-Paper-Scissors possesses 9 possible combinations (3 Tie scenarios, 3 Player Win scenarios, 3 Computer Win scenarios).
- The first check (
if A == B) knocks out all 3 Tie scenarios instantly. - The next block sequentially maps out the 3 specific Player Win scenarios.
- Because there are only 3 possible outcomes remaining (the Computer wins), we entirely bypass writing extensive code for evaluating losses; if the logic survives the Tie and Win checks, it forces the script into
goto ComputerWins.
- The first check (
- Score Tracking: Using standard expansion alongside
%wins%lets the script dynamically tally the score. Since labels like:GameStartare re-parsed on everygoto, the variables are expanded with their current values without needing Delayed Expansion.
Conclusion
Rock-Paper-Scissors highlights elegant logic flow reduction. Identifying combinations efficiently transforms an otherwise messy 20-line nested IF/ELSE disaster into a clean, rapidly executable conditional evaluation tree. Constructing simple mechanics like this natively hones precision and syntax hygiene for more massive automation scripts.