Batch file game trouble, missing operator error - batch-file

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

Related

Batch RPG : Fight Loop Broken

So, couple of problems I'm having.
When the TutorialFight runs, it works almost as I want it, except that the hero never gets hit.
When choosing Magic or Run, the if statements do not display the echos, and goes to the TutorialError.
Note: %stat% is the hero's stat, while %estat% is the enemy's stat.
:TutorialIntro
title Tutorial^^!
cls
set /a ehealth=15
set /a emana=15
set /a ebstat=5
set /a eostat=5
set /a ecstat=5
set /a elevel=1
set /a health=15
set /a mana=15
set /a bstat=5
set /a ostat=5
set /a cstat=5
set /a level=1
echo You have run into a
echo Level %elevel% Sock Puppet.
echo -I made this from a combination of a sock, glue, and some paper.
echo If you lose to this thing, I'm not looking you in the eye again.
echo You know, if I could actually look at you in the first place.
echo.
echo Level %level% Hero
echo %health% HP
echo %mana% MP
echo %bstat% Brawn
echo %ostat% Obscurity
echo %cstat% Cowardice
pause
goto Tutorial
:Tutorial
title Fight^^!
cls
if %ehealth% leq 0 goto TutorialWin
if %health% leq 0 goto TutorialLoss
echo Level %elevel% Sock Puppet.
echo (%ehealth% HP)
echo.
echo V.S.
echo.
echo Level %level% Hero
echo %health% HP
echo %mana% MP
echo %bstat% Brawn
echo %ostat% Obscurity
echo %cstat% Cowardice
echo.
set /p answer= Fight(1), Magic(2), Run(3)
if %answer%==1 goto TutorialFight
if %answer%==2 echo Sorry. You don't know magic yet.
if %answer%==3 echo No. You are not going to run from a sock.
goto TutorialError
:TutorialFight
set /a loss=0
set /a eloss=0
set /a num=%random% * ((%cstat%-%ecstat%) - 1 + 1) / 32768 + 1
if %num% lss (%ecstat%/2) set /a loss=%random% * (%ebstat% - (%ebstat%/2) + 1) / 32768 + (%ebstat%)/2)
if %num% geq (%ecstat%/2) set /a eloss=%random% * (%bstat% - (%bstat%/2) + 1) / 32768 + (%bstat%)/2)
set /a health=(%health% - %loss%)
set /a ehealth=(%ehealth% - %eloss%)
goto Tutorial
:TutorialError
title Huh?
cls
echo Try that again. I believe in you.
pause
goto Tutorial
:TutorialWin
title Victory
cls
echo Yay^^!
echo Now I think you are ready for the big pond^^!
pause
goto Intro
:TutorialLoss
title ...
cls
echo ...
echo.
echo Let's forget that ever happened.
pause
goto Menu
Use an additional calculation:
Set/A halfecstat=ecstat / 2
Then change the line from:
if %num% lss (%ecstat%/2)
to:
If %num% Lss %halfecstat%
Also I've noted that the calculation which follows it has unbalanced parentheses so you may want to give that another careful look too.

Batch file RPG game error

#echo off
TITLE Zombie Warrior
setlocal enabledelayerdexpansion
:new
set playerdmg= 23
set zombiedmg= 24
set coin= 0
set rewards= 10
set level= 0
goto refresh
:refresh
set health=100
set zombiehealth=200
set zombie2health=400
goto menu
:menu
cls
echo.
echo Zombie Warrior
echo Coins = %coin%
echo.
echo 1) Play!
echo 2) Exit.
echo 3) Shop
echo.
set /p c=C:\
if "%c%" == "1" goto home
if "%c%" == "2" exit
if "%c%" == "3" goto shop
goto menu
set health=100
set zombiehealth=200
:home
cls
echo Welcome to the game!
echo -+-+-+-+-+-+-+-+-+-+-+-+-
echo Coins: %coin%
echo Level: %level%
echo.
echo 1) FIGHT!
echo 2) Quit
set /p c=C:
if "%level%" == "1" (
if "%c%" == "1" goto encounter2
)
if "%level%" == "0" (
if "%c%" == "1" goto encounter1
)
if "%c%" == "2" goto menu
goto home
set health=100
set zombiehealth=200
:encounter1
cls
echo You: %health%
echo Zombies Level 1: %zombiehealth%
echo Your damage: %playerdmg%
echo.
echo 1) Attack
echo 2) Run!
echo.
set /p c=C:\
if "%c%" == "1" goto attack1
if "%c%" == "2" goto refresh
goto encounter1
:encounter2
cls
echo You: %health%
echo Zombies Level 2: %zombie2health%
echo Your damage: %playerdmg%
echo.
echo 1) Attack
echo 2) Run!
echo.
set /p c=C:\
if "%c%" == "1" goto attack2
if "%c%" == "2" goto refresh
goto encounter2
:attack2
set /a zombie2health-=playerdmg
set /a health-=zombiedmg
if %zombie2health% lss 0 goto win1
if %health% lss 0 goto lose
if !zombiehealth! lss 30 set /a coin+=5
goto encounter1
:attack1
set /a zombiehealth-=playerdmg
set /a health-=zombiedmg
if %zombiehealth% lss 0 goto win1
if %health% lss 0 goto lose
if !zombiehealth! lss 30 set /a coin+=5
goto encounter1
:win1
set /a coin+=rewards
set /a level+=1
if level == 1 set /a coin+=10
goto refresh
:lose
cls
echo You lost :(
pause
goto refresh
:shop
cls
echo What would you like to buy? (type q to quit)
echo Coins: %coin%
echo 1) Baseball bat
echo.
set /p c=C:\
if "%c%" == "1" goto bat1
if "%c%" == "q" goto menu
:bat1
if %coin% lss 30 goto nope
set /a playerdmg+=5
set /a coin-=30
goto menu
:nope
echo You don't have enough coins!
pause
goto menu
So that is my code. What I'm trying to make is basically if I am level 1 or 0, it will make me go somewhere else.
For example if I am level 0
goto encounter1
if I am level 3
goto encounter4
But I don't know how to do that. I think I'm close in this line:
if "%level%" == "1" (
if "%c%" == "1" goto encounter2
)
if "%level%" == "0" (
if "%c%" == "1" goto encounter1
)
Your problem is stemming from these lines:
set playerdmg= 23
set zombiedmg= 24
set coin= 0
set rewards= 10
set level= 0
All spaces are significant in a SET statement, so your values include a leading space. Your if "%level%" == "0" ( statement expands to if " 0" == "0" (, which of course is false.
Remove the leading space from the SET statements, and your immediate problem should be solved. (I haven't evaluated your entire code base to see if there are other problems)
A better way to structure your SET statements is to enclose the entire expression in quotes. The quotes mark the end of the assignment, but are not included in the value. This prevents inadvertent trailing spaced from being included in the value.
set "playerdmg=23"
set "zombiedmg=24"
set "coin=0"
set "rewards=10"
set "level=0"

Batch file that accepts 5 positive integers range is 0-9 and display the output in descending order non integers are not accepted

i have the code here but i dont know the logic on how the inputs will sort to descending order..
#echo off
:start1
cls
set /p num1=Enter #1:
if [%num1%]==[] goto error1
if %num1% LSS 0 goto a
if %num1% GTR 9 goto a
:start2
cls
set /p num2=Enter #2:
if [%num2%]==[] goto error2
if %num2% LSS 0 goto b
if %num2% GTR 9 goto b
:start3
cls
set /p num3=Enter #3:
if [%num3%]==[] goto error3
if %num3% LSS 0 goto c
if %num3% GTR 9 goto c
:start4
cls
set /p num4=Enter #4:
if [%num4%]==[] goto error4
if %num4% LSS 0 goto d
if %num4% GTR 9 goto d
:start5
cls
set /p num5=Enter #5:
if [%num5%]==[] goto error5
if %num5% LSS 0 goto e
if %num5% GTR 9 goto e
::Equations.........
::Error Trapping
:error1
echo No input!
pause
goto start1
:error2
echo No input!
pause
goto start2
:error3
echo No input!
pause
goto start3
:error4
echo No input!
pause
goto start4
:error5
echo No input!
pause
goto start5
:a
echo Input must be in the range of 0 to 9
pause
goto start1
:b
echo Input must be in the range of 0 to 9
pause
goto start2
:c
echo Input must be in the range of 0 to 9
pause
goto start3
:d
echo Input must be in the range of 0 to 9
pause
goto start4
:e
echo Input must be in the range of 0 to 9
pause
goto start5
:exit
pause
exit
you might try this:
#echo off &setlocal
for /f "delims==" %%a in ('set "$num" 2^>nul') do set "%%a="
:loop1
set /a count+=1
:loop2
set /p "$num%count%=Enter #%count%: "
call echo %%$num%count%%%|findstr /r "^[0-9]$" >nul|| (echo Error!&goto:loop2)
if %count% lss 5 goto:loop1
(for /f "tokens=2 delims==" %%a in ('set "$num"') do #echo %%a)|sort /r

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

set not expected at this time, batch?

OK so I have this little game I am writing on this Windows XP computer in batch. I get "set was not expected at this time" when I run the following code(sorry it's real long I just haven't made it smaller yet. NOTE I didn't ask for you to make my code smaller, so please DO NOT do it):
#echo off
set str=0
set int=0
set agi=0
set dex=0
goto chrselect
:chrselect
cls
echo Character select!
echo.
echo 1) mage
echo.
echo 2) swords man
echo.
echo 3) archer
set /p start=-
if %start%==1 goto skillsetupm
if %start%==2 goto skillsetups
if %start%==3 goto skillsetupa
:skillsetupm
set health=50
set mana=100
set str=3
set int=6
set agi=5
set dex=4
set chr=m1
goto skillsee
:skillsetups
set health=100
set mana=50
set str=6
set int=4
set agi=3
set dex=5
set chr=s1
goto skillsee
:skillsetupa
set health=75
set mana=75
set str=3
set int=4
set agi=6
set dex=5
set chr=a1
goto skillsee
:skillsee
cls
echo Skills as is:
echo.
echo Strength: %str%
echo Intelligence: %int%
echo Agility: %agi%
echo Dexterity: %dex%
echo.
echo 1) Choose new character
echo.
echo 2) Continue to see how stats influence combat!
set /p start=-
if %start%==1 goto chrselect
if %start%==2 goto skillc
:skillc
set /a handattk=3+%str%
set /a magicattk=2+%str%+%dex%
set /a block=%agility%+%dex%
set /a blockchnc=%block%*5
set /a bowattk=3+%str%+%dex%
set /a swrdattk=4+%str%+%dex%
set /a slowchnc=%str%*4
if %chr%==m1 goto skillcinf
if %chr%==s1 goto skillcinf
if %chr%==a1 goto skillcinf
:skillcinf
cls
echo Skill stuff!
echo.
echo Hand attack: %handattk%
echo.
echo Block chance: %blockchnc% %
echo.
echo Slow chance: %slowchnc% %
echo.
if %chr%==m1 echo Magic attack: %magicattk%
if %chr%==s1 echo Sword attack: %swrdattk%
if %chr%==a1 echo Bow attack: %bowattk%
echo.
echo 1) Choose new character(last chance)
echo.
echo 2) Start game
set /p start=-
if %start%==1 goto chrselect
if %start%==2 goto menue
:menue
cls
set bossh=500
set exp=0
set playcount=0
echo Health:///%health%///
echo Mana:////%mana%////
echo Experience:////%exp%/////
echo Play count: %playcount%
echo Battle?(y/n)
set /p start=-
if %start%==y goto battle
if %start%==n goto menue
:battle
set /a playcount=%playcount%+1
cls
echo Health:///%health%///
echo Mana:////%mana%////
echo Experience:////%exp%/////
echo Play count: %playcount%
echo Attack Dragon(Health:%bossh%) with?
echo.
echo 1-Hand(%handattk% damage)
echo.
echo 2-Block(%blockchnc% % chance of blocking)
echo.
if %chr%==m1 echo 3-Magic attack(%magicattk% damage)
if %chr%==s1 echo 3-Sword Attack(%swrdattack% damage)
if %chr%==a1 echo 3-Bow Attack (%bowattk% damage)
set /p start=-
pause >nul
if %start%==1 set /a bossh=%bossh%-%handattk%
if %start%==2 set /a blockrandm=%random% %%100
goto battle2
if %start%==3 goto spclattk
:spclattk
cls
if %chr%==m1 set /a bossh=%bossh%-%magicattk%%
if %chr%==s1 set /a bossh=%bossh%-%swrdattk%
if %chr%==a1 set /a bossh=%bossh%-%bowattk%
goto battle2
:battle2
if %bossh% leq 0 goto win
set /a bossd=%random% %%5
if %blockrandm%==%blockchnc% set bossd=0
set /a health=%health%-%bossd%
cls
echo Health:///%health%///
echo Mana:////%mana%////
echo Experience:////%exp%/////
echo Play count: %playcount%
echo Attack Dragon(Health:%bossh%) with?
echo.
echo 1-Hand(%handattk% damage)
echo.
echo 2-Block(%blockchnc% % chance of blocking)
echo.
if %chr%==m1 echo 3-Magic attack(%magicattk% damage)
if %chr%==s1 echo 3-Sword Attack(%swrdattack% damage)
if %chr%==a1 echo 3-Bow Attack (%bowattk% damage)
set /p start=-
if %start%==1 set /a bossh=%bossh%-%handattk%
if %start%==2 set /a blockrandm=%random% %%100
if %start%==3 goto battleattk
goto battle2
Anytime I get to the :battle screen and press an option I get set was not expected at this time.
Your variable %blockrandm% isn't initialised when you get to :battle2 so the comparison in line 148 reads
if ==0 set bossd=0
which obviously is invalid syntax. You can avoid it by quoting both sides of the comparison (which is a great idea in practically all cases when writing such code).
For future reference: Remove the echo off and you at least see what line is responsible and often also why it fails.

Resources