消磨时间的批处理数字拼图

来源:百度文库 编辑:神马文学网 时间:2024/04/28 21:28:58
2007-06-23 21:43
突然想起了以前玩电子词典的数字拼图这个小游戏来消磨时间,于是就用批处理弄了一个了,嘻嘻。
支持生成3×3到9×9的数字方阵(9×9的真的是消磨时间的好东西……);随机打乱那些数字的位置,所以是100%可以还原的。
W,S,A,D对应上下左右移动空白方格四周的方格,这个应该很熟悉了吧。
代码如下(6-23改了一点细节),消磨时间时玩玩:
@echo off
title 数字拼图——by dikex
setlocal enabledelayedexpansion
:INPUT
cls
set /p "n=方阵行数(3-9)?"
set n|findstr /r /x "n=[3-9]" 1>nul 2>nul||goto :INPUT
set /a "nn=%n%*%n%-1"
for /l %%i in (1,1,%nn%) do set "nstr=!nstr! %%i"
call :MARK %nstr% 0
:UPSET
set /a "usn=100*%n%"
for /l %%i in (1,1,%usn%) do (
set /a "rn=!random!%%4"
if "!rn!"=="0" set "rnn=w"
if "!rn!"=="1" set "rnn=s"
if "!rn!"=="2" set "rnn=a"
if "!rn!"=="3" set "rnn=d"
call :MOVE !rnn!
)
call :CHECK
if "%seterr%"=="0" goto :UPSET
:START
cls&echo.
for /l %%y in (1,1,%n%) do (
for /l %%x in (1,1,%n%) do (
if "%%x"=="%n%" (echo+ !p%%y-%%x:~-2,2!&echo.) else set /p= !p%%y-%%x:~-2,2! ^|)
)
call :CHECK
if "%seterr%"=="0" goto :END
set choice=set /p choice=选择(W,S,A,D)?
set choice|findstr /i /r /x "choice=[wsad]" 1>nul 2>nul||goto :START
call :MOVE %choice%
goto :START
:MARK
for /l %%y in (1,1,%n%) do (
for /l %%x in (1,1,%n%) do (
call :MARK2 %%x %%y %%1
shift
)
)
goto :EOF
:MARK2
if "%3"=="0" (set "p%2-%1=     "&set "null=%2-%1") else set "p%2-%1=     %3"
goto :EOF
:MOVE
set "my=!null:~0,1!"
set "mx=!null:~-1,1!"
if /i "%1"=="w" set /a "my=%my%+1"
if /i "%1"=="s" set /a "my=%my%-1"
if /i "%1"=="a" set /a "mx=%mx%+1"
if /i "%1"=="d" set /a "mx=%mx%-1"
if defined p%my%-%mx% (
set "null=%my%-%mx%"
set "p%null%=!p%my%-%mx%!"&set "p%my%-%mx%=     "
)
goto :EOF
:CHECK
set "seterr=0"
set "count=1"
for /l %%y in (1,1,%n%) do (
for /l %%x in (1,1,%n%) do (
if "%%y-%%x"=="%n%-%n%" goto :EOF
if not "!p%%y-%%x: =!"=="!count!" (set "seterr=1"&goto :EOF)
set /a "count+=1"
)
)
goto :EOF
:END
set choice=set /p "choice=GOOD!再来一次(Y/N)?"
set choice|findstr /i /r /x "choice=[YN]" 1>nul 2>nul||goto :END
if /i "%choice%"=="y" goto :INPUT
exit
消磨了不少时间弄好了一个9×9的!