项目输出
项目先决条件
要使用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 井字棋游戏的资料请关注其它相关文章!
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
暂无评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
2024年11月23日
2024年11月23日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]