Help with score system

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
KrunkJuice101
Posts: 3
Joined: 19 Sep 2014 20:11

Help with score system

#1 Post by KrunkJuice101 » 19 Sep 2014 20:34

The score wont update when the answer is correct and the points are added

@echo off
title Guess the number!
color 04
:start
cls
set /a number=(%random% %%15)+1
echo -------------------------------------------------------------------------------
echo GUESS THE NUMBER
echo -------------------------------------------------------------------------------
set /a score= 0
echo Score: %score%
echo %number%
echo.
echo - First attempt
echo.
set /P guess=- Your guess:
echo.
if "%guess%" NEQ "%number%" echo - You are incorrect!
if "%guess%" EQU "%number%" echo - You are correct!
if "%guess%" EQU "%number%" set /a score=score=%score%+10
if %guess% GTR %number% echo - Too high!
if %guess% LSS %number% echo - Too low!
pause
if "%guess%" NEQ "%number%" goto :second
if "%guess%" EQU "%number%" goto :start

:second
cls
echo -------------------------------------------------------------------------------
echo GUESS THE NUMBER
echo -------------------------------------------------------------------------------
echo Score: %score%
echo.
echo - Second attempt
echo.
set /P guess=Your guess:
echo.
if "%guess%" NEQ "%number%" echo - You are incorrect!
if "%guess%" EQU "%number%" echo - You are correct!
if "%guess%" GTR "%number%" echo - Too high!
if "%guess%" LSS "%number%" echo - Too low!
pause
if "%guess%" EQU "%number%" goto :start
if "%guess%" NEQ "%number%" goto :third

:third
cls
echo -------------------------------------------------------------------------------
echo GUESS THE NUMBER
echo -------------------------------------------------------------------------------
echo Score: %score%
echo.
echo - Final attempt
echo.
set /P guess=- Your guess:
echo.
if "%guess%" NEQ "%number%" echo - You are incorrect! You have no more attempts!
echo.
if "%guess%" NEQ "%number%" echo - The correct answer is %number%
if "%guess%" EQU "%number%" echo - You are correct!
pause
if "%guess%" EQU "%number%" goto :start
if "%guess%" NEQ "%number%" goto :start

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Help with score system

#2 Post by foxidrive » 19 Sep 2014 23:15

Change this

Code: Select all

if "%guess%" EQU "%number%" set /a score=score=%score%+10

to this

Code: Select all

if "%guess%" EQU "%number%" set /a score=score+10

KrunkJuice101
Posts: 3
Joined: 19 Sep 2014 20:11

Re: Help with score system

#3 Post by KrunkJuice101 » 20 Sep 2014 00:15

foxidrive wrote:Change this

Code: Select all

if "%guess%" EQU "%number%" set /a score=score=%score%+10

to this

Code: Select all

if "%guess%" EQU "%number%" set /a score=score+10


didnt work unfortunately

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Help with score system

#4 Post by foxidrive » 20 Sep 2014 00:30

KrunkJuice101 wrote:didnt work unfortunately


It certainly works to fix that syntax issue. :)

You really shouldn't initialise the score to zero before you print it though. ;)

tcpman
Posts: 53
Joined: 05 Mar 2014 15:01

Re: Help with score system

#5 Post by tcpman » 20 Sep 2014 07:17

ok i tested your code and now is works fine
foxdrive was correct bytheway!
also you can just use
set score=0
you dont need /a when you are giving a value to var without operation(+,-,%,...)

@echo off
title Guess the number!
color 04
set score= 0
:start
cls
set /a number=(%random% %%15)+1
echo -------------------------------------------------------------------------------
echo GUESS THE NUMBER
echo -------------------------------------------------------------------------------

echo Score: %score%
echo %number%
echo.
echo - First attempt
echo.
set /P guess=- Your guess:
echo.
if "%guess%" NEQ "%number%" echo - You are incorrect!
if "%guess%" EQU "%number%" echo - You are correct!
if "%guess%" EQU "%number%" set /a score=%score%+10
if %guess% GTR %number% echo - Too high!
if %guess% LSS %number% echo - Too low!
pause
if "%guess%" NEQ "%number%" goto :second
if "%guess%" EQU "%number%" goto :start

:second
cls
echo -------------------------------------------------------------------------------
echo GUESS THE NUMBER
echo -------------------------------------------------------------------------------
echo Score: %score%
echo.
echo - Second attempt
echo.
set /P guess=Your guess:
echo.
if "%guess%" NEQ "%number%" echo - You are incorrect!
if "%guess%" EQU "%number%" echo - You are correct!
if "%guess%" GTR "%number%" echo - Too high!
if "%guess%" LSS "%number%" echo - Too low!
pause
if "%guess%" EQU "%number%" goto :start
if "%guess%" NEQ "%number%" goto :third

:third
cls
echo -------------------------------------------------------------------------------
echo GUESS THE NUMBER
echo -------------------------------------------------------------------------------
echo Score: %score%
echo.
echo - Final attempt
echo.
set /P guess=- Your guess:
echo.
if "%guess%" NEQ "%number%" echo - You are incorrect! You have no more attempts!
echo.
if "%guess%" NEQ "%number%" echo - The correct answer is %number%
if "%guess%" EQU "%number%" echo - You are correct!
pause
if "%guess%" EQU "%number%" goto :start
if "%guess%" NEQ "%number%" goto :start

KrunkJuice101
Posts: 3
Joined: 19 Sep 2014 20:11

Re: Help with score system

#6 Post by KrunkJuice101 » 20 Sep 2014 08:28

Its still not working. I believe the score on the variable is updating, but it wont update on the top of the screen. It stays at 0 but when I echo it after the score it shows the 10 points were added. Another issue with is is the line to check if the number you guessed is too high or low. Sometimes it will say too high when I put like 4, then the answer ends up being 12.


tcpman wrote:ok i tested your code and now is works fine
foxdrive was correct bytheway!
also you can just use
set score=0
you dont need /a when you are giving a value to var without operation(+,-,%,...)

@echo off
title Guess the number!
color 04
set score= 0
:start
cls
set /a number=(%random% %%15)+1
echo -------------------------------------------------------------------------------
echo GUESS THE NUMBER
echo -------------------------------------------------------------------------------

echo Score: %score%
echo %number%
echo.
echo - First attempt
echo.
set /P guess=- Your guess:
echo.
if "%guess%" NEQ "%number%" echo - You are incorrect!
if "%guess%" EQU "%number%" echo - You are correct!
if "%guess%" EQU "%number%" set /a score=%score%+10
if %guess% GTR %number% echo - Too high!
if %guess% LSS %number% echo - Too low!
pause
if "%guess%" NEQ "%number%" goto :second
if "%guess%" EQU "%number%" goto :start

:second
cls
echo -------------------------------------------------------------------------------
echo GUESS THE NUMBER
echo -------------------------------------------------------------------------------
echo Score: %score%
echo.
echo - Second attempt
echo.
set /P guess=Your guess:
echo.
if "%guess%" NEQ "%number%" echo - You are incorrect!
if "%guess%" EQU "%number%" echo - You are correct!
if "%guess%" GTR "%number%" echo - Too high!
if "%guess%" LSS "%number%" echo - Too low!
pause
if "%guess%" EQU "%number%" goto :start
if "%guess%" NEQ "%number%" goto :third

:third
cls
echo -------------------------------------------------------------------------------
echo GUESS THE NUMBER
echo -------------------------------------------------------------------------------
echo Score: %score%
echo.
echo - Final attempt
echo.
set /P guess=- Your guess:
echo.
if "%guess%" NEQ "%number%" echo - You are incorrect! You have no more attempts!
echo.
if "%guess%" NEQ "%number%" echo - The correct answer is %number%
if "%guess%" EQU "%number%" echo - You are correct!
pause
if "%guess%" EQU "%number%" goto :start
if "%guess%" NEQ "%number%" goto :start

tcpman
Posts: 53
Joined: 05 Mar 2014 15:01

Re: Help with score system

#7 Post by tcpman » 20 Sep 2014 09:08

when we use set /a score=%score% + 10
we update the value of score
but the display update of score is only when we reach echo %score%
in each label at the top there is a echo %score% so when we reach the labels the score will be updated
but if you mean you want to update it Immediately when the score get a new value you cant do that!
but i think that is possible using multi threading batch files!

about the to high and to low problem looks like the problem is with second label

Code: Select all

@echo off
title Guess the number!
color 04
set score= 0
:start
cls
set /a number=(%random% %%15)+1
echo -------------------------------------------------------------------------------
echo GUESS THE NUMBER
echo -------------------------------------------------------------------------------

echo Score: %score%
echo %number%
echo.
echo - First attempt
echo.
set /P guess=- Your guess:
echo.
if "%guess%" NEQ "%number%" echo - You are incorrect!
if "%guess%" EQU "%number%" echo - You are correct!
if "%guess%" EQU "%number%" set /a score=%score%+10
if %guess% GTR %number% echo - Too high!
if %guess% LSS %number% echo - Too low!
pause
if "%guess%" NEQ "%number%" goto :second
if "%guess%" EQU "%number%" goto :start

:second
cls
echo -------------------------------------------------------------------------------
echo GUESS THE NUMBER
echo -------------------------------------------------------------------------------
echo Score: %score%
echo.
echo - Second attempt
echo.
set /P guess=Your guess:
echo.

if "%guess%" EQU "%number%" echo - You are correct! && set /a score=%score%+10
if %guess% GTR %number% echo - Too high! && echo - You are incorrect!
if %guess% LSS %number% echo - Too low!  && echo - You are incorrect!
pause
if "%guess%" EQU "%number%" goto :start
if "%guess%" NEQ "%number%" goto :third

:third
cls
echo -------------------------------------------------------------------------------
echo GUESS THE NUMBER
echo -------------------------------------------------------------------------------
echo Score: %score%
echo.
echo - Final attempt
echo.
set /P guess=- Your guess:
echo.
if "%guess%" NEQ "%number%" echo - You are incorrect! You have no more attempts!
echo.
if "%guess%" NEQ "%number%" echo - The correct answer is %number%
if "%guess%" EQU "%number%" echo - You are correct! && set /a score=%score%+10
pause
if "%guess%" EQU "%number%" goto :start
if "%guess%" NEQ "%number%" goto :start


your second problem might be solved now!

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Help with score system

#8 Post by foxidrive » 20 Sep 2014 14:01

Here's a method that uses a loop.

It depends on the user entering a number because there is still no error checking,
but it seems to be ok here.

Code: Select all

@echo off
title Guess the number!
color 04
set score=0
set ex=!
set tries=3
setlocal enabledelayedexpansion
:start
cls
set /a number=(%random% %% 15)+1
for /L %%a in (1,1,%tries%) do (
cls
echo -------------------------------------------------------------------------------
echo GUESS THE NUMBER
echo -------------------------------------------------------------------------------
echo Score: !score!
echo.
set /P "guess=%number% - attempt number %%a of %tries% - Your guess: "
echo.
if "!guess!" NEQ "%number%"   echo - You are incorrect!ex!
if "!guess!" EQU "%number%" ( echo - You are correct!ex!
                              set /a score=score+10
                              ping -n 3 localhost >nul
                              goto :start
                            )
                           
if !guess! GTR %number% echo - Too high!ex!
if !guess! LSS %number% echo - Too low!ex!
pause
)
if "%guess%" NEQ "%number%" ( echo - You are incorrect!ex! You have no more attempts!ex!
                              echo - The correct answer is %number%
                            )
ping -n 7 localhost >nul
pause



Post Reply