2. 汉诺塔算法版 - 自动 or 人工运算 For DOS
@echo off
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::
::本汉诺塔游戏的规则如下:
:: 把a上的盘子(从上到下依次为1.2.3...大于1的数字为多个重复只是为了直观的显示盘子的大小,
::并无其它意义)借住b逐个全部移到c上,在移动的过程中,不能把大的盘子放在小盘子上面。
:: 输入的规则为:第一个字符为要移动的盘子的编号,第二个字符为要移动的盘子所在的位置,
::第三个字符为盘子移动后的位置。如下所示:
:: 1
:: 222
:: 33333
:: _______ _______ _______
:: a b c
::要把编号为1的盘子移动到c上,输入"1ac"回车即可。
::
::以下为最终的目的:
:: 1
:: 222
:: 33333
:: _______ _______ _______
:: a b c
::
::在提示输入盘子个数时可以输入"h"观看游戏演示
::本游戏由于要考虑效率的关系,并没做太多错误输入的判断,所以请严格按要求输入...
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:bgn
echo.
setlocal enabledelayedexpansion
set "ttl=9"
:agn
set input=&set /p input=请输入盘子的个数(2~7)(h游戏演示,回车退出):
if not defined input goto :eof
if /i "%input%"=="h" (
set "movie=movie"
set "input=5"
goto :skp
)
if %input% lss 2 (echo.别那么弱智好不好...&goto :agn)
if %input% gtr %ttl% (echo.太多了,你想玩到猴年马月呀...&goto :agn)
:skp
cls
set /a a=1,all=1
for /l %%a in (%input% -1 1) do (
set /a n+=1,m=n*2-1,all*=2
set "space="
for /l %%i in (1 1 %%a) do (set "space=!space! ")
for /l %%x in (1 1 !m!) do (call set "code!n!=%%code!n!%%!n!")
call set "col_a!n!=!space!%%code!n!%%!space!"
call echo.%%col_a!n!%%
)
set /a m+=2,all-=1&set "space="&set "kong= "
for /l %%a in (1 1 %m%) do (
set "line=!line!_"
set "empty=!empty! "
)
set "line=!line!!kong!!line!!kong!!line!"
echo.!line!
set /a "tmp_=%m%/2"
for /l %%a in (1 1 %tmp_%) do (set "space=!space! ")
set "line2=!space!a!space!!kong!!space!b!space!!kong!!space!c"
echo.!line2!
for /l %%i in (1 1 %input%) do (
set "a%%i=ok"
set "col_b%%i=%empty%"
set "col_c%%i=%empty%"
)
if defined movie (goto :movie)
:game
echo.&set in=&set /p in=请输入(回车开始新游戏):
if not defined in (endlocal&goto :bgn)
if "%in:~-2,1%"=="%in:~-1%" (
echo.请别开这种玩笑好不好^^^!^^^!^^^!
goto :game
)
if %in:~0,-2% gtr !input! (
echo.盘子的编号超出范围了^^^!^^^!^^^!
goto :game
)
if %in:~0,-2% lss 1 (
echo.盘子的编号超出范围了^^^!^^^!^^^!
goto :game
)
set /a "tmp_=%in:~0,-2%-1"&set "tmp_1=!in:~-1!"&set "tmp_2=!in:~-2,1!"
if %tmp_% gtr 0 (
for /l %%a in (1 1 !tmp_!) do (
if defined !tmp_2!%%a (
echo.请先把上面的小盘子移走...
goto :game
)
if defined !tmp_1!%%a (
echo.大盘子不能放在小盘子的上面^^^!^^^!^^^!
goto :game
)
)
)
set /a "times+=1"
set "%in:~-2,1%%in:~0,-2%="
set "%in:~-1%%in:~0,-2%=ok"
call set "var=%%col_!in:~-2,1!!%in:~-2,1%!%%"
call :set_ %in:~-1% "!var!"
set "col_%in:~-2,1%!%in:~-2,1%!=%empty%"
set /a %in:~-2,1%+=1
cls
set n=0
for /l %%i in (1 1 %input%) do (
echo.!col_a%%i!!kong!!col_b%%i!!kong!!col_c%%i!
if defined c%%i (set /a n+=1)
)
echo.!line!&echo.!line2!
if "%n%"=="%input%" (
echo.&echo.恭喜你,完全正确,你用%times%步完成了这次游戏...&echo.
if "%times%"=="%all%" (
echo.你的方法是最便捷的,太厉害了^^^!^^^!^^^!
) else (
echo.最便捷的方法只用%all%步就可以完成哦,继续努力哦^^^!^^^!^^^!
)
endlocal
goto :bgn
)
goto :game
:set_
for /l %%a in (%input% -1 1) do (
if "!col_%1%%a: =!"=="" (
set "col_%1%%a=%~2"
set "%1=%%a"
goto :eof
)
)
goto :eof
:movie
type nul>"ans_hanoi.txt"
call :show !input! a b c
for /f "delims=" %%i in ('type "ans_hanoi.txt"') do (
set "in=%%i"
call call set "var=%%%%col_!in:~-2,1!%%!in:~-2,1!%%%%%%"
call :set_ !in:~-1! "!var!"
call set "col_!in:~-2,1!%%!in:~-2,1!%%=!empty!"
set /a "!in:~-2,1!+=1"
ping -n 2 127.1 >nul
cls
for /l %%m in (1 1 !input!) do (
echo.!col_a%%m!!kong!!col_b%%m!!kong!!col_c%%m!
)
echo.!line!&echo.!line2!
)
echo.&echo.演示完毕...&endlocal&goto :bgn
:show
if "%1"=="1" (
>>"ans_hanoi.txt" echo.%1%2%4
) else (
set /a "var=%1-1"
call :show !var! %2 %4 %3
>>"ans_hanoi.txt" echo.%1%2%4
set /a "var=%1-1"
call :show !var! %3 %2 %4
)
goto :eof