跳到正文

个人记录

Python 五子棋AI 代码

shikongyeyv约 86 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 86 分钟

版本

  1. v1当前

五子棋AI第四版

根据第三版加快速度,但棋力无提升。

Python
s=int(input('五子棋第四版\n===制作者:实空===\n棋盘几路(越大越慢,建议15):'))
nd=0
while nd<=0:
    nd=2*int(input('难度(须大于0,越难越慢,建议3为正常落子速度):'))
l=[[0 for _ in range(s)]for _ in range(s)]
c=s-1
jg=30
jp=0
hh=0
qz=[[],[]]
from time import*
from random import randint
from turtle import*
ht()
bgcolor((0,0,0))
penup()
tracer(0)
def draw(x,y):
    pensize(5)
    goto(s*jg/2,s*jg/2)
    pencolor((1,0.8,0))
    fillcolor((1,0.5,0))
    begin_fill()
    pendown()
    goto(s*jg/2,-s*jg/2)
    goto(-s*jg/2,-s*jg/2)
    goto(-s*jg/2,s*jg/2)
    goto(s*jg/2,s*jg/2)
    end_fill()
    penup()
    pencolor((0,0,0))
    goto(-c*jg/2,-c*jg/2)
    pendown()
    goto(-c*jg/2,c*jg/2)
    goto(c*jg/2,c*jg/2)
    goto(c*jg/2,-c*jg/2)
    goto(-c*jg/2,-c*jg/2)
    penup()
    pensize(1)
    for i in range(1,s-1):
        goto(-c*jg/2+i*jg,-c*jg/2)
        pendown()
        goto(-c*jg/2+i*jg,c*jg/2)
        penup()
        goto(-c*jg/2,-c*jg/2+i*jg)
        pendown()
        goto(c*jg/2,-c*jg/2+i*jg)
        penup()
    if s&1:
        goto(0,0)
        dot(10,(0,0,0))
    if s>12:
        goto((c/2-3)*jg,(c/2-3)*jg)
        dot(10,(0,0,0))
        goto((3-c/2)*jg,(c/2-3)*jg)
        dot(10,(0,0,0))
        goto((c/2-3)*jg,(3-c/2)*jg)
        dot(10,(0,0,0))
        goto((3-c/2)*jg,(3-c/2)*jg)
        dot(10,(0,0,0))
    if s>18 and s&1:
        goto(0,(c/2-3)*jg)
        dot(10,(0,0,0))
        goto((c/2-3)*jg,0)
        dot(10,(0,0,0))
        goto(0,(3-c/2)*jg)
        dot(10,(0,0,0))
        goto((3-c/2)*jg,0)
        dot(10,(0,0,0))
    for i in range(s):
        for j in range(s):
            if l[i][j]==1:
                goto(-c*jg/2+i*jg,-c*jg/2+j*jg)
                dot(27,(0,0,0))
            elif l[i][j]:
                goto(-c*jg/2+i*jg,-c*jg/2+j*jg)
                dot(27,(1,1,1))
            if i==x and j==y:
                dot(10,(1,0,0))
draw(s,s)
def pd(x,y,z):
    global l
    for i in range(x-4,x+1):
        if i>=0 and i<s-4:
            p=1
            for j in range(i,i+5):
                if not l[j][y]==z:
                    p=0
                    break
            if p:
                return 1
    for i in range(y-4,y+1):
        if i>=0 and i<s-4:
            p=1
            for j in range(i,i+5):
                if not l[x][j]==z:
                    p=0
                    break
            if p:
                return 1
    for i in range(-4,1):
        if x>=-i and x+i<s-4 and y>=-i and y+i<s-4:
            p=1
            for j in range(i,i+5):
                if not l[x+j][y+j]==z:
                    p=0
                    break
            if p:
                return 1
        if x>=-i and x+i<s-4 and y-i<s and y-i>=4:
            p=1
            for j in range(i,i+5):
                if not l[x+j][y-j]==z:
                    p=0
                    break
            if p:
                return 1
    return 0
def hq():
    c=[[0 for _ in range(s)]for _ in range(s)]
    for i in range(s):
        for j in range(s-4):
            hs=0
            bs=0
            for k in range(j,j+5):
                if l[i][k]==1:
                    hs+=1
                elif l[i][k]==2:
                    bs+=1
            if not hs or not bs:
                t=hs+bs
                for k in range(j,j+5):
                    c[i][k]+=t*t
            hs=0
            bs=0
            for k in range(j,j+5):
                if l[k][i]==1:
                    hs+=1
                elif l[k][i]==2:
                    bs+=1
            if not hs or not bs:
                t=hs+bs
                for k in range(j,j+5):
                    c[k][i]+=t*t
    for i in range(s-4):
        for j in range(s-4):
            hs=0
            bs=0
            for k in range(5):
                if l[i+k][j+k]==1:
                    hs+=1
                elif l[i+k][j+k]==2:
                    bs+=1
            if not hs or not bs:
                t=hs+bs
                for k in range(5):
                    c[i+k][j+k]+=t*t
            hs=0
            bs=0
            for k in range(5):
                if l[i+k][j+4-k]==1:
                    hs+=1
                elif l[i+k][j+4-k]==2:
                    bs+=1
            if not hs or not bs:
                t=hs+bs
                for k in range(5):
                    c[i+k][j+4-k]+=t*t
    k=[]
    v=0
    for i in range(s):
        for j in range(s):
            if not l[i][j]:
                if c[i][j]>v:
                    v=c[i][j]
                    k=[(i,j)]
                elif c[i][j]==v:
                    k.append((i,j))
    if hh==1:
        return[k[randint(0,len(k)-1)]]
    return k
def pf(x,z,b,alpha,beta):
    global l
    f=[]
    g=[[],[],[],[]]
    r=[]
    m=[]
    c=2*z-3
    ans=0.5
    if z==1:
        v=float('inf')
    else:
        v=-float('inf')
    for u in qz[2-z]:
        n=0
        for i in range(1,4):
            if u[0]<i:
                n=n*3+z
            else:
                n=n*3+l[u[0]-i][u[1]]
        if u[0]>s-3:
            continue
        if l[u[0]+1][u[1]]!=z:
            p=bool(l[u[0]+1][u[1]])
            if l[u[0]+2][u[1]]!=z:
                p+=2*bool(l[u[0]+2][u[1]])
                if not n and p==1:
                    r+=[(u[0]-1,u[1]),(u[0]-2,u[1])]
                    ans-=1
                elif n<3 and p==3:
                    m+=[(u[0]-1,u[1]),(u[0]-2,u[1])]
                    ans-=1<<8
                if u[0]>s-4:
                    continue
                if l[u[0]+3][u[1]]!=z:
                    p+=4*bool(l[u[0]+3][u[1]])
                    if n<3:
                        if p==1:
                            r+=[(u[0]-1,u[1]),(u[0]+2,u[1])]
                            ans-=1
                        elif p==2:
                            r+=[(u[0]-1,u[1]),(u[0]+1,u[1])]
                            ans-=1
                        elif p==3:
                            if u[0]+4<s:
                                if not l[u[0]+4][u[1]]:
                                    g[3].append((u[0]-1,u[1]))
                            else:
                                g[3]+=[(u[0]-2,u[1]),(u[0]-1,u[1]),(u[0]+3,u[1])]
                            ans-=1<<16
                    if n<9:
                        if p==3:
                            m+=[(u[0]-1,u[1]),(u[0]+3,u[1])]
                            ans-=1<<8
                        elif p==5:
                            m+=[(u[0]-1,u[1]),(u[0]+2,u[1])]
                            ans-=1<<8
                        elif p==6:
                            m+=[(u[0]-1,u[1]),(u[0]+1,u[1])]
                            ans-=1<<8
                        elif p==7:
                            g[1].append((u[0]-1,u[1]))
                            ans-=1<<24
                    if u[0]>s-5:
                        continue
                    if l[u[0]+4][u[1]]!=z:
                        p+=8*bool(l[u[0]+4][u[1]])
                        if n<9:
                            if p==1:
                                r+=[(u[0]+2,u[1]),(u[0]+3,u[1])]
                                ans-=1
                            elif p==2:
                                r+=[(u[0]+1,u[1]),(u[0]+3,u[1])]
                                ans-=1
                            elif p==3:
                                if n<3:
                                    g[3].append((u[0]+3,u[1]))
                                else:
                                    g[3]+=[(u[0]-1,u[1]),(u[0]+3,u[1]),(u[0]+4,u[1])]
                                ans-=1<<16
                            elif p==4:
                                r+=[(u[0]+1,u[1]),(u[0]+2,u[1])]
                                ans-=1
                            elif p==5:
                                g[3]+=[(u[0]-1,u[1]),(u[0]+2,u[1]),(u[0]+4,u[1])]
                                ans-=1<<16
                            elif p==6:
                                g[3]+=[(u[0]-1,u[1]),(u[0]+1,u[1]),(u[0]+4,u[1])]
                                ans-=1<<16
                        if p==3:
                            m+=[(u[0]+3,u[1]),(u[0]+4,u[1])]
                            ans-=1<<8
                        elif p==5:
                            m+=[(u[0]+2,u[1]),(u[0]+4,u[1])]
                            ans-=1<<8
                        elif p==6:
                            m+=[(u[0]+1,u[1]),(u[0]+4,u[1])]
                            ans-=1<<8
                        elif p==7:
                            g[1].append((u[0]+4,u[1]))
                            ans-=1<<24
                        elif p==9:
                            m+=[(u[0]+2,u[1]),(u[0]+3,u[1])]
                            ans-=1<<8
                        elif p==10:
                            m+=[(u[0]+1,u[1]),(u[0]+3,u[1])]
                            ans-=1<<8
                        elif p==11:
                            g[1].append((u[0]+3,u[1]))
                            ans-=1<<24
                        elif p==12:
                            m+=[(u[0]+1,u[1]),(u[0]+2,u[1])]
                            ans-=1<<8
                        elif p==13:
                            g[1].append((u[0]+2,u[1]))
                            ans-=1<<24
                        elif p==14:
                            g[1].append((u[0]+1,u[1]))
                            ans-=1<<24
        n=0
        for i in range(1,4):
            if u[1]<i:
                n=n*3+z
            else:
                n=n*3+l[u[0]][u[1]-i]
        if u[1]>s-3:
            continue
        if l[u[0]][u[1]+1]!=z:
            p=bool(l[u[0]][u[1]+1])
            if l[u[0]][u[1]+2]!=z:
                p+=2*bool(l[u[0]][u[1]+2])
                if not n and p==1:
                    r+=[(u[0],u[1]-1),(u[0],u[1]-2)]
                    ans-=1
                elif n<3 and p==3:
                    m+=[(u[0],u[1]-1),(u[0],u[1]-2)]
                    ans-=1<<8
                if u[1]>s-4:
                    continue
                if l[u[0]][u[1]+3]!=z:
                    p+=4*bool(l[u[0]][u[1]+3])
                    if n<3:
                        if p==1:
                            r+=[(u[0],u[1]-1),(u[0],u[1]+2)]
                            ans-=1
                        elif p==2:
                            r+=[(u[0],u[1]-1),(u[0],u[1]+1)]
                            ans-=1
                        elif p==3:
                            if u[1]+4<s:
                                if not l[u[0]][u[1]+4]:
                                    g[3].append((u[0],u[1]-1))
                            else:
                                g[3]+=[(u[0],u[1]-2),(u[0],u[1]-1),(u[0],u[1]+3)]
                            ans-=1<<16
                    if n<9:
                        if p==3:
                            m+=[(u[0],u[1]-1),(u[0],u[1]+3)]
                            ans-=1<<8
                        elif p==5:
                            m+=[(u[0],u[1]-1),(u[0],u[1]+2)]
                            ans-=1<<8
                        elif p==6:
                            m+=[(u[0],u[1]-1),(u[0],u[1]+1)]
                            ans-=1<<8
                        elif p==7:
                            g[1].append((u[0],u[1]-1))
                            ans-=1<<24
                    if u[1]>s-5:
                        continue
                    if l[u[0]][u[1]+4]!=z:
                        p+=8*bool(l[u[0]][u[1]+4])
                        if n<9:
                            if p==1:
                                r+=[(u[0],u[1]+2),(u[0],u[1]+3)]
                                ans-=1
                            elif p==2:
                                r+=[(u[0],u[1]+1),(u[0],u[1]+3)]
                                ans-=1
                            elif p==3:
                                if n<3:
                                    g[3].append((u[0],u[1]+3))
                                else:
                                    g[3]+=[(u[0],u[1]-1),(u[0],u[1]+3),(u[0],u[1]+4)]
                                ans-=1<<16
                            elif p==4:
                                r+=[(u[0],u[1]+1),(u[0],u[1]+2)]
                                ans-=1
                            elif p==5:
                                g[3]+=[(u[0],u[1]-1),(u[0],u[1]+2),(u[0],u[1]+4)]
                                ans-=1<<16
                            elif p==6:
                                g[3]+=[(u[0],u[1]-1),(u[0],u[1]+1),(u[0],u[1]+4)]
                                ans-=1<<16
                        if p==3:
                            m+=[(u[0],u[1]+3),(u[0],u[1]+4)]
                            ans-=1<<8
                        elif p==5:
                            m+=[(u[0],u[1]+2),(u[0],u[1]+4)]
                            ans-=1<<8
                        elif p==6:
                            m+=[(u[0],u[1]+1),(u[0],u[1]+4)]
                            ans-=1<<8
                        elif p==7:
                            g[1].append((u[0],u[1]+4))
                            ans-=1<<24
                        elif p==9:
                            m+=[(u[0],u[1]+2),(u[0],u[1]+3)]
                            ans-=1<<8
                        elif p==10:
                            m+=[(u[0],u[1]+1),(u[0],u[1]+3)]
                            ans-=1<<8
                        elif p==11:
                            g[1].append((u[0],u[1]+3))
                            ans-=1<<24
                        elif p==12:
                            m+=[(u[0],u[1]+1),(u[0],u[1]+2)]
                            ans-=1<<8
                        elif p==13:
                            g[1].append((u[0],u[1]+2))
                            ans-=1<<24
                        elif p==14:
                            g[1].append((u[0],u[1]+1))
                            ans-=1<<24
        n=0
        for i in range(1,4):
            if u[1]<i or u[0]<i:
                n=n*3+z
            else:
                n=n*3+l[u[0]-i][u[1]-i]
        if u[0]>s-3 or u[1]>s-3:
            continue
        if l[u[0]+1][u[1]+1]!=z:
            p=bool(l[u[0]+1][u[1]+1])
            if l[u[0]+2][u[1]+2]!=z:
                p+=2*bool(l[u[0]+2][u[1]+2])
                if not n and p==1:
                    r+=[(u[0]-1,u[1]-1),(u[0]-2,u[1]-2)]
                    ans-=1
                elif n<3 and p==3:
                    m+=[(u[0]-1,u[1]-1),(u[0]-2,u[1]-2)]
                    ans-=1<<8
                if u[0]>s-4 or u[1]>s-4:
                    continue
                if l[u[0]+3][u[1]+3]!=z:
                    p+=4*bool(l[u[0]+3][u[1]+3])
                    if n<3:
                        if p==1:
                            r+=[(u[0]-1,u[1]-1),(u[0]+2,u[1]+2)]
                            ans-=1
                        elif p==2:
                            r+=[(u[0]-1,u[1]-1),(u[0]+1,u[1]+1)]
                            ans-=1
                        elif p==3:
                            if u[0]+4<s and u[1]+4<s:
                                if not l[u[0]+4][u[1]+4]:
                                    g[3].append((u[0]-1,u[1]-1))
                            else:
                                g[3]+=[(u[0]-2,u[1]-2),(u[0]-1,u[1]-1),(u[0]+3,u[1]+3)]
                            ans-=1<<16
                    if n<9:
                        if p==3:
                            m+=[(u[0]-1,u[1]-1),(u[0]+3,u[1]+3)]
                            ans-=1<<8
                        elif p==5:
                            m+=[(u[0]-1,u[1]-1),(u[0]+2,u[1]+2)]
                            ans-=1<<8
                        elif p==6:
                            m+=[(u[0]-1,u[1]-1),(u[0]+1,u[1]+1)]
                            ans-=1<<8
                        elif p==7:
                            g[1].append((u[0]-1,u[1]-1))
                            ans-=1<<24
                    if u[0]>s-5 or u[1]>s-5:
                        continue
                    if l[u[0]+4][u[1]+4]!=z:
                        p+=8*bool(l[u[0]+4][u[1]+4])
                        if n<9:
                            if p==1:
                                r+=[(u[0]+2,u[1]+2),(u[0]+3,u[1]+3)]
                                ans-=1
                            elif p==2:
                                r+=[(u[0]+1,u[1]+1),(u[0]+3,u[1]+3)]
                                ans-=1
                            elif p==3:
                                if n<3:
                                    g[3].append((u[0]+3,u[1]+3))
                                else:
                                    g[3]+=[(u[0]-1,u[1]-1),(u[0]+3,u[1]+3),(u[0]+4,u[1]+4)]
                                ans-=1<<16
                            elif p==4:
                                r+=[(u[0]+1,u[1]+1),(u[0]+2,u[1]+2)]
                                ans-=1
                            elif p==5:
                                g[3]+=[(u[0]-1,u[1]-1),(u[0]+2,u[1]+2),(u[0]+4,u[1]+4)]
                                ans-=1<<16
                            elif p==6:
                                g[3]+=[(u[0]-1,u[1]-1),(u[0]+1,u[1]+1),(u[0]+4,u[1]+4)]
                                ans-=1<<16
                        if p==3:
                            m+=[(u[0]+3,u[1]+3),(u[0]+4,u[1]+4)]
                            ans-=1<<8
                        elif p==5:
                            m+=[(u[0]+2,u[1]+2),(u[0]+4,u[1]+4)]
                            ans-=1<<8
                        elif p==6:
                            m+=[(u[0]+1,u[1]+1),(u[0]+4,u[1]+4)]
                            ans-=1<<8
                        elif p==7:
                            g[1].append((u[0]+4,u[1]+4))
                            ans-=1<<24
                        elif p==9:
                            m+=[(u[0]+2,u[1]+2),(u[0]+3,u[1]+3)]
                            ans-=1<<8
                        elif p==10:
                            m+=[(u[0]+1,u[1]+1),(u[0]+3,u[1]+3)]
                            ans-=1<<8
                        elif p==11:
                            g[1].append((u[0]+3,u[1]+3))
                            ans-=1<<24
                        elif p==12:
                            m+=[(u[0]+1,u[1]+1),(u[0]+2,u[1]+2)]
                            ans-=1<<8
                        elif p==13:
                            g[1].append((u[0]+2,u[1]+2))
                            ans-=1<<24
                        elif p==14:
                            g[1].append((u[0]+1,u[1]+1))
                            ans-=1<<24
        n=0
        for i in range(1,4):
            if u[1]+i>=s or u[0]<i:
                n=n*3+z
            else:
                n=n*3+l[u[0]-i][u[1]+i]
        if u[0]>s-3 or u[1]<2:
            continue
        if l[u[0]+1][u[1]-1]!=z:
            p=bool(l[u[0]+1][u[1]-1])
            if l[u[0]+2][u[1]-2]!=z:
                p+=2*bool(l[u[0]+2][u[1]-2])
                if not n and p==1:
                    r+=[(u[0]-1,u[1]+1),(u[0]-2,u[1]+2)]
                    ans-=1
                elif n<3 and p==3:
                    m+=[(u[0]-1,u[1]+1),(u[0]-2,u[1]+2)]
                    ans-=1<<8
                if u[0]>s-4 or u[1]<3:
                    continue
                if l[u[0]+3][u[1]-3]!=z:
                    p+=4*bool(l[u[0]+3][u[1]-3])
                    if n<3:
                        if p==1:
                            r+=[(u[0]-1,u[1]+1),(u[0]+2,u[1]-2)]
                            ans-=1
                        elif p==2:
                            r+=[(u[0]-1,u[1]+1),(u[0]+1,u[1]-1)]
                            ans-=1
                        elif p==3:
                            if u[0]+4<s and u[1]>3:
                                if not l[u[0]+4][u[1]-4]:
                                    g[3].append((u[0]-1,u[1]+1))
                            else:
                                g[3]+=[(u[0]-2,u[1]+2),(u[0]-1,u[1]+1),(u[0]+3,u[1]-3)]
                            ans-=1<<16
                    if n<9:
                        if p==3:
                            m+=[(u[0]-1,u[1]+1),(u[0]+3,u[1]-3)]
                            ans-=1<<8
                        elif p==5:
                            m+=[(u[0]-1,u[1]+1),(u[0]+2,u[1]-2)]
                            ans-=1<<8
                        elif p==6:
                            m+=[(u[0]-1,u[1]+1),(u[0]+1,u[1]-1)]
                            ans-=1<<8
                        elif p==7:
                            g[1].append((u[0]-1,u[1]+1))
                            ans-=1<<24
                    if u[0]>s-5 or u[1]<4:
                        continue
                    if l[u[0]+4][u[1]-4]!=z:
                        p+=8*bool(l[u[0]+4][u[1]-4])
                        if n<9:
                            if p==1:
                                r+=[(u[0]+2,u[1]-2),(u[0]+3,u[1]-3)]
                                ans-=1
                            elif p==2:
                                r+=[(u[0]+1,u[1]-1),(u[0]+3,u[1]-3)]
                                ans-=1
                            elif p==3:
                                if n<3:
                                    g[3].append((u[0]+3,u[1]-3))
                                else:
                                    g[3]+=[(u[0]-1,u[1]+1),(u[0]+3,u[1]-3),(u[0]+4,u[1]-4)]
                                ans-=1<<16
                            elif p==4:
                                r+=[(u[0]+1,u[1]-1),(u[0]+2,u[1]-2)]
                                ans-=1
                            elif p==5:
                                g[3]+=[(u[0]-1,u[1]+1),(u[0]+2,u[1]-2),(u[0]+4,u[1]-4)]
                                ans-=1<<16
                            elif p==6:
                                g[3]+=[(u[0]-1,u[1]+1),(u[0]+1,u[1]-1),(u[0]+4,u[1]-4)]
                                ans-=1<<16
                        if p==3:
                            m+=[(u[0]+3,u[1]-3),(u[0]+4,u[1]-4)]
                            ans-=1<<8
                        elif p==5:
                            m+=[(u[0]+2,u[1]-2),(u[0]+4,u[1]-4)]
                            ans-=1<<8
                        elif p==6:
                            m+=[(u[0]+1,u[1]-1),(u[0]+4,u[1]-4)]
                            ans-=1<<8
                        elif p==7:
                            g[1].append((u[0]+4,u[1]-4))
                            ans-=1<<24
                        elif p==9:
                            m+=[(u[0]+2,u[1]-2),(u[0]+3,u[1]-3)]
                            ans-=1<<8
                        elif p==10:
                            m+=[(u[0]+1,u[1]-1),(u[0]+3,u[1]-3)]
                            ans-=1<<8
                        elif p==11:
                            g[1].append((u[0]+3,u[1]-3))
                            ans-=1<<24
                        elif p==12:
                            m+=[(u[0]+1,u[1]-1),(u[0]+2,u[1]-2)]
                            ans-=1<<8
                        elif p==13:
                            g[1].append((u[0]+2,u[1]-2))
                            ans-=1<<24
                        elif p==14:
                            g[1].append((u[0]+1,u[1]-1))
                            ans-=1<<24
    for u in qz[z-1]:
        n=0
        for i in range(1,4):
            if u[0]<i:
                n=n*3+3-z
            else:
                n=n*3+l[u[0]-i][u[1]]
        if u[0]>s-3:
            continue
        if l[u[0]+1][u[1]]!=3-z:
            p=bool(l[u[0]+1][u[1]])
            if l[u[0]+2][u[1]]!=3-z:
                p+=2*bool(l[u[0]+2][u[1]])
                if not n and p==1:
                    r+=[(u[0]-1,u[1]),(u[0]-2,u[1])]
                    ans+=1<<4
                elif n<3 and p==3:
                    f+=[(u[0]-1,u[1]),(u[0]-2,u[1])]
                    ans+=1<<12
                if u[0]>s-4:
                    continue
                if l[u[0]+3][u[1]]!=3-z:
                    p+=4*bool(l[u[0]+3][u[1]])
                    if n<3:
                        if p==1:
                            r+=[(u[0]-1,u[1]),(u[0]+2,u[1])]
                            ans+=1<<4
                        elif p==2:
                            r+=[(u[0]-1,u[1]),(u[0]+1,u[1])]
                            ans+=1<<4
                        elif p==3:
                            g[2]+=[(u[0]-2,u[1]),(u[0]-1,u[1]),(u[0]+3,u[1])]
                            ans+=1<<20
                    if n<9:
                        if p==3:
                            f+=[(u[0]-1,u[1]),(u[0]+3,u[1])]
                            ans+=1<<12
                        elif p==5:
                            f+=[(u[0]-1,u[1]),(u[0]+2,u[1])]
                            ans+=1<<12
                        elif p==6:
                            f+=[(u[0]-1,u[1]),(u[0]+1,u[1])]
                            ans+=1<<12
                        elif p==7:
                            g[0].append((u[0]-1,u[1]))
                            ans+=1<<28
                    if u[0]>s-5:
                        continue
                    if l[u[0]+4][u[1]]!=3-z:
                        p+=8*bool(l[u[0]+4][u[1]])
                        if n<9:
                            if p==1:
                                r+=[(u[0]+2,u[1]),(u[0]+3,u[1])]
                                ans+=1<<4
                            elif p==2:
                                r+=[(u[0]+1,u[1]),(u[0]+3,u[1])]
                                ans+=1<<4
                            elif p==3:
                                g[2]+=[(u[0]-1,u[1]),(u[0]+3,u[1]),(u[0]+4,u[1])]
                                ans+=1<<20
                            elif p==4:
                                r+=[(u[0]+1,u[1]),(u[0]+2,u[1])]
                                ans+=1<<4
                            elif p==5:
                                g[2]+=[(u[0]-1,u[1]),(u[0]+2,u[1]),(u[0]+4,u[1])]
                                ans+=1<<20
                            elif p==6:
                                g[2]+=[(u[0]-1,u[1]),(u[0]+1,u[1]),(u[0]+4,u[1])]
                                ans+=1<<20
                        if p==3:
                            f+=[(u[0]+3,u[1]),(u[0]+4,u[1])]
                            ans+=1<<12
                        elif p==5:
                            f+=[(u[0]+2,u[1]),(u[0]+4,u[1])]
                            ans+=1<<12
                        elif p==6:
                            f+=[(u[0]+1,u[1]),(u[0]+4,u[1])]
                            ans+=1<<12
                        elif p==7:
                            g[0].append((u[0]+4,u[1]))
                            ans+=1<<28
                        elif p==9:
                            f+=[(u[0]+2,u[1]),(u[0]+3,u[1])]
                            ans+=1<<12
                        elif p==10:
                            f+=[(u[0]+1,u[1]),(u[0]+3,u[1])]
                            ans+=1<<12
                        elif p==11:
                            g[0].append((u[0]+3,u[1]))
                            ans+=1<<28
                        elif p==12:
                            f+=[(u[0]+1,u[1]),(u[0]+2,u[1])]
                            ans+=1<<12
                        elif p==13:
                            g[0].append((u[0]+2,u[1]))
                            ans+=1<<28
                        elif p==14:
                            g[0].append((u[0]+1,u[1]))
                            ans+=1<<28
        n=0
        for i in range(1,4):
            if u[1]<i:
                n=n*3+3-z
            else:
                n=n*3+l[u[0]][u[1]-i]
        if u[1]>s-3:
            continue
        if l[u[0]][u[1]+1]!=3-z:
            p=bool(l[u[0]][u[1]+1])
            if l[u[0]][u[1]+2]!=3-z:
                p+=2*bool(l[u[0]][u[1]+2])
                if not n and p==1:
                    r+=[(u[0],u[1]-1),(u[0],u[1]-2)]
                    ans+=1<<4
                elif n<3 and p==3:
                    f+=[(u[0],u[1]-1),(u[0],u[1]-2)]
                    ans+=1<<12
                if u[1]>s-4:
                    continue
                if l[u[0]][u[1]+3]!=3-z:
                    p+=4*bool(l[u[0]][u[1]+3])
                    if n<3:
                        if p==1:
                            r+=[(u[0],u[1]-1),(u[0],u[1]+2)]
                            ans+=1<<4
                        elif p==2:
                            r+=[(u[0],u[1]-1),(u[0],u[1]+1)]
                            ans+=1<<4
                        elif p==3:
                            g[2]+=[(u[0],u[1]-2),(u[0],u[1]-1),(u[0],u[1]+3)]
                            ans+=1<<20
                    if n<9:
                        if p==3:
                            f+=[(u[0],u[1]-1),(u[0],u[1]+3)]
                            ans+=1<<12
                        elif p==5:
                            f+=[(u[0],u[1]-1),(u[0],u[1]+2)]
                            ans+=1<<12
                        elif p==6:
                            f+=[(u[0],u[1]-1),(u[0],u[1]+1)]
                            ans+=1<<12
                        elif p==7:
                            g[0].append((u[0],u[1]-1))
                            ans+=1<<28
                    if u[1]>s-5:
                        continue
                    if l[u[0]][u[1]+4]!=3-z:
                        p+=8*bool(l[u[0]][u[1]+4])
                        if n<9:
                            if p==1:
                                r+=[(u[0],u[1]+2),(u[0],u[1]+3)]
                                ans+=1<<4
                            elif p==2:
                                r+=[(u[0],u[1]+1),(u[0],u[1]+3)]
                                ans+=1<<4
                            elif p==3:
                                g[2]+=[(u[0],u[1]-1),(u[0],u[1]+3),(u[0],u[1]+4)]
                                ans+=1<<20
                            elif p==4:
                                r+=[(u[0],u[1]+1),(u[0],u[1]+2)]
                                ans+=1<<4
                            elif p==5:
                                g[2]+=[(u[0],u[1]-1),(u[0],u[1]+2),(u[0],u[1]+4)]
                                ans+=1<<20
                            elif p==6:
                                g[2]+=[(u[0],u[1]-1),(u[0],u[1]+1),(u[0],u[1]+4)]
                                ans+=1<<20
                        if p==3:
                            f+=[(u[0],u[1]+3),(u[0],u[1]+4)]
                            ans+=1<<12
                        elif p==5:
                            f+=[(u[0],u[1]+2),(u[0],u[1]+4)]
                            ans+=1<<12
                        elif p==6:
                            f+=[(u[0],u[1]+1),(u[0],u[1]+4)]
                            ans+=1<<12
                        elif p==7:
                            g[0].append((u[0],u[1]+4))
                            ans+=1<<28
                        elif p==9:
                            f+=[(u[0],u[1]+2),(u[0],u[1]+3)]
                            ans+=1<<12
                        elif p==10:
                            f+=[(u[0],u[1]+1),(u[0],u[1]+3)]
                            ans+=1<<12
                        elif p==11:
                            g[0].append((u[0],u[1]+3))
                            ans+=1<<28
                        elif p==12:
                            f+=[(u[0],u[1]+1),(u[0],u[1]+2)]
                            ans+=1<<12
                        elif p==13:
                            g[0].append((u[0],u[1]+2))
                            ans+=1<<28
                        elif p==14:
                            g[0].append((u[0],u[1]+1))
                            ans+=1<<28
        n=0
        for i in range(1,4):
            if u[1]<i or u[0]<i:
                n=n*3+3-z
            else:
                n=n*3+l[u[0]-i][u[1]-i]
        if u[0]>s-3 or u[1]>s-3:
            continue
        if l[u[0]+1][u[1]+1]!=3-z:
            p=bool(l[u[0]+1][u[1]+1])
            if l[u[0]+2][u[1]+2]!=3-z:
                p+=2*bool(l[u[0]+2][u[1]+2])
                if not n and p==1:
                    r+=[(u[0]-1,u[1]-1),(u[0]-2,u[1]-2)]
                    ans+=1<<4
                elif n<3 and p==3:
                    f+=[(u[0]-1,u[1]-1),(u[0]-2,u[1]-2)]
                    ans+=1<<12
                if u[0]>s-4 or u[1]>s-4:
                    continue
                if l[u[0]+3][u[1]+3]!=3-z:
                    p+=4*bool(l[u[0]+3][u[1]+3])
                    if n<3:
                        if p==1:
                            r+=[(u[0]-1,u[1]-1),(u[0]+2,u[1]+2)]
                            ans+=1<<4
                        elif p==2:
                            r+=[(u[0]-1,u[1]-1),(u[0]+1,u[1]+1)]
                            ans+=1<<4
                        elif p==3:
                            g[2]+=[(u[0]-2,u[1]-2),(u[0]-1,u[1]-1),(u[0]+3,u[1]+3)]
                            ans+=1<<20
                    if n<9:
                        if p==3:
                            f+=[(u[0]-1,u[1]-1),(u[0]+3,u[1]+3)]
                            ans+=1<<12
                        elif p==5:
                            f+=[(u[0]-1,u[1]-1),(u[0]+2,u[1]+2)]
                            ans+=1<<12
                        elif p==6:
                            f+=[(u[0]-1,u[1]-1),(u[0]+1,u[1]+1)]
                            ans+=1<<12
                        elif p==7:
                            g[0].append((u[0]-1,u[1]-1))
                            ans+=1<<28
                    if u[0]>s-5 or u[1]>s-5:
                        continue
                    if l[u[0]+4][u[1]+4]!=3-z:
                        p+=8*bool(l[u[0]+4][u[1]+4])
                        if n<9:
                            if p==1:
                                r+=[(u[0]+2,u[1]+2),(u[0]+3,u[1]+3)]
                                ans+=1<<4
                            elif p==2:
                                r+=[(u[0]+1,u[1]+1),(u[0]+3,u[1]+3)]
                                ans+=1<<4
                            elif p==3:
                                g[2]+=[(u[0]-1,u[1]-1),(u[0]+3,u[1]+3),(u[0]+4,u[1]+4)]
                                ans+=1<<20
                            elif p==4:
                                r+=[(u[0]+1,u[1]+1),(u[0]+2,u[1]+2)]
                                ans+=1<<4
                            elif p==5:
                                g[2]+=[(u[0]-1,u[1]-1),(u[0]+2,u[1]+2),(u[0]+4,u[1]+4)]
                                ans+=1<<20
                            elif p==6:
                                g[2]+=[(u[0]-1,u[1]-1),(u[0]+1,u[1]+1),(u[0]+4,u[1]+4)]
                                ans+=1<<20
                        if p==3:
                            f+=[(u[0]+3,u[1]+3),(u[0]+4,u[1]+4)]
                            ans+=1<<12
                        elif p==5:
                            f+=[(u[0]+2,u[1]+2),(u[0]+4,u[1]+4)]
                            ans+=1<<12
                        elif p==6:
                            f+=[(u[0]+1,u[1]+1),(u[0]+4,u[1]+4)]
                            ans+=1<<12
                        elif p==7:
                            g[0].append((u[0]+4,u[1]+4))
                            ans+=1<<28
                        elif p==9:
                            f+=[(u[0]+2,u[1]+2),(u[0]+3,u[1]+3)]
                            ans+=1<<12
                        elif p==10:
                            f+=[(u[0]+1,u[1]+1),(u[0]+3,u[1]+3)]
                            ans+=1<<12
                        elif p==11:
                            g[0].append((u[0]+3,u[1]+3))
                            ans+=1<<28
                        elif p==12:
                            f+=[(u[0]+1,u[1]+1),(u[0]+2,u[1]+2)]
                            ans+=1<<12
                        elif p==13:
                            g[0].append((u[0]+2,u[1]+2))
                            ans+=1<<28
                        elif p==14:
                            g[0].append((u[0]+1,u[1]+1))
                            ans+=1<<28
        n=0
        for i in range(1,4):
            if u[1]+i>=s or u[0]<i:
                n=n*3+3-z
            else:
                n=n*3+l[u[0]-i][u[1]+i]
        if u[0]>s-3 or u[1]<2:
            continue
        if l[u[0]+1][u[1]-1]!=3-z:
            p=bool(l[u[0]+1][u[1]-1])
            if l[u[0]+2][u[1]-2]!=3-z:
                p+=2*bool(l[u[0]+2][u[1]-2])
                if not n and p==1:
                    r+=[(u[0]-1,u[1]+1),(u[0]-2,u[1]+2)]
                    ans+=1<<4
                elif n<3 and p==3:
                    f+=[(u[0]-1,u[1]+1),(u[0]-2,u[1]+2)]
                    ans+=1<<12
                if u[0]>s-4 or u[1]<3:
                    continue
                if l[u[0]+3][u[1]-3]!=3-z:
                    p+=4*bool(l[u[0]+3][u[1]-3])
                    if n<3:
                        if p==1:
                            r+=[(u[0]-1,u[1]+1),(u[0]+2,u[1]-2)]
                            ans+=1<<4
                        elif p==2:
                            r+=[(u[0]-1,u[1]+1),(u[0]+1,u[1]-1)]
                            ans+=1<<4
                        elif p==3:
                            g[2]+=[(u[0]-2,u[1]+2),(u[0]-1,u[1]+1),(u[0]+3,u[1]-3)]
                            ans+=1<<20
                    if n<9:
                        if p==3:
                            f+=[(u[0]-1,u[1]+1),(u[0]+3,u[1]-3)]
                            ans+=1<<12
                        elif p==5:
                            f+=[(u[0]-1,u[1]+1),(u[0]+2,u[1]-2)]
                            ans+=1<<12
                        elif p==6:
                            f+=[(u[0]-1,u[1]+1),(u[0]+1,u[1]-1)]
                            ans+=1<<12
                        elif p==7:
                            g[0].append((u[0]-1,u[1]+1))
                            ans+=1<<28
                    if u[0]>s-5 or u[1]<4:
                        continue
                    if l[u[0]+4][u[1]-4]!=3-z:
                        p+=8*bool(l[u[0]+4][u[1]-4])
                        if n<9:
                            if p==1:
                                r+=[(u[0]+2,u[1]-2),(u[0]+3,u[1]-3)]
                                ans+=1<<4
                            elif p==2:
                                r+=[(u[0]+1,u[1]-1),(u[0]+3,u[1]-3)]
                                ans+=1<<4
                            elif p==3:
                                g[2]+=[(u[0]-1,u[1]+1),(u[0]+3,u[1]-3),(u[0]+4,u[1]-4)]
                                ans+=1<<20
                            elif p==4:
                                r+=[(u[0]+1,u[1]-1),(u[0]+2,u[1]-2)]
                                ans+=1<<4
                            elif p==5:
                                g[2]+=[(u[0]-1,u[1]+1),(u[0]+2,u[1]-2),(u[0]+4,u[1]-4)]
                                ans+=1<<20
                            elif p==6:
                                g[2]+=[(u[0]-1,u[1]+1),(u[0]+1,u[1]-1),(u[0]+4,u[1]-4)]
                                ans+=1<<20
                        if p==3:
                            f+=[(u[0]+3,u[1]-3),(u[0]+4,u[1]-4)]
                            ans+=1<<12
                        elif p==5:
                            f+=[(u[0]+2,u[1]-2),(u[0]+4,u[1]-4)]
                            ans+=1<<12
                        elif p==6:
                            f+=[(u[0]+1,u[1]-1),(u[0]+4,u[1]-4)]
                            ans+=1<<12
                        elif p==7:
                            g[0].append((u[0]+4,u[1]-4))
                            ans+=1<<28
                        elif p==9:
                            f+=[(u[0]+2,u[1]-2),(u[0]+3,u[1]-3)]
                            ans+=1<<12
                        elif p==10:
                            f+=[(u[0]+1,u[1]-1),(u[0]+3,u[1]-3)]
                            ans+=1<<12
                        elif p==11:
                            g[0].append((u[0]+3,u[1]-3))
                            ans+=1<<28
                        elif p==12:
                            f+=[(u[0]+1,u[1]-1),(u[0]+2,u[1]-2)]
                            ans+=1<<12
                        elif p==13:
                            g[0].append((u[0]+2,u[1]-2))
                            ans+=1<<28
                        elif p==14:
                            g[0].append((u[0]+1,u[1]-1))
                            ans+=1<<28
    if g[0]:
        if b:
            return g[0][0]
        return(x+4)*c<<28
    elif g[1]:
        g[1]=list(set(g[1]))
        if len(g[1])>1:
            if b:
                return g[1][0]
            return-(x+3)*c<<28
        elif not b and g[3]and not g[1][0]in g[3]:
            return-(x+1)*c<<28
        f=g[1]
    elif g[2]:
        if not b:
            return(x+2)*c<<28
        f=g[2]
    elif g[3]:
        f+=g[3]
    else:
        f+=m
        h=list({i for i in r if r.count(i)>1})
        if h:
            f+=h
        elif not f:
            f=r
        f+=hq()
    if not x:
        return c*ans
    f=list(set(f))
    if not f:
        return 0
    if len(f)==1 and b:
        return f[0]
    for i in f:
        l[i[0]][i[1]]=z
        qz[z-1].append((i[0],i[1]))
        e=pf(x-1,3-z,0,alpha,beta)
        l[i[0]][i[1]]=0
        qz[z-1].pop()
        if z==1:
            if e<v:
                v=e
                d=i
            if v<=alpha:
                break
            beta=min(beta,v)
        else:
            if e>v:
                v=e
                d=i
            if v>=beta:
                break
            alpha=max(alpha,v)
    if b:
        return d
    else:
        return v
def omc(x,y):
    global l,jg,c,hh,jp
    if jp==1:
        return
    u=round(x/jg+c/2)
    v=round(y/jg+c/2)
    if u<0 or u>=s or v<0 or v>=s:
        return
    if l[u][v]==0:
        st=time()
        hh+=1
        clear()
        l[u][v]=1
        qz[0].append((u,v))
        if pd(u,v,1):
            bgcolor((0,1,0))
            draw(u,v)
            goto(0,0)
            pencolor((1,0,1))
            write('玩家'+str(hh)+'回合胜',0,'center',font=('Arial',70,'normal'))
            pencolor((0,0,0))
            jp=1
            return
        if hh*2-1==s*s:
            bgcolor((0,1,1))
            draw(u,v)
            goto(0,0)
            pencolor((1,0,0))
            write(str(hh)+'回合平局',0,'center',font=('Arial',70,'normal'))
            pencolor((0,0,0))
            jp=1
            return
        t=pf(nd,2,1,-float('inf'),float('inf'))
        l[t[0]][t[1]]=2
        qz[1].append(t)
        if pd(t[0],t[1],2):
            bgcolor((0,0,1))
            draw(t[0],t[1])
            goto(0,0)
            pencolor((1,1,0))
            write('玩家'+str(hh)+'回合负',0,'center',font=('Arial',1<<6,'normal'))
            pencolor((0,0,0))
            jp=1
            return
        if hh*2==s*s:
            bgcolor((0,1,1))
            draw(t[0],t[1])
            goto(0,0)
            pencolor((1,0,0))
            write(str(hh)+'回合平局',0,'center',font=('Arial',1<<6,'normal'))
            pencolor((0,0,0))
            jp=1
            return
        draw(t[0],t[1])
        print('思考时间:'+str(round(time()-st,3))+'秒')
onscreenclick(omc)
mainloop()
原文