
Is it possible to write a math game in Batch? Yes. But it is not as easy to work with as in true programming languages. The code in this game should help anyone who wants to learn more about working with numbers in a batch file.
@echo off
title Batch Basic Math Quiz
color 1f
:title
CLS
echo.
echo Welcome to Batch Basic Math Quiz
echo http://ScriptingMadness.blogspot.com
echo.
echo + - * /
echo.
echo.
echo Pick a number:
echo.
echo 1 for Addition
echo 2 for Subtraction
echo 3 for Multiplication
echo 4 for Division
echo 5 for All the Above
echo 6 to Exit
echo.
set /p topic= :
goto begin
REM #####################################
REM #####################################
REM #####################################
REM ###### MULTI ##############
:multi
set /a int1=%random%%%100+1
set /a int2=%random%%%100+1
set question=%int1%*%int2%
set /a answer=%question%
echo.
echo.
set /p guess=%int1% x %int2% =
set RightWrong=Wrong
if %guess%==EXIT goto title
if %guess%==Exit goto title
if %guess%==exit goto title
if %guess%==%answer% set RightWrong=Correct
rem Score
set return=MultiScore
goto Score
:MultiScore
rem ----
echo.
echo = %answer%
echo.
echo.
echo You are %RightWrong%. Points: %score%
echo --------------------------------------------------------------
goto next
REM #####################################
REM #####################################
REM ###### Add #########
:add
set /a int1=%random%%%100+1
set /a int2=%random%%%100+1
set question=%int1%+%int2%
set /a answer=%question%
echo.
echo.
set /p guess=%int1% + %int2% =
set RightWrong=Wrong
if %guess%==EXIT goto title
if %guess%==Exit goto title
if %guess%==exit goto title
if %guess%==%answer% set RightWrong=Correct
rem Score
set return=AddScore
goto Score
:AddScore
rem ----
echo.
echo = %answer%
echo.
echo.
echo You are %RightWrong%. Points: %score%
echo --------------------------------------------------------------
goto next
REM #####################################
REM #####################################
REM #####################################
REM ###### Sub ##############
:sub
set /a int1=%random%%%100+1
set /a int2=%random%%%100+1
set question=%int1%-%int2%
set /a answer=%question%
echo.
echo.
set /p guess=%int1% - %int2% =
set RightWrong=Wrong
if %guess%==EXIT goto title
if %guess%==Exit goto title
if %guess%==exit goto title
if %guess%==%answer% set RightWrong=Correct
rem Score
set return=SubScore
goto Score
:SubScore
rem ----
echo.
echo = %answer%
echo.
echo.
echo You are %RightWrong%. Points: %score%
echo --------------------------------------------------------------
goto next
REM #####################################
REM #####################################
REM #####################################
REM ###### Div ##############
:div
set /a int1=%random%%%100+1
set /a int2=%random%%%20+1
set question=%int1%/%int2%
set /a answer=%question%
set /a remainder="(%int1%) %% (%int2%)"
if %remainder%==0 goto SkipRemainder
set answer=%answer%_%remainder%
:SkipRemainder
echo.
echo Seperate Answer and Remainder with an Underscore "_".
echo.
echo.
set /p guess=%int1% / %int2% =
set RightWrong=Wrong
if %guess%==EXIT goto title
if %guess%==Exit goto title
if %guess%==exit goto title
if %guess%==%answer% set RightWrong=Correct
rem Score
set return=divScore
goto Score
:divScore
rem ----
echo.
echo = %answer%
echo.
echo.
echo You are %RightWrong%. Points: %score%
echo --------------------------------------------------------------
goto next
REM #####################################
REM #####################################
REM #####################################
:begin
set score=0
echo.
echo --------------------------------------------------------------
echo TO RETURN TO MAIN MENU, TYPE EXIT
echo.
echo Reach a score of 20 to Pass. Try not to use a calculator!
echo #For a great flash cards maker, try flash-card-it.blogspot.com
echo --------------------------------------------------------------
echo.
:next
set all="false"
if %topic%==1 goto add
if %topic%==2 goto sub
if %topic%==3 goto multi
if %topic%==4 goto div
if %topic%==5 goto all
if %topic%==6 exit
goto title
REM #####################################
REM #####################################
REM #####################################
:Score
if %RightWrong%==Wrong set ScoreMath=%score%-1
if %RightWrong%==Correct set ScoreMath=%score%+1
set /a score=%ScoreMath%
if %score%==20 goto win
goto %return%
REM #####################################
REM #####################################
REM #####################################
:win
CLS
echo ------------------------------------------
echo - -
echo - CONGRATULATIONS!!! YOU PASSED! -
echo - -
echo ------------------------------------------
echo.
echo Score: 20
echo.
echo.
echo.
pause
cls
goto title
REM #####################################
REM #####################################
REM #####################################
:all
set /a pick=%random%%%4+1
if %pick%==1 goto add
if %pick%==2 goto sub
if %pick%==3 goto multi
if %pick%==4 goto div
goto error
REM #####################################
REM #####################################
REM #####################################
:error
echo The program has encountered an error!
echo It will now close
echo.
echo.
exit
The code is a bit disorganized, but it should still be fairly readable. The interesting command for working with numbers in a batch file is the set /a command. I had some trouble getting the command to work properly if I tried to use a varible and an operator like this:
set /a answer=%var1%+%var2%
It seems to only work if working with a single variable that already contains the problem like so.
set question=%var1%+%var2%
set /a answer=%question%
The above code works perfectly. Another important thing to remember when working with math in batch files is the modulus operator. It uses the % sign. The problem arises when trying to use the modulus operator on two varibles. It ends up looking like this:
%var1% % %var2%
Unfortunately, this code does not work. The batch file ignores the % operator. This can be solved by using two instead of one.
%var1% %% %var2%
Not pretty, but plenty functional.
Wish List:
Here are some ideas to further improve the script.
- Create levels of difficulty. Right now the problems are all fairly simple.
- Create a Accuracy Counter.
- Clean up the interface

