一:程序所需库:
thinker(messagebox,title,Lable,button等)
from tkinter import * #引入tkinter库
import tkinter.messagebox as msg #tkinter是UI界面库,可设置按钮,标签等
二:井字棋设计:
1.界面设计:
root = Tk()
root.title('井字棋') #设置界面名称
# labels
Label(root, text="player1 : x", font=("仿宋",20),fg='red').grid(row=0, column=1) #设置两个选手,player1和player2,两个选手棋子分别为x,t。
Label(root, text="player2 : t", font=("仿宋",20),fg='blue').grid(row=0, column=2) #font设置字体,grid表示位置,如row=0,column=1则表示第0行第1列。
digits = [1, 2, 3, 4, 5, 6, 7, 8, 9]
2.获胜情况:
def win(panels, sign): #能赢的8种情况
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))
3.落子设计(以落在第一个格为例):
if digit == 1 and digit in digits:
digits.remove(digit) #将子落下的位置从digits中删掉
#先x后t
if count % 2 == 0:
mark = 'x'
panels[digit] = mark
elif count % 2 != 0:
mark = 't'
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 == 't'):
msg.showinfo("Result", "Player2 wins")
root.destroy()
第一位玩家player1,棋子为“x”,每落一子系统会进行积分,同样下棋的位子也会减少一次。
三:全部的代码:
from tkinter import * #引入tkinter库
import tkinter.messagebox as msg #tkinter是UI界面库,可设置按钮,标签等
root = Tk()
root.title('井字棋') #设置界面名称
# labels
Label(root, text="player1 : x", font=("仿宋",20),fg='red').grid(row=0, column=1) #设置两个选手,player1和player2,两个选手棋子分别为x,t。
Label(root, text="player2 : t", font=("仿宋",20),fg='blue').grid(row=0, column=2) #font设置字体,grid表示位置,如row=0,column=1则表示第0行第1列。
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): #能赢的8种情况
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): #以下是第一位棋手下第一个棋子的9种情况
global count, mark, digits
# Check which button clicked
if digit == 1 and digit in digits:
digits.remove(digit) #将子落下的位置从digits中删掉
#先x后t
if count % 2 == 0:
mark = 'x'
panels[digit] = mark
elif count % 2 != 0:
mark = 't'
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 == 't'):
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 = 't'
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 == 't'):
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 = 't'
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 == 't'):
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 = 't'
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 == 't'):
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 = 't'
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 == 't'):
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 = 't'
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 == 't'):
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 = 't'
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 == 't'):
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 = 't'
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 == 't'):
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 = 't'
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 == 't'):
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, 't') == False):
msg.showinfo("Result", "Tie")
root.destroy()
####define buttons
button1 = Button(root, width=15, font=('黑体',20),fg='red',height=7, command=lambda: checker(1))
button1.grid(row=1, column=1)
button2 = Button(root, width=15, height=7, font=('黑体',20),fg='red', command=lambda: checker(2))
button2.grid(row=1, column=2)
button3 = Button(root, width=15, height=7, font=('黑体',20),fg='red', command=lambda: checker(3))
button3.grid(row=1, column=3)
button4 = Button(root, width=15, height=7, font=('黑体',20),fg='red', command=lambda: checker(4))
button4.grid(row=2, column=1)
button5 = Button(root, width=15, height=7, font=('黑体',20),fg='red', command=lambda: checker(5))
button5.grid(row=2, column=2)
button6 = Button(root, width=15, height=7, font=('黑体',20),fg='red', command=lambda: checker(6))
button6.grid(row=2, column=3)
button7 = Button(root, width=15, height=7, font=('黑体',20),fg='red', command=lambda: checker(7))
button7.grid(row=3, column=1)
button8 = Button(root, width=15, height=7, font=('黑体',20),fg='red', command=lambda: checker(8))
button8.grid(row=3, column=2)
button9 = Button(root, width=15, height=7, font=('黑体',20),fg='red', command=lambda: checker(9))
button9.grid(row=3, column=3)
root.mainloop()