Batch RPG Shop

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
CodingBear
Posts: 19
Joined: 26 Dec 2014 16:37

Batch RPG Shop

#1 Post by CodingBear » 26 Dec 2014 17:10

So I'm trying to make a shop in batch but when a player buys a weapon the DMG doesn't change. Sorry for the extensive code :(

@echo off
title Arena
::Stats
set HP=100
set OPHP=100
set DMG=10
set WepDMG=0
set HealthPots=0
set TotalDMG=%DMG%+%WepDMG%

:LogIn
cls
echo Welcome to the Arena
echo.
echo Press L if your a returning Champion
echo Press N if your a Newbie
echo.
set /p ch1=">>"
if %ch1%== L goto Load
if %ch1%== N goto Intro
if not %ch1%== L goto :LogIn
if not %ch1%== N goto :LogIn

:Intro
cls
echo The Arena is a cutthroat competition of strength.There is no mercy.No retrys either you do you die forever you have to start over so good luck out there.
pause
cls
echo Welcome Contestant!
echo.
echo What is your name?
echo.
set /p Name=">>"
cls
echo Welcome to the Arena %Name%, heres some gold to help you on your way.
echo.
echo You got 100 gold!
set Gold=100
goto Save

:Load
cls
echo What is your name Champion?
echo.
set /p Name=">>"
(
set /p Name=
set /p WepDMG=
set /p Gold=
set /p HealthPots=
)<%Name%.txt
goto Menu

:Menu
cls
echo Welcome to the Arena! %Name% Gold %Gold%
echo.
echo 1) Battle Someone
echo 2) Armory
echo 3) Save
echo 4) Exit
echo.
set /p ch2=">>"
if %ch2%== 1 goto Battle
if %ch2%== 2 goto Armory
if %ch2%== 3 goto Save
if %ch2%== 4 goto Exit
if not %ch2%== 1 goto Menu
if not %ch2%== 2 goto Menu
if not %ch2%== 3 goto Menu
if not %ch2%== 4 goto Menu

:Exit
cls
echo Are you Sure? Yes or No?
echo.
set /p ch3=">>"
if %ch3%== Yes Exit
if %ch3%== No goto Menu
if not %ch3%== goto Exit
if not %ch3%== goto Exit
if not defined %ch2% (
cls
goto Exit
)

:Save
(
echo %Name%
echo %WepDMG%
echo %Gold%
echo %HealthPots%
)>%Name%.txt
goto Menu

:Armory
cls
echo Welcome to the Armory, here you can upgrade your gear and buy health pots.
echo.
echo 1) Dagger +5 DMG 50 gold
echo 2) Go back
echo.
set /p ch5=">>"
if %ch5%== 1 goto BuyDagger
if %ch5%== 2 goto Menu
if not %ch5%== 1 goto Armory
if not %ch5%== 2 goto Armory

:BuyDagger
cls
if %Gold% lss 50 goto InsufficientFunds
echo You bought a Dagger for 50 gold!
set /a WepDMG+=5
set /a Gold=%Gold%-50
pause
goto Armory

:InsufficientFunds
cls
echo You can't buy this you too poor!
pause
goto Armory

:Battle
cls
if %OPHP% lss 0 goto Victory
if %HP% lss 0 goto Fail
echo %Name% %HP% Opponent %OPHP%
echo.
echo 1) Attack
echo 2) HealthPot
echo.
set /p ch4=">>"
if %ch4%== 1 goto Attack
if %ch4%== 2 goto HealthPot
if not %ch4%== 1 goto Battle
if not %ch4%== 2 goto Battle

:Attack
set /a OPHP=%OPHP%-%TotalDMG%
goto OPAttack

:HealthPot
if %HealthPots% EQU 0 goto NoHealthPots
set /a HP=%HP%+20
set /a HealthPots=%HealthPots%-1
echo.
echo You gained 20 Health
pause
cls
goto Battle

:OPAttack
set /a HP=%HP%-%Random% %%10-5%
goto Battle

:NoHealthPots
cls
echo You have no Health Pots!
pause
goto Battle

:Victory
cls
echo Congratulations! You killed the guy, You won 100 gold!
set /a Gold=%Gold%+100
set OPHP=100
pause
goto :Menu

:Fail
echo You failed!
echo You weren't fit for the Arena.
echo You are Dead.
Del %Name%.txt
pause
goto LogIn

I have tried multiple code but none of it works :(

ShadowThief
Expert
Posts: 1167
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Batch RPG Shop

#2 Post by ShadowThief » 26 Dec 2014 17:48

You're using %TotalDMG% do deal damage, but you update %WepDMG% when you buy a dagger and then you never update %TotalDMG% to match.

CodingBear
Posts: 19
Joined: 26 Dec 2014 16:37

Re: Batch RPG Shop

#3 Post by CodingBear » 26 Dec 2014 20:54

Ok I understand it a little bit but if TotalDMG equals WepDMG+DMG wouldn't I just need to update the WepDMG, sorry if its a stupid question I have only been in the batch community a week or so but I have a good understanding of it (I actually wrote this code myself :D )

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch RPG Shop

#4 Post by foxidrive » 26 Dec 2014 21:42

You need to execute this statement again to change the TotalDMG to the new value.

Code: Select all

set /a TotalDMG=%DMG%+%WepDMG%


This style of input is far more robust and protects the code in case things like & or < are entered.
The last four lines have changed.

Code: Select all

:Armory
cls
echo Welcome to the Armory, here you can upgrade your gear and buy health pots.
echo.
echo 1) Dagger +5 DMG 50 gold
echo 2) Go back
echo.
set /p ch5=">>"
if "%ch5%"=="1" goto BuyDagger
if "%ch5%"=="2" goto Menu
echo Try again... & ping -n 3 localhost>nul
goto Armory

CodingBear
Posts: 19
Joined: 26 Dec 2014 16:37

Re: Batch RPG Shop

#5 Post by CodingBear » 26 Dec 2014 23:17

Thnx but now during the battle it only does 5 dmg I added a set DMG=10 to the Buy Dagger code but nothing happened

CodingBear
Posts: 19
Joined: 26 Dec 2014 16:37

Re: Batch RPG Shop

#6 Post by CodingBear » 26 Dec 2014 23:21

:BuyDagger
cls
if %Gold% lss 50 goto InsufficientFunds
echo You bought a Dagger for 50 gold!
set /a WepDMG=%WepDMG%+5
set /a DMG=%DMG%+10
set TotalDMG=%DMG%+%WepDMG%
set /a Gold=%Gold%-50
pause
goto Armory

This is my script but NOW it does 10 dmg in the battle instead of the 15?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch RPG Shop

#7 Post by foxidrive » 27 Dec 2014 08:28

I edited my post - you are missing the /a in the set arithmetic command.

Just for the record, the set /a command doesn't need % around environment variables in the equation, which can make it easier to read.

CodingBear
Posts: 19
Joined: 26 Dec 2014 16:37

Re: Batch RPG Shop

#8 Post by CodingBear » 27 Dec 2014 10:17

It's still not working its not doing 15 dmg :evil:

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch RPG Shop

#9 Post by foxidrive » 27 Dec 2014 10:33

try this in your code


Code: Select all

echo TotalDMG="%TotalDMG%"
set /a TotalDMG= DMG + WepDMG
echo TotalDMG="%TotalDMG%"
pause

CodingBear
Posts: 19
Joined: 26 Dec 2014 16:37

Re: Batch RPG Shop

#10 Post by CodingBear » 27 Dec 2014 10:50

Thnx it works heres the code

:BuyDagger
cls
if %Gold% lss 50 goto InsufficientFunds
echo You bought a Dagger for 50 gold!
set /a WepDMG=%WepDMG%+5
echo TotalDMG="%TotalDMG%"
set /a TotalDMG= DMG + WepDMG
echo TotalDMG="%TotalDMG%"
pause
goto Armory

problem is it echos TotalDMG=10+0 but then it says TotalDMG=15?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch RPG Shop

#11 Post by foxidrive » 27 Dec 2014 11:14

The line before the :login label is missing a /a too and is where it gets set with the plus sign.

The lines above that one all set the variable to a single number as so don't need the /a but you can add the /a to them too.

CodingBear
Posts: 19
Joined: 26 Dec 2014 16:37

Re: Batch RPG Shop

#12 Post by CodingBear » 27 Dec 2014 11:21

OMG thank you so much it works :D

CodingBear
Posts: 19
Joined: 26 Dec 2014 16:37

Re: Batch RPG Shop

#13 Post by CodingBear » 27 Dec 2014 11:43

Ok um everything is working except for this weird bug with buying health pots
It runs the buy Health pot script but it also goes to the insufficient funds thing and doesn't echo that you bought it but you can still use it in battle

:Armory
cls
echo Welcome to the Armory, here you can upgrade your gear and buy health pots.
echo.
echo 1) Dagger +5 DMG 50 gold
echo 2) HealthPot +20 Health 75 gold
echo 3) Go back
echo.
set /p ch5=">>"
if %ch5%== 1 goto BuyDagger
if %ch5%== 2 goto BuyHealthPot
if %ch5%== 3 goto Menu
if not %ch5%== 1 goto Armory
if not %ch5%== 2 goto Armory
if not %ch5%== 3 goto Armory

:BuyHealthPot
cls
if %Gold% lss 75 goto InsufficientFunds
echo You Bought a HealthPot for 75 gold!
echo.
echo You had %HealthPots% HealthPots!
set /a HealthPots=%HealthPots%+1
set /a Gold=%Gold%-75
echo You now have %HealthPots% HealthPots!

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch RPG Shop

#14 Post by foxidrive » 27 Dec 2014 12:17

You didn't implement the enhancement I suggested in a previous input loop - to make it more robust and to also catch any input other than the ones you need.
Note the double quotes and the simple goto.

That doesn't seem to be the issue here - you seem to have some other logic flaw elsewhere from your description.

Use the echo command with the %variable% name and a pause here and there and see if you can track down where it is being updated incorrectly.


Code: Select all

:Armory
cls
echo Welcome to the Armory, here you can upgrade your gear and buy health pots.
echo.
echo 1) Dagger +5 DMG 50 gold
echo 2) HealthPot +20 Health 75 gold
echo 3) Go back
echo.
set /p ch5=">>"
if "%ch5%"=="1" goto BuyDagger
if "%ch5%"=="2" goto BuyHealthPot
if "%ch5%"=="3" goto Menu

goto Armory

CodingBear
Posts: 19
Joined: 26 Dec 2014 16:37

Re: Batch RPG Shop

#15 Post by CodingBear » 27 Dec 2014 15:27

I added the ping localhost code and the quotes but now when I try to buy anything it runs it

Post Reply