批处理if语句错误

时间:2014-02-20 00:24:40

标签: batch-file

我试图批量制造扫雷,但我遇到了奇怪的错误。当我运行文件时,它会放置5个地雷。然后它通过其余的方块并检查每个方块附近有多少个地雷。我的问题是它只能感觉到东南方的输出。东南代码与任何其他方向的代码没有什么不同。这对我来说非常困惑。似乎没有其他人有这个错误。这是一个示例输出:

M 0 1 0 0
0 1 0 M 0
M 0 M 0 0
0 0 1 0 0
0 0 0 M 0

非常感谢答案。

@echo off
setlocal enableDelayedExpansion
setlocal ENABLEEXTENSIONS
title Batch Minesweeper 
color 80 
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 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
cls
echo Batch Minesweeper
echo.
echo [1] - Play
echo [2] - Info
echo [3] - Exit
echo.
echo Batch Minesweeper made by Matt Pauly
choice /c 123 /n /m ""
if %errorlevel%==1 goto startGame 
if %errorlevel%==1 goto info 
if %errorlevel%==1 goto end

:startGame
cls
set /a ranX=%RANDOM% * 5 / 32768 + 1
set /a ranY=%RANDOM% * 5 / 32768 + 1
if not %mines%==5 (
    if !mine%ranX%_%ranY%!==0 (
        set mine%ranX%_%ranY%=M
        set /a mines=%mines% + 1
        goto startGame
        )
    goto startGame
    )
goto setup

:setup
cls
echo Loading...
set curVal=%setX%_%setY%
if %curVal%==1_6 goto display
if %setX%==6 (
    set setX=1
    set /a setY=%setY% + 1
    goto setup
    )
if !mine%setX%_%setY%!==M (
    set /a setX=%setX% + 1
    goto setup
    )
set /a testY=%setY% + 1
set testX=%setX%    
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 testY=%setY%
set /a testX=%setX% - 1 
set S=!mine%testX%_%testY%! 
set /a testY=%setY% - 1
set /a testX=%setX% - 1 
set SW=!mine%testX%_%testY%!    
set /a testY=%setY% + 1
set testX=%setX%    
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 "%S%"=="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 setup

:display    
cls
::echo 5    %rect1_5%    %rect2_5%    %rect3_5%    %rect4_5%    %rect5_5%
::echo.
::echo 4    %rect1_4%    %rect2_4%    %rect3_4%    %rect4_4%    %rect5_4%
::echo.
::echo 3    %rect1_3%    %rect2_3%    %rect3_3%    %rect4_3%    %rect5_3%
::echo.
::echo 2    %rect1_2%    %rect2_2%    %rect3_2%    %rect4_2%    %rect5_2%
::echo.
::echo 1    %rect1_1%    %rect2_1%    %rect3_1%    %rect4_1%    %rect5_1%
::echo.
::echo      a    b    c    d    e 
echo 5    %mine1_5%    %mine2_5%    %mine3_5%    %mine4_5%    %mine5_5%
echo.
echo 4    %mine1_4%    %mine2_4%    %mine3_4%    %mine4_4%    %mine5_4%
echo.
echo 3    %mine1_3%    %mine2_3%    %mine3_3%    %mine4_3%    %mine5_3%
echo.
echo 2    %mine1_2%    %mine2_2%    %mine3_2%    %mine4_2%    %mine5_2%
echo.
echo 1    %mine1_1%    %mine2_1%    %mine3_1%    %mine4_1%    %mine5_1%
echo.
echo      a    b    c    d    e 
pause

:info

:end
exit

1 个答案:

答案 0 :(得分:2)

您的主要问题是线条上的尾随空格。在传统的set语句中,任何尾随空格都包含在分配的值中,"M""M "是完全不同的东西,因此如果您有set X=!something!则分配的值与set X=!something!指定的不同。

如果您将XY混淆,也会遇到问题。例如,使用WX设置Y+1 - 与设置N相同。这应该是X-1Y