纯 bat 五子棋
特点:
新增功能:判定胜负后,可以显示胜出的棋子位置。
不产生临时文件,能自动判定胜负
作了一定的走子错误判断,如目标位置已经有子,或目标位置不存在等。。
初步测试没发现错误,欢迎测试。。。
可以选择由电脑随机走棋,以判断程序是否正确
:- @echo off&mode con cols=80 lines=22&color 1f&title 批处理五子棋
- :: code 随风 @bbs.bathome.net 2008-04-06
- echo\&echo 请选择 y 电脑自动走棋(随机乱走,以判断程序是否正确)
- echo\&echo 选择 n 人走棋,不能人机对弈
- echo\&set /p cs= y n ?
- if /i not "%cs%"=="y" (set "cs=") ELSE (set cs=现在是由电脑随机走棋)
- set gz1= 走棋方法
- set gz3= 输入行和列的代号即可
- set gz5= 英文字母代表列,数字代表行
- set gz7= 如输入: h8 或 8h
- set gz9= 即是在 h列 和 行8 的交叉点落子
- set gz11= 如屏幕变成红色,则说明走棋错误
- :start
- setlocal enabledelayedexpansion&cls&color 1f
- set a1=┌&set o1=┐&set a12=└&set "o12=┘"&set "yans=0"
- for %%j in (2 3 4 5 6 7 8 9 10 11) do (set a%%j=├&set o%%j=┤)
- for %%z in (b c d e f g h i j k l m n) do (
- set %%z1=┬&set "%%z12=┴"
- for /l %%j in (2 1 11) do set "%%z%%j=┼"
- )
- for %%z in (a b c d e f g h i j k l m n o) do (
- set /a max+=1&set %%z=!max!&set "!max!=%%z"
- )
- for /l %%a in (0 1 9) do set abc%%a=a
- goto :xs
- :loop
- if !yans! equ 0 (set ya=白棋&set bk=●) else (set ya=黑棋&set bk=○)
- if not defined cs (
- set flag=&echo\&set /p zou= 请走!ya!
- ) else (
- set zzzmm=ABCDEFGHIJKLMNO
- set /a www=!random!%%12+1
- set /a www1=!random!%%14+0
- call set zzzmm1=%%zzzmm:~!www1!,1%%
- set flag=&echo\&set zou=!zzzmm1!!www!
- )
- if "!zou!"=="" set flag= 输入不能为空&set zou=&goto xs
- set "zou=!zou: =!"
- if "!zou:~1,1!"=="" set flag= !zou! 输入不正确!&set zou=&goto xs
- if not "!zou:~3,1!"=="" set flag= !zou! 输入不正确!&set zou=&goto xs
- set "abc=!zou:~0,1!"
- if defined abc!abc! set "zou=!zou:~-1!!zou:~0,-1!"
- set "abc=!zou:~0,1!"
- if defined abc!abc! (set flag= 输入不正确!&set zou=&goto xs)
- if /i "!zou!"=="ccc" endlocal&goto start
- if not defined !zou! set flag= !zou! 目标位置不存在!&set zou=&goto xs
- if defined !zou!x set flag= 不能走 !zou!&set zou=&goto xs
- set "!zou!x=!ya!"
- set "!zou!=!bk!"
- :xs 显示棋盘
- cls&echo 批处理五子棋
- echo\&echo 输入 ccc 重新开始游戏 %cs%
- if not defined flag set /a yans=^^!yans
- echo\&echo A B C D E F G H I J K L M N O
- for /l %%i in (1 1 12) do (
- set "ko= "
- if %%i lss 10 (set ko= !ko!)
- set/p=!ko!%%i <nul
- for %%z in (a b c d e f g h i j k l m n o) do set/p=!%%z%%i!<nul
- if defined gz%%i set /p= !gz%%i!<nul
- echo.
- )
- if defined flag (
- echo\&echo !flag!&color cf
- ping/n 2 127.1>nul&color 1f
- goto loop
- )
- if not defined zou goto loop
- ::判断胜负
- :pd
- set hang=!zou:~0,1!&set lie=!zou:~1!
- set /a _hangx=!lie!-4,_hangda=!lie!+4
- call set _lie=%%!hang!%%
- set /a lx=!_lie!-4,lda=!_lie!+4
- :: 判断列是否定出胜负
- for /l %%a in (!_hangx! 1 !_hangda!) do (
- set zon=!zou:~0,1!%%ax
- call set _zon=%%!zon!%%-!zon!
- if defined _zon (set _pphen=!_pphen! !_zon!) else (
- set _pphen=!_pphen! x
- ))
- call :sss "!_pphen!"
- if defined jiesu endlocal&goto ok
- :: 判断横向是否定出胜负
- set _pphen=&set _x_x=&set xie=!_hangx!
- set _you=&set _you1=
- for /l %%a in (!lx! 1 !lda!) do (
- set "phx=!%%a!!zou:~1!x"
- call set "pphen=%%!phx!%%-!%%a!!zou:~1!x"
- if defined pphen (set _pphen=!_pphen! !pphen!) else (
- set "_pphen=!_pphen! x"
- )
- rem 判断左上至右下是否胜出
- call set "_xie=%%!%%a!!xie!x%%"
- call set "_xie_=%%_xie%%-!%%a!!xie!x"
- set /a xie+=1
- if defined _xie_ (set _x_x=!_x_x! !_xie_!) else (
- set "_x_x=!_x_x! x"
- )
- rem 判断右上至左下是否胜出
- call set "you=%%!%%a!!_hangda!x%%"
- call set "_you=%%you%%-!%%a!!_hangda!x"
- set /a _hangda-=1
- if defined _you (set _you1=!_you1! !_you!) else (
- set "_you1=!_you1! x"
- )
- )
- call :sss "!_pphen!"
- if defined jiesu endlocal&goto ok
- call :sss "!_you1!"
- if defined jiesu endlocal&goto ok
- call :sss "!_x_x!"
- if defined jiesu endlocal&goto ok
- goto loop
- :sss
- set _pphen=%~1&set pdpd=0&set num1=&set "num2="&set "jiesu="
- for %%a in (!_pphen!) do (
- set "num1=!num2!"
- set "num2=%%a"
- if /i "!num1:~0,3!"=="!num2:~0,3!" (
- if not "!num1:~0,1!"=="-" (
- set /a pdpd+=1&set "senc=!senc! !num2:~0,-1!"
- if !pdpd! geq 4 (
- call set "jiesu=%%senc:!num2:~0,3!=%%"
- echo\&echo !num1:~0,2! 胜出 !jiesu!
- goto :eof
- )
- ) else (set pdpd=0&set senc=!num2:~0,-1!)
- ) else (set pdpd=0&set senc=!num2:~0,-1!)
- )
- goto :eof
- :ok
- echo\&echo 按任意键重新开始。。。
- for /l %%i in (1 1 5) do (
- for %%a in (a d) do color %%af&ping/n 1 127.1>nul
- )
- pause>nul
- goto start
复制代码
|