3
\$\begingroup\$

I have a fully working "Guess my number" game, in which the batch file will generate a random number between 0-whatever you choose. I got the idea from this thread, in which I had a rather hilarious conversation with a certain member. So I got bored tonight, and reworked the code a little, to show that member how this code could work to prove him correct, kind of. Any suggestions or ideas welcomed.

:: Batch file game for a "Guess my number".
:: Idea puled from https://stackoverflow.com/questions/33275807/whats-wrong-with-this-guess-my-number-batch-game
@ECHO OFF
COLOR 0A

:Start
TITLE Guess My Number
CLS
:: Allow user to select highest possible number to generate.
SET /P Highest=Highest possible number? 

:: Set a check value for further inspection.
:: Could easily set a default value for 'Highest', but what fun is that?
IF DEFINED Highest (
    SET /A CheckValue=Highest
) ELSE (
    GOTO :Start
)
:: Compare user input to valid input.
IF %Highest% EQU %CheckValue% (
:: Check for negative user input.
    IF %Highest% LEQ 0 (
        CLS
        ECHO Please enter a positive number.
        TIMEOUT>NUL /T 2
        GOTO :Start
    )
:: Check for user input that's greater than what %RANDOM% can provide.
:: Could easily generate a higher number, but why bother?
    IF %Highest% GTR 32767 (
        CLS
        ECHO Please enter a number lower than 32767
        TIMEOUT>NUL /T 2
        GOTO :Start
    )
) ELSE (
:: User inputted an invalid ending number.
    CLS
    ECHO Please enter a valid numeric value between "0-32767"
    TIMEOUT>NUL /T 2
    GOTO :Start
)
TITLE Guess My Number (0 to %Highest%)
:: This will generate a random number from 0-%Highest%.
SET /A Correct=%RANDOM%%%(%Highest%+1)
:: Set Attempts counter to 0.
SET Attempts=0

:GuessNow
:: Could set default values for 'Guess' & 'CheckValue' to avoid any confusion,
:: With future code, but eh, error handling's pretty much handled.
CLS &ECHO. &ECHO.
:: List last guess, if you've already guessed at-least once.
IF %Attempts% GTR 0 (
    ECHO Number of attempts: %Attempts%
    ECHO Last guess: %Guess% %HighLow%
    ECHO.
)
:: Accept user input for their guess.
SET /P Guess=What is your guess? (0-%Highest%): 
:: Set a check value for further inspection.
SET /A CheckValue=Guess
:: Increase attempts counter by 1.
SET /A Attempts+=1
CLS &ECHO. &ECHO.
:: Further inspection of the user guess versus the check value.
IF "%Guess%" EQU "%CheckValue%" (
:: Check for a negative user input.
    IF %CheckValue% LSS 0 (
        SET "HighLow=[Out of range - negative value]"
        ECHO My number is a positive value between 0-%Highest%.
        TIMEOUT>NUL /T 2
        GOTO :GuessNow
    )
:: Check for user input beyond what was originally set for the highest number.
    IF %CheckValue% GEQ 0 (
        IF %CheckValue% GTR %Highest% (
            SET "HighLow=[Out of range - above max]"
            ECHO My number is less than the maximum of %Highest%.
            TIMEOUT>NUL /T 2
            GOTO :GuessNow
        )
:: User input is higher than the correct answer.
        IF %CheckValue% GTR %Correct% (
            SET "HighLow=[TO HIGH]"
            ECHO My Number is less than %Guess%.
        )
:: User input is loswer than the correct answer.
        IF %CheckValue% LSS %Correct% (
            SET "HighLow=[TO LOW]"
            ECHO My Number is greater than %Guess%.
        )
:: User input matches the correct answer.
        IF %Correct% EQU %CheckValue% (
            ECHO CORRECT!
            ECHO You Guessed it in %Attempts% attempts.
            TIMEOUT>NUL /T 2 &GOTO :PlayAgain
        )
        TIMEOUT>NUL /T 2
        GOTO :GuessNow
    )
) ELSE (
:: User input is invalid.
    SET "HighLow=[INVALID]"
    ECHO Please enter a valid numeric value between "0-%Highest%"
    TIMEOUT>NUL /T 2
    GOTO :GuessNow
)
:PlayAgain
:: End of game, allow user to play again.
ECHO. &SET /P "PlayAgain=Would you like to play again? [Y/N] "
IF /I "%PlayAgain%" EQU "Y" (
    GOTO :Start
) ELSE (
    ECHO Thanks for playing!
    TIMEOUT>NUL /T 2
    GOTO :EOF
)
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.