Batch window opens and shuts immediately? - batch-file

I have this code in my batch file, but when I open it, it closes itself immediately. What do I do? It should work without adding any pauses.
#echo off
color f0
:start1
cls
set /a wait=50000
set /a limit=2147483646
set /a current=0
echo WAITING ROOM SIMULATOR 2014
echo Current Number:%current%
echo Your number is 2,147,483,647
goto loop1
:loop1
set /a wait=%wait%-50000
set /a current=%current%+1
if current=limit goto pause1
goto start2
:loop2
set /a wait=%wait%+1
if %wait%=50000 goto loop1
:start2
cls
echo WAITING ROOM SIMULATOR 2014
echo Current Number:%current%
echo Your number is 2,147,483,647
goto loop2
:pause1
goto pause2
:pause2
goto pause1

Seems like you have a problem with syntax, specifically if current=limit goto pause1 and if %wait%=50000 goto loop1 see the fixed file below:
#echo off
color f0
:start1
cls
set /a wait=50000
set /a limit=2147483646
set /a current=0
echo WAITING ROOM SIMULATOR 2014
echo Current Number:%current%
echo Your number is 2,147,483,647
goto loop1
:loop1
set /a wait=%wait%-50000
set /a current=%current%+1
if %current%==%limit% goto pause1
goto start2
:loop2
set /a wait=%wait%+1
if %wait%==50000 goto loop1
:start2
cls
echo WAITING ROOM SIMULATOR 2014
echo Current Number:%current%
echo Your number is 2,147,483,647
goto loop2
:pause1
goto pause2
:pause2
goto pause1

Related

How to calculate square root in cmd?

I have a project for square root calculation to get the larger side of in a right triangle using Pythagorean theorem.
Here's what I've tried :
#echo off
set /p a=Pleas Enter Value(1) =
echo.
set /p b=Pleas Enter Value(2) =
set /a c=%a%*%a%
set /a d=%b%*%b%
set /a F=%d%+%c%
echo.
:calculation Value(3)
rem root Number
√%F%
echo.
pause>nul
Batch
This is the native batch method(not accurate):
set divider=0
:loop
set /a divider=divider+1
set /a sqrt=F / divider
if %divider% equ %sqrt% goto break
if %divider% gtr %sqrt% goto break
goto loop
:break
echo %sqrt%
It will floor the number if the square root is floating point number.
Again, will not support floating point arithmetic, but this code works:
#echo off
Title SquareRoot
:StartSquareRoot
cls
echo Number:
set /p number=
call :SquareRoot %number%
echo Square: %number%
echo Root: %answer%
pause
goto StartSquareRoot
:SquareRoot
set root=1
set /a sqr=%root%*%root%
:Loop
if %sqr% LSS %number% (
set /a root=root+1
set /a sqr=root*root
goto Loop
)
(EndLocal && set answer=%root% && exit /B)

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

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