就是不能实现电脑智能和人机对栾(情况过于复杂,批处理难以胜任):
- @echo off&setlocal enabledelayedexpansion
- rem 设置窗口大小及色彩----------------------------------------------------------------------
- color 1f&mode con cols=55 lines=27
- title 五子棋 by zw19750516 thanks for namejm
- echo.&echo.&echo.&echo.&echo 程序载入中...
- set "codes=A B C D E F G H I J K L M N O P Q R S"
- set "code= %codes: =%"&set /a n=-1,m=0
- rem ----------------------------------------------------------------------------------------
-
- rem 获取电脑随机步数并乱序------------------------------------------------------------------
- for /l %%a in (1,1,19) do (
- for /l %%i in (1,1,19) do (
- set "a=!code:~%%i,1!"&set /a num+=1
- set ".!num!=%%a!a!"
- ))
- for /l %%i in (1,1,361) do (
- set /a a=!random!%%361+1
- call,set "b=%%.!a!%%"
- set ".!a!=!.%%i!"
- set ".%%i=!b!"
- set "a="&set "b="
- )
- rem ----------------------------------------------------------------------------------------
-
- rem 走子大循环(输出棋盘)--------------------------------------------------------------------
- :begin
- set "num=%m%"
- set /a n+=1,m+=1&if !m! equ 2 set m=0
- if %m% equ 0 (set "zi=●"&set "vins=●●●●●") else (set "zi=○"&set "vins=○○○○○")
- if defined vin set "zi=胜"
- if defined auto (set "_!.%n%!=%zi%") else (set "_%a%%b%=%zi%")
- echo.>temp.txt
- echo ┌───────────────────┐>>temp.txt
- for /l %%a in (1,1,19) do (
- for /l %%i in (1,1,19) do (
- if %%a equ 1 (
- if %%i equ 1 (
- set "_p=│┌"
- ) else (
- if %%i equ 19 (
- set "_p=┐│"
- ) else (
- set "_p=┬"
- ))) else (
- if %%a equ 19 (
- if %%i equ 1 (
- set "_p=│└"
- ) else (
- if %%i equ 19 (
- set "_p=┘│"
- ) else (
- set "_p=┴"
- ))) else (
- if %%i equ 1 (
- set "_p=│├"
- ) else (
- if %%i equ 19 (
- set "_p=┤│"
- ) else (
- set "_p=┼"
- ))))
- set str=!code:~%%i,1!
- if defined _%%a!str! (
- call :lop %%a %%i
- ) else (
- if %%i equ 19 (
- set /p=!_p!%%a<nul>>temp.txt
- ) else (
- set /p=!_p!<nul>>temp.txt
- )))
- echo.>>temp.txt
- )
- echo └───────────────────┘>>temp.txt
- echo %codes%>>temp.txt
- cls&type temp.txt
- rem ----------------------------------------------------------------------------------------
-
- rem 选择走棋方式----------------------------------------------------------------------------
- if defined hand goto step
- if defined auto goto next
- :choice
- set /p select=请选择(1.人下 2.电脑下):
- if %select% equ 2 set "auto=a"&goto begin
- if %select% equ 1 set "hand=a"&set /a n-=1&goto begin
- goto wrong
- :step
- if defined vin goto next
- if defined step (
- if %m% equ 0 (set /p=黑子下,<nul) else (set /p=白子下,<nul)
- echo 对手上一步走的:%step%<nul
- ) else (
- set /p=黑子先,<nul
- )
- set "ab="&set /p ab=请按纵、横顺序输入坐标(如:4h):
- rem ----------------------------------------------------------------------------------------
-
- rem 判断人工输入是否正确-------------------------------------------------------------------
- if not defined ab goto wrong
- if "%ab:~1%" equ "" goto wrong
- if "%ab:~3%" neq "" goto wrong
- if "%ab:~2%" equ "" (set "a=%ab:~,1%"&set "b=%ab:~1%") else (set "a=%ab:~,2%"&set "b=%ab:~2,1%")
- if "%a:~,1%" equ "0" goto wrong
- echo %a%|findstr "[^0-9]">nul&&goto wrong
- echo %b%|findstr "[0-9]">nul&&goto wrong
- if %a% lss 1 goto wrong
- if %a% gtr 19 goto wrong
- if "!codes:%b%=!" equ "%codes%" goto wrong
- if defined _%a%%b% goto wrong
- set "step=%ab%"
- rem ----------------------------------------------------------------------------------------
-
- rem 输出结果--------------------------------------------------------------------------------
- :next
- del /q temp.txt
- if defined vin (
- if %m% equ 0 (set /p=白子胜,<nul) else (set /p=黑子胜,<nul)
- echo 最后落子为:%a%%b%。
- pause>nul&exit
- )
- if %n% neq 361 (goto begin) else (echo 结果不分胜负!&pause>nul&exit)
- rem ----------------------------------------------------------------------------------------
-
- rem 判断是否胜出----------------------------------------------------------------------------
- :lop
- if %2 equ 1 (set "ll=set /p=│<nul>>temp.txt&") else (set "ll=")
- if %2 equ 19 (set "rr=&set /p=│%1<nul>>temp.txt") else (set "rr=")
- %ll%set /p=!_%1%str%!<nul>>temp.txt%rr%
- if "%1%str%" equ "!.%n%!" set "a=%1"&set "b=%str%"&goto judge
- if /i "%1%str%"=="%step%" goto judge
- goto :eof
- :judge
- set "hang="&set "lie="&set "ls="&set "rs="&set "ss="&set "x=0"&set "y=0"&set "z=0"
- set /a x1=%2-4,x2=%2+4,y1=%1+4,y2=%1-4
- if %x1% lss 0 set "x1=0"
- if %y2% lss 0 set "y2=0"
- if %x2% gtr 19 set "x2=19"
- if %y1% gtr 19 set "y1=19"
- for /l %%i in (%x1%,1,%x2%) do set "ss=!ss! !code:~%%i,1!"
- for %%i in (%ss%) do if defined _%1%%i (set "hang=!hang!!_%1%%i!") else (set "hang=!hang!#")
- for /l %%i in (%y2%,1,%y1%) do if defined _%%i%str% (set "lie=!lie!!_%%i%str%!") else (set "lie=!lie!#")
- for %%a in (%ss%) do (
- set /a x+=1,y=0,z=0
- for /l %%b in (%y1%,-1,%y2%) do (
- set /a y+=1
- if !x! equ !y! (
- if defined _%%b%%a (
- set "ls=!ls!!_%%b%%a!"
- ) else (
- set "ls=!ls!#"
- )))
- for /l %%c in (%y2%,1,%y1%) do (
- set /a z+=1
- if !x! equ !z! (
- if defined _%%c%%a (
- set "rs=!rs!!_%%c%%a!"
- ) else (
- set "rs=!rs!#"
- ))))
- for %%i in (%hang% %lie% %ls% %rs%) do (
- set var=%%i
- if defined var if "!var:%vins%=!" neq "!var!" set /a n-=1&set "vin=a"&set "m=%num%"&goto begin
- )
- goto :eof
- rem ----------------------------------------------------------------------------------------
-
- rem 报错-----------------------------------------------------------------------------------
- :wrong
- set /p=错误,请正确输入。<nul&set "m=%num%"
- ping /n 2 127.1>nul&set "a="&set "b="&set /a n-=1&goto begin
- rem ----------------------------------------------------------------------------------------
复制代码
[ 本帖最后由 zw19750516 于 2008-7-19 13:23 编辑 ] |