Is there a way to create Greater than equal to in BATCH - batch-file

Is there a way to make the system check if the value is Greater than or Equal to a certain number. I am trying to create an RPG and if the Health goes to 0 then I would like it to go to another section.
I have tried this
If "%playerhealth%"== "0' goto checkpoint
This is my whole code
:firstfight
color 04
echo FIGHT!!!
pause
cls
color 0F
echo Player %playerhealth% Enemy %foehealth%
echo --------
echo.
echo 1) FIGHT!!!
echo 2) QUIT :(
echo.
set /p battleoption=What will %name% do
if "%battleoption%"== "1" goto result1
if "%battleoption%"== "2" goto youlose
if "%foehealth%"== "0" goto youwin1
if %playerhealth% EQU 0 goto gameover
goto result1

IF %playerhealth% GEQ 0 goto :checkpoint
quotes enforces a string comparison so you need to remove them.
For more info check IF /?

Related

RPG game damage issues

I am trying to make an RPG style game and when I attack the enemy, it will do no damage and the enemy sets my health to -3, can someone help me?
#echo off
title RPG BETA
color 0b
set /p player=Welcome to Xero, what's your name?:
cls
echo Hello %player%!
pause
goto start
rem easteregg
if '%player%'=='Niko' goto win
if '%player%'=='nko' goto win
:start
echo In the distant future, the world was on the brink of destrucion.
timeout 3 /nobreak >nul
echo In the midst of the wasteland, a single man, named %player%, will overcome the odds
pause
goto BFight
:BFight
cls
set CPU=20
set CPUN=Scorpion
set gun=1
set gund=10
set playerh=20
:Fight
cls
if '%CPU%'=='0' goto win
if '%playerh%'=='0' goto lose
cls
echo You encounter a %CPUN%!
echo %CPUN% Health: %CPU%
echo.
echo Your Health: %playerh%
echo [1]Shoot (%gund%) (%gun%)
echo [2]Punch (3 damage)
echo [3]Flee
set /p fp=What do you do?
if 'fp'=='1' goto gun
if 'fp'=='2' goto punch
if 'fp'=='3' goto flee
:gun
if '%gun%'=='0' goto egun
set /a %gun%=-1
echo You fire at the %CPUN%
timeout 4 >nul
echo It hits!
set /a %CPU%=-%gund%
pause
goto cpufight
:egun
echo You have no bullets!
pause
goto Fight
:punch
echo You punch the %CPUN%
timeout 4 >nul
echo It hits!
set /a CPU=-3
pause
goto cpufight
:cpufight
echo The %CPUN% Attacks!
timeout 4
echo It hits!
set /a playerh= -3
pause
goto fight
:win
cls
echo Congradulations, %player%! You win!
pause
Note: the game is no quite finished, but if you play it you should be able to see how attacking doesn't work. I also have not programmed the flee option.
There are several minor issues with this code. I presume it's a homework question rather than something you have developed yourself?
Deducting damage values
There are issues when deducting values to show damage or gun bullets being used. Examples include:
set /a %gun%=-1
set /a %CPU%=-%gund%
set /a CPU=-3
The variables are named without the percentage symbols, so %GUN% should be named GUN when using SET. In addition, when performing arithmatic, the code x =- 1 will NOT subtract one. It will set x to equal -1.
The following are corrections of the above:
SET /a gun = gun - 1
SET /a CPU = CPU - gund
SET /a CPU = CPU - 3
Collecting input and comparing it
Another issue is with your 'command' section where you handle the command results in fp:
if 'fp' == '1' goto gun
if 'fp' == '2' goto punch
if 'fp' == '3' goto flee
This basically says if the text fp equals the text 1. Obviously that doesn't work. You need to modify this to use the value of the variable fp and the number (remove the '):
if %fp% == 1 goto gun
if %fp% == 2 goto punch
if %fp% == 3 goto flee
Winning the game
There is a final issue which is where you calculate the end game when either the player or CPU reaches 0 or lower health. You currently have:
if '%CPU%'=='0' goto win
if '%playerh%'=='0' goto lose
This is again comparing the text CPU or playerh to the textual value of 0. Remove the ' characters:
if %CPU% == 0 goto win
if %playerh% == 0 goto lose
However, this will only work if the health reaches 0. If the health goes below 0 then it will never happen. You need to look for 0 or below:
if %CPU% LEQ 0 goto win
if %playerh% LEQ 0 goto lose

How To Set Variable Value In If

Hi I make simple batch file now what I tried to add correct answers number at the end but it keeps saying zero because the variable values are not changing when the answers are chosen. Here is my code below
#echo off
title Game One
color 1f
::#############################
:one
set correctn=0
set correctn2=0
cls
echo What is 2 + 2?
echo.
echo.
echo A) 6
echo B) 4
echo C) 49
echo D) 17
echo.
echo.
echo Type the correct answer.
set /p ch1=
echo.
echo.
if not defined ch1 (goto one)
if %ch1%==A goto no
if %ch1%==A correctn=0
if %ch1%==B goto yes
if %ch1%==B correctn=1
if %ch1%==C goto no
if %ch1%==C correctn=0
if %ch1%==D goto no
if %ch1%==D correctn=0
pause>null
::#########################################
:no
cls
echo Sorry, that answer is incorrect.
echo.
echo.
echo The correct choice was B, which was 4.
pause>null
goto two
::#########################################
:yes
cls
echo You are correct. Congratulations.
echo Press any key to continue.
pause>null
goto two
::##########################################
:two
cls
echo What is 100 divided by 2?
echo A) 45
echo B) 50
echo C) 90
echo D) 17
echo.
echo.
set/p ch2=
echo.
echo.
if not defined ch2 (goto two)
if %ch2%==A goto no2
if %ch2%==A correctn2=0
if %ch2%==B goto yes2
if %ch2%==B correctn2=1
if %ch2%==C goto no2
if %ch2%==C correctn2=0
if %ch2%==D goto no2
if %ch2%==D correctn2=0
echo Invalid Choice, Please Try Again!
pause>null
::#################################
:no2
cls
echo Sorry, that answer is incorrect.
echo.
echo.
echo The correct choice was B, which was 50.
pause>null
::########################################
:yes2
cls
echo You are correct. Congratulations.
echo Press any key to continue.
pause>null
goto end
::######################################
:end
set/a correct=%correctn% + %correctn2%
echo Number of answers correct was %correct%
pause>null
So how to change variable value in if statement if already variable exists?
You need to set a variable first and then goto. As currently written, your script goes to a label yes first. Hence, if %ch1%==B set correctn=1 line is never reached:
if %ch1%==B goto yes
if %ch1%==B set correctn=1
if %ch1%==B goto yes
Moreover, IF command string comparison is case sensitive without /I switch; try the following:
if /I %ch1%==B (set correctn=1&goto yes)
I'd suggest using (Windows native) CHOICE.EXE instead of set /p for a single key-press user input, e.g as follows:
CHOICE /C abcd
IF %errorlevel%==2 (
set correctn=1
goto yes
) else (
set correctn=0
goto no
)

Check if a string variable is empty in batch script

I am trying to write a batch script to get a string in a variable and check if it is empty, and if it is empty then it is directed to a loop. The below code represents the problem
:loop
set /p cide=
IF NOT "a%1"=="a" (set cide="%1")
IF [%1]==[] goto loop
ELSE
echo IDE entered
TIMEOUT 5 > NUL
The program starts to loop again even if i give a string.
I tried to put IF [%cide%]==[] goto loop or IF %cide%==[] goto loop it gave an error stating "ELSE" not recognized.
Any help is appreciated. Thanks
You can try something like that :
#echo off
:loop
cls & Color 0A
echo Type what you want !
set /p "cide="
IF "%cide%"=="" (
Cls & Color 0C
echo You must enter something
Timeout /T 2 /NoBreak>nul
goto loop
) ELSE (
Goto Next
)
:Next
Cls
echo %cide% is entered
pause
#echo off
:loop
cls & Color 0A
echo Type what you want !
set /p "cide="
IF [%cide%]==[] (
Cls & Color 0C
echo You must enter something
choice /d y /t 2 > nul
goto loop
) ELSE (
Goto Next
)
:Next
Cls
echo %cide% is entered
pause

Switch statement equivalent in Windows batch file

I wonder if there is a simple way to branch execution in a Windows batch file depending on the value of one single expression. Something akin to switch/case blocks in C, C++, C#, Java, JavaScript, PHP, and other real programming languages.
My only workaround is a plain if/else block where the same expression is repeatedly checked for equality against different values:
IF "%ID%"=="0" (
REM do something
) ELSE IF "%ID%"=="1" (
REM do something else
) ELSE IF "%ID%"=="2" (
REM do another thing
) ELSE (
REM default case...
)
So dumb. Is there a better solution?
I ended up using label names containing the values for the case expressions as suggested by AjV Jsy. Anyway, I use CALL instead of GOTO to jump into the correct case block and GOTO :EOF to jump back. The following sample code is a complete batch script illustrating the idea.
#ECHO OFF
SET /P COLOR="Choose a background color (type red, blue or black): "
2>NUL CALL :CASE_%COLOR% # jump to :CASE_red, :CASE_blue, etc.
IF ERRORLEVEL 1 CALL :DEFAULT_CASE # If label doesn't exist
ECHO Done.
EXIT /B
:CASE_red
COLOR CF
GOTO END_CASE
:CASE_blue
COLOR 9F
GOTO END_CASE
:CASE_black
COLOR 0F
GOTO END_CASE
:DEFAULT_CASE
ECHO Unknown color "%COLOR%"
GOTO END_CASE
:END_CASE
VER > NUL # reset ERRORLEVEL
GOTO :EOF # return from CALL
This is simpler to read:
IF "%ID%"=="0" REM do something
IF "%ID%"=="1" REM do something else
IF "%ID%"=="2" REM do another thing
IF %ID% GTR 2 REM default case...
Compact form for short commands (no 'echo'):
IF "%ID%"=="0" ( ... & ... & ... ) ELSE ^
IF "%ID%"=="1" ( ... ) ELSE ^
IF "%ID%"=="2" ( ... ) ELSE ^
REM default case...
After ^ must be an immediate line end, no spaces.
I guess all other options would be more cryptic. For those who like readable and non-cryptic code:
IF "%ID%"=="0" (
REM do something
) ELSE IF "%ID%"=="1" (
REM do something else
) ELSE IF "%ID%"=="2" (
REM do another thing
) ELSE (
REM default case...
)
It's like an anecdote:
Magician: Put the egg under the hat, do the magic passes ... Remove the hat and ... get the same egg but in the side view ...
The IF ELSE solution isn't that bad. It's almost as good as python's if elif else. More cryptic 'eggs' can be found here.
I searched switch / case in batch files today and stumbled upon this. I used this solution and extended it with a goto exit.
IF "%1"=="red" echo "one selected" & goto exit
IF "%1"=="two" echo "two selected" & goto exit
...
echo "Options: [one | two | ...]
:exit
Which brings in the default state (echo line) and no extra if's when the choice is found.
Hariprasad didupe suggested a solution provided by Batchography, but it could be improved a bit. Unlike with other cases getting into default case will set ERRORLEVEL to 1 and, if that is not desired, you should manually set ERRORLEVEL to 0:
goto :switch-case-N-%N% 2>nul || (
rem Default case
rem Manually set ERRORLEVEL to 0
type nul>nul
echo Something else
)
...
The readability could be improved for the price of a call overhead:
call:Switch SwitchLabel %N% || (
:SwitchLabel-1
echo One
goto:EOF
:SwitchLabel-2
echo Two
goto:EOF
:SwitchLabel-3
echo Three
goto:EOF
:SwitchLabel-
echo Default case
)
:Switch
goto:%1-%2 2>nul || (
type nul>nul
goto:%1-
)
exit /b
Few things to note:
As stated before, this has a call overhead;
Default case is required. If no action is needed put rem inside to
avoid parenthesis error;
All cases except the default one are executed in the sub-context. If
you want to exit parent context (usually script) you may use this;
Default case is executed in a parent context, so it cannot be
combined with other cases (as reaching goto:EOF will exit parent
context). This could be circumvented by replacing goto:%1- in
subroutine with call:%1- for the price of additional call overhead;
Subroutine takes label prefix (sans hyphen) and control variable. Without label
prefix switch will look for labels with :- prefix (which are valid) and
not passing a control variable will lead to default case.
Try by this way. To perform some list of operations like
Switch case has been used.
Checking the conditional statements.
Invoking the function with more than two arguments.
#echo off
:Start2
cls
goto Start
:Start
echo --------------------------------------
echo Welcome to the Shortcut tool
echo --------------------------------------
echo Choose from the list given below:
echo [1] 2017
echo [2] 2018
echo [3] Task
set /a one=1
set /a two=2
set /a three=3
set /a four=4
set input=
set /p input= Enter your choice:
if %input% equ %one% goto Z if NOT goto Start2
if %input% equ %two% goto X if NOT goto Start2
if %input% equ %three% goto C if NOT goto Start2
if %input% geq %four% goto N
:Z
cls
echo You have selected year : 2017
set year=2017
echo %year%
call:branches year
pause
exit
:X
cls
echo You have selected year : 2018
set year=2018
echo %year%
call:branches year
pause
exit
:C
cls
echo You have selected Task
call:Task
pause
exit
:N
cls
echo Invalid Selection! Try again
pause
goto :start2
:branches
cls
echo Choose from the list of Branches given below:
echo [1] January
echo [2] Feburary
echo [3] March
SETLOCAL
set /a "Number1=%~1"
set input=
set /p input= Enter your choice:
set /a b=0
set /a bd=3
set /a bdd=4
if %input% equ %b% goto N
if %input% leq %bd% call:Z1 Number1,input if NOT goto Start2
if %input% geq %bdd% goto N
:Z1
cls
SETLOCAL
set /a "Number1=%~1"
echo year = %Number1%
set /a "Number2=%~2"
echo branch = %Number2%
call:operation Number1,Number2
pause
GOTO :EOF
:operation
cls
echo Choose from the list of Operation given below:
echo [1] UB
echo [3] B
echo [4] C
echo [5] l
echo [6] R
echo [7] JT
echo [8] CT
echo [9] JT
SETLOCAL
set /a "year=%~1"
echo Your have selected year = %year%
set /a "month=%~2"
echo You have selected Branch = %month%
set operation=
set /p operation= Enter your choice:
set /a b=0
set /a bd=9
set /a bdd=10
if %input% equ %b% goto N
if %operation% leq %bd% goto :switch-case-N-%operation% if NOT goto Start2
if %input% geq %bdd% goto N
:switch-case-N-1
echo Januray
echo %year%,%month%,%operation%
goto :switch-case-end
:switch-case-N-2
echo Feburary
echo %year%,%month%,%operation%
goto :switch-case-end
:switch-case-N-3
echo march
echo %year%,%month%,%operation%
goto :switch-case-end
:switch-case-end
echo Task Completed
pause
exit
goto :start2
:Task
cls
echo Choose from the list of Operation given below:
echo [1] UB
echo [3] B
echo [4] C
echo [5] l
echo [6] R
echo [7] JT
echo [8] CT
echo [9] JT
SETLOCAL
set operation=
set /p operation= Enter your choice:
set /a b=0
set /a bd=9
set /a bdd=10
if %input% equ %b% goto N
if %operation% leq %bd% goto :switch-case-N-%operation% if NOT goto Start2
if %input% geq %bdd% goto N
:switch-case-N-1
echo Januray
echo %operation%
goto :switch-case-end
:switch-case-N-2
echo Feburary
echo %year%,%month%,%operation%
goto :switch-case-end
:switch-case-N-3
echo march
echo %year%,%month%,%operation%
goto :switch-case-end
:switch-case-end
echo Task Completed
pause
exit
goto :start2
If if is not working you use:
:switch case %n%=1
statements;
goto :switch case end
etc..
http://lallouslab.net/2016/12/21/batchography-switch-case/
It might be a bit late, but this does it:
set "case1=operation1"
set "case2=operation2"
set "case3=operation3"
setlocal EnableDelayedExpansion
!%switch%!
endlocal
%switch% gets replaced before line execution. Serious downsides:
You override the case variables
It needs DelayedExpansion
Might eventually be usefull in some cases.

If number is at certain range, do (command here)

I would like to ask you a question.
I am creating a simple program that scans numbers (people's grades) and checks if they are at a certain range (say, 75% to 90%) and if they are at a certain range, do the following command; here's the code.
(More text below the code)
#echo off
color a
title Happy Factor Decoder
echo Hello!
set /p eg="Exam Grade (RAW): "
set /p teg="TOTAL RAW Exam Grade (The highest score): "
echo Calculating
set /a m=%teg% - %eg%
echo You had %m% mistakes
echo Breaking down...
timeout /t 1 >nul
set /a bdf1=%eg% / 4
echo %bdf1%
set /a bdf2=%teg% / 4
echo %bdf2%
set /a bdf3=%m% / 4
echo %bdf3%
echo I BROKE IT DOWN YEAH :D
if %eg% == 4 goto happy
if %eg% == 3 goto kindahappy
if %eg% == 2 goto kindasad
if %eg% == 1 goto sad
:happy
echo Your father will be happy about this
pause
:kindahappy
echo Your father will be KINDA happy about this
pause
:kindasad
echo Your father will be KINDA sad about this
pause
:sad
echo Your father will be sad about this
pause
You see, What i want it to do is this (in pseudocode)
IF BDF1 IS AT CERTAIN RANGE (80-90) GOTO HAPPY
Any ideas?
One number between minand max is just number>=max AND number<=min, right???
IF %BDF1% GEQ 80 IF %BFD1% LEQ 90 GOTO :HAPPY
I don't know the units of your calculations, but you can check a range from lowest to high:
#ECHO OFF &SETLOCAL
REM some calculation here
IF %BDF1% leq 25 GOTO :sad
IF %BDF1% leq 50 GOTO :kindasad
IF %BDF1% leq 75 GOTO :kindahappy
IF %BDF1% leq 100 GOTO :happy
ECHO uups!
goto:eof
:sad
ECHO sad
goto:eof
:kindasad
ECHO kindased
goto:eof
:kindahappy
ECHO kindahappy
goto:eof
:happy
REM please enter your code here :)

Resources