score not echoing back

August 28, 2011 at 02:51:04
Specs: Windows Vista
I was writing a game in batch, and you have to guess a random number. (While I was making it, I forced it to print the random number for bug tracking.) when I request it to print score, it echoes ECHO is off, even though the random variable echoes back fine.

@echo off & setLocal EnableDelayedExpansion
set /a score==1

:gen
cls
for /L %%a in (1 1 100) do (
set random=!random!
)
echo %random%
pause
goto game
:game
cls
set/p input= guess the random number
if %input%==%random% goto win
if %input%==score echo %score%
goto wrong
:win
cls
echo Correct!
pause
goto score
:wrong
echo incorrect
set /a %score% lss =1
pause
goto gen
:score
set /a %score%+=1
goto gen


See More: score not echoing back

Report •


#1
August 28, 2011 at 03:21:41
set /a score==1

Should be:

set /a score=1

-----------------
for /L %%a in (1 1 100) do (
set random=!random!
)

Not only is this pointless, but you are making the "random" variable become static. For the rest of the entire script !random! will always be the same number.

In short don't set to the "random" variable, or any dynamic or system variable for that matter.

----------------------

set /a %score% lss =1

Two problems here, firstly you shouldn't expand a variable when you set to it, you are asking the command processor to set to a variable named with the contents of "score", not score itself. Finally "lss" means nothing to "set /a", it will just assume it is a variable name and expand it, if it's undefined it will default to zero.

set /a score-=1

------------
set /a %score%+=1

See above:

set /a score+=1

---------------------

Another note is input validation, it can be annoying:

:game
cls
set/p input=Guess the random number:  
if not defined input goto game
for /f "tokens=1* delims=SsCcOoRrEe0123456789" %%a in ("A0!input!") do (
    if not "%%b"=="" goto game
)
if /i %input%==score (
    echo Your score is: %score%
    pause
    goto game
)
for %%a in (s c o r e) do if defined input set input=!input:%%a=!
if not defined input goto game
if %input%==%random% goto win
goto wrong
:win


Report •

#2
August 28, 2011 at 08:56:20
That helps a lot, but I added a cheats section. If you type cheats, then it brings up a menu telling you that entering certain values (such as a,b,c) into an input will modify the game. I want one that follows the same batch format as above, and I want it to say :

Enter 9 to set score to 99999999
Enter h to get hints for the random number
Enter r to return to game
>

How could I achieve this?


Report •

#3
August 28, 2011 at 11:52:43
LOL I have made the exact same kind of program, except much more complex. It uses user scores saved from game to game and automatically saves your score. It also supports up to 3 players.

As far as yours, it is easy to make a menu like that:

ECHO choose an option:
ECHO Enter 9 to set score to 99999999
ECHO Enter h to get hints for the random number
ECHO Enter r to return to game
SET CHOICE=
SET /P CHOICE=
IF CHOICE==9 SET /A SCORE=999999
IF CHOICE==H ECHO %RANDOM%
IF CHOICE==h ECHO %RANDOM%
IF CHOICE==R GOTO GEN
IF CHOICE==r GOTO GEN

By the way, numbers in batch cannot get any higher than 999,999. No decimal numbers either.

Mouse could not be found. Click OK to continue.
What to do now?


Report •

Related Solutions

#4
August 28, 2011 at 12:39:59
@RMT2

A Signed 32 bit integer is -2147483648 to 2147483647 you will always get at least that from xp and up.


Report •

#5
August 28, 2011 at 12:45:12
@Judago:
Thanks for letting me know, didn't know that.

Mouse could not be found. Click OK to continue.
What to do now?


Report •

#6
August 29, 2011 at 05:02:41
@rmt2

I had already made the menu, but I need help with accessing the the command through the input validation that was set up. I have added the uppercase and lowercase letters to the possible input, but it says incorrect, press any key to continue instead of going to the menu. so now I have

set/p input=Guess the random number:
if not defined input goto game
for /f "tokens=1* delims=CcAaHhTtSsCcOoRrEe0123456789" %%a in ("A0!input!") do (
if not "%%b"=="" goto game
)
if /i %input%==score (
echo Your score is: %score%
pause
goto game
)
for %%a in (s c o r e) do if defined input set input=!input:%%a=!
if %input%==cheat goto cheated
if not defined input goto game
if %input%==%random% goto win
goto wrong

and the tag cheated has what you said I should use for the cheating GUI er, TUI (it was better than my setup).
I want it setup so that like in the code Judago gave me, it would look like this

if /i %input%==score (
echo Your score is: %score%
pause
goto game
)


Report •

#7
August 29, 2011 at 12:34:25
:game
cls
set/p input=Guess the random number:
if not defined input goto game
for /f "tokens=1* delims=AaHhTtSsCcOoRrEe0123456789" %%a in ("A0!input!") do (
if not "%%b"=="" goto game
)
if /i %input%==score (
echo Your score is: %score%
pause
goto game
)
if /i %input%==cheat goto cheated

for %%a in (s c o r e h a t) do if defined input set input=!input:%%a=!
if not defined input goto game
if %input%==%random% goto win
goto wrong


Report •


Ask Question