python中的XO游戏AI麻烦

python中的XO游戏AI麻烦,python,python-3.x,artificial-intelligence,turtle-graphics,Python,Python 3.x,Artificial Intelligence,Turtle Graphics,我用python turtle编写了一个XO游戏,但是AI中有一个问题。我在VB6中用同样的算法编写了这个游戏,它运行得很好,但在python中我找不到它的问题。电脑不能玩得很好,很容易丢失! 这是一组gif图片 #difining libraries import turtle from tkinter import messagebox as msgbox #defining constants osc=xsc=asc=0#scores vars np="n.gif" op="o.gif

我用python turtle编写了一个XO游戏,但是AI中有一个问题。我在VB6中用同样的算法编写了这个游戏,它运行得很好,但在python中我找不到它的问题。电脑不能玩得很好,很容易丢失! 这是一组gif图片

#difining libraries
import turtle
from tkinter import messagebox as msgbox

#defining constants
osc=xsc=asc=0#scores vars

np="n.gif"
op="o.gif"
xp="x.gif"
pturn=xp
turtle.register_shape(np)
turtle.register_shape(op)
turtle.register_shape(xp)

t=[turtle.Turtle() for x in range(9)]

arra=[[0]*3 for x in range(3)]#array for holding scores
tempa=[[0]*3 for x in range(3)]#array for holding scores
temparray=[[0]*3 for x in range(3)]#array for holding scores


def tur0(x,y):
    tclick(0)
def tur1(x,y):
    tclick(1)
def tur2(x,y):
    tclick(2)
def tur3(x,y):
    tclick(3)
def tur4(x,y):
    tclick(4)
def tur5(x,y):
    tclick(5)
def tur6(x,y):
    tclick(6)
def tur7(x,y):
    tclick(7)
def tur8(x,y):
    tclick(8)


def drawTable():#creating buttons and table
    turtle.ht()
    turtle.width(5)
    turtle.color("black")
    xd=-133
    yd=133
    for i in range(9):
        t[i].shape(np)
        t[i].up()
        t[i].goto(xd,yd)
        xd+=133
        if xd==2*133:
            xd=-133
            yd-=133
    xd=-64
    yd=64
    for i in range(2):
        turtle.up()
        turtle.goto(-197.5,yd)
        turtle.setheading(0)
        turtle.down()
        turtle.fd(394)
        yd-=128
        turtle.up()
        turtle.goto(xd,197.5)
        turtle.setheading(270)
        turtle.down()
        turtle.fd(394)
        xd+=128
    t[0].onclick(tur0)
    t[1].onclick(tur1)
    t[2].onclick(tur2)
    t[3].onclick(tur3)
    t[4].onclick(tur4)
    t[5].onclick(tur5)
    t[6].onclick(tur6)
    t[7].onclick(tur7)
    t[8].onclick(tur8)

def inarray():#put scores in array
    global arra
    for i in range(3):
        for j in range(3):
            arra[i][j]=0
    xd=0
    yd=0
    for i in range(9):
        if t[i].shape()==np:
            arra[xd][yd]=0
        elif t[i].shape()==xp:
            arra[xd][yd]=1
        elif t[i].shape()==op:
            arra[xd][yd]=2
        yd+=1
        if yd==3:
            yd=0
            xd+=1
def grestart():
    if msgbox.askyesno(title="The End", message="Game finished. Do you want to restart?")==True:
        turtle.clear()
        drawTable()
    else:
        quit()


#these are for checking winner
def check(i,j,k):
    a=t[i].shape()+t[j].shape()+t[k].shape()
    turtle.color("green")
    turtle.width(5)
    turtle.up()
    if a==xp+xp+xp:
        turtle.goto(t[i].pos())
        turtle.down()
        turtle.goto(t[k].pos())
        turtle.up()
        p=msgbox.showinfo(title="Congratulations!", message="Player X is Winner!")
        grestart()
    elif a==op+op+op:
        turtle.goto(t[i].pos())
        turtle.down()
        turtle.goto(t[k].pos())
        turtle.up()
        p=msgbox.showinfo(title="Failed! :(", message="Player O is Winner!")
        grestart()

def ccheck():
    check(0,1,2)
    check(3,4,5)
    check(6,7,8)
    check(0,3,6)
    check(1,4,7)
    check(2 ,5,8)
    check(0 ,4,8)
    check(2 ,4,6)
#---------------------------
def chturn():
    global pturn
    if pturn==xp:
        pturn=op
    else:
        pturn=xp


def xWayT():#player x ways Temperary
    global temparray
    global xsc
    xsc=0
    '''for i in range(3):
        if (temparray[i][0]==1 or temparray[i][0]==0) and (temparray[i][1]==1 or temparray[i][1]==0) and (temparray[i][2]==1 or temparray[i][2]==0):
            xsc+=1
    for i in range(3):
        if (temparray[0][i]==1 or temparray[0][i]==0) and (temparray[1][i]==1 or temparray[1][i]==0) and (temparray[2][i]==1 or temparray[2][i]==0):
            xsc+=1'''
    if (temparray[0][0]==1 or temparray[0][0]==0) and (temparray[0][1]==1 or temparray[0][1]==0) and (temparray[0][2]==1 or temparray[0][2]==0):
        xsc+=1
    if (temparray[1][0]==1 or temparray[1][0]==0) and (temparray[1][1]==1 or temparray[1][1]==0) and (temparray[1][2]==1 or temparray[1][2]==0):
        xsc+=1
    if (temparray[2][0]==1 or temparray[2][0]==0) and (temparray[2][1]==1 or temparray[2][1]==0) and (temparray[2][2]==1 or temparray[2][2]==0):
        xsc+=1
    if (temparray[0][0]==1 or temparray[0][0]==0) and (temparray[1][0]==1 or temparray[1][0]==0) and (temparray[2][0]==1 or temparray[2][0]==0):
        xsc+=1
    if (temparray[0][1]==1 or temparray[0][1]==0) and (temparray[1][1]==1 or temparray[1][1]==0) and (temparray[2][1]==1 or temparray[2][1]==0):
        xsc+=1
    if (temparray[0][2]==1 or temparray[0][2]==0) and (temparray[1][2]==1 or temparray[1][2]==0) and (temparray[2][2]==1 or temparray[2][2]==0):
        xsc+=1
    if (temparray[0][0]==1 or temparray[0][0]==0) and (temparray[1][1]==1 or temparray[1][1]==0) and (temparray[2][2]==1 or temparray[2][2]==0):
        xsc+=1
    if (temparray[0][2]==1 or temparray[0][2]==0) and (temparray[2][2]==1 or temparray[2][2]==0) and (temparray[2][0]==1 or temparray[2][0]==0):
        xsc+=1

def oWayT():#player o ways Temperary
    global temparray
    global osc
    osc=0
    '''for i in range(3):
        if (temparray[i][0]==2 or temparray[i][0]==0) and (temparray[i][1]==2 or temparray[i][1]==0) and (temparray[i][2]==2 or temparray[i][2]==0):
            osc+=1
    for i in range(3):
        if (temparray[0][i]==2 or temparray[0][i]==0) and (temparray[1][i]==2 or temparray[1][i]==0) and (temparray[2][i]==2 or temparray[2][i]==0):
            osc+=1
    if (temparray[0][0]==2 or temparray[0][0]==0) and (temparray[1][1]==2 or temparray[1][1]==0) and (temparray[2][2]==2 or temparray[2][2]==0):
        osc+=1
    if (temparray[0][2]==2 or temparray[0][2]==0) and (temparray[2][2]==2 or temparray[2][2]==0) and (temparray[2][0]==2 or temparray[2][0]==0):
        osc+=1'''
    if (temparray[0][0]==2 or temparray[0][0]==0) and (temparray[0][1]==2 or temparray[0][1]==0) and (temparray[0][2]==2 or temparray[0][2]==0):
        osc+=1
    if (temparray[1][0]==2 or temparray[1][0]==0) and (temparray[1][1]==2 or temparray[1][1]==0) and (temparray[1][2]==2 or temparray[1][2]==0):
        osc+=1
    if (temparray[2][0]==2 or temparray[2][0]==0) and (temparray[2][1]==2 or temparray[2][1]==0) and (temparray[2][2]==2 or temparray[2][2]==0):
        osc+=1
    if (temparray[0][0]==2 or temparray[0][0]==0) and (temparray[1][0]==2 or temparray[1][0]==0) and (temparray[2][0]==2 or temparray[2][0]==0):
        osc+=1
    if (temparray[0][1]==2 or temparray[0][1]==0) and (temparray[1][1]==2 or temparray[1][1]==0) and (temparray[2][1]==2 or temparray[2][1]==0):
        osc+=1
    if (temparray[0][2]==2 or temparray[0][2]==0) and (temparray[1][2]==2 or temparray[1][2]==0) and (temparray[2][2]==2 or temparray[2][2]==0):
        osc+=1
    if (temparray[0][0]==2 or temparray[0][0]==0) and (temparray[1][1]==2 or temparray[1][1]==0) and (temparray[2][2]==2 or temparray[2][2]==0):
        osc+=1
    if (temparray[0][2]==2 or temparray[0][2]==0) and (temparray[2][2]==2 or temparray[2][2]==0) and (temparray[2][0]==2 or temparray[2][0]==0):
        osc+=1

def omove(a,b):
    global temparray,tempa
    for i in range(3):
        if (temparray[i][0]==2) and (temparray[i][1]==2) and (temparray[i][2]==2):
            tempa[a][b]=-100
    for i in range(3):
        if (temparray[0][i]==2) and (temparray[1][i]==2) and (temparray[2][i]==2):
            tempa[a][b]=-100
    if (temparray[0][0]==2) and (temparray[1][1]==2) and (temparray[2][2]==2):
        tempa[a][b]=-100
    if (temparray[0][2]==2) and (temparray[2][2]==2) and (temparray[2][0]==2):
        tempa[a][b]=-100

def xmove(a,b):
    global temparray,tempa
    for i in range(3):
        if (temparray[i][0]==1) and (temparray[i][1]==1) and (temparray[i][2]==1):
            tempa[a][b]=-100
    for i in range(3):
        if (temparray[0][i]==1) and (temparray[1][i]==1) and (temparray[2][i]==1):
            tempa[a][b]=-100
    if (temparray[0][0]==1) and (temparray[1][1]==1) and (temparray[2][2]==1):
        tempa[a][b]=-100
    if (temparray[0][2]==1) and (temparray[2][2]==1) and (temparray[2][0]==1):
        tempa[a][b]=-100

def aip(i,j):#ai process function
    global xsc,osc,asc,temparray,tempa
    if temparray[i][j]==0:
        temparray[i][j]=2
        xWayT()#calling player x ways
        oWayT()#calling player o ways
        asc=xsc-osc
        tempa[i][j]=asc
        omove(i,j)
        temparray[i][j]=1
        xmove(i,j)
        temparray[i][j]=0

def fmin():#find the minimum score for machines move
    global tempa
    xd=-1
    yd=-1
    '''xd2=0
    yd2=0'''
    mini=100

    for i in range(3):
        for j in range(3):
            if (tempa[i][j]<mini) and (tempa[i][j]!=0):
                mini=tempa[i][j]
                xd=i
                yd=j
    if xd == 0 and yd == 0:
        t[0].shape(op)
    if xd == 0 and yd == 1:
        t[1].shape(op)
    if xd == 0 and yd == 2:
        t[2].shape(op)
    if xd == 1 and yd == 0:
        t[3].shape(op)
    if xd == 1 and yd == 1 :
        t[4].shape(op)
    if xd == 1 and yd == 2 :
        t[5].shape(op)
    if xd == 2 and yd == 0:
        t[6].shape(op)
    if xd == 2 and yd == 1 :
        t[7].shape(op)
    if xd == 2 and yd == 2 :
        t[8].shape(op)
    '''for i in range(9):
        if xd==xd2 and yd==yd2:
            t[i].shape(op)
        yd2+=1
        if yd2==3:
            yd2=0
            xd2+=1'''
    ccheck()
    chturn()

def payoffda():#readung temperory arrays and starting part of ai processing
    global temparray,tempa,arra
    for i in range(3):
        for j in range(3):
            temparray[i][j]=arra[i][j]
            tempa[i][j]=0
            aip(i,j)
    for i in range(3):
        for j in range(3):
            aip(i,j)


def tclick(i):
    if t[i].shape()==np:
        t[i].shape(xp)
        inarray()
        ccheck()
        chturn()
        payoffda()
        fmin()
turtle.clear()
drawTable()
inarray()
#定义库
进口海龟
从tkinter将messagebox作为msgbox导入
#定义常数
osc=xsc=asc=0#分数变量
np=“n.gif”
op=“o.gif”
xp=“x.gif”
pturn=xp
龟形(np)
海龟。注册_形(op)
海龟。注册_形(xp)
t=[turtle.turtle()表示范围(9)内的x]
arra=[[0]*3表示范围(3)]#内的x表示保留分数的数组
tempa=[[0]*3表示范围(3)]#内的x表示保留分数的数组
temparray=[[0]*3表示范围(3)]#用于保存分数的数组
def tur0(x,y):
t单击(0)
def tur1(x,y):
tclick(1)
def tur2(x,y):
tclick(2)
def tur3(x,y):
tclick(3)
def tur4(x,y):
tclick(4)
def tur5(x,y):
单击(5)
def tur6(x,y):
tclick(6)
def tur7(x,y):
tclick(7)
def tur8(x,y):
tclick(8)
def drawTable():#创建按钮和表格
海龟
乌龟。宽度(5)
乌龟。颜色(“黑色”)
xd=-133
yd=133
对于范围(9)内的i:
t[i].形状(np)
t[i].up()
t[i].goto(xd,yd)
xd+=133
如果xd==2*133:
xd=-133
yd-=133
xd=-64
yd=64
对于范围(2)中的i:
乌龟
乌龟。后藤(-197.5码)
乌龟。设置标题(0)
乌龟
乌龟。fd(394)
yd-=128
乌龟
乌龟。后藤(xd,197.5)
乌龟.设定航向(270)
乌龟
乌龟。fd(394)
xd+=128
t[0].onclick(tur0)
t[1]。onclick(tur1)
t[2].onclick(tur2)
t[3]。onclick(tur3)
t[4]。onclick(tur4)
t[5]。onclick(tur5)
t[6]。onclick(tur6)
t[7]。onclick(tur7)
t[8]。onclick(tur8)
def inarray():#将分数放入数组
全球阿拉
对于范围(3)中的i:
对于范围(3)内的j:
arra[i][j]=0
xd=0
yd=0
对于范围(9)内的i:
如果t[i].shape()==np:
阿拉[xd][yd]=0
elif t[i].shape()==xp:
阿拉[xd][yd]=1
elif t[i].shape()==op:
阿拉[xd][yd]=2
yd+=1
如果yd==3:
yd=0
xd+=1
def grestart():
如果msgbox.askyesno(title=“结束”,message=“游戏结束。是否要重新启动?”)==True:
乌龟
绘图台()
其他:
退出
#这些是用来检查胜利者的
def检查(i、j、k):
a=t[i].shape()+t[j].shape()+t[k].shape()
海龟。颜色(“绿色”)
乌龟。宽度(5)
乌龟
如果a==xp+xp+xp:
turtle.goto(t[i].pos())
乌龟
turtle.goto(t[k].pos())
乌龟
p=msgbox.showinfo(title=“恭喜!”,message=“玩家X获胜!”)
grestart()
elif a==op+op+op:
turtle.goto(t[i].pos())
乌龟
turtle.goto(t[k].pos())
乌龟
p=msgbox.showinfo(title=“Failed!”:(“,message=“玩家O是赢家!”)
grestart()
def ccheck():
检查(0,1,2)
检查(3,4,5)
检查(6,7,8)
检查(0,3,6)
检查(1,4,7)
检查(2,5,8)
检查(0,4,8)
检查(2,4,6)
#---------------------------
def chturn():
全局pturn
如果pturn==xp:
pturn=op
其他:
pturn=xp
def xWayT():#玩家x路临时
全局temparray
全局xsc
xsc=0
“适用于范围(3)内的i”:
如果(temparray[i][0]==1或temparray[i][0]==0)和(temparray[i][1]==1或temparray[i][1]==0)和(temparray[i][2]==1或temparray[i][2]==0):
xsc+=1
对于范围(3)中的i:
如果(temparray[0][i]==1或temparray[0][i]==0)和(temparray[1][i]==1或temparray[1][i]==0)和(temparray[2][i]==1或temparray[2][i]==0):
xsc+=1''
如果(temparray[0][0]==1或temparray[0][0]==0)和(temparray[0][1]==1或temparray[0][1]==0)和(temparray[0][2]==1或temparray[0][2]==0):
xsc+=1
如果(temparray[1][0]==1或temparray[1][0]==0)和(temparray[1][1]==1或temparray[1][1]==0)和(temparray[1][2]==1或temparray[1][2]==0):
xsc+=1
如果(temparray[2][0]==1或temparray[2][0]==0)和(temparray[2][1]==1或temparray[2][1]==0)和(temparray[2][2]==1或temparray[2][2]==0):
xsc+=1
如果(临时数组[0][0]==1或临时数组[0][0]==0)和(临时数组[1][0]==1或临时数组[1][0]==0)和(临时数组[2][0]==1或临时数组[2][0]==0):
xsc+=1
如果(temparray[0][1]==1或temparray[0][1]==0)和(temparray[1][1]==1或temparray[1][1]==0)和(temparray[2][1]==1或temparray[2][1]==0):
xsc+=1
如果(temparray[0][2]==1或temparray[0][2]==0)和(temparray[1][2]==1或temparray[1][2]==0)和(temparray[2][2]==1或temparray[2][2]==0):
xsc+=1
如果(temparray[0][0]==1或temparray[0][0]==0)和(temparray[1][1]==1或temparray[1][1]==0)和(temparray[2][2]==1或temparray[2][2]==0):
xsc+=1
如果(temparray[0][2]==1或temparray[0][2]==0)和(temparray[2][2]==1或temparray[2][2]==0)和(temparray[2][0]==1或temparray[2][0]==0):
xsc+=1
def oWayT():#玩家的方式是暂时的
全局temparray
全球osc
osc=0
“适用于范围(3)内的i”:
如果(temparray[i][0]==2或temparray[i][0]==0)和(temparray[i][1]==2或temparray[i][1]==0)和(temparray[i][2]==2或temparray[i][2]==0):
osc+=1
对于范围(3)中的i:
如果(temparray[0][i]==2或temparray[0][i]==0)和(temparray[1][i]==2或temparray[1][i]==0)和(temparray[2][i]==2或temparray[2][i]==0):
osc+=1
如果(temparray[0][0]==2或temparray[0][0]==0)和(temparray[1][1]==2或temparray[1][1]==0)和(temparray[2][2]==2或temparray[2][2]==0):
osc+=1
如果(temparray[0][2]==2或temparray[0][2]==0)和(temparray[2][2]==2或temparray[2][2]==0)和(temparray[2][0]==2或temparray[2][0]==0):
osc+=1''
如果(temparray[0][0]==2或temparray[0][0]==0)和(temparray[0][1]==2或temparray[0][1]==0)和(temparray[0][2]==2或temparray[