0

At the moment I am trying to make a fan-made Pokemon game. I'm making a man who sells poke balls. Here is the code I used to make it. But for some reason, the file fails to execute the last part. Btw I am also a noob at coding. To some extent, at least. Also, I use windows Batch.

@echo off
:setvariables
cls
set/a pokeballs=0
set/a pokemondollar=1000
set/a manprice=500

:Pokeball_Sale
cls
echo How many do you want to buy?
set/p pokeballamount=
set/a totalpokeballprice="pokeballamount * manprice"
echo The price = %totalpokeballprice%
pause 
echo Do you want to buy it?
echo Press y for yes
echo Or press n for no
set/p ha=Choose

if %ha% == y goto Payment
if %ha% == n goto Upstairs_House3

:Payment
cls
if "pokemondollar%" GEQ "pokeballprice" set/a pokemondollar-=totalpokeballprice
set/a pokeballs+=pokeballamount
echo You spent %totalpokeballprice% on %pokeballamount%. You now have %pokeballs%.
  • 2
    While `set /a` doesn't need the percent signs around variables, `if ` does: `if "pokemondollar%" GEQ "pokeballprice" ` should be `if "%pokemondollar%" GEQ "%pokeballprice%" ` – Stephan Apr 06 '20 at 12:16
  • 1
    Consider using [choice](https://ss64.com/nt/choice.html) instead of `set /p` for simple choices (for example `Y/N`) - it does it's own error-handling (you can only enter valid choices, all other inputs are rejected) – Stephan Apr 06 '20 at 12:19
  • btw, if you want to experiment and learn to code I suggest you to ditch windows batch, in favor of a more modern and friendly scripting language (i.e. python) – Federico Destefanis Apr 06 '20 at 12:25
  • 1
    @FedericoDestefanis, python is hardly modern, it's almost 30 years old! It also does not come as part of the [tag:windows] Operating System. – Compo Apr 06 '20 at 12:36

2 Answers2

1

Aside from the advice you've already been given in the comments, I have decided to post this example, showing a methodology which would make more sense.

@Echo Off

:SetVariables
ClS
Set /A PokeBalls=0,PokemonDollar=1000,ManPrice=500

:PokeBall_Sale
ClS
Set /A PokeBallAmount=0,MaxPokeBalls=PokemonDollar/ManPrice
Set /P "PokeBallAmount=How many Poke Balls do you want to buy [Maximum %MaxPokeBalls%]? "
If %PokeBallAmount% Equ 0 GoTo Upstairs_House3
If %PokeBallAmount% Gtr %MaxPokeBalls% (
    Echo You do not have enough funds!
    "%__AppDir__%timeout.exe" 3 /NoBreak>NUL
    GoTo PokeBall_Sale
)
Set /A TotalPokeBallPrice=PokeBallAmount*ManPrice
"%__AppDir__%choice.exe" /M "The cost is %TotalPokeBallPrice%. Do you want to buy it"
If ErrorLevel 2 GoTo Upstairs_House3

:Payment
ClS
If %PokemonDollar% GEq %TotalPokeBallPrice% (
    Echo You spent %TotalPokeBallPrice% on %PokeBallAmount% Poke Balls.
    Set /A PokemonDollar-=TotalPokeBallPrice
    Set /A PokeBalls+=PokeBallAmount
    SetLocal EnableDelayedExpansion
    Echo You now have !PokeBalls! Poke Balls and !PokemonDollar! Pokemon Dollars.
    EndLocal
    "%__AppDir__%timeout.exe" 5 /NoBreak>NUL

)

:Upstairs_House3
ClS
Compo
  • 36,585
  • 5
  • 27
  • 39
1

Yes, two Integer variables can indeed be used in Comparisons, so long as they are expanded. Quoting integers for the sake of comparisons is one means to safeguard against invalid variables, another is to make use of Delayed Expansion (Enabled)

There's a great explanation of integer comparison syntax issues here

The below MathCro can be used to assign and modify variables if your interested. In the event an attempt to operate on the 1st Argument with an undeclared variable is made, No change to the variable will occur.

@Echo Off

%= Establish Macros =%
    setlocal DisableDelayedExpansion
    (set LF=^


    %= Newline =%)
    Set ^"\n=^^^%LF%%LF%^%LF%%LF%^^"

    (Set "Operate=Endlocal ^& Set /A ""%%G%%H=%%I"""

%= 'Tunnels' variable value, Allows Definition of Arithmetic within Macro =%)

    Set @M=for /L %%n in (1 1 2) do if %%n==2 (%\n%
    for /F "tokens=1,2,3 delims=, " %%G in ("!argv!") do (%\n%
    %Operate%%\n%
%=  Display value of variable. Optional. If not syntax is required to constrain expansion =%
    If Not "!%%G!"=="" Echo(%%G: !%%G!%\n%
    ) %\n%
) ELSE setlocal enableDelayedExpansion ^& set argv=, 


%= script main body =%
%=  Facilitate modification of variables within codeblocks. =%
Setlocal EnableDelayedExpansion

REM macro can be used to define as well as modify variables
REM output of macro can be redirected to nul (hidden) like so:
REM (%@M% hp + 50)>nul

    %@M% hp + 50
    (%@M% heal + 40)>nul
    %@M% hp - 30
    %@M% hp + 25
    %@M% hp * 3
    %@M% hp / 2
    %@M% hp + heal
    %@M% heal - 10
    %@M% hp + heal
%=  Demonstrates use of an equation beyond the initial Operator. Spaces and parentheses in equation must be ommited =%
%=  Increments variable by a random amount in the range of 10 to 20 =%
    For /L %%A in (1,1,50) do (%@M% hp + !random!%%10+10)>nul
    Echo(hp: %hp%
    For /L %%A in (1,1,50) do IF Not !hp! LSS !heal! (%@M% hp - !random!%%15+10)

pause >nul
Exit /B
T3RR0R
  • 2,747
  • 3
  • 10
  • 25