鱼C论坛

 找回密码
 立即注册
查看: 3304|回复: 2

[技术交流] python 井字棋游戏 简单版,不知道为什么不判断,用Tkinter做的界面

[复制链接]
发表于 2017-12-8 05:43:08 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
看了下论坛里的帖子 就自己学着写了一个简单版(未加入电脑),但是判断胜负的那段代码没起作用,不知道为啥。
来个人帮我解答下呗,万分感激!!!
我把代码贴上来:


  1. # -*- coding: utf-8 -*-
  2. # bulid by Bean_Wei/ 2017/12/07 22:17

  3. from Tkinter import *
  4. import tkMessageBox
  5. root=Tk()
  6. root.title("X O")

  7. #棋盘现况
  8. chessboard=['','','','','','','','','']
  9. #胜局数组
  10. winchess=[[0,1,2],[3,4,5],[6,7,8],[0,3,6],[1,4,7],[2,5,8],[0,4,8],[2,4,6]]

  11. #判断胜负
  12. def judge():
  13.     w=0
  14.     for c in chessboard:
  15.         if c!='':
  16.             w+=1
  17.             return w
  18.     if w!=0:
  19.         for win in range(0,len(winchess)):
  20.             if chessboard[winchess[win][0]]==chessboard[winchess[win][1]]==chessboard[winchess[win][2]]:
  21.                 tkMessageBox.showinfo(title='结果', message='%s胜'%chessboard[winchess[win][0]])
  22.             elif w==9:
  23.                 tkMessageBox.showinfo(title='结果', message='平局')
  24.             else:
  25.                 continue

  26. #更新棋盘 ,先手为“X”后手为“O”
  27. def rebtn_1():
  28.     num=0
  29.     for i in chessboard:
  30.         if i=='':
  31.             num+=1
  32.     if (num % 2)==0:
  33.         button_1['text']='O'
  34.     else:
  35.         button_1['text']='X'
  36.     button_1['state']='disabled'
  37.     chessboard[0]=button_1['text']
  38.     judge()

  39. def rebtn_2():
  40.     num=0
  41.     for i in chessboard:
  42.         if i=='':
  43.             num+=1
  44.     if (num % 2)==0:
  45.         button_2['text']='O'
  46.     else:
  47.         button_2['text']='X'
  48.     button_2['state']='disabled'
  49.     chessboard[1]=button_2['text']
  50.     judge()

  51. def rebtn_3():
  52.     num=0
  53.     for i in chessboard:
  54.         if i=='':
  55.             num+=1
  56.     if (num % 2)==0:
  57.         button_3['text']='O'
  58.     else:
  59.         button_3['text']='X'
  60.     button_3['state']='disabled'
  61.     chessboard[2]=button_3['text']
  62.     judge()

  63. def rebtn_4():
  64.     num=0
  65.     for i in chessboard:
  66.         if i=='':
  67.             num+=1
  68.     if (num % 2)==0:
  69.         button_4['text']='O'
  70.     else:
  71.         button_4['text']='X'
  72.     button_4['state']='disabled'
  73.     chessboard[3]=button_4['text']
  74.     judge()

  75. def rebtn_5():
  76.     num=0
  77.     for i in chessboard:
  78.         if i=='':
  79.             num+=1
  80.     if (num % 2)==0:
  81.         button_5['text']='O'
  82.     else:
  83.         button_5['text']='X'
  84.     button_5['state']='disabled'
  85.     chessboard[4]=button_5['text']
  86.     judge()

  87. def rebtn_6():
  88.     num=0
  89.     for i in chessboard:
  90.         if i=='':
  91.             num+=1
  92.     if (num % 2)==0:
  93.         button_6['text']='O'
  94.     else:
  95.         button_6['text']='X'
  96.     button_6['state']='disabled'
  97.     chessboard[5]=button_6['text']
  98.     judge()

  99. def rebtn_7():
  100.     num=0
  101.     for i in chessboard:
  102.         if i=='':
  103.             num+=1
  104.     if (num % 2)==0:
  105.         button_7['text']='O'
  106.     else:
  107.         button_7['text']='X'
  108.     button_7['state']='disabled'
  109.     chessboard[6]=button_7['text']
  110.     judge()

  111. def rebtn_8():
  112.     num=0
  113.     for i in chessboard:
  114.         if i=='':
  115.             num+=1
  116.     if (num % 2)==0:
  117.         button_8['text']='O'
  118.     else:
  119.         button_8['text']='X'
  120.     button_8['state']='disabled'
  121.     chessboard[7]=button_8['text']
  122.     judge()

  123. def rebtn_9():
  124.     num=0
  125.     for i in chessboard:
  126.         if i=='':
  127.             num+=1
  128.     if (num % 2)==0:
  129.         button_9['text']='O'
  130.     else:
  131.         button_9['text']='X'
  132.     button_9['state']='disabled'
  133.     chessboard[8]=button_9['text']
  134.     judge()

  135. #创建棋盘
  136. button_1=Button(root,width=13,height=5,cursor='hand2')
  137. button_1['text']=''
  138. button_1['command']=rebtn_1
  139. button_1.grid(row=0,column=0)

  140. button_2=Button(root,width=13,height=5,cursor='hand2')
  141. button_2['text']=''
  142. button_2['command']=rebtn_2
  143. button_2.grid(row=0,column=1)

  144. button_3=Button(root,width=13,height=5,cursor='hand2')
  145. button_3['text']=''
  146. button_3['command']=rebtn_3
  147. button_3.grid(row=0,column=2)

  148. button_4=Button(root,width=13,height=5,cursor='hand2')
  149. button_4['text']=''
  150. button_4['command']=rebtn_4
  151. button_4.grid(row=1,column=0)

  152. button_5=Button(root,width=13,height=5,cursor='hand2')
  153. button_5['text']=''
  154. button_5['command']=rebtn_5
  155. button_5.grid(row=1,column=1)

  156. button_6=Button(root,width=13,height=5,cursor='hand2')
  157. button_6['text']=''
  158. button_6['command']=rebtn_6
  159. button_6.grid(row=1,column=2)

  160. button_7=Button(root,width=13,height=5,cursor='hand2')
  161. button_7['text']=''
  162. button_7['command']=rebtn_7
  163. button_7.grid(row=2,column=0)

  164. button_8=Button(root,width=13,height=5,cursor='hand2')
  165. button_8['text']=''
  166. button_8['command']=rebtn_8
  167. button_8.grid(row=2,column=1)

  168. button_9=Button(root,width=13,height=5,cursor='hand2')
  169. button_9['text']=''
  170. button_9['command']=rebtn_9
  171. button_9.grid(row=2,column=2)


  172. root.resizable(width='false',height='false')
  173. root.mainloop()
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-12-8 11:38:21 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-12-11 04:46:58 | 显示全部楼层
jerryxjr1220 发表于 2017-12-8 11:38
给你参考下http://bbs.fishc.com/thread-82270-1-1.html

现在可以判断了,但是卡在 棋盘满且有一方获胜的情况 了,不晓得怎么写。
  1. def judge():
  2.     for win in range(0,len(winchess)):
  3.         if chessboard[winchess[win][0]]==chessboard[winchess[win][1]]==chessboard[winchess[win][2]]:
  4.             if chessboard[winchess[win][0]]!='':
  5.                 tkMessageBox.showinfo(title='结果', message='%s胜' % chessboard[winchess[win][0]])
  6.                 break
  7.     if '' not in chessboard:
  8.         tkMessageBox.showinfo(title='结果', message='平局')
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-3-28 22:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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