bat、vbs小程序教学之井字棋、五子棋

您所在的位置:网站首页 vb游戏小程序 bat、vbs小程序教学之井字棋、五子棋

bat、vbs小程序教学之井字棋、五子棋

2024-06-14 09:05| 来源: 网络整理| 查看: 265

井字棋代码,bat文件

::又叫井字棋

::当然玩法上可能会有差别

::CODE BY wxcute AT BBS.bathome.net

@echo off

mode con lines=12 cols=37

setlocal enabledelayedexpansion

for /l %%s in (1,1,7) do set SP= !sp!

title .%SP%三子棋

goto :start

::变量说明

argument是否显示为提示棋盘的指针

fate下子总计数

special是否进行总计数

Turn更换选手指针

sub_flag选手指针

input输入

record下子记录

remain剩下的位置

winflag胜利标记

::标签说明

start进入

newGame新游戏

begin循环开始

Machine电脑下子

T_flash下子棋盘更新

:start

::设置棋盘棋子

set chs1=●

set chs0=×

set L7=┏━┳━┳━┓

set L6=┃7┃8┃9┃

set L5=┣━╋━╋━┫

set L4=┃4┃5┃6┃

set L3=┣━╋━╋━┫

set L2=┃1┃2┃3┃

set L1=┗━┻━┻━┛

::============================================================

::开始

:newGame

color 79

setlocal

set argument=1

set/a whoFirst=%random%%%2

set "remain= 1 2 3 4 5 6 7 8 9"

::----------------------------------------

:begin

cls

call :show

::调用胜负判断

if "%fate%" geq "5" call :judgeVic

if defined winflag goto :end

if "%fate%" geq "9" goto :end

::计算下子数与判断下子方

if not defined special (

 set/a fate+=1,Turn="(whoFirst+fate+1)%%2"

) else set special=

if %Turn% equ 0 goto :Machine

::下子

set sub_flag=%Turn%

set input=

set/p input=%SP:~3%等待您的选择:

if not defined input goto :judgeIn

if %input% lss 1 goto :judgeIn

if %input% gtr 9 goto :judgeIn

if defined _%input% (

:judgeIn

 call :err

 set special=1

 goto :begin

)

goto :T_flash

::电脑下子

:Machine

set sub_flag=%Turn%

if %fate% gtr 1 call :PCI

::电脑随机下子

:PCR

set Rc=

if defined bN (set/a Rc=%random%%%bN) else set/a Rc="%random%%%(9-fate)"

if not defined Best set Best=%remain: =%

set input=!Best:~%Rc%,1!

set/p=%SP:~3%电脑正在思考 nul

goto :eof

::结束判断,胜负平

::最笨的列举法

:judgeVic

for %%i in (123 456 789 147 258 369 159 357) do (

 set had=%%i

 set had=!had:~,1! !had:~1,1! !had:~-1!

 for /f "tokens=1-3" %%j in ("!had!") do (

  if "!_%%j!!_%%k!!_%%l!"=="%sub_flag%%sub_flag%%sub_flag%" set winflag=%sub_flag%

 )

)

goto :eof

::电脑智能下子

:PCI

::echo Remain%remain%

set/a _subFlag=(sub_flag+1)%%2

set bN=

set ept=&set Epx=

set Best=

set input=

set d_3flag=

for %%c in (%remain%) do (

 set d1=&set d2=&set d3=

 set d_1=&set d_2=&set d_3=

 for %%i in ("1 2 3" "4 5 6" "7 8 9" "1 4 7" "2 5 8" "3 6 9" "1 5 9" "3 5 7") do (

  set isFlag=

  set noFlag=&set _noFlag=

  set/a baseN=1,_baseN=1

  for %%j in (%%~i) do (

   if "%%j"=="%%c" set isFlag=1

   if "!_%%j!"=="%_subFlag%" set/a noFlag=1,_baseN+=1

   if "!_%%j!"=="%sub_flag%" set/a baseN+=1,_noFlag=1

  )

  if defined isFlag (

   if not defined noFlag set/a d!baseN!+=1

   if not defined _noFlag set/a d_!_baseN!+=1

  )

  if defined d3 set Best=%%c&set bN=1&goto :eof

  if defined d_3 set d_3flag=%%c

 )

 set/a ept=baseN+d!baseN!+_baseN+d_!_baseN!

 if "!Epx!" equ "!ept!" set Best=%%c!Best!&set/a bN+=1

 if "!Epx!" lss "!ept!" set Epx=!ept!&set Best=%%c&set bN=1

)

if defined d_3flag set Best=%d_3flag%&set bN=1

goto :eof

五子棋代码,bat文件

    @echo off&setlocal enabledelayedexpansion

    title  …By-VBT6_村庄 #测试版#

    :starts

    color f0&mode con cols=68 lines=14

    set zm=A   B   C   D   E   F   G   H   I   J   K   L   M   N   O   P

    set hz=●&set bz=◎&set hb=&set szj=╋&set ai=0&set shu=0&set "kg=      "

    cls&call :sysm&echo.&set /p sr=默认为选手先手,是否交换(Y/N)#

    ::初始表

    color f0&mode con cols=68 lines=40

    for /l %%a in (1,1,16) do set "hb=!hb!┃  "

    for /l %%a in (1,1,256) do set "bj_%%a=!szj!"

    if /i "!sr!"=="Y" set bj_120=!bz!

    call :huabiao

    ::游戏开始

    :yxks

    echo.选手上次输入#!sr! #%1   ::   电脑上次输入#!dn_l!!dn_h! #!bz!

    shift&shift&set /p sr=请输入坐标(如:3a;a3;A3;3A)#

    if /i !sr!==xx (goto starts) else (call :zb)

    for /l %%a in (9 -1 1) do set /a s=%%a-1&for %%b in (!s!) do if %%b==0 (set xs%%a=!shu!) else (set xs%%a=!xs%%b!)

    set lz=!hz!&call :luozi&call :pdsy

    ::电脑算法

    echo.&echo.!kg!!kg!!kg!电脑正在思考!请稍等…………

    set /a x1=1,x2=16,x3=15,x4=17

    if "!ai!"=="0" set /a shu=!zb!+1&goto :dnlz

    ::AI思考过程

    for %%x in (1 4 5 6) do (for /l %%a in (1 1 %%x) do if !ai%%a! gtr 0 call :cpu !bz! lp%%x !ai%%a!

    for /l %%a in (1 1 %%x) do call :cpu !hz! lp%%x !xs%%a!)

    ::不符合AI条件,随机在附近落子

    :sjlz  

    for %%b in (!x1! -!x1! !x2! -!x2! !x3! -!x3! !x4! -!x4!) do (set /a lsbl=!zb!+%%b

       for %%a in (!lsbl!) do if "!bj_%%a!"=="!szj!" set shu=!lsbl!&goto :dnlz)

    set /a x1+=1,x2+=16,x3+=15,x4+=17

    goto :sjlz

    :dnlz

    for /l %%a in (9 -1 1) do set /a s=%%a-1&for %%b in (!s!) do if %%b==0 (set ai%%a=!ai!) else (set ai%%a=!ai%%b!)

    set lz=!bz!&call :luozi

    if !xx!==4 call :pdsy

    set /a dn_h=!ai!/16&set /a dn_l=!ai!-(dn_h*16)&set /a dn_h+=1

    set s=0&for %%a in (!zm!) do (set /a s+=1&if !s!==!dn_l! set dn_l=%%a)

    goto :yxks

    ::电脑算法

    :cpu

    for %%a in (4 3 2 1) do (set shu=%3&set js_b=0&set js_h=0&set js_=0&call :_%%a

      for /l %%x in (1 1 5) do (

        if !h1! gtr 0 if !shu! leq 256 for %%b in (!h1! !h2! !h3! !h4! !shu!) do (

     if "!bj_%%b!"=="!bz!" (set /a js_b+=1) else (if "!bj_%%b!"=="!hz!" (set /a js_h+=1) else (set /a js_+=1)))

     set xx=&call :%2

     set js_b=0&set js_h=0&set js_=0&set /a shu+=!bc!&call :_%%a)

    )

    goto :eof

    :lp6

    for %%a in (h b) do if !js_%%a!==3 set "xx=3"

    if !xx!==3 for %%b in (!h4! !h3! !h2! !h1!) do if "!bj_%%b!"=="!szj!" set shu=%%b&goto :dnlz

    for %%a in (h b) do if !js_%%a!==2 set "xx=2"

    if !xx!==2 for %%b in (!h3! !h2! !h4!) do if "!bj_%%b!"=="!szj!" set shu=%%b&goto :dnlz

    goto :eof

    :lp5

    for %%a in (h b) do if !js_%%a!==3 set "xx=3"

    if !xx!==3 if !js_!==2 (for %%b in (!h1!) do if "!bj_%%b!"=="!szj!" for %%c in (!h4! !h3! !h2!) do if "!bj_%%c!"=="!szj!" set shu=%%c&goto :dnlz

    for %%b in (!h3! !h2!) do if "!bj_%%b!"=="!szj!" set shu=%%c&goto :dnlz)

    for %%a in (h b) do if !js_%%a!==2 set "xx=2"

    if !xx!==2 if !js_!==3 for %%b in (!h3! !h4! !h2!) do if "!bj_%%b!"=="!szj!" set shu=%%b&goto :dnlz

    goto :eof

    :lp4

    for %%a in (h b) do if !js_%%a!==3 set "xx=3"

    if !xx!==3 if !js_!==2 for %%b in (!h1!) do if "!bj_%%b!"=="!szj!" for %%c in (!shu!) do if "!bj_%%c!"=="!szj!" set shu=%%c&goto :dnlz

    goto :eof

    :lp1

    for %%a in (h b) do if !js_%%a!==4 set "xx=4"

    if !xx!==4 if !js_!==1 for %%b in (!h1! !h2! !h3! !h4! !shu!) do if "!bj_%%b!"=="!szj!" set shu=%%b&goto :dnlz

    goto :eof

    ::落子

    :luozi

    if !lz!==!bz! set ai=!shu!

    for %%a in (!shu!) do if "!bj_%%a!"=="!szj!" (set bj_%%a=!lz!) else (call :yxks 此处已有子)

    call :huabiao&goto :eof

    :pdsy

    set lp=&set zb=!shu!

    for %%a in (4 3 2 1) do (set shu=!zb!&set js=0&call :_%%a

     for /l %%x in (1 1 5) do (

        for %%b in (!h1! !h2! !h3! !h4! !shu!) do if "!bj_%%b!"=="!lz!" set /a js+=1

        if !js!==5 (set hw=0&for %%b in (!h1! !h2! !h3! !h4! !shu!) do set /a xx=%%b%%16&if !xx! lss 2 set /a hw+=1

           if not !hw!==2 set lp=OK) else (set js=0&set /a shu+=!bc!&call :_%%a))

     if !lp!==OK goto _ying

    )

    goto :eof

    :_1

    set bc=1&set /a h1=!shu!-4&set /a h2=!h1!+1&set /a h3=!h2!+1&set /a h4=!h3!+1&goto :eof

    :_2

    set bc=17&set /a h1=!shu!-68&set /a h2=!h1!+17&set /a h3=!h2!+17&set /a h4=!h3!+17&goto :eof

    :_3

    set bc=15&set /a h1=!shu!-60&set /a h2=!h1!+15&set /a h3=!h2!+15&set /a h4=!h3!+15&goto :eof

    :_4

    set bc=16&set /a h1=!shu!-64&set /a h2=!h1!+16&set /a h3=!h2!+16&set /a h4=!h3!+16&goto :eof

    ::判断输赢模块

    :_ying

    set hw=0&for /l %%a in (1,1,256) do if !bj_%%a!==!lz! set /a hw+=1 

    if !lz!==!hz! (msg %username% " ###恭喜!你在第"!hw!"手赢了###") else (

      set /a hw+=1&msg %username% " ###小样!你在第"!hw!"手输了###")

    set/p=&goto starts

    :zb

    ::下面屏蔽部分错误输入,增强兼容性

    set "shu="&if not "!sr:~3!"=="" set shu=-1

    if "!sr:~0,1!" gtr "9" set sr=!sr:~1!!sr:~0,1!

    if !sr:~-1! lss 9 (set shu=-1) else (if /i !sr:~-1! gtr p set shu=-1)

    set x=!sr:~0,-1!&if !x! gtr 16 (set shu=-1) else (if !x! lss 0 set shu=-1)

    if !shu! lss 0 (call :huabiao&call :yxks 输入错误)

    ::正确输入数据坐标

    set lie=0&set /a hang=(!sr:~0,-1!-1)*16&set /a hw=!hang!+16

    for %%a in (!zm!) do set /a lie+=1&if /i "%%a"=="!sr:~-1!" set /a shu=!hang!+!lie!&goto :eof

    ::画表模块

    :huabiao

    cls&set _n=1&set _nn=16

    echo.!kg!!kg!     $$$$$选手执黑!hz! 电脑执白:!bz!$$$$$

    echo.&echo     !zm!

    for /l %%i in (1,1,16) do (

       if %%i lss 10 (set hs= %%i) else set hs=%%i

       set hb#=&for /l %%a in (!_n!,1,!_nn!) do set "hb#=!hb#!!bj_%%a!━"

       set hb#=!hb#:~0,-1!&set /a _n+=16&set /a _nn+=16

       echo !hs! !hb#!

       if %%i lss 16 echo    !hb!)  

    goto :eof

    ::使用说明数据

    :sysm

    echo.使用说明:

    echo....默认为电脑“执白”,选手“执黑”

    echo....输入坐标打开对应方块(如:a3)

    echo....输入兼容(如:A3;3a;3A;a3)均代表同一坐标点

    echo....输入“xx”重新开始

    echo....补充说明——考虑到效率,AI只初步对比前3-6个落子情况,并据此作出判断,即使这样反应也有些慢

    goto :eof

想看视频演示的,可以去看我在b站上传的视频



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3