How to Create a Word Scramble Game in Batch Script
Word scramble games are not only fun but also serve as a profound introduction to string manipulation and advanced randomization in programming. Creating one in Windows Batch Script requires you to push the boundaries of what the command line can do. In this guide, we will build a fully functional Word Scramble game from scratch, exploring how to break strings into characters, shuffle them, and manage a game loop.
This project is ideal for mid-level Batch developers who want to understand how to handle dynamic data and interactive logic within a terminal environment.
The Core Objective
The game logic follows a simple but effective flow:
- Select a random word from a predefined list.
- Break the word into individual characters.
- Shuffle those characters to create a "scrambled" version.
- Present the scrambled word to the user and ask for their guess.
- Validate the guess and provide feedback.
Handling Strings in Batch
One of the biggest hurdles in Batch Scripting is that strings are treated as single units. Unlike Python or JavaScript, you cannot simply call word.split(''). Instead, we must use string slicing within a loop to extract characters one by one.
String Slicing Syntax
To get a single character at a specific position, we use: !variable:~position,1!.
Before we can slice a string, we need to know its length. Since Batch doesn't have a len() function, we create a small loop that increments a counter until the character at that position is empty.
Implementing the Scrambling Logic
Shuffling is the most complex part of this script. We will use a technique where we pick characters at random from the original pool until none are left, building the scrambled word one character at a time.
The Wrong Way: Simple Swapping
A common mistake is just swapping two adjacent characters. This doesn't produce a "true" scramble and often leaves the word recognizable.
:: WRONG WAY
set "word=BATCH"
:: Just swap the first and second letter
set "scrambled=%word:~1,1%%word:~0,1%%word:~2%"
echo %scrambled%
Output Concern: If the word is "BATCH", the output "ABTCH" is barely scrambled. This lack of randomization makes for a poor gaming experience.
The Correct Way: Random Extraction Shuffle
The "right" way involves picking a random character from the original word, adding it to the scrambled string, and removing it from the pool. The full implementation below demonstrates this approach.
Step-by-Step Game Construction
1. Initializing the Game
We start by setting up our word list and choosing one randomly.
set "w1=COMMAND"
set "w2=SCRIPT"
set "w3=WINDOWS"
set "w4=VARIABLE"
set "w5=FUNCTION"
set /a "rand_idx=(%RANDOM% %% 5) + 1"
call set "target_word=%%w!rand_idx!%%"
2. Calculating Length and Dismantling
We need to know how many characters are in target_word to iterate through them.
3. The Shuffling Loop
We will store each character in the word pool and then reconstruct the word by picking characters at random positions, removing each from the pool as it is used.
Complete Word Scramble Game Script
The following script integrates all these concepts into a polished command-line game.
@echo off
title Word Scramble Challenge
setlocal enabledelayedexpansion
:init
:: Define our secret words
set "w1=TERMINAL" & set "w2=PROGRAM" & set "w3=AUTOMATION" & set "w4=CMDLINE" & set "w5=SCRIPTER"
set "w6=KERNEL" & set "w7=PROCESS" & set "w8=BINARY" & set "w9=COMPILER" & set "w10=DATABASE"
set "word_count=10"
:setup_game
set /a "ridx=(!RANDOM! %% %word_count%) + 1"
call set "original_word=%%w!ridx!%%"
set "scrambled_word="
:: 1. Calculate length of the target word
set "len=0"
set "test_word=!original_word!"
:calc_len
if defined test_word (
set "test_word=!test_word:~1!"
set /a "len+=1"
goto calc_len
)
:: 2. Scramble logic
:: We extract characters randomly until the pool is empty
set "pool=!original_word!"
set "current_len=!len!"
:scramble_loop
if !current_len! gtr 0 (
set /a "char_idx=!RANDOM! %% current_len"
:: Extract the character at the random position
for %%a in (!char_idx!) do (
set "char=!pool:~%%a,1!"
set "scrambled_word=!scrambled_word!!char!"
:: Remove the character from the pool
set "prefix=!pool:~0,%%a!"
set /a "suffix_start=%%a + 1"
for %%b in (!suffix_start!) do set "suffix=!pool:~%%b!"
set "pool=!prefix!!suffix!"
)
set /a "current_len-=1"
goto scramble_loop
)
:: Prevent showing an unscrambled word (reshuffle if identical)
if "!scrambled_word!"=="!original_word!" goto setup_game
:: 3. Game Interface
:guess_screen
cls
color 0E
echo ===========================================
echo WORD SCRAMBLE CHALLENGE
echo ===========================================
echo.
echo Unscramble the word below:
echo.
echo [ !scrambled_word! ]
echo.
echo ===========================================
set "user_guess="
set /p "user_guess=Your Guess (or type HINT / SKIP): "
if not defined user_guess goto guess_screen
if /i "!user_guess!"=="SKIP" (
echo.
echo The word was: !original_word!
echo.
pause
goto setup_game
)
if /i "!user_guess!"=="HINT" (
echo.
echo Hint: The word starts with "!original_word:~0,1!" and is !len! letters long.
echo.
pause
goto guess_screen
)
if /i "!user_guess!"=="!original_word!" (
color 0A
echo.
echo [CORRECT] Well done^^!
echo.
pause
goto setup_game
) else (
color 0C
echo.
echo [WRONG] That is not the right word. Try again.
echo.
pause
goto guess_screen
)
Explaining the Technical Highlights
call set "original_word=%%w!ridx!%%": This resolves a nested variable lookup in two phases. Delayed expansion first resolves!ridx!to a number (e.g.,3), then thecallforces a second parse pass that resolves%w3%to the stored word (e.g.,AUTOMATION).- Sub-string removal: Inside the
:scramble_loop, we remove the used character by concatenating everything before it (prefix) and everything after it (suffix). This is a powerful technique for list management in Batch. - Case-insensitive check: The
if /icommand ensures that "terminal" is just as correct as "TERMINAL", making the game more user-friendly. - Reshuffle guard: After scrambling, the script checks if the scrambled word is identical to the original. If so, it reshuffles to ensure the player always sees a different arrangement.
Best Practices for Game Development in Batch
- Delayed Expansion: Always use
!variable!for variables that change inside loops. - Color Feedback: Change the text color (e.g.,
0Afor green,0Cfor red) to provide immediate visual feedback for correct or incorrect answers. - Clean UI: Use
echo.to add spacing and clear the screen (cls) to prevent the console from becoming cluttered. - Input Clearing: Always
set "var="beforeset /pto ensure empty input is detected properly, sinceset /pdoes not modify the variable when the user presses Enter without typing.
Conclusion
Building a Word Scramble game in Batch Script is an excellent way to master string manipulation techniques that are usually overlooked. By dismantling words and randomly reconstructing them, you gain a deep understanding of how Batch handles variables and loops. Whether you use this as a learning tool or the basis for a more complex automation script, the logic of "pool-and-extract" scrambling is a vital skill for any Batch developer.