I'm new to stackOverflow. I have a question. Why is my code not working...it's nested if statements, that should work...I'm stumped. You are supposed to have money, a home (type 1 to 3) and phone. The phone is the problem. callLine is used to tell which line will be displayed each time callText is called. It adds 1 to itself every time it is called in Calling2. Whenever it gets to a certain number, it changes callText (the text displayed) to something else. But with callWho variable (work or home) it needs to be checked aswell. Here it is:
# echo off
set money=10000
set house=1
set item=Phone
set /a callLine=0
setlocal enabledelayedexpansion
echo Hello there! I am Joe, and I am the mayor of Redcrest Town.
pause
cls
echo I'm so happy to see another resident move into the Redcrest Town.
echo So, will you do me a favour and tell me your name?
set /p Name=
cls
echo Well then, %name%! Welcome!
pause
cls
echo I hope you have a great time here. You have 10,000 dollars, and a small house.
echo Please enjoy your time here!
pause
cls
:UsePhone
echo Money: %money%
echo.
echo You are using your phone. This will cost money, each time you call.
echo.
echo.
echo __i [CONTACT LIST]
echo ^|---^| [1: HOME ]
echo ^|[_]^| [2: WORK ]
echo ^|:::^|
echo ^|:::^|
echo `\ \
echo \_=_\
set /p callWho=Select Number:
if %callWho% == 2 goto Calling
echo %callText%
pause
:Calling
cls
echo Calling......
ping localhost -n 4 >nul
:Calling2
if callLine == 0 if %callWho% == 2 set callText="Hello? Is this %name%?"
if callLine == 1 if %callWho% == 2 set callText="Oh hello! It's really nice to talk to you again."
if callLine == 2 if %callWho% == 2 set callText="Well, I will see you soon, %name%! Bye!"
if callLine == 3 if %callWho% == 2 set callText="END"
set /a callLine = callLine + 1
cls
echo __i
echo ^|---^|
echo ^|[_]^| PHONE: %callText%
echo ^|:::^|
echo ^|:::^|
echo `\ \
echo \_=_\
ping localhost -n 5 >nul
if %callText% == "END" goto UsePhone
goto Calling2
I keep crashing with: . is unexpected at this time....
Really unusual.
You forgot the %% around callLine.
:Calling2
if %callLine% == 0 if %callWho% == 2 set callText="Hello? Is this %name%?"
if %callLine% == 1 if %callWho% == 2 set callText="Oh hello! It's really nice to talk to you again."
if %callLine% == 2 if %callWho% == 2 set callText="Well, I will see you soon, %name%! Bye!"
if %callLine% == 3 if %callWho% == 2 set callText="END"
set /a callLine = callLine + 1
When testing variables with if statements, the variables must be enclosed in % %
Related
So I'm making a very ambitious game using batch to challenge myself. I've been working on the "shop" segment of this game but for some reason when I enter 'b' or 'B' it doesn't send me back to the menu, but reads it as I wanted to buy a medkit and sends me to the "medkit purchased" line of code. I've tried to add pause >nul and pause between the shop line of code and the purchased line of code, but it didn't help. I also tried double checking to see if I typed menu right many times. There has to be something I'm missing, help appreciated, I've been messing with it for almost 2 hours now.
Edit: I have not entered the other events yet, only medkit and go back to menu.
Edit2: I've added some more variables and instead of taking me to them it takes me to the medkit code again.
Edit: I removed the percents, you guys can stop bugging me bout it. The issue still is not fixed and I do not have choice.exe so choice commands don't work.
Here is the code:
:shop
cls
echo Welcome to the shop!
echo Money: $%money%
echo.
echo 1. Buy Med Kit -$30
echo 2. Buy Shotgun Ammo (1) -$15
echo 3. Buy Assult Rifle Ammo (5) -$20
echo 4. Buy Missle (1) -$50
echo 5. View Armor
echo 6. View Modifiers
echo.
echo Enter 'B' to go back.
echo.
set /p %shopOp%=
if '%shopOp%' == '1' goto medkit1
if '%shopOp%' == '2' goto sg1
if '%shopOp%' == '3' goto ar1
if '%shopOp%' == '4' goto rpg1
if '%shopOp%' == '5' goto armors1
if '%shopOp%' == '6' goto mods1
if '%shopOp%' == 'B' goto menu
if '%shopOp%' == 'b' goto menu
:medkit1
cls
set /a money=%money%-30
if %money% LSS 0 goto noBuy1
set /a medK=%medK%+1
echo You bought 1 medkit for $30!
echo You now have %medK% med kits now!
echo.
echo Press enter to continue
pause >nul
goto shop
:noBuy1
cls
set /a money=%money%+30
echo You dont have enough money!
echo You have $%money%. You need $30.
echo.
echo Press enter to continue
pause >nul
goto shop
Okay so you don't have choice. How about xcopy?
A version adapted from the link in my comment that substitutes the choice utility with xcopy to return keypress:
#Echo off
Set Echo.ID=Call :ID.Label "%%~1" "%%~2"
Setlocal EnableDelayedExpansion
Title Menu Navigation Example
:Bar
Set "Left.Date="
IF /I "!Left.Bar!" == "Yes" Goto :Date
Call :[+Menu] "Bar" "Drink Fight Chat Leave"
Goto :Bar
:Date
Set "Left.Bar="
IF /I "!Left.Date!" == "Yes" Goto :Bar
Call :[+Menu] "Date" "Dinner Flirt Leave"
Goto :Date
:[+Menu] <Location / Core Menu option> <Sub Actions / Menu Items as list>
::: - Build menu and choice list for display
CLS
Set "CHOICES="
Set "Actions="
For %%A in (%~2) Do (
Set "Option=%%~A"
Set Actions=!Actions! "%%~A"
Set CHOICES=!CHOICES! "!Option:~0,1!"
Set "Option=[!Option:~0,1!]!Option:~1,100!"
Echo !Option!
)
::: - Return key literal from xcopy command and call relevent menu item with Identifying Params
Set "Option="
Set Key=
For /F "Delims=" %%A in ('xcopy /w "%~f0" "%~f0" 2^>nul') do (
If Not Defined Key Set "Key=%%A"
)
Set "Key=!Key:~-1!"
::: - Restrict keypress to available options, fall through to 'Goto :[Menu]' when no Call is made (Invalid keypress)
For %%O in (%~2) Do (
Set "Option=%%~O"
Set "Option=!Option:~0,1!"
If /I "!Option!" == "!Key!" Call :[%~1_%%~O-!Key!] "%%~1 %%~O" "%~1"
)
Goto :[+Menu]
:[Bar_Drink-D]
%Echo.ID%
Goto :!Loc!
:[Bar_Fight-F]
%Echo.ID%
Goto :!Loc!
:[Bar_Chat-C]
%Echo.ID%
Goto :!Loc!
:[Bar_Leave-L]
%Echo.ID%
Set "Left.Bar=Yes"
Goto :!Loc!
:[Date_Dinner-D]
%Echo.ID%
Goto :!Loc!
:[Date_Flirt-F]
%Echo.ID%
Goto :!Loc!
:[Date_Leave-L]
Set "Left.Date=Yes"
%Echo.ID%
Goto :!Loc!
:ID.Label <%~1> <%~2>
Title %~1
::: - Identify return Label
Set "Loc=%~2"
Exit /B
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
I recently followed some tutorials and learnt how to create and program batch files. I started making a game meant to simulate fighting monsters. One of the mechanics of this game was to be an inventory where once programmed, you'd be able to buy certain items from a store and put them in your inventory which you can already access in battle. The only programmed level so far is the tutorial and only the start of it. What you should be able to do is select an inventory slot when asked, then the computer should run the inventory function which it doesn't seem to do. Some error shows up, then closes the window automatically.If anyone could explain in simple terms why this isn't working and how i can fix it, it would really help me out. Thanks :D Here's my code...
::SETUP
#echo off
title Platformer
color 0a
::Variables
set my_level=0
set money=0
set inventory_slot_1=
set inventory_slot_2=
set inventory_slot_3=
set inventory_slot_4=
set inventory_slot_5=
set current_level=-1
set pin=0
::MONSTER VARIABLES
set monster_level = 0
::MAIN MENU
:main_menu
cls
echo.
echo PPPPP l aaaaa ttttt fffff ooooo rrrr mmmmm eeeee rrrr
echo P P l a a t f o o r r m m m e r r
echo PPPPP l aaaaa t ffff o o rrrr m m m eeee rrrr
echo P l a a t f o o r r m m m e r r
echo P l a a t f ooooo r r m m eeeee r r
echo.
echo 1990 inc.
echo.
echo LVL.%my_level% $%money%
echo ________________________________________________________________________________
echo.
echo 1 - Play
echo 2 - Save
echo 3 - Load
echo.
set /p input=">>> "
if %input%==1 goto level_select
if %input%==2 goto save
if %input%==3 goto load
pause >nul
::LEVEL SELECT
:level_select
cls
echo LVL.%my_level% $%money%
echo ________________________________________________________________________________
echo.
echo 1 - Tutorial
echo 2 - Level 1
echo 3 - level 2
echo 4 - level 3
echo 5 - Boss
echo 6 - BACK
echo.
set /p input=">>> "
if %input%==1 goto tutorial
if %input%==2 goto level_select
if %input%==3 goto level_select
if %input%==4 goto level_select
if %input%==5 goto level_select
if %input%==6 goto main_menu
goto main_menu
pause >nul
::INVENTORY
:inventory
if %~1==rock set %damage%=2
if %~1==mercy set mercy_chance=%RANDOM%*%monster_leve%/32768+1
EXIT /b
::TUTORIAL
:tutorial
cls
echo LVL.%my_level% $%money%
echo ________________________________________________________________________________
echo.
set /a current_level = 0
call:summon_dragon
pause >nul
::Monster -- Dragon
:summon_dragon
set /a monster_level=%current_level%+1
set /a rand=%RANDOM%%%3+1
set /a monster_hp=%monster_level%*3+%rand%
echo A Lvl.%monster_level% Dragon Has Been Summoned
echo What Will You Do?
echo.
echo HP: %monster_hp%
echo.
echo 1 - [ %inventory_slot_1% ]
echo 2 - [ %inventory_slot_2% ]
echo 3 - [ %inventory_slot_3% ]
echo 4 - [ %inventory_slot_4% ]
echo 5 - [ %inventory_slot_5% ]
echo 6 - [ Mercy ]
echo.
set /p input=">>> "
if input==1 callinventory %inventory_slot_1%
if input==2 call:inventory %inventory_slot_2%
if input==3 call:inventory %inventory_slot_3%
if input==4 call:inventory %inventory_slot_4%
if input==5 call:inventory %inventory_slot_5%
if input==6 (
call:inventory mercy
if %mercy_chance%==1 (
set my_level=%my_level%+1
goto win
)
)
set monster_hp=%monster_hp%-%damage%
if monster_hp<=0(
set my_level=%my_level%+1
set current_level=%current_level%+1
goto win
)
EXIT /b
::WIN
:win
cls
echo YOU WIN!
pause >nul
::SAVE
:save
cls
echo LVL.%my_level% $%money%
echo ________________________________________________________________________________
echo.
set /p pin="Pin: "
echo.
if pin==nul(set /p pin = Change Pin: goto save)
(
echo #echo off
echo set my_level=%my_level%
echo set money=%money%
) >> game_saves\%pin%.cmd
echo SAVED
pause >nul
goto main_menu
::LOAD
:load
cls
echo LVL.%my_level% $%money%
echo ________________________________________________________________________________
echo.
set /p pin="Pin: "
echo.
if pin==nul( pause >Invalid Pin goto load)
call game_saves\%pin%.cmd
echo Loaded
pause >nul
goto main_menu
if %~1==rock set %damage%=2
Is probably incorrect. If %~1 is empty, then there will be a syntax error.
You are attempting to set the variable that is the contents of damage to 2. Probably this is incorrect. If damage is not set at this time, you will get an error as cmd attempts to parse set =2
Go to Start>Programs>Accessories and set yourself a shortcut to Command Prompt.
Clicking that will put you in to cmd and you can then navigate about and run the batch by changing to the appropriate directory with cd c:\wherever\your\batch\is and simply running nameofyourbatchfile.
To exit cmd, type exit and to get help on commands type commandname /?
All acions are terminated with Enter
I am trying to make a little connect four game and whenever I hit enter without typing anything into the console it says that goto was unexpected at this time All I want it to dos go back to :X for now if there is no user input. Any help would be aappreciated.
:X
cls
echo --------------
echo Connect Four X
echo --------------
echo.
echo CHOOSE A COLUMN
echo.
echo.
echo 1 2 3 4 5 6 7
echo.
echo (%a6%) (%b6%) (%c6%) (%d6%) (%e6%) (%f6%) (%g6%)
echo.
echo (%a5%) (%b5%) (%c5%) (%d5%) (%e5%) (%f5%) (%g5%)
echo.
echo (%a4%) (%b4%) (%c4%) (%d4%) (%e4%) (%f4%) (%g4%)
echo.
echo (%a3%) (%b3%) (%c3%) (%d3%) (%e3%) (%f3%) (%g3%)
echo.
echo (%a2%) (%b2%) (%c2%) (%d2%) (%e2%) (%f2%) (%g2%)
echo.
echo (%a1%) (%b1%) (%c1%) (%d1%) (%e1%) (%f1%) (%g1%)
echo.
echo ---------------------------
echo.
echo.
set /p xchoice=:
if %xchoice% == 1 goto xcheck1
if %xchoice% == 2 goto xcheck2
if %xchoice% == 3 goto xcheck3
if %xchoice% == 4 goto xcheck4
if %xchoice% == 5 goto xcheck5
if %xchoice% == 6 goto xcheck6
if %xchoice% == 7 goto xcheck7
goto X
If you are entering a string with a set/p, then there's no saying that the data entered doesn't contain Spaces. The way to get over that is to "enclose the strings on both sides of the comparison operator in quotes" - that is, double-quotes 'not single quotes'
IF syntax is if string1 operator string2 action.
You need
if "%possiblyemptyvariable%"=="1" goto ...
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 :)