我打赌大家在课堂上肯定玩过这个,想想当年和同桌玩这个废了好几本本子。程序运行截图:


完整程序代码

from tkinter import *
import tkinter.messagebox as msgroot = Tk()
root.title('井字棋-Python代码大全')
# labels
Label(root, text="player1 : X", font="times 15").grid(row=0, column=1)
Label(root, text="player2 : O", font="times 15").grid(row=0, column=2)digits = [1, 2, 3, 4, 5, 6, 7, 8, 9]# for player1 sign = X and for player2 sign= Y
mark = ''# counting the no. of click
count = 0panels = ["panel"] * 10def win(panels, sign):return ((panels[1] == panels[2] == panels[3] == sign)or (panels[1] == panels[4] == panels[7] == sign)or (panels[1] == panels[5] == panels[9] == sign)or (panels[2] == panels[5] == panels[8] == sign)or (panels[3] == panels[6] == panels[9] == sign)or (panels[3] == panels[5] == panels[7] == sign)or (panels[4] == panels[5] == panels[6] == sign)or (panels[7] == panels[8] == panels[9] == sign))def checker(digit):global count, mark, digits# Check which button clickedif digit == 1 and digit in digits:digits.remove(digit)##player1 will play if the value of count is even and for odd player2 will playif count % 2 == 0:mark = 'X'panels[digit] = markelif count % 2 != 0:mark = 'O'panels[digit] = markbutton1.config(text=mark)count = count + 1sign = markif (win(panels, sign) and sign == 'X'):msg.showinfo("Result", "Player1 wins")root.destroy()elif (win(panels, sign) and sign == 'O'):msg.showinfo("Result", "Player2 wins")root.destroy()if digit == 2 and digit in digits:digits.remove(digit)if count % 2 == 0:mark = 'X'panels[digit] = markelif count % 2 != 0:mark = 'O'panels[digit] = markbutton2.config(text=mark)count = count + 1sign = markif (win(panels, sign) and sign == 'X'):msg.showinfo("Result", "Player1 wins")root.destroy()elif (win(panels, sign) and sign == 'O'):msg.showinfo("Result", "Player2 wins")root.destroy()if digit == 3 and digit in digits:digits.remove(digit)if count % 2 == 0:mark = 'X'panels[digit] = markelif count % 2 != 0:mark = 'O'panels[digit] = markbutton3.config(text=mark)count = count + 1sign = markif (win(panels, sign) and sign == 'X'):msg.showinfo("Result", "Player1 wins")root.destroy()elif (win(panels, sign) and sign == 'O'):msg.showinfo("Result", "Player2 wins")root.destroy()if digit == 4 and digit in digits:digits.remove(digit)if count % 2 == 0:mark = 'X'panels[digit] = markelif count % 2 != 0:mark = 'O'panels[digit] = markbutton4.config(text=mark)count = count + 1sign = markif (win(panels, sign) and sign == 'X'):msg.showinfo("Result", "Player1 wins")root.destroy()elif (win(panels, sign) and sign == 'O'):msg.showinfo("Result", "Player2 wins")root.destroy()if digit == 5 and digit in digits:digits.remove(digit)if count % 2 == 0:mark = 'X'panels[digit] = markelif count % 2 != 0:mark = 'O'panels[digit] = markbutton5.config(text=mark)count = count + 1sign = markif (win(panels, sign) and sign == 'X'):msg.showinfo("Result", "Player1 wins")root.destroy()elif (win(panels, sign) and sign == 'O'):msg.showinfo("Result", "Player2 wins")root.destroy()if digit == 6 and digit in digits:digits.remove(digit)if count % 2 == 0:mark = 'X'panels[digit] = markelif count % 2 != 0:mark = 'O'panels[digit] = markbutton6.config(text=mark)count = count + 1sign = markif (win(panels, sign) and sign == 'X'):msg.showinfo("Result", "Player1 wins")root.destroy()elif (win(panels, sign) and sign == 'O'):msg.showinfo("Result", "Player2 wins")root.destroy()if digit == 7 and digit in digits:digits.remove(digit)if count % 2 == 0:mark = 'X'panels[digit] = markelif count % 2 != 0:mark = 'O'panels[digit] = markbutton7.config(text=mark)count = count + 1sign = markif (win(panels, sign) and sign == 'X'):msg.showinfo("Result", "Player1 wins")root.destroy()elif (win(panels, sign) and sign == 'O'):msg.showinfo("Result", "Player2 wins")root.destroy()if digit == 8 and digit in digits:digits.remove(digit)if count % 2 == 0:mark = 'X'panels[digit] = markelif count % 2 != 0:mark = 'O'panels[digit] = markbutton8.config(text=mark)count = count + 1sign = markif (win(panels, sign) and sign == 'X'):msg.showinfo("Result", "Player1 wins")root.destroy()elif (win(panels, sign) and sign == 'O'):msg.showinfo("Result", "Player2 wins")root.destroy()if digit == 9 and digit in digits:digits.remove(digit)if count % 2 == 0:mark = 'X'panels[digit] = markelif count % 2 != 0:mark = 'O'panels[digit] = markbutton9.config(text=mark)count = count + 1sign = markif (win(panels, sign) and sign == 'X'):msg.showinfo("Result", "Player1 wins")root.destroy()elif (win(panels, sign) and sign == 'O'):msg.showinfo("Result", "Player2 wins")root.destroy()###if count is greater then 8 then the match has been tiedif (count > 8 and win(panels, 'X') == False and win(panels, 'O') == False):msg.showinfo("Result", "Match Tied")root.destroy()####define buttons
button1 = Button(root, width=15, font=('Times 16 bold'), height=7, command=lambda: checker(1))
button1.grid(row=1, column=1)
button2 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(2))
button2.grid(row=1, column=2)
button3 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(3))
button3.grid(row=1, column=3)
button4 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(4))
button4.grid(row=2, column=1)
button5 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(5))
button5.grid(row=2, column=2)
button6 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(6))
button6.grid(row=2, column=3)
button7 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(7))
button7.grid(row=3, column=1)
button8 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(8))
button8.grid(row=3, column=2)
button9 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(9))
button9.grid(row=3, column=3)root.mainloop()

更多Python源代码,请关注微信公众号:Python代码大全。

Python井字棋源代码相关推荐

  1. python井字棋。分与人机和朋友下的那种

    python井字棋快来看看孩子的头发 怎么用python做井字棋游戏,废话不多说上代码! 相关说明 怎么用python做井字棋游戏,废话不多说上代码! 觉得有帮助送我上去!!!!!!!!!!!!!!! ...

  2. python井字棋_用Python做一个井字棋小游戏

    井字棋是一个经典的小游戏,在九宫格上玩家轮流画OXO,当每列或每行或是两个对角成一线时便是获胜. 今天就用Python编写一个井字棋小游戏,与电脑对战. 程序执行画面如下图所示: 程序提供了两种人工智 ...

  3. python井字棋ai_实现AI下井字棋的alpha-beta剪枝算法(python实现)

    代码参考自中国大学mooc上人工智能与信息社会陈斌老师的算法,我在原来的基础上增加了玩家输入的异常捕获 AlphaBeta剪枝算法是对Minimax方法的优化,能够极大提高搜索树的效率,如果对这个算法 ...

  4. python井字棋游戏人机对战_用Python做一个井字棋小游戏

    井字棋是一个经典的小游戏,在九宫格上玩家轮流画OXO,当每列或每行或是两个对角成一线时便是获胜. 今天就用Python编写一个井字棋小游戏,与电脑对战. 程序执行画面如下图所示: 程序提供了两种人工智 ...

  5. python井字棋ai_[Python100行系列]-井字棋游戏

    博客:Hzy的博客 | Hzy Blog​hzeyuan.cn一些学习python的小项目,小游戏.python小项目​github.com 话不多说,今天尝试用turtle库来写一个井字棋游戏.1. ...

  6. python井字棋_[Python100行系列]-井字棋游戏

    博客:Hzy的博客 | Hzy Blog​hzeyuan.cn一些学习python的小项目,小游戏.python小项目​github.com 话不多说,今天尝试用turtle库来写一个井字棋游戏.1. ...

  7. python井字棋_python实现井字棋小游戏

    本文为大家分享了python实现井字棋小游戏,供大家参考,具体内容如下 周五晚上上了python的选修课,本来以为老师是从python的基础语法开始的,没想到是从turtle画图开始,正好补上了我以前 ...

  8. python井字棋ai,python 井字棋(Tic Tac Toe)

    说明 用python实现了井字棋,整个框架是本人自己构思的,自认为比较满意.另外,90%+的代码也是本人逐字逐句敲的. minimax算法还没完全理解,所以参考了这里的代码,并作了修改. 特点 可以选 ...

  9. python井字棋游戏代码_python实现井字棋游戏

    python实现井字棋游戏 来源:中文源码网    浏览: 次    日期:2018年9月2日 [下载文档:  python实现井字棋游戏.txt ] (友情提示:右键点上行txt文档名->目标 ...

最新文章

  1. Python培训讲解二叉树的三种深度
  2. flirtlib 测试过程
  3. 为PHP设置服务器(Apache/Nginx)环境变量
  4. mac wordpress php7,Mac 下基于 wordpress 搭建个人博客系统
  5. 17.立体匹配——动态规划公式(Dynamic Programming Formulation),二维网格上的相干立体_4
  6. 图像编码中的小白问题sps ,pps ,nalu ,frame ,silce ect....
  7. SpringBoot 2.1.5(38)---热部署(devtools)配置操作
  8. [python] ZZ 随机数生成
  9. 前景检测算法(十五)--LOBSTER算法
  10. openstack nova后端使用ceph rbd(增加在线迁移live_migrate和快照snapshot功能)
  11. 编译错误:vulkan/vulkan.h:没有那个文件或目录
  12. paip.activex控件在WEB中使用流程与工具
  13. 6U VPX 超高速信号采集处理板(XC7K325T + 4 片DSP TMS320C6678)
  14. 高通android7.0刷机工具使用介绍
  15. 王阳明的智慧:如何让职场从“举步维艰”到“平步青云”?
  16. 未来规划——北京大学数院432应用统计备考攻略
  17. 路普达-区块链技术的本质与未来应用趋势
  18. 矩阵论(一):广义逆矩阵(上)
  19. 360影视大全 python_「www.dy2018.com」python爬取电影天堂(www.dy2018.com)所有视屏的所有链接 - 金橙教程网...
  20. java 图片相似搜索_java获取两张图片的相似度

热门文章

  1. 小 H 的数字c++
  2. Vue学习笔记_组件化
  3. 维谛技术(Vertiv):场景驱动的边缘计算
  4. flash全屏显示和退出代码
  5. 医疗行业数据防泄露解决方案
  6. 大专程序员面试了25家公司,总结出来的痛苦经验!
  7. 三十天学会绘画pdf_【推荐】30天学会绘画pdf百度云下载|30天学会绘画电子版!...
  8. xml文件中处理大于号小于号的方法
  9. FTP工具,3款FTP工具推荐
  10. 初识C语言,了解一下C语言轮廓