移动瓷砖BATCH

时间:2017-09-17 20:40:12

标签: batch-file

我一直在努力拼图,但出于某种原因,这不起作用。

每张幻灯片是一个随机字母,“a,b,c,d,e,f,g,h,i,j,k,l,m,n,o”我知道50%的时间这个可能是一个无法解决的难题(不知道除了常数之外怎么避免它)

除了它无法解决的一半时间运动不起作用,可能是因为瓷砖是字母而不是数字?这些动作由wasd键控制(w = up,a = left,s = down,d = right)

最后一点是,这可以更简单/有效吗?

echo  ____ ____ ____ ____
echo ^|    ^|    ^|    ^|    ^|
echo ^| %slide1% ^| %slide2% ^| %slide3% ^| %slide4% ^|
echo ^|____^|____^|____^|____^|
echo ^|    ^|    ^|    ^|    ^|
echo ^| %slide5% ^| %slide6% ^| %slide7% ^| %slide8% ^|
echo ^|____^|____^|____^|____^|
echo ^|    ^|    ^|    ^|    ^|
echo ^| %slide9% ^| %slide10% ^| %slide11% ^| %slide12% ^|
echo ^|____^|____^|____^|____^|
echo ^|    ^|    ^|    ^|    ^|
echo ^| %slide13% ^| %slide14% ^| %slide15% ^| %slide16%  ^|
echo ^|____^|____^|____^|____^|

choice /c wasdr /n
if %errorlevel% == 1 goto movew
if %errorlevel% == 2 goto movea
if %errorlevel% == 3 goto moves
if %errorlevel% == 4 goto moved
if %errorlevel% == 5 goto reset
:movew
if %pos% GEQ 13 goto display
set /a helper=%pos% + 4
set /a slide%pos%=!slide%helper%!
set slide%helper%=%default%
set /a pos=%pos% + 4
:movea
if %pos% == 4 goto display
if %pos% == 8 goto display
if %pos% == 12 goto display
if %pos% == 16 goto display
set /a helper=%pos% + 1
set /a slide%pos%=!slide%helper%!
set slide%helper%=%default%
set /a pos=%pos% + 1
goto display
:moves
if %pos% LEQ 4 goto display
set /a helper=%pos% - 4
set /a slide%pos%=!slide%helper%!
set slide%helper%=%default%
set /a pos=%pos% - 4
goto display
:moved
if %pos% == 1 goto display
if %pos% == 5 goto display
if %pos% == 9 goto display
if %pos% == 13 goto display
set /a helper=%pos% - 1
set /a slide%pos%=!slide%helper%!
set slide%helper%=%default%
set /a pos=%pos% - 1
goto display

谢谢,

1 个答案:

答案 0 :(得分:0)

你问题的正确回答是:“是的!”。

然而,在这种情况下,你的下一个问题是“如何?”当然,回答这个新问题的唯一方法就是显示代码。所以这就是:

@echo off
setlocal EnableDelayedExpansion

set "size=4"
set "empty=  "

:label-5 reset
rem Populate the puzzle with random numbers
set /A size2=size*size-1
set "numbers="
for /L %%i in (1,1,%size2%) do (
   set "number= %%i"
   set "numbers=!numbers!!number:~-2!"
)
set "numbers=%numbers%%empty%"
set "nums=%numbers%"
set /A n=size2+1
for /L %%j in (1,1,%size%) do (
   for /L %%i in (1,1,%size%) do (
      set /A num=!random!%%n*2, numP2=num+2, n-=1
      for /F "tokens=1,2" %%m in ("!num! !numP2!") do (
         set "slide[%%j][%%i]=!nums:~%%m,2!"
         if "!nums:~%%m,2!" equ "%empty%" set /A i=%%i, j=%%j
         set "nums=!nums:~0,%%m!!nums:~%%n!"
      )
   )
)

:refresh
rem Show the puzzle
set "nums="
< NUL (
   cls
   for /L %%i in (1,1,%size%) do set /P "=.____"
   echo .
   for /L %%j in (1,1,%size%) do (
      for /L %%i in (1,1,%size%) do set /P "=|    "
      echo ^|
      for /L %%i in (1,1,%size%) do set /P "=| !slide[%%j][%%i]! " & set "nums=!nums!!slide[%%j][%%i]!"
      echo ^|
      for /L %%i in (1,1,%size%) do set /P "=|____"
      echo ^|
   )
)
if "%nums%" equ "%numbers%" goto completed

:getKey
rem Move the empty square
choice /C wasdrx /N
if errorlevel 5 goto label-%errorlevel%  Reset or eXit
set /A "mod2=%errorlevel% %% 2, newJ=j+(%errorlevel%-2)*mod2, newI=i+(%errorlevel%-3)*^!mod2"
if not defined slide[%newJ%][%newI%] goto getKey
for /F "tokens=1,2" %%i in ("%newI% %newJ%") do set "slide[%j%][%i%]=!slide[%%j][%%i]!" & set "slide[%%j][%%i]=%empty%"
set /A i=newI, j=newJ
goto refresh

:completed
echo Completed^^^!

:label-6 exit