标题: [游戏娱乐] 批处理版汉诺塔游戏 [打印本页]
作者: more 时间: 2009-6-11 23:21 标题: 批处理版汉诺塔游戏
- @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>"%temp%\ans_hanoi.txt"
- call :show !input! a b c
- for /f "delims=" %%i in ('type "%temp%\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" (
- >>"%temp%\ans_hanoi.txt" echo.%1%2%4
- ) else (
- set /a "var=%1-1"
- call :show !var! %2 %4 %3
- >>"%temp%\ans_hanoi.txt" echo.%1%2%4
- set /a "var=%1-1"
- call :show !var! %3 %2 %4
- )
- goto :eof
复制代码
作者: pusofalse 时间: 2009-6-12 14:45
请问是原创吗?如果是,这样的代码放在原创版块比较好。
作者: more 时间: 2009-6-12 17:07
回版主,这是本人原创的.
如果版主觉得有必要的话,就请把帖子移到原创代码区吧.
下面的代码是对顶楼的一些小小的修改,修改的主要内容有:
1.演示的盘子个数为随机的3~7个.
2.由于担心代码会被使用者修改引起变量长度超出范围从而导致演示过程出现错误,所以演示的过程产生了一个临时文件,如果不修改的话是可以不产生临时文件的,如下面的代码.
3.增加了一个对输入错误的判断.- @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 /a "input=!random!%%5+3"
- 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)
- echo.%in:~-2%|findstr /i /v "^[abc]*$" >nul
- if "%errorlevel%"=="0" (
- echo.目标位置不正确^^^!^^^!^^^!
- goto :game
- )
- 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
- set "str="
- call :show !input! a b c
- for %%i in (!str!) 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" (
- set "str=!str!%1%2%4 "
- ) else (
- set /a "var=%1-1"
- call :show !var! %2 %4 %3
- set "str=!str!%1%2%4 "
- set /a "var=%1-1"
- call :show !var! %3 %2 %4
- )
- goto :eof
复制代码
作者: PakTC 时间: 2009-6-14 15:02
直接仿照c 的汉诺塔解法, 换成批处理的-
- @echo off
- set /p inp="1-7: "
- call :next %inp% a c b
- pause
- :next
- if %1 equ 0 goto :eof
- set /a now=%1-1
- call :next %now% %2 %4 %3
- echo,%1 from %2 to %3
- set /a now=%1-1
- call :next %now% %4 %3 %2
复制代码
刚刚做了一个无聊举动,把上面显示答案部分 改成 %1%2%3直接显示
然后复制一整列,在游戏上粘贴…… 自移动速度好快。
[ 本帖最后由 PakTC 于 2009-6-14 15:13 编辑 ]
作者: garyng 时间: 2011-9-1 20:48
7个盘很多了也~
2^7-1=127
最少要127步勒~
欢迎光临 批处理之家 (http://bathome.net./) |
Powered by Discuz! 7.2 |