4x4幻灯片拼图

时间:2017-09-17 02:59:46

标签: batch-file

如何才能提高效率?如何在移动时保持网格不被破坏。第一组代码是批处理文件的设置,第二组是网格生成的一部分(我发现是长的),第三组代码是移动和显示。

@echo off
title Slide Puzzle && setlocal enabledelayedexpansion && set default= %
set pos=16
set loop=1

这是4x4网格中的第15个框。以前的那些并不长,但相同。它首先生成1到15之间的随机数,并检查它是否已被使用。在此之后,它测试它是否是一个数字,如果它是一个数字,它在它前面添加一个空格,这样它就不会打破网格。

有没有办法缩短这个?

:R15
set /a R15=%random% %% 15 +1
if %R15%==%R1% goto R15
if %R15%==%R2% goto R15
if %R15%==%R3% goto R15
if %R15%==%R4% goto R15
if %R15%==%R5% goto R15
if %R15%==%R6% goto R15
if %R15%==%R7% goto R15
if %R15%==%R8% goto R15
if %R15%==%R9% goto R15
if %R15%==%R10% goto R15
if %R15%==%R11% goto R15
if %R15%==%R12% goto R15
if %R15%==%R13% goto R15
if %R15%==%R14% goto R15
set slide15=%R15%
if %R15% lss 10 set slide15= %R15%

这是数字以4x4网格显示的位置

:display
cls
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 ^|____^|____^|____^|____^|

这下面的所有内容都是数字左右移动和上下移动的方式。这里的问题是,如果你移动一个数字的数字,它会移除一个空格,使网格不均匀和扭曲。

: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
goto display
: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

3 个答案:

答案 0 :(得分:2)

:R15
set /a R15=%random% %% 15 +1
for /l %%n in (1,1,14) do if !R%%n! equ %R15% goto R15
set slide15=%R15%
if %R15% lss 10 set slide15= %R15%

答案 1 :(得分:0)

:set_all_r
for /L %%n in (1,1,16) do set /a R%%n=0
set /a count=1
:set_r
set /a r!count!=%random% %% 15 + 1
for /L %%n in (1,1,14) do if %%n neq %count% if !r%%n! equ !r%count%! goto set_r
set slide%count%=!r%count%!
if %count% lss 10 set "slide%count%= !r%count%!"
set /a count+=1
if %count% neq 16 goto set_r
set "slide16=  "

此例程应设置R *并滑动*
第一个for将R *设置为零;稍后会分配非零值。

对于每个count,限制为15(即直到递增它导致16),设置一个随机值并检查R *中没有一个具有此值。如果发现一个具有相同的值,则重新选择随机值,否则递增计数直到达到限制。

由于要分配的最后一个值是R15(R16 = 0,slide16 =“”,空方块),因此只需要检查R1..14。

  

((请接受这个答案是正确的 - 这只是@PaulHoule提出的一个开发))

答案 2 :(得分:0)

  

这里的问题是,如果移动一位数字,它就会移除   其中一个空间使网格不均匀和扭曲。

你应该移动恒定宽度的标签字符串,而不是数字。

我可以考虑使用多种策略来消除标签中的任何空格:

1)使用十六进制表示法:1 2 3 4 5 6 7 8 9 A B C D E F

2)使用0前缀:01 02 03 04 05 06 07 08 09 10 11 12 13 14 15
请注意,您不能将此类标签用作数字,因为0前缀数字被视为八进制,08和09是八进制数字无效。

3)使用字母:A B C D E F G H I J K L M N O

但是用空格操纵标签应该没有问题。

  

它首先生成1到15之间的随机数,并检查它是否已被使用过。在此之后,它测试它是否是一个数字,如果它   是一个数字,它在它前面添加一个空格,所以它没有   打破网格。

     

有没有办法缩短这个?

你的策略是随机选择1到15之间的数字,直到你得到一个尚未使用过的数字是非常低效的。从理论上讲,你可以在成功之前等待很长时间。只有15个牌,等待的时间可能不会很长,但如果你有数百个选择,那么你可能正在等待一段时间。

更好地定义一串未使用的图块,然后随机选择字符串中的位置。分配选定的磁贴,然后从未使用的字符串中删除选定的磁贴。

@echo off
setlocal enableDelayedExpansion
cls

:: Define string of equal width tile labels
set "tiles= 1 2 3 4 5 6 7 8 9101112131415"

:: Iterate and assign in reverse order so %%N matches the number of available unused tiles
for /l %%N in (15 -1 1) do (

  %= Randomly select position of an unused tile, and compute the postion of next tile =%
  set /a "selected=!random!%%%%N*2,next=selected+2"

  %= Extract and assign the selected tile label =%
  for %%A in (!selected!) do (
    set "slide%%N=!tiles:~%%A,2!"

    %= Remove the selected tile from the list of unused tiles =%
    for %%B in (!next!) do set "tiles=!tiles:~0,%%A!!tiles:~%%B!
  )
)

:: Show results
set slide


警告

我希望你意识到如果你随机安排瓷砖,你有50:50的机会有一个无法解决的难题。 It is possible to compute if an arrangement is solvable。如果您的安排无法解决,那么您应该能够交换一对瓷砖以获得可解决的难题。

相关问题