python实现简单的井字棋游戏(gui界面)


Posted in Python onJanuary 22, 2021

项目输出

python实现简单的井字棋游戏(gui界面)

项目先决条件

要使用python构建井字游戏,我们需要tkinter模块和python的基本概念

Tkinter模块是用于渲染图形的标准图形用户界面。

Tkinter.messagebox用于显示消息框

要安装tkinter模块,我们在命令提示符下使用了pip install命令:

pip install tkinter

项目文件结构

这些是使用python构建井字游戏的步骤:

  • 导入模块
  • 初始化窗口
  • 检查结果的功能
  • 检查获胜者的功能
  • 定义标签和按钮

1.导入模块

from tkinter import *
import tkinter.messagebox as msg

在此步骤中,我们导入tkinter和messsagebox模块

2.初始化窗口

root= Tk()
root.title('TIC-TAC-TOE---DataFlair')

digits = [1,2,3,4,5,6,7,8,9]
mark = '' “
count = 0
panels = ["panel"]*10
  • Tk()用于初始化窗口
  • title()用于设置窗口的标题

3.检查结果的功能

def 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))

在此功能中,将通过检查哪个玩家连续打出三个标记(上,下,对角或对角线)来检查结果。

4.检查获胜者的功能

def checker(digit):
 global count, mark, digits
 if digit==1 and digit in digits:
  digits.remove(digit)
  if count%2==0:
   mark ='X'
   panels[digit]=mark
  elif count%2!=0:
   mark = 'O'
   panels[digit]=mar
  button1.config(text = mark)
  count = count+1
  sign = mark
  if(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]=mark
  elif count%2!=0:
   mark = 'O'
   panels[digit]=mark
  button2.config(text = mark)
  count = count+1
  sign = mark
  if(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]=mark
  elif count%2!=0:
   mark = 'O'
   panels[digit]=mark
  button3.config(text = mark)
  count = count+1
  sign = mark
  if(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]=mark
  elif count%2!=0:
   mark = 'O'
   panels[digit]=mark
  button4.config(text = mark)
  count = count+1
  sign = mark
  if(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]=mark
  elif count%2!=0:
   mark = 'O'
   panels[digit]=mark
  button5.config(text = mark)
  count = count+1
  sign = mark
  if(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]=mark
  elif count%2!=0:
   mark = 'O'
   panels[digit]=mark
  button6.config(text = mark)
  count = count+1
  sign 
if(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]=mark
  elif count%2!=0:
   mark = 'O'
   panels[digit]=mark
  button7.config(text = mark)
  count = count+1
  sign = mark
  if(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]=mark
  elif count%2!=0:
   mark = 'O'
   panels[digit]=mark
  button8.config(text = mark)
  count = count+1
  sign = mark
  if(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]=mark
  elif count%2!=0:
   mark = 'O'
   panels[digit]=mark
  button9.config(text = mark)
  count = count+1
  sign = mark
  if(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>8 and win(panels,'X')==False and win(panels,'O')==False):
  msg.showinfo("Result","Match Tied")
  root.destroy()

玩家总共有9次点击以玩游戏。玩家每次单击时,如果count的值大于8,则通过将count的值增加1来减少机会,则游戏结果为平局

  • 如果count的值为偶数,则玩家1将玩,否则玩家2将玩。
  • config()用于用适当的文本标记按钮
  • messagebox小部件中的showinfo()方法用于显示一些相关信息
  • destroy()停止mainloop退出程序

5.定义标签和按钮

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)

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()

Label()小部件,用于显示用户无法修改的文本。
Button()小部件显示按钮

  • root是我们引用的窗口的名称
  • 文本存储我们在标签上显示的值
  • 文字所使用的字体
  • 单击按钮时将调用命令
  • lambda()函数用于将特定数据发送到回调函数。

要运行程序时,将执行mainloop()方法。

完整代码

from tkinter import *
import tkinter.messagebox as msg

root= Tk()
root.title('TIC-TAC-TOE---Project Gurukul')
#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 = 0


panels = ["panel"]*10


def 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 clicked 
 
 if digit==1 and digit in digits:
  digits.remove(digit)
##player1 will play if the value of count is even and for odd player2 will play
  if count%2==0:
   mark ='X'
   panels[digit]=mark
  elif count%2!=0:
   mark = 'O'
   panels[digit]=mark

  button1.config(text = mark)
  count = count+1
  sign = mark

  if(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]=mark
  elif count%2!=0:
   mark = 'O'
   panels[digit]=mark

  button2.config(text = mark)
  count = count+1
  sign = mark

  if(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]=mark
  elif count%2!=0:
   mark = 'O'
   panels[digit]=mark

  button3.config(text = mark)
  count = count+1
  sign = mark

  if(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]=mark
  elif count%2!=0:
   mark = 'O'
   panels[digit]=mark

  button4.config(text = mark)
  count = count+1
  sign = mark

  if(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]=mark
  elif count%2!=0:
   mark = 'O'
   panels[digit]=mark

  button5.config(text = mark)
  count = count+1
  sign = mark

  if(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]=mark
  elif count%2!=0:
   mark = 'O'
   panels[digit]=mark

  button6.config(text = mark)
  count = count+1
  sign = mark

  if(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]=mark
  elif count%2!=0:
   mark = 'O'
   panels[digit]=mark

  button7.config(text = mark)
  count = count+1
  sign = mark

  if(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]=mark
  elif count%2!=0:
   mark = 'O'
   panels[digit]=mark

  button8.config(text = mark)
  count = count+1
  sign = mark

  if(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]=mark
  elif count%2!=0:
   mark = 'O'
   panels[digit]=mark

  button9.config(text = mark)
  count = count+1
  sign = mark

  if(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 tied 
 if(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 相关文章推荐
Python原始字符串(raw strings)用法实例
Oct 13 Python
python让图片按照exif信息里的创建时间进行排序的方法
Mar 16 Python
python实现备份目录的方法
Aug 03 Python
Python正则表达式教程之二:捕获篇
Mar 02 Python
对Python 2.7 pandas 中的read_excel详解
May 04 Python
python获取地震信息 微信实时推送
Jun 18 Python
pyqt 实现为长内容添加滑轮 scrollArea
Jun 19 Python
python对csv文件追加写入列的方法
Aug 01 Python
Python+OpenCV实现将图像转换为二进制格式
Jan 09 Python
vscode写python时的代码错误提醒和自动格式化的方法
May 07 Python
Python使用tkinter实现小时钟效果
Feb 22 Python
pytorch Dropout过拟合的操作
May 27 Python
Django url 路由匹配过程详解
Jan 22 #Python
浅析pandas随机排列与随机抽样
Jan 22 #Python
python 合并多个excel中同名的sheet
Jan 22 #Python
Python读取pdf表格写入excel的方法
Jan 22 #Python
python 基于UDP协议套接字通信的实现
Jan 22 #Python
详解matplotlib中pyplot和面向对象两种绘图模式之间的关系
Jan 22 #Python
Jmeter调用Python脚本实现参数互相传递的实现
Jan 22 #Python
You might like
提示Trying to clone an uncloneable object of class Imagic的解决
2011/10/27 PHP
php修改数组键名的方法示例
2017/04/15 PHP
Laravel框架中Blade模板的用法示例
2017/08/30 PHP
JS的replace方法详细介绍
2012/11/09 Javascript
jQuery 选择器项目实例分析及实现代码
2012/12/28 Javascript
jquery实现美观的导航菜单鼠标提示特效代码
2015/09/06 Javascript
JavaScript 性能优化小结
2015/10/12 Javascript
Bootstrap风格的WPF样式
2016/12/07 Javascript
详解Vue生命周期的示例
2017/03/10 Javascript
vue.js内部自定义指令与全局自定义指令的实现详解(利用directive)
2017/07/11 Javascript
vue-cli脚手架-bulid下的配置文件
2018/03/27 Javascript
解决layui中table异步数据请求不支持自定义返回数据格式的问题
2018/08/19 Javascript
微信小程序学习笔记之登录API与获取用户信息操作图文详解
2019/03/29 Javascript
vue实现微信分享链接添加动态参数的方法
2019/04/29 Javascript
Vue axios 将传递的json数据转为form data的例子
2019/10/29 Javascript
小程序识别身份证,银行卡,营业执照,驾照的实现
2019/11/05 Javascript
[20:57]Ti4主赛事第三天开幕式
2014/07/21 DOTA
[50:48]LGD vs CHAOS 2019国际邀请赛小组赛 BO2 第二场 8.15
2019/08/16 DOTA
Python wxPython库使用wx.ListBox创建列表框示例
2018/09/03 Python
如何在django里上传csv文件并进行入库处理的方法
2019/01/02 Python
Python mutiprocessing多线程池pool操作示例
2019/01/30 Python
在Python中COM口的调用方法
2019/07/03 Python
Django 设置admin后台表和App(应用)为中文名的操作方法
2020/05/10 Python
Pytorch 中的optimizer使用说明
2021/03/03 Python
美体小铺加拿大官方网站:The Body Shop加拿大
2016/10/30 全球购物
都柏林通行卡/城市通票:The Dublin Pass
2020/02/16 全球购物
Java里面如何创建一个内部类的实例
2015/01/19 面试题
SQL Server数据库笔试题和答案
2016/02/04 面试题
中餐厅经理岗位职责
2014/04/11 职场文书
快餐公司创业计划书
2014/04/29 职场文书
文明社区申报材料
2014/08/21 职场文书
2015年禁毒宣传活动总结
2015/03/25 职场文书
信用卡工资证明范本
2015/06/19 职场文书
《蟋蟀的住宅》教学反思
2016/02/17 职场文书
2016年共产党员公开承诺书
2016/03/24 职场文书
分享很少见很有用的SQL功能CORRESPONDING
2022/08/05 MySQL