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

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 :)

Related

Convert number to different number in batch file

This must be very basic but i can't seem to find a way to get this done
my batch script goes like this:
#echo off
echo Type In Desired Volume And Press Enter
echo.
echo 0 = 0 %%
echo 1 = 100 %%
echo 0.10 = 10 %%
echo 0.65 = 65 %%
echo.
set /p input=
echo %input% > "C:\SetVol\Source\Volume.txt
I want to make it more user friendly by letting the user input a number between 0 and 100 instead of, for example, 0.10 for 10% audio volume. But i still need to output the 0.10 to a textfile if the user enters 10.
Google appears to longer be my best friend and we cant seem to communicate on this.
If anybody could help me get starter that would be great.
A simple way is to convert the input number to the requested output format.
One step is to prefix the input with 0.
set /p input=[Enter volume in %%]:
set "output=0.%input%"
echo %input% > "C:\SetVol\Source\Volume.txt
But this would fails for one digit values like 2%, becomes 0.2 instead of 0.02.
This can be fixed with prefix each number with 00 and take the last three digits and add a dot between.
set /p input=[Enter volume in %%]:
set "temp=00%input%"
set "output=%temp:~-3,1%.%temp:~-2%"
echo %output% > "C:\SetVol\Source\Volume.txt
You can use choice in a loop to make a key based 'slider', and then modifiy the variable value to include a 0. prefix or be 1 using an if condition:
#Echo off
set "volume=50"
:Volume
cls
Echo( Current volume: %Volume%%% [I]ncrease [D]ecrease [C]ontinue
For /f "delims=" %%G in ('Choice /N /C:IDC')Do (
If "%%G"=="I" If not %Volume% GEQ 100 Set /A Volume+=1
If "%%G"=="D" If not %Volume% LEQ 0 Set /A Volume-=1
If not "%%G"=="C" Goto :Volume
)
IF %volume% Equ 100 ( Set "Volume=1" )Else If %volume% LSS 10 (
Set "Volume=0.0%Volume%"
) Else Set "Volume=0.%Volume%"
:#your script here
For NTFS systems, a variant that stores the last set volume in an alternate data stream and reasigns the last value on return:
#Echo off
set "volume=50"
For /f "Usebackq delims=" %%G in ("%~f0:Volume")Do Set "%%G"
:Volume
cls
Echo( Current volume: %Volume%%% [I]ncrease [D]ecrease [C]ontinue
For /f "delims=" %%G in ('Choice /N /C:IDC')Do (
If "%%G"=="I" If not %Volume% GEQ 100 Set /A Volume+=1
If "%%G"=="D" If not %Volume% LEQ 0 Set /A Volume-=1
If not "%%G"=="C" Goto :Volume
)
Set Volume >"%~f0:Volume"
IF %volume% Equ 100 ( Set "Volume=1" )Else If %volume% LSS 10 (
Set "Volume=0.0%Volume%"
) Else Set "Volume=0.%Volume%"
:#your script here
Note: by using this method of input, invalid input cannot be entered.
Figured out a way to use the choice input, if anyone has a more neat or different way to do this please let me know... 291 more lines to edit...
#echo off
echo Choose Audio Volume 0-100 %%
:choice
set /P c=
if /I "%c%" EQU "0" goto :0
if /I "%c%" EQU "1" goto :1
if /I "%c%" EQU "2" goto :2
goto :choice
:0
echo 0 > C:\users\%username%\desktop\numbertest.txt
echo You chose %c% %%
pause
exit
:1
echo 0.01 > C:\users\%username%\desktop\numbertest.txt
echo You chose %c% %%
pause
exit
:2
echo 0.02 > C:\users\%username%\desktop\numbertest.txt
echo You chose %c% %%
pause
exit

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

Getting goto was unexpected at the time

I am currently making a game that has a persuasion system in it. I had all the code for one of the systems set up, but then I set up 2 more, and it started give me an error that said '(number) was not expected at this time'. when I put in 2 for the second choice, and 3 for the 3rd choice.
The code is like this.
#echo off
SETLOCAL EnableDelayedExpansion
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
set "DEL=%%a"
)
set name=Quantum
cls
color 0a
Echo King Redwood: So 2000?
pause >nul
echo.
call :colorText 09 "1. 2500"
echo.
call :colorText 0e "2. 3000"
echo.
call :colorText 0c "3. 4000"
echo.
echo 4. 2000
echo.
set /p "purs=Enter:"
if /i %purs% == 1 (
goto CheckB )
if /i %purs% == 2 (
goto CheckY )
if /i %purs% == 3 (
goto CheckR )
if /i %purs% == 4 (
goto Convo )
:CheckB
set bleu=%random:~-2,1%
if %bleu% GTR 10 (
goto CheckB )
if %bleu% LSS 0 (
goto CheckB )
set /a num = 3
set /a reward = 2500
goto Res
:CheckY
set Yel=%random:~-2,1%
if %Yel% GTR 10 (
goto CheckY )
if %Yel% LSS 0 (
goto CheckY )
set /a num = 5
set reward = 3000
goto Res
:CheckR
set red=%random:~-2,1%
if %red% GTR 10 (
goto CheckB )
if %red% LSS 0 (
goto CheckB )
set /a num = 7
set /a reward = 4000
goto Res
:Convo
set /a reward = 2000
Echo %name%: I think that is a reasonable price.
Echo King Redwood: Very well.
Echo King Redwood: We will now take you to make sure you are
echo ready.
pause >nul
:Res
if %bleu% GEQ %num% goto Succeed
if NOT %bleu% GEQ %num% goto Fail
:Succeed
Echo %name%: I think that the struggles for such a long trip will be more then that
Echo %name%: How about %reward%?
Echo King Redwod: OK %reward% will work.
pause >nul
goto end
:Fail
Echo %name%: I think that you can give me %reward%.
Echo %name%: You know, for the struggles that there will be along the way.
echo If 2000 isn't good enough for you, I'll just have someone else do it.
pause >nul
:end
exit
:colorText
echo off
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1i
First, make sure to close the FOR loop by putting a ) before :CheckB.
For the 'was not expected at this time' error, you're sometimes comparing an empty variable to something. For example, by following CheckY, you set Yel, then proceed to Res and check Bleu, which is empty because it hasn't been set. You're putting nothing next to the binary GEQ operator, and that's why it's complaining.
Tip: to debug, try inserting ECHO statements like this:
:Res
echo bleu=%bleu%,num=%num%
Another problem: when using SET, do not surround the = by spaces. SET /a will work with spaces around =, just because of the nature of /a, but plain SET will not. Well, it will append your variable name with a space and prepend your value with a space, which is not what you want.
Another tip: you can constrain what RANDOM returns through SET /a and the modulus operator, like this.
SET /a red=%random% %% 11
This will set red to a number between 0 and 10, so there is no need for the substrings and goto routines you're using after picking your random number.
Also, consider using EXIT /b to exit the batch file and not the whole CMD environment.

Batch file game trouble, missing operator error

I've been working on a batch file game for some time, but I am having trouble with the shop I am making. When I run the script and try to buy something in the shop it gives me "missing operator" and then displays the message for buying the item, but doesn't add the item to the player's inventory. Heres the code. Any help is much appreciated.
:w_shop
title Nova: In the Weapon Shop
:sword_screen
cls
echo.
echo You have %money% gold.
echo.
echo What will you buy?
echo 1) Wooden Sword: 500 gold
echo You own %sword1%
echo.
echo 2) Stone Sword: 1000 gold
echo You own %sword2%
echo.
echo 3) Iron Blade: 5000 gold
echo You own %sword3%
echo.
echo 4) Mythril Sabre: 7500 gold
echo You own %sword4%
echo.
echo 5) Mythril Longsword: 10000 gold
echo You own %sword5%
echo.
echo 6) Next
echo.
echo 7) Leave Shop
set/p sword_screen=
if %sword_screen% LEQ 0 goto sword_screen
if %sword_screen% GEQ 8 goto sword_screen
if %sword_screen% EQU 1 goto buy_sword1
if %sword_screen% EQU 2 goto buy_sword2
if %sword_screen% EQU 3 goto buy_sword3
if %sword_screen% EQU 4 goto buy_sword4
if %sword_screen% EQU 5 goto buy_sword5
if %sword_screen% EQU 6 goto gauntlet_screen
if %sword_screen% EQU 7 goto main_menu
:buy_sword1
set price=500
set att=100
if %money% LSS %price% goto lack_funds
set/a money=%money%-%price%
set/a %sword1%=%sword1%+1
echo.
echo You bought a Wooden Sword. This weapon has %att% attack.
pause>nul
goto sword_screen
:buy_sword2
set price=1000
set att=150
if %money% LSS %price% goto lack_funds
set /a money=%money%-%price%
set /a %sword2%=%sword2%+1
echo.
echo You bought a Stone Sword. This weapon has %att% attack.
pause>nul
goto sword_screen
:buy_sword3
set price=5000
set att=300
if %money% LSS %price% goto lack_funds
set /a money=%money%-%price%
set /a %sword3%=%sword3%+1
echo.
echo You bought a Iron Blade. This weapon has %att% attack.
pause>nul
goto sword_screen
:buy_sword4
set price=7500
set att=500
if %money% LSS %price% goto lack_funds
set /a money=%money%-%price%
set /a %sword4%=%sword4%+1
echo.
echo You bought a Mythril Sabre. This weapon has %att% attack.
pause>nul
goto sword_screen
:buy_sword5
set /a price=10000
set /a att=1000
if %money% LSS %price% goto lack_funds
set /a money=%money%-%price%
set /a %sword5%=%sword5%+1
echo.
echo You bought a Mythril Longsword. This weapon has %att% attack.
pause>nul
goto sword_screen
Just to be clear, the sword variables and the money variable are declared earlier on in the code
since you did not define money nor sword variables the lines
if %money% LSS %price% goto lack_funds
set/a %sword1%=%sword1%+1
expand to
if LSS 500 goto lack_funds
set/a =+1
which is wrong. Those are common mistakes. change the line like this:
if [%money%] LSS [%price%] goto lack_funds
set/a sword1=0%sword1%+1
Note that the variables are surrounded by square brackets so even if the variable is undefinde you'll get at least a valid line
Note that the left-param in the set-line must not have the %-signs and the right-param has an additional 0 so that if the variable is undefined you'll get 0+1 which is valid
btw, there is not lack_funds lable in your code snippet and also i recommend adding
echo off
set money=5000
at the beginning of the script

Batch file writes wrong data onto file

I'm a programmer in batch and I've just joined this site to ask 1 question...
I made this program that writes random numbers from 1 to 10 and encodes/writes them into a file... The problem is, ANOTHER Batch file must read the numbers and check if it's below 5.
The batch file that generates random numbers is not inputting a random number into the file;
Instead, the file says
ECHO is off
Which confuses the other batch file, and therefore it crashes.
Here's the code of both batch files;
The "Writer" of the numbers
#echo off >nul
title Batch Arithmetic Communicator
:check
timeout /t 1 >nul /nobreak
echo %t% >>wait.rsm
SET /A t=%RANDOM% * 10 / 32768 + 1
goto cont
:cont
timeout /t 1 >nul
if exist pack.rsm goto cont2
if NOT exist pack.rsm goto cont
:cont2
set /p data=<pack.rsm
del pack.rsm
if %data% LSS 5 goto move
if %data% GTR 5 goto check
The receiver of the numbers
#echo off
:a
timeout /t 1 >nul
if exist wait.rsm goto b
if NOT exist wait.rsm goto a
:b
set /p d=<wait.rsm
if %d% GTR 5 goto a
if %d% LSS 5 goto w
:w
echo 3>>pack.rsm
goto a
Help?
Your writer is echoing %t% before it's setting it:
echo %t% >>wait.rsm
SET /A t=%RANDOM% * 10 / 32768 + 1
At the time of that first statement, %t% is set to nothing so it's as if you said:
echo >>wait.rsm
which is why you're getting ECHO is off, what you see with a "naked" echo command.
Basically, you need to set %t before trying to echo it so swap those two statements around:
SET /A t=%RANDOM% * 10 / 32768 + 1
echo %t% >>wait.rsm

Resources