亚洲免费在线视频-亚洲啊v-久久免费精品视频-国产精品va-看片地址-成人在线视频网

您的位置:首頁技術文章
文章詳情頁

python實現簡單的井字棋游戲(gui界面)

瀏覽:16日期:2022-06-29 13:08:56
項目輸出

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 = 0panels = ['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 msgroot= Tk()root.title(’TIC-TAC-TOE---Project Gurukul’)#labelsLabel(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= Ymark = ’’#counting the no. of clickcount = 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 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 buttonsbutton1=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 編程
相關文章:
主站蜘蛛池模板: 亚欧成人 | 亚洲人成网7777777国产 | 亚洲欧美日韩综合一区久久 | 精品久久久久久久久免费影院 | 男女性生活网站 | 99国产福利视频在线观看 | 亚洲男人的天堂在线 | 玖草在线视频 | 国产精品久久久免费视频 | 亚洲天码中文字幕第一页 | 国产精品短视频免费观看 | 成人福利在线 | 好吊色综合网天天高清 | 在线欧美精品二区三区 | 最刺激黄a大片免费网站 | 国产午夜毛片v一区二区三区 | tom影院亚洲国产日本一区 | 一级特级欧美aaaaa毛片 | 午夜日韩精品 | 看全黄男人和女人视频 | www.操操 | 69成人免费视频 | 能在线观看的一区二区三区 | 亚洲成年人专区 | 亚洲欧美一区二区三区在线 | 成人精品久久 | 国产专区中文字幕 | 最新亚洲国产有精品 | 久久www免费人成_看片高清 | 欧美兽皇video | 亚洲偷自拍另类图片二区 | 免费在线亚洲视频 | 女人张开腿给人桶免费视频 | 久久影视一区 | 理论片日韩 | 久久怡红院国产精品 | 午夜久久视频 | 国产成人精选免费视频 | 一区二区三区精品国产欧美 | 一本色道久久爱88av | 看真人一一级毛片 |