RPG game damage issues - batch-file

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

Related

why does my program that changes color not work? Batch

i was trying to make a programme that changed colour but it just does not work.
I press any button and it does not change and then just closes. I have no clue why it doesent work
#echo off
set letter2=0
:1
set color=%random%
if %color% LSS 10 goto next
goto 1
:next
set letter=%random%
if %random% LSS 6 goto 2
:2
if %letter% == 0 goto A
if %letter% == 1 goto B
if %letter% == 2 goto C
if %letter% == 3 goto D
if %letter% == 4 goto E
if %letter% == 5 goto F
goto next
:a
set %letterr2% == a
goto final
:b
set %letterr2% == b
goto final
:c
set %letterr2% == c
goto final
:d
set %letterr2% == d
goto final
:e
set %letterr2% == e
goto final
:f
set %letterr2% == f
:final
set realcolor=%letter2%+%color%
cls
color %realcolor%
echo hey this color is %color%
pause>nul
goto 1
There are a couple things going on here.
The biggest problem is that your set statements in :a through :f are completely wrong.
When you write a set statement, only use one = symbol.
When you say set %letter2%=c, you are not setting the value of letter2 to c, you are saying "set the value of what the value of letter2 is to c."
Remove the spaces from both sides of the = symbol; batch allows spaces to be part of variable names, so you've created a variable called %letterr2 % and set its value to c.
You made a typo and called the variable %letterr2% instead of %letter2%.
You don't need a + for string concatenation, just stick the two variables next to each other.
Your script is taking forever to run because %random% returns a number between 1 and 32768. The odds of it being under 10 are miniscule. The odds of it being under 6 are even smaller. When you want a random number between 1 and n, use the code set /a number=%random% %% n.
Ultimately, your code is going to look something like this:
#echo off
set letter2=0
:1
set /a color=%random%%%10
set /a letter=%random%%%6
if %letter% == 0 goto A
if %letter% == 1 goto B
if %letter% == 2 goto C
if %letter% == 3 goto D
if %letter% == 4 goto E
if %letter% == 5 goto F
:a
set letter2=a
goto final
:b
set letter2=b
goto final
:c
set letter2=c
goto final
:d
set letter2=d
goto final
:e
set letter2=e
goto final
:f
set letter2=f
:final
set realcolor=%letter2%%color%
cls
color %realcolor%
echo hey this color is %color%
pause>nul
goto 1
If you want to know why your code does not work, see the extensive explanation of #SomethingDark.
However, if you want to see a simpler way to do the same thing, then you may review this code:
#echo off
setlocal EnableDelayedExpansion
set letters=abcdef
:loop
set /A color=%random% %% 10
set /A letter=%random% %% 6
set letter2=!letters:~%letter%,1!
set realcolor=%letter2%%color%
cls
color %realcolor%
echo hey this color is %realcolor%
pause>nul
goto loop
They keypoint in this code is the use of Delayed Expansion, so I suggest you to search for such a term in this site where are tons of related answers, like this one, or this one, or this one, or this one, or...
#echo off
:loop
call :getRandom
cls
color %randomColor%
echo This color is %randomColor%
pause >nul
goto :loop
:getRandom
set "randomColor="
:getRandomLoop
set /a "color=%random% %% 15"
set "if=if %color%=="
set "then=set color="
%if%10 %then%A
%if%11 %then%B
%if%12 %then%C
%if%13 %then%D
%if%14 %then%E
%if%15 %then%F
set "randomColor=%randomColor%%color%"
if "%randomColor:~1,1%"=="" goto :getRandomLoop
goto :EOF

Assistance with nested if statements

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

Is there a way to create Greater than equal to in BATCH

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

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

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