Solved Batch file Number guessing game problem

February 13, 2012 at 14:53:31
Specs: Windows 7
I am having trouble making a batch file number guessing game. Can anyone help me?
I'm not sure what's going wrong- here's the code

@echo off
:MAIN
cls
:setting
set /a %random%
if %random% GTR 100 goto setting
ping localhost -n 1 >nul
echo MISTER FOX'S NUMBER GUESSING GAME!
ping localhost -n 1 >nul
echo.
ping localhost -n 1 >nul
echo.
ping localhost -n 1 >nul
echo.
ping localhost -n 1 >nul
echo It's easy to play!
ping localhost -n 1 >nul
echo.
ping localhost -n 1 >nul
echo The computer generates a number between 1 and 100, and you try to guess it!
ping localhost -n 1 >nul
echo.
ping localhost -n 1 >nul
echo.
ping localhost -n 1 >nul
pause
cls
echo To begin the game, type your name and press Enter
echo.
echo.
echo.
set /p web=%name%
:HOME
cls
echo GUESS THE NUMBER!
echo.
echo.
set /p web=%guess%
if %guess% LSS %random% goto LOWER
if %guess% GTR %random% goto HIGHER
if %guess% EQU %random% goto WINNER
:LOWER
cls
echo Your guess is lower than the number!
echo.
echo.
pause
goto HOME
:HIGHER
cls
echo Your guess is higher than the number!
echo.
echo.
pause
goto HOME
:WINNER
cls
echo -------------------------------
echo Congratulations, %name%! You guessed the number correctly!
echo.
echo.
echo The number was %number%
echo.
echo.
echo Press 1 to play again
echo.
echo Press 2 to quit
echo.
choice /C:12 /n
if errorlevel 2 goto MAIN
if errorlevel 1 goto QUIT
:QUIT
cls
echo Thanks for playing!
ping localhost -n 2 >nul
exit


See More: Batch file Number guessing game problem

Report •


#1
February 13, 2012 at 15:35:25
✔ Best Answer
Nice attempt, However there were some errors. Corrected them and removed few lines which can be removed without thinking much. The working version is below

Tip : %random:~0,2% will give you numbers below 100 so we can skip the check whether the random is lower then 100 or not

#1. you should be assigning set a random value to the variable which can be utilize later to find the winner. not using random itself as a variable.

#2. Check the line where you were assiging value to %web variable. you will get what was missing.
#3. Scan below one, i know you will catch the points.

============================================================
@echo off
:MAIN
cls
set number=%random:~0,2%


ping localhost -n 1 >nul
echo MISTER FOX'S NUMBER GUESSING GAME!
ping localhost -n 1 >nul
echo.
ping localhost -n 1 >nul
echo.
ping localhost -n 1 >nul
echo.
ping localhost -n 1 >nul
echo It's easy to play!
ping localhost -n 1 >nul
echo.
ping localhost -n 1 >nul
echo The computer generates a number between 1 and 100, and you try to guess it!
ping localhost -n 1 >nul
echo.
ping localhost -n 1 >nul
echo.
ping localhost -n 1 >nul
pause
cls
echo
echo.
echo.
echo.
echo "To begin the game, type your name and press Enter":
set /p web=
:HOME
cls
echo Well %web% Now GUESS THE NUMBER!
echo.
echo.
set /p guess=
echo
if %guess% LSS %number% goto LOWER
if %guess% GTR %number% goto HIGHER
if %guess% EQU %number% goto WINNER
:LOWER
cls
echo Your guess is lower than the number!
echo.
echo.
pause
goto HOME
:HIGHER
cls
echo Your guess is higher than the number!
echo.
echo.
pause
goto HOME
:WINNER
cls
echo -------------------------------
echo Congratulations, %name%! You guessed the number correctly!
echo.
echo.
echo The number was %number%
echo.
echo.
echo Press 1 to play again
echo.
echo Press 2 to quit
echo.
choice /C:12 /n
if errorlevel 2 goto MAIN
if errorlevel 1 goto QUIT
:QUIT
cls
echo Thanks for playing!
ping localhost -n 2 >nul
exit

===========================================================

- Subhash
 Monthly giveaway -  Must have Online safety Guide


Report •

#2
February 13, 2012 at 15:39:11
you can improve it further like this.

instead of

if %guess% LSS %number% goto LOWER

you can use this

if %guess% LSS %number% (
echo.
echo.
echo you guess is lower, try again
echo.
echo. )

- Subhash
 Monthly giveaway -  Must have Online safety Guide


Report •

#3
February 13, 2012 at 15:42:45
Your edited file works great, but there's one problem- the random number is always 16

Report •

Related Solutions

#4
February 13, 2012 at 16:22:55
Sorry about my post about the number always being 16.

It works fine. I guess the 16 thing was some MAJOR coincidence.

And now I know a lot more about batch, thanks!


Report •

#5
February 13, 2012 at 16:43:28
Cool. Now mind closing the question ?

- Subhash
 Monthly giveaway -  Must have Online safety Guide


Report •


Ask Question