user4942732
user4942732

Reputation:

Confusion with Batch-file code

So recently I started learning Batch-file, and today I decided to try and make a game with it. I have a few errors, and can't figure out what is wrong with the code. Heres the code:

@echo off
title Batch Rpg
color 07

echo Welcome to Batch Rpg!
echo.
echo.
pause

:menu
cls
echo.
echo    -Menu-
echo.
echo.
echo.
echo    1) Begin
echo.
echo    2) How to play
echo.
echo    3) Exit
echo.
echo.
echo.
echo    ---------
echo.
    set /p c=Choice: 
        if %c%==1 goto prestart1
        if %c%==2 goto howtoplay
        if %c%==3 goto cfr_exit
        if NOT %c%==1 if NOT %c%==2 if NOT %c%==3 goto menu


:cfr_exit
cls
echo.
echo    Are you sure you want to exit?
echo.
    set /p c=(Y/N): 
        if %c%=="Y" exit
        if %c%=="N" goto menu
        if NOT %c%=="Y" if NOT %c%=="N" goto cfr_exit2

:cfr_exit2
cls
echo.
echo    You must enter a valid option.
echo.
pause
goto cfr_exit

:howtoplay
cls
echo.
echo    -How to play-
echo.
echo.
echo.
echo    This game is very simple. There will be a number with an option after it, type the option in and it will perform an action(As the option would say).
echo.
pause
goto menu

:prestart1
cls
echo.
echo    Welcome to land of Fageryth!
echo.
echo    What is your name, adventurer?
echo.
    set /p playername=Name: 
goto prestart2

:prestart2
cls
echo.
echo    What would be your more valued statistic, Attack damage, or Hit points?
echo.
echo.
echo.
echo    1)Attack damage(Atk)
echo.
echo    2)Hit points(Hp)
echo.
echo.
echo.
echo    ---------
echo.
    set /p playermorevaluedstat=Choice: 
        if %playermorevaluedstat%==1
            set playeratk=6
            set playerhp=25
        if %playermorevaluedstat%==2 
            set playeratk=4
            set playerhp=30
        if NOT %playermorevaluedstat%==1 if NOT %playermorevaluedstat%==2 goto prestart2
cls
echo playeratk
echo playerhp
pause

I'm having trouble with the :prestart2 section of my code. With the end of it, I tried to make it where if the variable wasn't equal to 1 or 2, which were the options, then it send the player back up to the start of the section again, and also, when it finishes checking, I'm trying to make it display the two variables playeratk and playerhp but instead it just closes out. I am really lost here and would appreciate the help!

Upvotes: 1

Views: 55

Answers (1)

Wes Larson
Wes Larson

Reputation: 1062

A couple things before we start:

First, when troubleshooting your batch scripts, get rid of (comment out) your echo off and cls lines so that you can see where it's going wrong.

Second, you should always double-quote your variables to make sure you're not inadvertently including spaces when setting and comparing them. It doesn't seem to actually be causing problems in your code, but it's a good habit to get into:

set "var1=something"
set "var2=a string with spaces"
if "%var1%"=="something" echo %var1%

Now, the problem in your code is with the two if statements stretching over multiple lines. If you're going to do that, you have to put them inside of parentheses.

set /p playermorevaluedstat=Choice: 
        if %playermorevaluedstat%==1 (
            set playeratk=6
            set playerhp=25
            )
        if %playermorevaluedstat%==2 (
            set playeratk=4
            set playerhp=30
            )

Upvotes: 1

Related Questions