最近沉默了一段时间,昨天就绞尽脑汁写了一个批处理迷宫,代码就是长了些,乱了些,毕竟首次尝试制作,要参考前辈的又看不懂……
爱用call的坏习惯依然改不了(当你看到你的代码变成一个一个模块,可以一call就调用时,你会爱上call……虽然效率低)
不知各位前辈们是否也用类似方法,即以截取字符来完成这个任务~- @echo off
- setlocal enabledelayedexpansion
- set char=o
- :head
- cls
- set menu=
- set /p menu=Select map (1-3):
- if not "!menu!"=="1" if not "!menu!"=="2" if not "!menu!"=="3" goto head
- call :loadlevel!menu!
-
- :gamedisp
- cls
- for /l %%i in (1 1 !numline!) do (
- echo !line%%i! )
- ckey 38 40 37 39
- call :checkinput
- goto gamedisp
-
- :loadlevel1
- set line1=::::::::::::::::::::::
- set line2=:[o]:x ::
- set line3=:: :::::::::: ::
- set line4=:: ::
- set line5=::::::::::::::::::::::
- set oldy=2
- set oldx=2
- set numline=5
- exit /b
-
- :loadlevel2
- set line1=::::::::::::::
- set line2=:: [o] ::
- set line3=:: ::
- set line4=:: :::::::::::
- set line5=:: ::
- set line6=::::::::::: ::
- set line7=::x ::
- set line8=::::::::::::::
- set oldx=2
- set oldy=7
- set numline=8
- exit /b
-
- :loadlevel3
- set line1=:::::::::::::::::
- set line2=:[o]: :: ::
- set line3=:: :: :: :: :: ::
- set line4=:: :: :: :: :: ::
- set line5=:: :: :: :: ::
- set line6=:::::::: :: :: ::
- set line7=:: :: :: ::
- set line8=:: :::::::: :: ::
- set line9=:: ::x::
- set line10=:::::::::::::::::
- set oldx=2
- set oldy=2
- set numline=10
- exit /b
-
- :win
- cls
- echo Map !menu! completed..
- echo You Win.
- pause>nul
- goto head
-
- :checkinput
- if errorlevel 4 (call :right) else (
- if errorlevel 3 (call :left) else (
- if errorlevel 2 (call :down) else (
- if errorlevel 1 (call :up))))
- exit /b
-
- :up
- set /a newx=!oldx!-1
- set /a tempy=!oldy!+1
- if "!line%newx%:~%oldy%,1!"==":" exit /b
- if "!line%newx%:~%oldy%,1!"=="]" exit /b
- if "!line%newx%:~%oldy%,1!"=="[" exit /b
- if "!line%newx%:~%oldy%,1!"=="x" goto win
- set line%oldx%=!line%oldx%:~0,%oldy%! !line%oldx%:~%tempy%!
- set line%newx%=!line%newx%:~0,%oldy%!!char!!line%newx%:~%tempy%!
- set oldx=!newx!
- exit /b
-
- :right
- set /a tempy=!oldy!+1
- if "!line%oldx%:~%tempy%,1!"==":" exit /b
- if "!line%oldx%:~%tempy%,1!"=="]" exit /b
- if "!line%oldx%:~%tempy%,1!"=="[" exit /b
- if "!line%oldx%:~%tempy%,1!"=="x" goto win
- set line%oldx%=!line%oldx%:~0,%oldy%! !line%oldx%:~%tempy%!
- set /a oldy+=1
- set /a newy=!oldy!+1
- set line%oldx%=!line%oldx%:~0,%oldy%!!char!!line%oldx%:~%newy%!
- exit /b
-
- :left
- set /a tempy=!oldy!+1
- set /a tempy2=!oldy!-1
- if "!line%oldx%:~%tempy2%,1!"==":" exit /b
- if "!line%oldx%:~%tempy2%,1!"=="]" exit /b
- if "!line%oldx%:~%tempy2%,1!"=="[" exit /b
- if "!line%oldx%:~%tempy2%,1!"=="x" goto win
- set line%oldx%=!line%oldx%:~0,%oldy%! !line%oldx%:~%tempy%!
- set /a oldy-=1
- set /a newy=!oldy!+1
- set line%oldx%=!line%oldx%:~0,%oldy%!!char!!line%oldx%:~%newy%!
- exit /b
-
- :down
- set /a newx=!oldx!+1
- set /a tempy=!oldy!+1
- if "!line%newx%:~%oldy%,1!"==":" exit /b
- if "!line%newx%:~%oldy%,1!"=="]" exit /b
- if "!line%newx%:~%oldy%,1!"=="[" exit /b
- if "!line%newx%:~%oldy%,1!"=="x" goto win
- set line%oldx%=!line%oldx%:~0,%oldy%! !line%oldx%:~%tempy%!
- set line%newx%=!line%newx%:~0,%oldy%!!char!!line%newx%:~%tempy%!
- set oldx=!newx!
- exit /b
复制代码
|