批处理实现三子棋游戏
03-15
@echo off
setlocal enabledelayedexpansion
mode con lines=25
:start
cls
set str=a1a2a3b1b2b3c1c2c3
set slt1=0
for %%i in (a b c) do (
set m%%i=
for %%j in (1 2 3) do (
set m%%j=
set m%%i%%j=
set n%%i%%j=
)
)
set mlr=
set mrl=
set mmlr=a1b2c3
set mmrl=c1b2a3
set win=
for %%i in (a,b,c) do (
for %%j in (1,2,3) do (
set %%i%%j=
)
)
echo ┏━┳━┳━┓
echo ┃1 ┃2 ┃3 ┃
echo ┣━╋━╋━┫
echo ┃4 ┃5 ┃6 ┃
echo ┣━╋━╋━┫
echo ┃7 ┃8 ┃9 ┃
echo ┗━┻━┻━┛
echo 谁先走?(c代表电脑,h代表人)
set /p slt1=
set human=
set cpu=
if %slt1% equ c set human=○&set cpu=●&goto 2
if %slt1% equ C set human=○&set cpu=●&goto 2
set human=●&set cpu=○
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:begin
:1
echo 请您走棋!(输入数字1~9)
set /p slt2=
if !slt2! equ 1 set slt2=a1
if !slt2! equ 2 set slt2=b1
if !slt2! equ 3 set slt2=c1
if !slt2! equ 4 set slt2=a2
if !slt2! equ 5 set slt2=b2
if !slt2! equ 6 set slt2=c2
if !slt2! equ 7 set slt2=a3
if !slt2! equ 8 set slt2=b3
if !slt2! equ 9 set slt2=c3
if "!n%slt2%!" neq "" echo 此处已经有子!&goto 1
set str=!str:%slt2%=!
set mmrl=!mmrl:%slt2%=!
set mmlr=!mmlr:%slt2%=!
set %slt2%=%human%
set m%slt2%=h
set n%slt2%=h
cls
echo ┏━┳━┳━┓
echo ┃%a1%┃%b1%┃%c1%┃
echo ┣━╋━╋━┫
echo ┃%a2%┃%b2%┃%c2%┃
echo ┣━╋━╋━┫
echo ┃%a3%┃%b3%┃%c3%┃
echo ┗━┻━┻━┛
call :judge
if !win! equ h echo 你居然赢了,太让我惊讶了!&pause&goto end
if !win! equ c echo 嘿嘿,不好意思我赢了!&pause&goto end
if "!str!" equ "" echo 平局!&pause&goto end
:2
echo 等待电脑走棋
call :ai
for /l %%i in (1,1,15) do set/p r=^><nul&ping /n 0 127.1>nul
set slt3=!max!
set str=!str:%slt3%=!
set mmrl=!mmrl:%slt3%=!
set mmlr=!mmlr:%slt3%=!
cls
set %slt3%=%cpu%
set m%slt3%=c
set n%slt3%=c
echo ┏━┳━┳━┓
echo ┃%a1%┃%b1%┃%c1%┃
echo ┣━╋━╋━┫
echo ┃%a2%┃%b2%┃%c2%┃
echo ┣━╋━╋━┫
echo ┃%a3%┃%b3%┃%c3%┃
echo ┗━┻━┻━┛
call :judge
if !win! equ h echo 你居然赢了,太让我惊讶了!&pause&goto end
if !win! equ c echo 嘿嘿,不好意思我赢了!&pause&goto end
if "!str!" equ "" echo 平局!&pause&goto end
goto begin
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: :judge这一段是判断胜负的
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:judge
for %%i in (a,b,c) do (
for %%j in (1,2,3) do (
set m%%i=!m%%i!!m%%i%%j!
set m%%j=!m%%j!!m%%i%%j!
)
)
set mlr=!mlr!!ma1!!mb2!!mc3!
set mrl=!mrl!!ma3!!mb2!!mc1!
for %%i in (a,b,c) do (
if !m%%i! equ hhh set win=h
if !m%%i! equ ccc set win=c
)
for /l %%j in (1,1,3) do (
if !m%%j! equ hhh set win=h
if !m%%j! equ ccc set win=c
)
if !mlr! equ hhh set win=h
if !mrl! equ hhh set win=h
if !mlr! equ ccc set win=c
if !mrl! equ ccc set win=c
for %%i in (a,b,c) do (
for %%j in (1,2,3) do (
set m%%i%%j=
)
)
goto :eof
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: :ai这一段是电脑下棋的思路
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:ai
set k=0
for /l %%i in (0,2,16) do (
set/a k+=1
set t!k!=!str:~%%i,2!
if not defined t!k! goto next
)
:next
set/a k=!k!-1
for /l %%i in (1,1,!k!) do (
set score%%i=0
)
for /l %%j in (1,1,!k!) do (
set left%%j=!t%%j:~0,1!
set right%%j=!t%%j:~1,1!
set bb%%j=!left%%j!!right%%j!
if !mlr! equ hh if !bb%%j! equ !mmlr! set score%%j=4
if !mrl! equ hh if !bb%%j! equ !mmrl! set score%%j=4
if !mlr! equ cc if !bb%%j! equ !mmlr! set score%%j=5
if !mrl! equ cc if !bb%%j! equ !mmrl! set score%%j=5
for %%n in (a b c) do (
if !left%%j! equ %%n (
if !left%%j! equ b if !right%%j! equ 2 set score%%j=3
if !m%%n! equ hh if !score%%j! lss 4 set score%%j=4
if !m%%n! equ cc set score%%j=5
)
)
for %%m in (1 2 3) do (
if !right%%j! equ %%m (
if !m%%m! equ hh if !score%%j! lss 4 set score%%j=4
if !m%%m! equ cc set score%%j=5
)
)
)
set max=!t1!
set/a tempk=!k!+1
for /l %%i in (1,1,!tempk!) do (
set qq=%%i+1
for /l %%j in (!qq!,1,!k!) do (
if !score%%i! equ 5 set max=!t%%j!&goto 3
if !score%%i! lss !score%%j! set max=!t%%j!
)
)
:3
goto :eof
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:end
cls
echo 重玩吗?(是/y,否/n)
set /p sss=
if !sss! equ y goto start
if !sss! equ Y goto start