bestallen
发表于 2018-4-18 22:47:42
学习
ABC23
发表于 2018-4-19 12:10:15
回复
大大饭
发表于 2018-4-19 17:50:06
瞧一瞧看一看
空灵神探
发表于 2018-4-19 21:32:01
看看
JDZ
发表于 2018-4-20 11:49:08
看看
JDZ
发表于 2018-4-20 11:54:51
41531928@qq.com 发表于 2017-5-10 15:59
Tkinter这个模块报错,怎么解决
我也想问
kede
发表于 2018-4-20 20:22:40
learning
wzh313000
发表于 2018-4-21 12:23:49
...
daimiaopeng
发表于 2018-4-23 21:52:34
6666666666666
没见过海爱尔兰
发表于 2018-4-24 23:17:32
看看
spynelf
发表于 2018-5-26 15:59:28
看看,谢谢分享
糖妖妖66
发表于 2018-5-28 14:55:47
表示这个棋 第一步下中心 然后不是太傻就不会输 最多平局 后下的 只要 下在对角线上 基本也不会输 不管对方第一步下在哪
luroot153
发表于 2018-5-28 20:51:31
..
噬魂之诗
发表于 2018-5-29 22:19:38
凄凄切切群群群群群群群群群群群群群群群群
xuwenqiang224
发表于 2018-5-29 22:41:41
回过头来还是fishC论坛号
mty
发表于 2018-6-4 13:01:14
1111111111
My_A
发表于 2018-6-5 10:11:43
wanghaiyang 发表于 2016-11-4 22:22
这个棋逻辑简单,搜索算法可以直接考虑所有情况,所以你最多平局,不可能赢他
{:5_91:}
不喝农夫的山泉
发表于 2018-6-5 15:14:26
挑战楼主
不喝农夫的山泉
发表于 2018-6-9 17:38:41
为啥总是电脑下第一步,太赖了{:5_94:}。另外可能有部分鱼油不能运行程序,是模块的名称改了,可以复制我修改后的代码用。
#-*-coding:utf-8-*-
import tkinter
import tkinter.messagebox
root = tkinter.Tk()
root.title("XO棋")
#保存棋盘现状
chessboard=['','','','','','','','','']
#胜局数组8种情况
victorychesskeep=[,,,,,,,]
isgameover = True
#回调函数
def updatebttx_1():
button_1['text']='O'
button_1['state']='disabled'#点了就冻结
chessboard='O'
if judgemodulcase.judge()== False:#O走完判断
computerstepcase.computerchess()
judgemodulcase.judge()#X走完判断
def updatebttx_2():
button_2['text']='O'
button_2['state']='disabled'
chessboard='O'
if judgemodulcase.judge()== False:
computerstepcase.computerchess()
judgemodulcase.judge()
def updatebttx_3():
button_3['text']='O'
button_3['state']='disabled'
chessboard='O'
if judgemodulcase.judge()== False:
computerstepcase.computerchess()
judgemodulcase.judge()
def updatebttx_4():
button_4['text']='O'
button_4['state']='disabled'
chessboard='O'
if judgemodulcase.judge()== False:
computerstepcase.computerchess()
judgemodulcase.judge()
def updatebttx_5():
button_5['text']='O'
button_5['state']='disabled'
chessboard='O'
if judgemodulcase.judge()== False:
computerstepcase.computerchess()
judgemodulcase.judge()
def updatebttx_6():
button_6['text']='O'
button_6['state']='disabled'
chessboard='O'
if judgemodulcase.judge()== False:
computerstepcase.computerchess()
judgemodulcase.judge()
def updatebttx_7():
button_7['text']='O'
button_7['state']='disabled'
chessboard='O'
if judgemodulcase.judge()== False:
computerstepcase.computerchess()
judgemodulcase.judge()
def updatebttx_8():
button_8['text']='O'
button_8['state']='disabled'
chessboard='O'
if judgemodulcase.judge()== False:
computerstepcase.computerchess()
judgemodulcase.judge()
def updatebttx_9():
button_9['text']='O'
button_9['state']='disabled'
chessboard='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] == chessboard] ==chessboard]:
if chessboard] != '':
if chessboard] == 'X':
end =1000
return end
elif chessboard] == 'O':
end = -1000
return end
elif end != 1000 or end != -1000:#没赢没输
if isfull == True:#棋盘满了
end = 0#平局
return end
elif isfull == False:#没赢没输没满,判断2连子
if chessboard]==chessboard] or chessboard]==chessboard]:
if chessboard] != '':
if chessboard] == 'X':
Xcx = Xcx +1
elif chessboard] =='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 == '':
chessboard= 'O'
recordvalue = self.recurmax(searchdeep-1,alpha,min(bettervalue,beta))
bettervalue = min(recordvalue,bettervalue)
chessboard =''
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 == '':
chessboard = 'X'
recordvalue = self.recurmin(searchdeep-1,max(alpha,bettervalue),beta)
bettervalue = max(recordvalue,bettervalue)
chessboard =''
return bettervalue
def minMax(self,searchdeep):
keepindex=[]
bettervalue = -1000
for ech in range(0,len(chessboard)):
if chessboard == '':
chessboard = 'X'#假设
recordvalue = self.recurmin(searchdeep,-1000,1000)
if(recordvalue >= bettervalue):#最大值
bettervalue = recordvalue
betterposition = ech
chessboard = ''#还原
return betterposition
class Computerstep:#电脑走棋类
def computerchess(self):
nextsp = chessthink.minMax(5)
if nextsp == 0:
button_1["text"]='X'
button_1['state']='disabled'#点了就冻结
chessboard='X'
elif nextsp ==1:
button_2["text"]='X'
button_2['state']='disabled'
chessboard='X'
elif nextsp ==2:
button_3["text"]='X'
button_3['state']='disabled'
chessboard='X'
elif nextsp ==3:
button_4["text"]='X'
button_4['state']='disabled'
chessboard='X'
elif nextsp ==4:
button_5["text"]='X'
button_5['state']='disabled'
chessboard='X'
elif nextsp ==5:
button_6["text"]='X'
button_6['state']='disabled'
chessboard='X'
elif nextsp ==6:
button_7["text"]='X'
button_7['state']='disabled'
chessboard='X'
elif nextsp ==7:
button_8["text"]='X'
button_8['state']='disabled'
chessboard='X'
elif nextsp ==8:
button_9["text"]='X'
button_9['state']='disabled'
chessboard='X'
def main():
unchessboardfreeze()#解冻
computerstepcase.computerchess()#第一步
chessboard=['','','','','','','','','']
victorychesskeep=[,,,,,,,]
isgameover = True
chessthink = Thinkfunc()
computerstepcase = Computerstep()
judgemodulcase = Judgemodul()
main()
root.resizable(width='false',height='false')#不改变窗体大小
root.mainloop()
xiaqiyi
发表于 2018-8-5 14:56:37
41531928@qq.com 发表于 2017-5-10 15:59
Tkinter这个模块报错,怎么解决
是tkinter T是小写的
页:
1
2
[3]
4
5
6
7
8
9
10
11
12