vendredi 4 novembre 2016

If statements 'leaking' into each other batch

OK, so I'm creating a math game in batch for school(because I thought it would be easier) I came across an odd problem when using if statements. So, basically when I enter 2 into the command line it goes to a line in the MIDDLE of an if statement inside another if statement it wasn't even supposed to go into.

@echo off
setlocal enabledelayedexpansion

:Start
@echo start game(1) or join game(2) note:game must be started for P2 to join
set /p YN=

IF %YN%==1 (
    @echo creating files...
    if not exist quizFiles (
        md "quizFiles"
    )
    @echo P1> quizFiles/gameState.txt
    @echo 48> quizFiles/cards.txt
    @echo.0> quizFiles/P1Score.txt
    @echo.0> quizFiles/P2Score.txt
    @echo false> quizFiles/P2Joined.txt
    set /a gameMode=%RANDOM% %%4
    @echo.!gameMode!> quizFiles/gameMode.txt
    IF "!gameMode!"=="0" (
        set gameMode=LCM
    ) ELSE IF "!gameMode!"=="1" (
    set gameMode=GCF
    ) ELSE IF "!gameMode!"=="2" (
        set gameMode=Muliplication of fractions
    ) ELSE IF "!gameMode!"=="3" (
        set gameMode=Division of fractions
    )
    @echo.!gameMode!
    timeout /t 1 /nobreak > NUL
    @echo waiting for other player to join...
    set dot="2"
    :WP2
    set /p P2Joined=<quizFiles/P2Joined.txt
    IF NOT "!P2Joined!"=="true" (
        timeout /t 2 /nobreak > NUL
        @echo waiting...
        GOTO WP2
    )
    @echo joined
    :P1G
        set /p cardsRemaining=<quizFiles/cards.txt
        @echo cards left: !cardsRemaining!
        set /p P1Score=<quizFiles/P1Score.txt
        @echo P1 has: !P1Score! cards
        set /p P2Score=<quizFiles/P2Score.txt
        @echo P2 has: !P2Score! cards
        set /p currentPlayer=<quizFiles/gameState.txt
        @echo It is now !currentPlayer!'s turn
        @echo 4 cards chosen
        set /p gameMode=<quizFiles/gameMode.txt
        IF "!gameMode!"=="0" (
            set /a card1=%RANDOM% %%96
            set /a card2=%RANDOM% %%96
            set /a card3=%RANDOM% %%96
            set /a card4=%RANDOM% %%96
        )
        @echo card1: !card1!
        @echo card2: !card2!
        @echo card3: !card3!
        @echo card4: !card4!
        IF "!gameMode!"=="0" (
            @echo choose your first card(1-4)
            set /p chosenCard1=
            @echo choose your second card(1-4)
            set /p chosenCard2=
            @echo what is the LCM of these two cards
            set /p userAnswer=
         )
    GOTO P1G
) ELSE IF %YN%==2 (
    @echo joining game...
    @echo true> quizFiles/P2Joined.txt
    timeout /t 1 /nobreak > NUL
    @echo joined
)
PAUSE

Aucun commentaire:

Enregistrer un commentaire