2

我用 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()
4

0 回答 0