mercredi 13 mai 2015

Batch IF-Else commands in multi-layer nesting

I just started learning batch, and figured the best way to learn would be to give myself a project. I'm attempting a text-based RPG, and am hung up in one spot. I am trying to nest multiple IF-IF NOT DEFINED, and IF-ELSE "questions" in one "conversation".

if "%choice18%"=="talk hag" (
        if "%glove%"=="0" (
                if "%ketchup%"=="2" (
                        echo.
                        echo "AHH! Can it really be ye, Cramforth?" the hag asks. "If it be, then ye wouldn't mind answering a few personal questions bout us to prove it, would ye?" GULP! Art thou ready to answer the hag's personnal questions?
                        echo.
                        pause
                        :hag
                        echo.
                        echo Yes or no:
                        set /p choice19=
                        echo.
                        if "%choice19%"=="no" (
                                cls
                                echo.
                                echo "Then I'll never know for sure," sniffs the hag. You Dungeonheartbreaker.
                                echo.
                                pause
                                cls
                                goto West2a
                        )
                        if "%choice19%"=="yes" (
                                cls
                                echo.
                                echo "Very well. If ye answers questions three, then I'll know thou art me precious Cramforth."
                                echo.
                                pause
                                cls
                                echo Where didst we geaux on our honeymoon? A: Wensleydire, B: Rottenscab, C: Blood Area, or D: Zork?
                                echo.
                                set /p choice20=
                                echo.
                                if "%choice20%"=="b" (
                                        cls
                                        echo.
                                        echo What did ye get me for me 114th birthday? A: Toad, B: Eyeball, C: N64 Paddle, D: Commemorative mug?
                                        echo.
                                        set /p choice21=
                                        echo.
                                        if "%choice21%"=="d" (
                                                cls
                                                echo.
                                                echo How many hags didst we have together? 1, 2, 3, or 4?
                                                echo.
                                                set /p choice22=
                                                echo.
                                                if "%choice22%"=="3" (
                                                        cls
                                                        echo.
                                                        echo "Cramforth! Tis really ye!" mistaking thee for her dead husband. Ye suffer several prickly kisses from the hag which leave the odor of rat liquer on thy face. "No wonder ye died of a bloody head, ye forgot thy FLASK GETTING GLOVE before ye set out to vanquish that rogue dungeon."
                                                        echo.
                                                        pause
                                                        echo.
                                                        echo "Perhaps ye can still defeat that dungeon, even in thy new hotter, spectal form." Um, eww. "Here!" she says and hands ye a wicked FLASK GETTING GLOVE!
                                                        echo.
                                                        pause
                                                        set glove=1
                                                        goto West2a
                                                ) else (
                                                        cls
                                                        echo.
                                                        echo "IMPOSTER!!" screams Hagatha Christie and leaps at you with jagged claws. At first, ye thinks she's just pouring ketchup all o'er ye. But then ye're like, oh right, that's my blood. Thou art dead. Next time maybe use the NOTES section we provided at the back of the manual.
                                                        echo.
                                                        pause
                                                        cls
                                                        goto HD
                                                )
                                        ) else (
                                                cls
                                                echo.
                                                echo "IMPOSTER!!" screams Hagatha Christie and leaps at you with jagged claws. At first, ye thinks she's just pouring ketchup all o'er ye. But then ye're like, oh right, that's my blood. Thou art dead. Next time maybe use the NOTES section we provided at the back of the manual.
                                                echo.
                                                pause
                                                cls
                                                goto HD
                                        )
                                ) else (
                                        cls
                                        echo.
                                        echo "IMPOSTER!!" screams Hagatha Christie and leaps at you with jagged claws. At first, ye thinks she's just pouring ketchup all o'er ye. But then ye're like, oh right, that's my blood. Thou art dead. Next time maybe use the NOTES section we provided at the back of the manual.
                                        echo.
                                        pause
                                        cls
                                        goto HD
                                )
                        )
                        if not defined "%choice19%" (
                                cls
                                echo.
                                echo Just answer the hag 'yes' or 'no'. Please?
                                echo.
                                goto hag
                        )
                ) else (
                        echo.
                        echo "Away with ye," she shrieks, "I speaketh only to me dead husband who died of a bloody head!" She becomes abruptly silent and goes back to her landbeforetimeing. A bloody head, eh?
                        echo.
                        pause
                        cls
                        goto West2a
                )
        ) else (
                echo.
                echo "Ye go show that dungeon who's lord, Cramforth! You da lord!"
                echo.
                pause
                cls
                goto West2a
        )
)

The problem I am running into, is that the first user input variable set /p choice19= ignores the IF commands the first time around.

Example:

:hag
echo.
echo Yes or no:
set /p choice19=

User inputs: yes

Ignores: if "%choice19%"=="yes" (

Goes to: if not defined "%choice19%" ( cls echo. echo Just answer the hag 'yes' or 'no'. Please? echo. goto hag

Redirects back to:

:hag
echo.
echo Yes or no:
set /p choice19=

User again inputs: yes

And this time it follows the correct command continuing down the "yes" branch.

However, this poses a problem for outcomes that do not redirect back to the question (ie. if "%choice20%"=="b" () and end the game.

I reviewed a similar issue (Nested IF ( IF ( ... ) ELSE( .. ) ) statement in batch) however, I'm noticing that it is slightly different than my case, being that my variables are defined. So, I'm at a loss. Any ideas?

Note: I am aware that I could restructure the commands without deep nesting to solve the problem, and just GOTO to different :LABELS. I just want to see what's causing this issue.

Aucun commentaire:

Enregistrer un commentaire