Batch file 批处理文件在使用基本命令时运行极慢

Batch file 批处理文件在使用基本命令时运行极慢,batch-file,Batch File,我决定建立一个非正式的项目,一个有3种模式的tic-tac-toe机器人。由于某些原因,使用if对bot的检测速度非常慢。 运行此代码最多可能需要30秒: ` ` 我很困惑为什么要花这么长时间。一行这样的话,一秒钟之内就走了。为什么需要这么长时间?您可以通过跟踪变量中可用的网格位置来减少条件测试,然后在移动时使用子字符串修改从变量中减去网格位置。在另一个变量中跟踪AI的成功移动,并在第三个伪数组变量中跟踪获胜网格移动的列表。在for循环中使用findstring函数测试AI对获胜移动数组的移动,

我决定建立一个非正式的项目,一个有3种模式的tic-tac-toe机器人。由于某些原因,使用
if
对bot的检测速度非常慢。 运行此代码最多可能需要30秒: `

`
我很困惑为什么要花这么长时间。一行这样的话,一秒钟之内就走了。为什么需要这么长时间?

您可以通过跟踪变量中可用的网格位置来减少条件测试,然后在移动时使用子字符串修改从变量中减去网格位置。在另一个变量中跟踪AI的成功移动,并在第三个伪数组变量中跟踪获胜网格移动的列表。在for循环中使用findstring函数测试AI对获胜移动数组的移动,并为每一个匹配增加一个值(处理数组中的下一个元素时重置为0)-如果有两个匹配,则从获胜移动一步,您可以测试available grid positions变量,查看相关位置是否可用


同样地,您可以以相同的方式跟踪玩家的移动,并允许AI访问相同的检查,检查玩家离获胜的距离,以便在AI无法获胜时阻止玩家移动布局


@echo off&&setlocal enabledelayedexpansion
如果/i“!ui1!!ui2!!ui3!”==“XX3”(设置“ui3=O”&&goto:easymoduleuser)
如果/i“!ui1!!ui4!!ui7!”==“XX7”(设置“ui7=O”&&goto:easymoduleuser)
如果/i“!ui7!!ui8!!ui9!”==“XX9”(设置“ui9=O”&&goto:easymoduleuser)
如果/i“!ui4!!ui5!!ui6!”==“XX6”(设置“ui6=O”&&goto:easymoduleuser)
如果/i“!ui3!!ui6!!ui9!”==“XX9”(设置“ui9=O”和&goto:easymoduleuser)
如果/i“!ui2!!ui5!!ui8!”==“XX8”(设置“ui9=O”&&goto:easymoduleuser)
如果/i“!ui5!!ui2!!ui8!”==“XX8”(设置“ui9=O”&&goto:easymoduleuser)
如果/i“!ui1!!ui7!!ui4!”==“XX4”(设置“ui4=O”&&goto:easymoduleuser)
如果/i“!ui1!!ui3!!ui2!”==“XX4”(设置“ui2=O”&&goto:easymoduleuser)
如果/i“!ui7!!ui9!!ui8!”==“XX8”(设置“ui8=O”和&goto:easymoduleuser)
如果/i“!ui2!!ui8!!ui5!”==“XX5”(设置“ui5=O”&&goto:easymoduleuser)
如果/i“!ui4!!ui6!!ui5!”==“XX5”(设置“ui5=O”&&goto:easymoduleuser)
如果/i“!ui3!!ui9!!ui6!”==“XX6”(设置“ui6=O”&&goto:easymoduleuser)
如果/i“!ui4!!ui6!!ui5!”==“XX5”(设置“ui5=O”&&goto:easymoduleuser)
当前位置现在,它试图找到任何它能赢的地方。
如果/i“!ui1!!ui2!!ui3!”==“OO3”(设置“ui3=O”&goto:easymoduleuser)
如果/i“!ui1!!ui4!!ui7!”==“OO7”(设置“ui7=O”&goto:easymoduleuser)
如果/i“!ui7!!ui8!!ui9!”==“OO9”(设置“ui9=O”&goto:easymoduleuser)
如果/i“!ui4!!ui5!!ui6!”==“OO6”(设置“ui6=O”&goto:easymoduleuser)
如果/i“!ui3!!ui6!!ui9!”==“OO9”(设置“ui9=O”&goto:easymoduleuser)
如果/i“!ui2!!ui5!!ui8!”==“OO8”(设置“ui9=O”&goto:easymoduleuser)
如果/i“!ui5!!ui2!!ui8!”==“OO8”(设置“ui9=O”&goto:easymoduleuser)
如果/i“!ui1!!ui7!!ui4!”==“OO4”(设置“ui4=O”&goto:easymoduleuser)
如果/i“!ui1!!ui3!!ui2!”==“OO4”(设置“ui2=O”&goto:easymoduleuser)
如果/i“!ui7!!ui9!!ui8!”==“OO8”(设置“ui8=O”&goto:easymoduleuser)
如果/i“!ui2!!ui8!!ui5!”==“OO5”(设置“ui5=O”&goto:easymoduleuser)
如果/i“!ui4!!ui6!!ui5!”==“OO5”(设置“ui5=O”&goto:easymoduleuser)
如果/i“!ui3!!ui9!!ui6!”==“OO6”(设置“ui6=O”&goto:easymoduleuser)
如果/i“!ui4!!ui6!!ui5!”==“OO5”(设置“ui5=O”&goto:easymoduleuser)
::现在,如果它到达这一部分,它将尝试制作一个图案
::它赢的地方(所有它能把两个放在一起的地方)。
::它不会试图在对角线上获胜)
如果/i“!ui1!!ui2!!ui3!”==“O23”(设置“ui2=O”&&goto:easymoduleuser)
如果/i“!ui3!!ui2!!ui1!”==“O21”(设置“ui1=O”&&goto:easymoduleuser)
如果/i“!ui1!!ui4!!ui7!”==“O47”(设置“ui4=O”&&goto:easymoduleuser)
如果/i“!ui2!!ui5!!ui8!”==“O58”(设置“ui5=O”&&goto:easymoduleuser)
如果/i“!ui3!!ui6!!ui9!”=“O69”(设置“ui6=O”和&goto:easymoduleuser)
如果/i“!ui5!!ui2!!ui8!”==“O28”(设置“ui2=O”和&goto:easymoduleuser)
如果/i“!ui7!!ui8!!ui9!”==“O89”(设置“ui8=O”&&goto:easymoduleuser)
如果/i“!ui4!!ui5!!ui6!”==“O56”(设置“ui5=O”和&goto:easymoduleuser)

  • 如果您的变量区分大小写,请将
    /i
    删除到:

@echo off&&setlocal enabledelayedexpansion
如果“!ui1!!ui2!!ui3!”==“XX3”(设置“ui3=O”&&goto:easymoduleuser)
如果“!ui1!!ui4!!ui7!”==“XX7”(设置“ui7=O”&&goto:easymoduleuser)
如果“!ui7!!ui8!!ui9!”==“XX9”(设置“ui9=O”&&goto:easymoduleuser)
如果“!ui4!!ui5!!ui6!”==“XX6”(设置“ui6=O”&&goto:easymoduleuser)
如果“!ui3!!ui6!!ui9!”==“XX9”(设置“ui9=O”&&goto:easymoduleuser)
如果“!ui2!!ui5!!ui8!”==“XX8”(设置“ui9=O”&&goto:easymoduleuser)
如果“!ui5!!ui2!!ui8!”==“XX8”(设置“ui9=O”&&goto:easymoduleuser)
如果“!ui1!!ui7!!ui4!”==“XX4”(设置“ui4=O”&&goto:easymoduleuser)
如果“!ui1!!ui3!!ui2!”==“XX4”(设置“ui2=O”&&goto:easymoduleuser)
如果“!ui7!!ui9!!ui8!”==“XX8”(设置“ui8=O”&&goto:easymoduleuser)
如果“!ui2!!ui8!!ui5!”==“XX5”(设置“ui5=O”&&goto:easymoduleuser)
如果“!ui4!!ui6!!ui5!”==“XX5”(设置“ui5=O”&&goto:easymoduleuser)
如果“!ui3!!ui9!!ui6!”==“XX6”(设置“ui6=O”&&goto:easymoduleuser)
如果“!ui4!!ui6!!ui5!”==“XX5”(设置“ui5=O”&&goto:easymoduleuser)
当前位置现在,它试图找到任何它能赢的地方。
如果“!ui1!!ui2!!ui3!”==“OO3”(设置“ui3=O”&goto:easymoduleuser)
如果“!ui1!!ui4!!ui7!”==“OO7”(设置“ui7=O”&goto:easymoduleuser)
如果“!ui7!!ui8!!ui9!”==“OO9”(设置“ui9=O”&goto:easymoduleuser)
如果“!ui4!!ui5!!ui6!”==“OO6”(设置“ui6=O”&goto:easymoduleuser)
如果“!ui3!!ui6!!ui9!”==“OO9”(设置“ui9=O”&goto:easymoduleuser)
如果“!ui2!!ui5!!ui8!”==“OO8”(设置“ui9=O”&goto:easymoduleuser)
如果“!ui5!!ui2!!ui8!”==“OO8”(设置“ui9=O”&goto:easymoduleuser
if %ui1%==X if %ui2%==X if %ui3%==3 set "ui3=O" & goto :easymoduleuser
if %ui1%==X if %ui4%==X if %ui7%==7 set "ui7=O" & goto :easymoduleuser
if %ui7%==X if %ui8%==X if %ui9%==9 set "ui9=O" & goto :easymoduleuser
if %ui4%==X if %ui5%==X if %ui6%==6 set "ui6=O" & goto :easymoduleuser
if %ui3%==X if %ui6%==X if %ui9%==9 set "ui9=O" & goto :easymoduleuser
if %ui2%==X if %ui5%==X if %ui8%==8 set "ui9=O" & goto :easymoduleuser
if %ui5%==X if %ui2%==X if %ui8%==8 set "ui9=O" & goto :easymoduleuser
if %ui1%==X if %ui7%==X if %ui4%==4 set "ui4=O" & goto :easymoduleuser
if %ui1%==X if %ui3%==X if %ui2%==4 set "ui2=O" & goto :easymoduleuser
if %ui7%==X if %ui9%==X if %ui8%==8 set "ui8=O" & goto :easymoduleuser
if %ui2%==X if %ui8%==X if %ui5%==5 set "ui5=O" & goto :easymoduleuser
if %ui4%==X if %ui6%==X if %ui5%==5 set "ui5=O" & goto :easymoduleuser
if %ui3%==X if %ui9%==X if %ui6%==6 set "ui6=O" & goto :easymoduleuser
if %ui4%==X if %ui6%==X if %ui5%==5 set "ui5=O" & goto :easymoduleuser
rem now, it tries to find any places it can win.
if %ui1%==O if %ui2%==O if %ui3%==3 set "ui3=O" & goto :easymoduleuser
if %ui1%==O if %ui4%==O if %ui7%==7 set "ui7=O" & goto :easymoduleuser
if %ui7%==O if %ui8%==O if %ui9%==9 set "ui9=O" & goto :easymoduleuser
if %ui4%==O if %ui5%==O if %ui6%==6 set "ui6=O" & goto :easymoduleuser
if %ui3%==O if %ui6%==O if %ui9%==9 set "ui9=O" & goto :easymoduleuser
if %ui2%==O if %ui5%==O if %ui8%==8 set "ui9=O" & goto :easymoduleuser
if %ui5%==O if %ui2%==O if %ui8%==8 set "ui9=O" & goto :easymoduleuser
if %ui1%==O if %ui7%==O if %ui4%==4 set "ui4=O" & goto :easymoduleuser
if %ui1%==O if %ui3%==O if %ui2%==4 set "ui2=O" & goto :easymoduleuser
if %ui7%==O if %ui9%==O if %ui8%==8 set "ui8=O" & goto :easymoduleuser
if %ui2%==O if %ui8%==O if %ui5%==5 set "ui5=O" & goto :easymoduleuser
if %ui4%==O if %ui6%==O if %ui5%==5 set "ui5=O" & goto :easymoduleuser
if %ui3%==O if %ui9%==O if %ui6%==6 set "ui6=O" & goto :easymoduleuser
if %ui4%==O if %ui6%==O if %ui5%==5 set "ui5=O" & goto :easymoduleuser
rem now, if it reaches this part, it will try to make a pattern in which it wins (all places where it can put two together. It doesn't try to win diagonally)
if %ui1%==O if %ui2%==2 if %ui3%==3 set "ui2=O" & goto :easymoduleuser
if %ui3%==O if %ui2%==2 if %ui1%==1 set "ui1=O" & goto :easymoduleuser
if %ui1%==O if %ui4%==4 if %ui7%==7 set "ui4=O" & goto :easymoduleuser
if %ui2%==O if %ui5%==5 if %ui8%==8 set "ui5=O" & goto :easymoduleuser
if %ui3%==O if %ui6%==6 if %ui9%==9 set "ui6=O" & goto :easymoduleuser
if %ui5%==O if %ui2%==2 if %ui8%==8 set "ui2=O" & goto :easymoduleuser
if %ui7%==O if %ui8%==8 if %ui9%==9 set "ui8=O" & goto :easymoduleuser
if %ui4%==O if %ui5%==5 if %ui6%==6 set "ui5=O" & goto :easymoduleuser