Number-guessing game in Windows Batch Programming


Windows batch script is less powerful, at least in my opinion, than the bash script under linux environment. However, with the latest OS like Win7, using batch allows to do some more stuffs than before.

The IF, SET, GOTO are three most important keywords that can be used to create program flows in the batch script. The IF keyword can be used to compare the numbers, strings and even to check if a file or directory exists or not. The Set keyword can be used to assign a number, string or perform a mathematic evaluation. The GOTO keyword unconditionally jumps to a label, which can be used to simulate the while, for and other loops.

The script can be downloaded at https://github.com/DoctorLai/BatchUtils/blob/master/guess.bat and it is tested under WIN7 64.

To generate a random number in batch, you can use %RANDOM%, which returns a random integer number between 0 and 32767 (inclusive).

The following presents the batch script.

@echo off
rem a very simple number guessing game
rem to demonstrate some usage of windows batch programming
rem https://helloacm.com
rem 1-June-2012

setlocal
set /a curbest=99999

:newgame
  set /a cur=0
  set /a num=%RANDOM%%%100+1
  set left=1
  set right=100

  :repeat
    set /p guess=Please guess[%left%,%right%]:
    set /a cur=cur+1
    if %guess% EQU %num% (
      echo You have guessed %cur% times to get it right!
      if %cur% LSS %curbest% (
        set /a curbest=cur
      )
      goto ask
    )
    if %guess% LSS %num% (
      echo It is a bigger number!
      set /a left=%guess%+1
      goto repeat
    )
    echo It is a smaller number!
    set /a right=%guess%-1
    goto repeat

:ask
  choice /c:yn /n /m "Do you want to play again?"
  if %errorlevel% EQU 1 goto newgame

:bye
  echo Thank you! Bye!
  if not %curbest% EQU 99999 echo Best Guess = %curbest%

endlocal

The below shows the output of this batch script. It asks for a guess for the number and help you narrow down the range by each time printing a new guess range. The number of turns to guess the number is recorded and the fastest turn will be printed at the end of the game. At the end of each game, the user will be prompted if to play again.

The choice is an windows utility (.exe) that prompts the message and asks for a pre-defined input. The parameter /c is followed by the allowed input characters. The /n suppresses printing the choice list and /m allows customisation of the message. After the input, the keypressed value from the user is stored at %errorlevel being 1 for the first choice made, 2 for the second and so on.

guess Number-guessing game in Windows Batch Programming algorithms batch script beginner games implementation technical

It is often a good practise to include the setlocal and endlocal, between which, the variables created by set are only visible inside the script. They will be cleared automatically after the endlocal.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
588 words
Last Post: Codeforces: A. Life Without Zeros
Next Post: A Quick Performance Comparison on Languages at Codeforces

The Permanent URL is: Number-guessing game in Windows Batch Programming

Leave a Reply