鱼C论坛

 找回密码
 立即注册
楼主: wanghaiyang

[技术交流] XO棋,算法有瑕疵,不过你是赢不了AI的

  [复制链接]
发表于 2018-4-18 22:47:42 | 显示全部楼层
学习
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-4-19 12:10:15 | 显示全部楼层
回复
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-4-19 17:50:06 | 显示全部楼层
瞧一瞧看一看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-19 21:32:01 | 显示全部楼层
看看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-4-20 11:49:08 | 显示全部楼层
看看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-4-20 11:54:51 | 显示全部楼层
41531928@qq.com 发表于 2017-5-10 15:59
Tkinter这个模块报错,怎么解决

我也想问
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-20 20:22:40 | 显示全部楼层
learning
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-21 12:23:49 | 显示全部楼层
...
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-4-23 21:52:34 | 显示全部楼层
6666666666666
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-24 23:17:32 | 显示全部楼层
看看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-5-26 15:59:28 | 显示全部楼层
看看,谢谢分享
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-5-28 14:55:47 | 显示全部楼层
表示这个棋    第一步下中心 然后不是太傻就不会输 最多平局   后下的 只要 下在对角线上 基本也不会输 不管对方第一步下在哪
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-5-28 20:51:31 | 显示全部楼层
..
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-5-29 22:19:38 | 显示全部楼层
凄凄切切群群群群群群群群群群群群群群群群
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-5-29 22:41:41 | 显示全部楼层
回过头来还是fishC论坛号
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-6-4 13:01:14 | 显示全部楼层
1111111111
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-6-5 10:11:43 | 显示全部楼层
wanghaiyang 发表于 2016-11-4 22:22
这个棋逻辑简单,搜索算法可以直接考虑所有情况,所以你最多平局,不可能赢他

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-6-5 15:14:26 | 显示全部楼层
挑战楼主
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-6-9 17:38:41 | 显示全部楼层
为啥总是电脑下第一步,太赖了。另外可能有部分鱼油不能运行程序,是模块的名称改了,可以复制我修改后的代码用。
#-*-coding:utf-8-*-
import tkinter
import tkinter.messagebox

root = tkinter.Tk()
root.title("XO棋")

#保存棋盘现状
chessboard=['','','','','','','','','']
#胜局数组8种情况
victorychesskeep=[[0,1,2],[3,4,5],[6,7,8],[0,3,6],[1,4,7],[2,5,8],[0,4,8],[2,4,6]]
isgameover = True

        
#回调函数
def updatebttx_1():
        button_1['text']='O'
        button_1['state']='disabled'#点了就冻结
        chessboard[0]='O'
        if judgemodulcase.judge()== False:#O走完判断
                computerstepcase.computerchess()
                judgemodulcase.judge()#X走完判断
        
def updatebttx_2():
        button_2['text']='O'
        button_2['state']='disabled'
        chessboard[1]='O'
        if judgemodulcase.judge()== False:
                computerstepcase.computerchess()
                judgemodulcase.judge()

def updatebttx_3():
        button_3['text']='O'
        button_3['state']='disabled'
        chessboard[2]='O'
        if judgemodulcase.judge()== False:
                computerstepcase.computerchess()
                judgemodulcase.judge()

def updatebttx_4():
        button_4['text']='O'
        button_4['state']='disabled'
        chessboard[3]='O'
        if judgemodulcase.judge()== False:
                computerstepcase.computerchess()
                judgemodulcase.judge()

def updatebttx_5():
        button_5['text']='O'
        button_5['state']='disabled'
        chessboard[4]='O'
        if judgemodulcase.judge()== False:
                computerstepcase.computerchess()
                judgemodulcase.judge()

def updatebttx_6():
        button_6['text']='O'
        button_6['state']='disabled'
        chessboard[5]='O'
        if judgemodulcase.judge()== False:
                computerstepcase.computerchess()
                judgemodulcase.judge()

def updatebttx_7():
        button_7['text']='O'
        button_7['state']='disabled'
        chessboard[6]='O'
        if judgemodulcase.judge()== False:
                computerstepcase.computerchess()
                judgemodulcase.judge()

def updatebttx_8():
        button_8['text']='O'
        button_8['state']='disabled'
        chessboard[7]='O'
        if judgemodulcase.judge()== False:
                computerstepcase.computerchess()
                judgemodulcase.judge()

def updatebttx_9():
        button_9['text']='O'
        button_9['state']='disabled'
        chessboard[8]='O'
        if judgemodulcase.judge()== False:
                computerstepcase.computerchess()
                judgemodulcase.judge()
        
def chessboardfreeze():#棋盘冻结
        button_1['state']='disabled'
        button_2['state']='disabled'
        button_3['state']='disabled'
        button_4['state']='disabled'
        button_5['state']='disabled'
        button_6['state']='disabled'
        button_7['state']='disabled'
        button_8['state']='disabled'
        button_9['state']='disabled'        
def unchessboardfreeze():#棋盘冻结
        button_1['state']='normal'
        button_2['state']='normal'
        button_3['state']='normal'
        button_4['state']='normal'
        button_5['state']='normal'
        button_6['state']='normal'
        button_7['state']='normal'
        button_8['state']='normal'
        button_9['state']='normal'

#棋盘布局
button_1 = tkinter.Button(root,width = 13,height=5,cursor = "hand2")
button_1['text']=''
#button_1.bind('<Button-1>',updatebttx_1)
button_1["command"] = updatebttx_1
button_1.grid(row=0,column = 0)

button_2 = tkinter.Button(root,width = 13,height=5,cursor = "hand2")
button_2['text']=''
button_2["command"] = updatebttx_2
button_2.grid(row=0,column = 1)

button_3 = tkinter.Button(root,width = 13,height=5,cursor = "hand2")
button_3['text']=''
button_3["command"] =updatebttx_3
button_3.grid(row=0,column = 2)

button_4 = tkinter.Button(root,width = 13,height=5,cursor = "hand2")
button_4['text']=''
button_4["command"] =updatebttx_4
button_4.grid(row=1,column = 0)

button_5 = tkinter.Button(root,width = 13,height=5,cursor = "hand2")
button_5['text']=''
button_5["command"] = updatebttx_5
button_5.grid(row=1,column = 1)

button_6 = tkinter.Button(root,width = 13,height=5,cursor = "hand2")
button_6['text']=''
button_6["command"] = updatebttx_6
button_6.grid(row=1,column = 2)

button_7 = tkinter.Button(root,width = 13,height=5,cursor = "hand2")
button_7['text']=''
button_7["command"] = updatebttx_7
button_7.grid(row=2,column = 0)

button_8 = tkinter.Button(root,width = 13,height=5,cursor = "hand2")
button_8['text']=''
button_8["command"] = updatebttx_8
button_8.grid(row=2,column = 1)

button_9 = tkinter.Button(root,width = 13,height=5,cursor = "hand2")
button_9['text']=''
button_9["command"] = updatebttx_9
button_9.grid(row=2,column = 2)

        
class Judgemodul:#判断胜负类
        def judge(self):
                ishasend = False
                eval = chessthink.evaluatefunc()
                if eval == 1000 or eval == -1000 or eval ==0:
                        ishasend = True
                        chessboardfreeze()
                        if(eval == 1000):
                            tkinter.messagebox.showinfo(title='结果', message='电脑赢了')
                        if(eval == -1000):
                            tkinter.messagebox.showinfo(title='结果', message='你赢了')
                        if(eval == 0):
                            tkinter.messagebox.showinfo(title='结果', message='平局')
                return ishasend        


class Thinkfunc:#AI走法类
               
        def        evaluatefunc(self):#评估函数
                end = 1#结果初始化 0:平局,1:继续,1000:赢......
                isfull = True#棋盘是否已满
                Xcx = 0#X的2连子计量
                Ocx = 0
                for ech in chessboard:
                        if ech == '':
                                isfull = False
                                break        
                for x in range(0,len(victorychesskeep)):
                        #3连子
                        if chessboard[victorychesskeep[x][0]] == chessboard[victorychesskeep[x][1]] ==chessboard[victorychesskeep[x][2]]:
                                if chessboard[victorychesskeep[x][0]] != '':
                                        if chessboard[victorychesskeep[x][0]] == 'X':
                                                end =1000
                                                return end
                                        elif chessboard[victorychesskeep[x][0]] == 'O':
                                                end = -1000
                                                return end
                        elif end != 1000 or end != -1000:#没赢没输
                                if isfull == True:#棋盘满了
                                        end = 0#平局
                                        return end
                                elif isfull == False:#没赢没输没满,判断2连子
                                        if chessboard[victorychesskeep[x][0]]==chessboard[victorychesskeep[x][1]] or chessboard[victorychesskeep[x][2]]==chessboard[victorychesskeep[x][1]]:                        
                                                if chessboard[victorychesskeep[x][1]] != '':
                                                        if chessboard[victorychesskeep[x][1]] == 'X':
                                                                Xcx = Xcx +1
                                                        elif chessboard[victorychesskeep[x][1]] =='O':
                                                                Ocx = Ocx +1
               
                end = Xcx*50 - Ocx*50#2连子差
                if end == 0:#不是平局的0,则继续
                        end = 1
                return end                                
                                
        
        def recurmin(self,searchdeep,alpha,beta):
                isgameover = False
                bettervalue = 1000
                value = self.evaluatefunc()
                if value == 1000 or value == -1000 or value == 0:
                        isgameover = True
                if(alpha >= beta):#对min最有利>=对max最有利,β剪枝
                        return value
                if(searchdeep == 0 or isgameover == True):
                        return value
                for ech in range(0,len(chessboard)):
                        if chessboard[ech] == '':
                                chessboard[ech]= 'O'
                                recordvalue = self.recurmax(searchdeep-1,alpha,min(bettervalue,beta))
                                bettervalue = min(recordvalue,bettervalue)
                                chessboard[ech] =''
                return bettervalue
        
        def recurmax(self,searchdeep,alpha,beta):
                isgameover = False
                bettervalue = -1000
                value = self.evaluatefunc()
                if value ==1000 or value == -1000 or value == 0:
                        isgameover = True
                if(beta <= alpha):#对max最有利<=对min最有利,α剪枝
                        return value
                if(searchdeep ==0 or isgameover == True):
                        return value
                for ech in range(0,len(chessboard)):
                        if chessboard[ech] == '':
                                chessboard[ech] = 'X'
                                recordvalue = self.recurmin(searchdeep-1,max(alpha,bettervalue),beta)
                                bettervalue = max(recordvalue,bettervalue)
                                chessboard[ech] =''
                return bettervalue

                        
        def minMax(self,searchdeep):
                keepindex=[]
                bettervalue = -1000
                for ech in range(0,len(chessboard)):
                        if chessboard[ech] == '':
                                chessboard[ech] = 'X'#假设
                                recordvalue = self.recurmin(searchdeep,-1000,1000)
                                if(recordvalue >= bettervalue):#最大值
                                        bettervalue = recordvalue
                                        betterposition = ech
                                chessboard[ech] = ''#还原
                return betterposition



class Computerstep:#电脑走棋类
        def computerchess(self):
                nextsp = chessthink.minMax(5)
                if nextsp == 0:
                        button_1["text"]='X'
                        button_1['state']='disabled'#点了就冻结
                        chessboard[0]='X'
                elif nextsp ==1:
                        button_2["text"]='X'
                        button_2['state']='disabled'
                        chessboard[1]='X'
                elif nextsp ==2:
                        button_3["text"]='X'
                        button_3['state']='disabled'
                        chessboard[2]='X'
                elif nextsp ==3:
                        button_4["text"]='X'
                        button_4['state']='disabled'
                        chessboard[3]='X'
                elif nextsp ==4:
                        button_5["text"]='X'
                        button_5['state']='disabled'
                        chessboard[4]='X'
                elif nextsp ==5:
                        button_6["text"]='X'
                        button_6['state']='disabled'
                        chessboard[5]='X'
                elif nextsp ==6:
                        button_7["text"]='X'
                        button_7['state']='disabled'
                        chessboard[6]='X'
                elif nextsp ==7:
                        button_8["text"]='X'
                        button_8['state']='disabled'
                        chessboard[7]='X'
                elif nextsp ==8:
                        button_9["text"]='X'
                        button_9['state']='disabled'
                        chessboard[8]='X'

def main():
    unchessboardfreeze()#解冻               
    computerstepcase.computerchess()#第一步
    chessboard=['','','','','','','','','']
    victorychesskeep=[[0,1,2],[3,4,5],[6,7,8],[0,3,6],[1,4,7],[2,5,8],[0,4,8],[2,4,6]]
    isgameover = True
        
chessthink = Thinkfunc()
computerstepcase = Computerstep()
judgemodulcase = Judgemodul()
main()
root.resizable(width='false',height='false')#不改变窗体大小
root.mainloop()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2018-8-5 14:56:37 | 显示全部楼层
41531928@qq.com 发表于 2017-5-10 15:59
Tkinter这个模块报错,怎么解决

是tkinter   T是小写的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-19 22:28

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表