批量变量没有响应

时间:2014-02-22 05:07:45

标签: batch-file

我正在制作一个扫雷游戏,我在检测周围是否显示0和0时遇到问题。例如,它会输出:

4 
3 
2 
1 0
  a b c d 

而不是:

4
3 1
2 0 2 1
1 0 0 1
  a b c d

这是我的代码。线路是我遇到麻烦的。标题“0tester”是我的问题所在。

@echo off
setlocal enableDelayedExpansion
setlocal ENABLEEXTENSIONS
title Batch Minesweeper 
color 80
goto define

:define
set setX=1
set setY=1
set testX=1
set testY=1
set plrX=1
set plyY=1
set mines=0
set mineNum=0
set N=
set NE=
set E=
set SE=
set S=
set SW=
set W=
set NW=
set rect1_1= 
set rect2_1= 
set rect3_1= 
set rect4_1= 
set rect5_1= 
set rect1_2= 
set rect2_2= 
set rect3_2= 
set rect4_2= 
set rect5_2= 
set rect1_3= 
set rect2_3= 
set rect3_3= 
set rect4_3= 
set rect5_3= 
set rect1_4= 
set rect2_4= 
set rect3_4= 
set rect4_4= 
set rect5_4= 
set rect1_5= 
set rect2_5= 
set rect3_5= 
set rect4_5= 
set rect5_5= 
set rect6_1= 
set rect7_1= 
set rect8_1= 
set rect9_1= 
set rect6_2= 
set rect7_2= 
set rect8_2= 
set rect9_2= 
set rect6_3= 
set rect7_3= 
set rect8_3= 
set rect9_3= 
set rect6_4= 
set rect7_4= 
set rect8_4= 
set rect9_4= 
set rect6_5= 
set rect7_5= 
set rect8_5= 
set rect9_5= 
set rect1_6= 
set rect2_6= 
set rect3_6= 
set rect4_6= 
set rect5_6= 
set rect6_6= 
set rect7_6= 
set rect8_6= 
set rect9_6= 
set rect1_7= 
set rect2_7= 
set rect3_7= 
set rect4_7= 
set rect5_7= 
set rect6_7= 
set rect7_7= 
set rect8_7= 
set rect9_7= 
set rect1_8= 
set rect2_8= 
set rect3_8= 
set rect4_8= 
set rect5_8= 
set rect6_8= 
set rect7_8= 
set rect8_8= 
set rect9_8= 
set rect1_9= 
set rect2_9= 
set rect3_9= 
set rect4_9= 
set rect5_9= 
set rect6_9= 
set rect7_9= 
set rect8_9= 
set rect9_9= 
set mine1_1=0
set mine2_1=0
set mine3_1=0
set mine4_1=0
set mine5_1=0
set mine1_2=0
set mine2_2=0
set mine3_2=0
set mine4_2=0
set mine5_2=0
set mine1_3=0
set mine2_3=0
set mine3_3=0
set mine4_3=0
set mine5_3=0
set mine1_4=0
set mine2_4=0
set mine3_4=0
set mine4_4=0
set mine5_4=0
set mine1_5=0
set mine2_5=0
set mine3_5=0
set mine4_5=0
set mine5_5=0
set mine6_1=0
set mine7_1=0
set mine8_1=0
set mine9_1=0
set mine6_2=0
set mine7_2=0
set mine8_2=0
set mine9_2=0
set mine6_3=0
set mine7_3=0
set mine8_3=0
set mine9_3=0
set mine6_4=0
set mine7_4=0
set mine8_4=0
set mine9_4=0
set mine6_5=0
set mine7_5=0
set mine8_5=0
set mine9_5=0
set mine1_6=0
set mine2_6=0
set mine3_6=0
set mine4_6=0
set mine5_6=0
set mine6_6=0
set mine7_6=0
set mine8_6=0
set mine9_6=0
set mine1_7=0
set mine2_7=0
set mine3_7=0
set mine4_7=0
set mine5_7=0
set mine7_7=0
set mine8_7=0
set mine9_7=0
set mine1_8=0
set mine2_8=0
set mine3_8=0
set mine4_8=0
set mine5_8=0
set mine6_8=0
set mine7_8=0
set mine8_8=0
set mine9_8=0
set mine1_9=0
set mine2_9=0
set mine3_9=0
set mine4_9=0
set mine5_9=0
set mine6_9=0
set mine7_9=0
set mine8_9=0
set mine9_9=0
goto menu

:menu
color 80
cls
echo Batch Minesweeper
echo.
echo [1] - Play
echo [2] - Info
echo [3] - Exit
echo.
echo Batch Minesweeper made by XXXXXXXXX
choice /c 123 /n /m ""
if %errorlevel%==1 goto play
if %errorlevel%==2 goto info 
if %errorlevel%==3 goto end

:info
cls
echo Each number rebresents how many mines are around it.
pause
echo Every thing else is self explanitory!
pause 
goto menu

:play
cls
color 80
echo Play Menu
echo.
echo [1] - Easy
echo [2] - Medium
echo [3] - Hard
echo [4] - Custom
echo [5] - Back to Menu
choice /c 12345 /n /m ""
if %errorlevel%==1 goto Estartgame
if %errorlevel%==2 goto Mplay
if %errorlevel%==3 goto Hplay
if %errorlevel%==4 goto Cplay
if %errorlevel%==5 goto menu

:Mplay 
cls
echo This game mode is currently univalible.
pause
goto menu

:Hplay
cls
echo This game mode is currently univalible.
pause
goto menu

:Cplay
cls
echo This game mode is currently univalible.
pause
goto menu

:EstartGame
cls
set /a ranX=%RANDOM% * 9 / 32768 + 1
set /a ranY=%RANDOM% * 9 / 32768 + 1
if not %mines%==10 (
    if !mine%ranX%_%ranY%!==0 (
        set mine%ranX%_%ranY%=M
        set /a mines=%mines% + 1
        goto EstartGame
        )
    goto EstartGame
    )
goto Esetup

:Esetup
cls
echo Loading...
set curVal=%setX%_%setY%
if %curVal%==1_10 goto Edisplay
if %setX%==10 (
    set setX=1
    set /a setY=%setY% + 1
    goto Esetup
    )
if !mine%setX%_%setY%!==M (
    set /a setX=%setX% + 1
    goto Esetup
    )
set testX=%setX%
set /a testY=%setY% + 1
set N=!mine%testX%_%testY%!
set /a testY=%setY% + 1
set /a testX=%setX% + 1
set NE=!mine%testX%_%testY%!
set testY=%setY%
set /a testX=%setX% + 1
set E=!mine%testX%_%testY%!
set /a testY=%setY% - 1
set /a testX=%setX% + 1
set SE=!mine%testX%_%testY%!
set testX=%setX%
set /a testY=%setY% - 1
set S=!mine%testX%_%testY%!
set /a testY=%setY% - 1
set /a testX=%setX% - 1
set SW=!mine%testX%_%testY%!
set /a testX=%setX% - 1
set testY=%setY%
set W=!mine%testX%_%testY%!
set /a testY=%setY% + 1
set /a testX=%setX% - 1
set NW=!mine%testX%_%testY%!
set mineNum=0
if defined N if "%N%"=="M" set /a mineNum=%mineNum% + 1
if defined NW if "%NW%"=="M" set /a mineNum=%mineNum% + 1
if defined W if "%W%"=="M" set/a mineNum=%mineNum% + 1
if defined SW if "%SW%"=="M" set /a mineNum=%mineNum% + 1
if defined S if "%S%"=="M" set /a mineNum=%mineNum% + 1
if defined SE if "%SE%"=="M" set /a mineNum=%mineNum% + 1
if defined E if "%E%"=="M" set /a mineNum=%mineNum% + 1
if defined NE if "%NE%"=="M" set /a mineNum=%mineNum% + 1
set mine%setX%_%setY%=%mineNum%
set /a setX=%setX% + 1
goto Esetup

:Edisplay   
cls
echo 9    %rect1_9%    %rect2_9%    %rect3_9%    %rect4_9%    %rect5_9%    %rect6_9%    %rect7_9%    %rect8_9%    %rect9_9%
echo.
echo 8    %rect1_8%    %rect2_8%    %rect3_8%    %rect4_8%    %rect5_8%    %rect6_8%    %rect7_8%    %rect8_8%    %rect9_8%
echo.
echo 7    %rect1_7%    %rect2_7%    %rect3_7%    %rect4_7%    %rect5_7%    %rect6_7%    %rect7_7%    %rect8_7%    %rect9_7%
echo.
echo 6    %rect1_6%    %rect2_6%    %rect3_6%    %rect4_6%    %rect5_6%    %rect6_6%    %rect7_6%    %rect8_6%    %rect9_6%
echo.
echo 5    %rect1_5%    %rect2_5%    %rect3_5%    %rect4_5%    %rect5_5%    %rect6_5%    %rect7_5%    %rect8_5%    %rect9_5%
echo.
echo 4    %rect1_4%    %rect2_4%    %rect3_4%    %rect4_4%    %rect5_4%    %rect6_4%    %rect7_4%    %rect8_4%    %rect9_4%
echo.
echo 3    %rect1_3%    %rect2_3%    %rect3_3%    %rect4_3%    %rect5_3%    %rect6_3%    %rect7_3%    %rect8_3%    %rect9_3%
echo.
echo 2    %rect1_2%    %rect2_2%    %rect3_2%    %rect4_2%    %rect5_2%    %rect6_2%    %rect7_2%    %rect8_2%    %rect9_2%
echo.
echo 1    %rect1_1%    %rect2_1%    %rect3_1%    %rect4_1%    %rect5_1%    %rect6_1%    %rect7_1%    %rect8_1%    %rect9_1%
echo.
echo      a    b    c    d    e    f    g    h    j  
set /p inputX="Enter a,b,c,d,e,f,g,h or j and press enter."
set /p inputY="Enter 1,2,3,4,5,6,7,8 or 9 and press enter."
if %inputX%==a set inputX=1
if %inputX%==b set inputX=2
if %inputX%==c set inputX=3
if %inputX%==d set inputX=4
if %inputX%==e set inputX=5
if %inputX%==f set inputX=6
if %inputX%==g set inputX=7
if %inputX%==h set inputX=8
if %inputX%==j set inputX=9
if "!mine%inputX%_%inputY%!"=="M" (
cls
color 40 
echo BOOM, you lose
echo.
pause
cls
set curLVL=E
echo Play again?
pause
goto define
)
if "!mine%inputX%_%inputY%!"=="0" (
set rect%inputX%_%inputY%=0
set setX=%inputX%
set setY=%inputY%
goto 0tester
)
set rect%inputX%_%inputY%=!mine%inputX%_%inputY%!
goto Edisplay 

:0tester
set mbyX=0
set mbyY=0
set testX=%setX%
set /a testY=%setY% + 1
if defined mine%testX%_%testY% if !mine%testX%_%testY%!=="0" (
set rect%testX%_%testY%=0
set mbyX=%testX%
set mbyY=%testY%
)
set /a testY=%setY% + 1
set /a testX=%setX% + 1
if defined mine%testX%_%testY% if !mine%testX%_%testY%!=="0" (
set rect%testX%_%testY%=0
set mbyX=%testX%
set mbyY=%testY%
)
set testY=%setY%
set /a testX=%setX% + 1
if defined mine%testX%_%testY% if !mine%testX%_%testY%!=="0" (
set rect%testX%_%testY%=0
set mbyX=%testX%
set mbyY=%testY%
)
set /a testY=%setY% - 1
set /a testX=%setX% + 1
if defined mine%testX%_%testY% if !mine%testX%_%testY%!=="0" (
set rect%testX%_%testY%=0
set mbyX=%testX%
set mbyY=%testY%
)
set testX=%setX%
set /a testY=%setY% - 1
if defined mine%testX%_%testY% if !mine%testX%_%testY%!=="0" (
set rect%testX%_%testY%=0
set mbyX=%testX%
set mbyY=%testY%
)
set /a testY=%setY% - 1
set /a testX=%setX% - 1
if defined mine%testX%_%testY% if !mine%testX%_%testY%!=="0" (
set rect%testX%_%testY%=0
set mbyX=%testX%
set mbyY=%testY%
)
set /a testX=%setX% - 1
set testY=%setY%
if defined mine%testX%_%testY% if !mine%testX%_%testY%!=="0" (
set rect%testX%_%testY%=0
set mbyX=%testX%
set mbyY=%testY%
)
set /a testY=%setY% + 1
set /a testX=%setX% - 1
if defined mine%testX%_%testY% if !mine%testX%_%testY%!=="0" (
set rect%testX%_%testY%=0
set mbyX=%testX%
set mbyY=%testY%
)
if "%mbyX%%mbyY%"=="00" goto Edisplay
set setX=%setX%
set setY=%mbyY%
goto 0tester

:end
exit

1 个答案:

答案 0 :(得分:0)

if defined mine%testX%_%testY% if !mine%testX%_%testY%!=="0" (

上一行有两点:

  • 当您比较两个值的相等性时,这些值必须具有相同的形式,即两个值必须用引号括起来,或者两者都不用。否则,0无法等同于"0"
  • 如果未定义变量mine%testX%_%testY%,则前一行首先展开作为if defined mineX_Y if =="0" (然后解析,从而产生错误。修复此错误(以及前一个错误)的方法是将变量值括在引号中:

if defined mine%testX%_%testY% if "!mine%testX%_%testY%!"=="0" (

这样,如果未定义变量,则该行变为:if defined mineX_Y if ""=="0" (,其语法正确。