0

I am creating a typing game for my office for whoever would like to try and improve their numberpad typing skill. My code keeps score, if the input is correct or incorrect, and with help from the stackoverflow community (the great Compo), my code has been improved. now I am trying to add difficulty options for the user. but I seem to be having a problem making the batch reference back to the entered difficulty and following the correct difficulty option. I know I can use IF and ELSE but im still getting used to how batch works.

version 1 of code before adding difficulty

@ECHO OFF

SET/A "correct=incorrect=0"

:START
CLS
COLOR 07
SET "num=%RANDOM%"
ECHO.
ECHO SCORE - CORRECT %correct%, INCORRECT %incorrect%
ECHO.
ECHO %num%
ECHO.
SET /P "INP= "
ECHO.
IF "%INP%"=="%num%" (
    COLOR 27
    ECHO CORRECT
    SET/A "correct+=1"
    PING -n 2 0.0.0.0 1>NUL
) ELSE (
    COLOR 47
    ECHO INCORRECT
    SET/A "incorrect+=1"
    PING -n 2 0.0.0.0 1>NUL
)
GOTO START

version 1 of code with difficulty option

@ECHO OFF

SET/A "correct=incorrect=0"

:STARTDIFF
ECHO DIFFICULTY?
ECHO.
ECHO EASY
ECHO HARD
ECHO.
SET /P "DIFF= "
IF "%DIFF%" EQU EASY GOTO STARTEASY
IF "%DIFF%" EQU HARD GOTO STARTHARD
::IF "%DIFF%" NEQ EASY GOTO STARTDIFF

: STARTEASY
CLS
COLOR 07
SET "num=%RANDOM%"
GOTO TEST

: STARTHARD
CLS
COLOR 07
SET "num=%RANDOM%%RANDOM%"
GOTO TEST

: TEST
ECHO.
ECHO SCORE - CORRECT %correct%, INCORRECT %incorrect%
ECHO.
ECHO %num%
ECHO.
SET /P "INP= "
ECHO.
IF "%INP%"=="%num%" (
    COLOR 27
    ECHO CORRECT
    SET/A "correct+=1"
    PING -n 2 0.0.0.0 1>NUL
) ELSE (
    COLOR 47
    ECHO INCORRECT
    SET/A "incorrect+=1"
    PING -n 2 0.0.0.0 1>NUL
)

ECHO %DIFF%>NUL
IF "%DIFF%" EQU EASY GOTO STARTEASY
IF "%DIFF%" EQU HARD GOTO STARTHARD
::IF "%DIFF%" NEQ EASY GOTO STARTDIFF

::GOTO START

how do tell the code to reference back to the set difficulty from the beginning and continue using that difficulty?

My apologies, I wish I new the correct terminology to better explain what I'm trying to do.

Slovakolarik
  • 17
  • 1
  • 5

3 Answers3

0

Try to run this code, got some little modification.

@echo off

SET/A "correct=incorrect=0"

:STARTDIFF
ECHO DIFFICULTY?
ECHO.
ECHO EASY
ECHO HARD
ECHO.
SET /P "DIFF= "
IF "%DIFF%" EQU EASY GOTO STARTEASY
IF "%DIFF%" EQU HARD GOTO STARTHARD
::IF "%DIFF%" NEQ EASY GOTO STARTDIFF

:STARTEASY
SET X="EASY"
CLS
COLOR 07
SET "num=%RANDOM%"
GOTO TEST

:STARTHARD
SET X="HARD"
CLS
COLOR 07
SET "num=%RANDOM%%RANDOM%"


:TEST
ECHO.
ECHO SCORE - CORRECT %correct%, INCORRECT %incorrect%
ECHO.
ECHO %num%
ECHO.
SET /P "INP= "
ECHO.
IF "%INP%"=="%num%" (
    COLOR 27
    ECHO CORRECT
    SET/A "correct+=1"
    PING -n 2 0.0.0.0 1>NUL
) ELSE (
    COLOR 47
    ECHO INCORRECT
    SET/A "incorrect+=1"
    PING -n 2 0.0.0.0 1>NUL
)
IF %X%=="EASY" GOTO STARTEASY
IF  %X%=="HARD" GOTO STARTHARD
pause
Regie Baguio
  • 241
  • 5
  • 13
  • i tried running this on my office windows xp, i know make your jokes, but whether i first input hard or easy, it still has the typical number count of easy – Slovakolarik Apr 27 '17 at 14:04
0

if is very literal, and the two arguments must be exactly the same to be declared equal.

Hence, for instance

IF "%DIFF%" EQU EASY GOTO STARTEASY

should be

IF "%DIFF%" EQU "EASY" GOTO STARTEASY

or better,

IF /I "%DIFF%" EQU "EASY" GOTO STARTEASY

which makes the comparison case-insensitive.

Note however that since you are assigning a quoted-string to the variable x, that comparison is correct.

Perhaps you should learn about subroutines.

:STARTEASY
CLS
COLOR 07
SET "num=%RANDOM%"
call :TEST
goto starteasy

Then remove the last 2 if statements in the routine :test and follow the pause (which is temporary for debugging, no doubt) with a goto :eof line. This causes the routine to return to the statement after the call.

Actually, the goto :eof is not strictly required, just good practice so that if you add more routines, you don't get flow-through from one to the next.

Note that the colons are important. call :somelabel executes a routine at the label somelabel within this batch file. call somelabel (with no colon) executes an external executable named somelabel.

The : in :eof is also important - it is understood by cmd to mean "end-of-this-file"

Magoo
  • 77,302
  • 8
  • 62
  • 84
0

I'm not sure why you decided to completely change the structure back to a series of GOTO's:

Try this:

@ECHO OFF

ECHO DIFFICULTY?
ECHO.
ECHO 1. EASY
ECHO 2. HARD
ECHO.
CHOICE /C 12
SET "LEVEL=%ERRORLEVEL%"
SET/A "correct=incorrect=0"

:START
CLS
COLOR 07
IF "%LEVEL%"=="2" (Set "num=%RANDOM%%RANDOM%") ELSE SET "num=%RANDOM%" 
ECHO.
ECHO SCORE - CORRECT %correct%, INCORRECT %incorrect%
ECHO.
ECHO %num%
ECHO.
SET/P "INP= "
ECHO.
IF "%INP%"=="%num%" (
    COLOR 27
    ECHO CORRECT
    SET/A "correct+=1"
    TIMEOUT 1 1>NUL
) ELSE (
    COLOR 47
    ECHO INCORRECT
    SET/A "incorrect+=1"
    TIMEOUT 1 1>NUL
)
GOTO START
Compo
  • 36,585
  • 5
  • 27
  • 39
  • i seem to have been trying to learn chinese from a fortune cookie. I tried learning only what i thought i needed to know from forums and articls, i definitely have major learning to do. I cant seem to wrap my head around if, else, and if else. I know getting rid of goto will make my life easier when coding these things. I cant thank you enough for the help, – Slovakolarik Apr 27 '17 at 14:06
  • i tried running the code but there is chance to choose the difficulty, it goes straight to the number generated by %random% – Slovakolarik Apr 27 '17 at 14:14