初学者游戏-井字游戏
我在学习Tkinter之后的第一次尝试
from Tkinter import *
import tkFont
class myApp:
"""
Defining The Geometry of the GUI
And the variables Used In the The methods
"""
def __init__(self,parent):
self.parent_instance=parent
self.parent_instance.geometry("650x400")
self.myContainer1=Frame(parent)
self.myContainer1.pack()
#Setting the Dimension constraints for few buttons
button_width = 6
button_padx = "2m"
button_pady = "1m"
buttons_frame_padx = "3m"
buttons_frame_pady = "2m"
buttons_frame_ipadx = "3m"
buttons_frame_ipady = "1m"
# label frame
self.label_frame = Frame(self.myContainer1)
self.label_frame.pack(
side=TOP,
ipadx=buttons_frame_ipadx,
ipady=buttons_frame_ipady,
padx=buttons_frame_padx,
pady=buttons_frame_pady,
)
# middle frame
self.middle_frame = Frame(self.myContainer1)
self.middle_frame.pack(side=TOP,
ipadx=buttons_frame_ipadx,
ipady=buttons_frame_ipady,
padx=buttons_frame_padx,
pady=buttons_frame_pady,
fill=BOTH,
expand=YES,
) ###
# bottom frame
self.bottom_frame = Frame(self.myContainer1, relief=GROOVE,
height=50
) ###
self.bottom_frame.pack(side=TOP,
ipadx=buttons_frame_ipadx,
ipady=buttons_frame_ipady,
padx=buttons_frame_padx,
pady=buttons_frame_pady,
fill=BOTH,
expand=YES,
) ###
# left_frame
self.left_frame = Frame(self.middle_frame,
borderwidth=10, relief=RIDGE,
height=250,
width=250,
) ###
self.left_frame.grid(sticky=W+E+S+N)
self.left_frame.pack(side=LEFT,
ipadx=buttons_frame_ipadx,
ipady=buttons_frame_ipady,
padx=buttons_frame_padx,
pady=buttons_frame_pady,
fill=BOTH,
expand=YES,
) ###
### right_frame
self.right_frame = Frame(self.middle_frame,
#borderwidth=5, relief=RIDGE,
width=50,
)
self.right_frame.pack(side=RIGHT,
ipadx=buttons_frame_ipadx,
ipady=buttons_frame_ipady,
padx=buttons_frame_padx,
pady=buttons_frame_pady,
fill=BOTH,
expand=YES,
) ###
### Defining Working Variables
self.plstate=1 ### State of the player in COMPUTER mode
self.statevalue=0 ### State of the player in player mode
self.winvar=0 ### Determine if a player has won
self.dvar=0 ### Detrmine if the match is a draw
self.svar=0### Determine if the result is declared
self.clist=[0,0,0,0,0,0,0,0,0] ### Operating List
self.gstart=0 ### to start the game
self.nstart = 1 ### to disallow only start
self.implist=[]
self.visualist=[]
self.logicalgroups=[[0,4,8],[2,4,6],[0,1,2],[3,4,5],[6,7,8],[0,3,6],[1,4,7],[2,5,8]] ### Possible combinational List hardcoded...:-(
self.helv20 = tkFont.Font ( family="Helvetica",size=20, weight="bold" ) ### Font defined to display
def action_button(self):
"""
Grid Layout for the buttons in action
"""
self.button0 = Button( self.left_frame,width=2,height=2)
self.button0.grid( row = 0, column = 0, sticky = W+E+N+S )
self.button0.bind("<Button-1>",
lambda
event,arg1=0,arg2='M':
self.buttonclick(event,arg1,arg2))
self.button1 = Button( self.left_frame,width=2,height=2)
self.button1.grid( row = 0, column = 1, sticky = W+E+N+S )
self.button1.bind("<Button-1>",
lambda
event,arg1=1,arg2='M':
self.buttonclick(event,arg1,arg2))
self.button2 = Button(self.left_frame,width=2,height=2)
self.button2.grid( row = 0, column = 2, sticky = W+E+N+S )
self.button2.bind("<Button-1>",
lambda
event,arg1=2,arg2='M':
self.buttonclick(event,arg1,arg2))
self.button3 = Button(self.left_frame,width=2,height=2)
self.button3.grid( row = 1, column = 0, sticky = W+E+N+S )
self.button3.bind("<Button-1>",
lambda
event,arg1=3,arg2='M':
self.buttonclick(event,arg1,arg2))
self.button4 = Button( self.left_frame,width=2,height=2)
self.button4.grid( row = 1, column = 1, sticky = W+E+N+S )
self.button4.bind("<Button-1>",
lambda
event,arg1=4,arg2='M':
self.buttonclick(event,arg1,arg2))
self.button5 = Button( self.left_frame,width=2,height=2)
self.button5.grid( row = 1, column = 2, sticky = W+E+N+S )
self.button5.bind("<Button-1>",
lambda
event,arg1=5,arg2='M':
self.buttonclick(event,arg1,arg2))
self.button6 = Button( self.left_frame,width=2,height=2)
self.button6.grid( row = 2, column = 0, sticky = W+E+N+S )
self.button6.bind("<Button-1>",
lambda
event,arg1=6,arg2='M':
self.buttonclick(event,arg1,arg2))
self.button7 = Button( self.left_frame,width=2,height=2)
self.button7.grid( row = 2, column = 1, sticky = W+E+N+S )
self.button7.bind("<Button-1>",
lambda
event,arg1=7,arg2='M':
self.buttonclick(event,arg1,arg2))
self.button8 = Button( self.left_frame,width=2,height=2)
self.button8.grid( row = 2, column = 2, sticky = W+E+N+S )
self.button8.bind("<Button-1>",
lambda
event,arg1=8,arg2='M':
self.buttonclick(event,arg1,arg2))
self.stare(1)
def stare(self,val):
for id in range(9):
if val == 1 :
refresh="self.button%d.configure(text='!',background='grey',state=ACTIVE)"%id
exec(refresh)
else:
refresh="self.button%d.configure(text='',background='white',state=ACTIVE)"%id
exec(refresh)
def menu_button(self):
"""
Buttons for the control actions
"""
###Defining The New Button
self.newbutton=Button(self.right_frame)
self.newbutton.configure(background='tan',text='NEW GAME',width=10)
self.newbutton.grid(row=0,column=0)
self.newbutton.bind("<Button-1>",self.newbuttonclick)
###Defining The Close Button
self.closebutton=Button(self.right_frame)
self.closebutton.configure(background='tan',text='CLOSE GAME',width=10)
self.closebutton.grid(row=0,column=1)
self.closebutton.bind("<Button-1>",self.closebuttonclick)
###Defining The start Button
self.startbutton=Button(self.right_frame)
self.startbutton.configure(background='tan',text='START',width=10)
self.startbutton.grid(row=10,column=0)
self.startbutton.bind("<Button-1>",self.startbuttonclick)
###Defining The Status Button
self.statusbutton1=Button(self.right_frame,width=10,relief=GROOVE)
self.statusbutton1.configure(background='blue',text='PLAYER ONE TURN',width=15)
self.statusbutton1.grid(row=10,column=1)
self.cplabel=Label(self.right_frame,text="PLAYER VS",fg='blue')
self.cplabel.grid(row=1,column=0)
self.mlabel=Label(self.right_frame,text="TO-START",fg='blue')
self.mlabel.grid(row=4,column=1)
self.gslabel=Label(self.right_frame,text="MODES",fg='blue')
self.gslabel.grid(row=1,column=2)
###Defining The Radio Button
self.rvar=IntVar()
self.avar=IntVar() #To start
self.mvar=IntVar()
self.RB1=Radiobutton(self.right_frame,text='PLAYER',value=0,variable=self.rvar)
self.RB1.grid(row=2,column=0)
self.RB2=Radiobutton(self.right_frame,text='COMPUTER',value=1,variable=self.rvar)
self.RB2.grid(row=3,column=0)
self.RB3=Radiobutton(self.right_frame,text='PLAYER',value=0,variable=self.avar)
self.RB3.grid(row=5,column=1)
self.RB4=Radiobutton(self.right_frame,text='COMPUTER',value=1,variable=self.avar)
self.RB4.grid(row=6,column=1)
self.RB5=Radiobutton(self.right_frame,text='EASY',value=0,variable=self.mvar)
self.RB5.grid(row=2,column=2)
self.RB6=Radiobutton(self.right_frame,text='NORMAL',value=1,variable=self.mvar)
self.RB6.grid(row=3,column=2)
self.RB7=Radiobutton(self.right_frame,text='HARD',value=2,variable=self.mvar)
self.RB7.grid(row=4,column=2)
self.rvar.set(0)
self.avar.set(0)
self.mvar.set(0)
def label(self):
"""
Display Constraints of various labels
"""
g_label=Label(self.label_frame,text="Tic Tac Toe",font=self.helv20,fg='blue')
g_label.pack(side=TOP,anchor=CENTER)
s_label=Label(self.bottom_frame,text='COURTESY: QUINTEGRA SOLUTIONS',fg='grey')
s_label.pack(anchor=CENTER)
def newbuttonclick(self,event):
"""
Event That happens if I click
The New Game button
"""
print 'NEW GAME'
for i in range(9):
refresh="self.button%d.configure(text='',background='white',state=ACTIVE)"%i
exec(refresh)
### Initialising all variables
self.statusbutton1.configure(background='blue',text='PLAYER ONE TURN',width=15)
self.clist=[0,0,0,0,0,0,0,0,0]
self.nstart = 1
toplay = self.avar.get()
if toplay == 1:
self.plstate=1
else:
self.plstate=2
self.statevalue=0
self.dvar=0
self.svar=0
self.winvar=0
self.gstart=0
self.implist=[]
self.RB1.config(state=ACTIVE)
self.RB2.config(state=ACTIVE)
self.RB3.config(state=ACTIVE)
self.RB4.config(state=ACTIVE)
self.RB5.config(state=ACTIVE)
self.RB6.config(state=ACTIVE)
self.RB7.config(state=ACTIVE)
self.stare(1)
###
def closebuttonclick(self,event):
"""
Event That happens if I click
The close button
"""
print 'CLOSE GAME'
self.parent_instance.destroy()
def startbuttonclick(self,event):
if self.nstart == 1 :
self.gstart = 1
self.stare(0)
if self.rvar.get() == 1:
self.RB1.config(state=DISABLED)
else:
self.RB2.config(state=DISABLED)
self.RB3.config(state=DISABLED)
self.RB4.config(state=DISABLED)
self.RB5.config(state=DISABLED)
self.RB6.config(state=DISABLED)
self.RB7.config(state=DISABLED)
if self.avar.get() == 1:
self.plstate=2 ### State of the player in COMPUTER mode
self.playmygame()
self.RB3.config(state=DISABLED)
else:
self.plstate=1
self.RB4.config(state=DISABLED)
if self.mvar.get() == 0 :
self.RB6.config(state=DISABLED)
self.RB7.config(state=DISABLED)
elif self.mvar.get() == 1 :
self.RB5.config(state=DISABLED)
self.RB7.config(state=DISABLED)
else:
self.RB5.config(state=DISABLED)
self.RB6.config(state=DISABLED)
self.nstart = 0
def buttonclick(self,event,arg1,arg2):
"""
For Every Action There is an
Equal and opposite reaction
Defining Button Click Events In player mode and COMPUTER mode
"""
if self.gstart == 1 :
if self.rvar.get() == 0:
if self.statevalue==0:
if self.clist[arg1] == 0:
if self.clist[arg1] != 3:
mse_cmd="self.button%d.configure(text='O',background='blue')"%arg1
exec(mse_cmd)
self.statusbutton1.configure(text='PLAYER TWO TURN',background='red')
self.clist[arg1]=1
self.statevalue=1
self.checkwin()
else :
if self.clist[arg1] == 0 :
if self.clist[arg1] != 3:
mse_cmd="self.button%d.configure(text='X',background='red')"%arg1
exec(mse_cmd)
self.statusbutton1.configure(text='PLAYER ONE TURN',background='blue',width=15)
self.clist[arg1]=2
self.statevalue=0
self.checkwin()
else:
if self.plstate == 1:
if self.clist[arg1] == 0:
mse_cmd="self.button%d.configure(text='O',background='blue')"%arg1
exec(mse_cmd)
self.clist[arg1]=1
### COMPUTER 's Turn
self.checkwin()
if self.dvar != 1 :
self.plstate=2
self.statusbutton1.configure(text='COMPUTER TURN',background='red')
if self.plstate == 2 :
self.win()
self.caution()
if self.mvar.get() == 2 :
self.playmygame()
elif self.mvar.get() == 1:
self.normal()
else :
self.easy()
self.checkwin()
###Check for winning possiblity for both the players
###And then implement it
def win(self):
"""
Check the winning Possiblities in COMPUTER mode
"""
for subgroup in self.logicalgroups:
if self.plstate==2:
if self.clist[subgroup[0]] == 2 and self.clist[subgroup[1]] == 2:
if self.clist[subgroup[2]] == 0:
pce_cmd="self.button%d.configure(text='X',background='red')"%subgroup[2]
exec(pce_cmd)
self.clist[subgroup[2]]=2
self.plstate=1
if self.plstate==2:
if self.clist[subgroup[0]] == 2 and self.clist[subgroup[2]] == 2:
if self.clist[subgroup[1]] == 0:
pce_cmd="self.button%d.configure(text='X',background='red')"%subgroup[1]
exec(pce_cmd)
self.clist[subgroup[1]]=2
self.plstate=1
if self.plstate==2:
if self.clist[subgroup[1]] == 2 and self.clist[subgroup[2]] == 2:
if self.clist[subgroup[0]]==0:
pce_cmd="self.button%d.configure(text='X',background='red')"%subgroup[0]
exec(pce_cmd)
self.clist[subgroup[0]]=2
self.plstate=1
self.statusbutton1.configure(text='PLAYER ONE TURN',background='blue')
def caution(self):
"""
Check failures the in COMPUTER mode
"""
if self.mvar.get() == 0 :
pass
else :
for subgroup in self.logicalgroups:
if self.plstate==2:
if self.clist[subgroup[0]] == 1 and self.clist[subgroup[1]] == 1:
if self.clist[subgroup[2]] == 0:
pce_cmd="self.button%d.configure(text='X',background='red')"%subgroup[2]
exec(pce_cmd)
self.clist[subgroup[2]]=2
self.plstate=1
if self.plstate==2:
if self.clist[subgroup[0]] == 1 and self.clist[subgroup[2]] == 1:
if self.clist[subgroup[1]]==0:
pce_cmd="self.button%d.configure(text='X',background='red')"%subgroup[1]
exec(pce_cmd)
self.clist[subgroup[1]]=2
self.plstate=1
if self.plstate==2:
if self.clist[subgroup[1]] == 1 and self.clist[subgroup[2]] == 1:
if self.clist[subgroup[0]]==0:
pce_cmd="self.button%d.configure(text='X',background='red')"%subgroup[0]
exec(pce_cmd)
self.clist[subgroup[0]]=2
self.plstate=1
self.statusbutton1.configure(text='PLAYER ONE TURN',background='blue')
##########################################
def playmygame(self):
for index,implements in enumerate(self.clist):
if implements == 2:
self.implist.append(index)
if self.clist[4] == 0 :
if self.plstate == 2 :
self.button4.configure(text='X',background='red')
self.clist[4] =2
self.plstate=1
self.proximity()
def proximity(self):
store=[]
proxlist=[]
for index,val in enumerate(self.clist):
if val == 1 :
store.append(index)
for sub in self.logicalgroups:
for index,enter in enumerate(store):
if enter in sub :
if sub[0] == enter :
proxlist.append(sub[1])
elif sub[1] == enter :
proxlist.append(sub[0])
proxlist.append(sub[2])
elif sub[2] == enter :
proxlist.append(sub[1])
for proxies in proxlist:
if self.plstate == 2 :
if self.clist[proxies] == 0 :
self.clist[proxies] = 2
pr_cmd="self.button%d.configure(text='X',background='red')"%proxies
exec(pr_cmd)
self.plstate = 1
##########################################
def normal(self):
for index,implements in enumerate(self.clist):
if implements == 2:
self.implist.append(index)
self.reason()
for index,j in enumerate(self.reasoninglist):
if j == 0 :
self.visualise(self.logicalgroups[index])
for index1,sgp in enumerate(self.visualist):
if sgp == 0:
if self.plstate == 2 :
temp=self.logicalgroups[index]
pce_cmd="self.button%d.configure(text='X',background='red')"%temp[index1]
exec(pce_cmd)
self.clist[temp[index1]]=2
self.plstate=1
if self.plstate == 2 :
for index2,nonval in enumerate(self.clist):
if nonval == 0 :
if self.plstate == 2 :
nce_cmd="self.button%d.configure(text='X',background='red')"%index2
exec(nce_cmd)
self.clist[index2]=2
self.plstate=1
def visualise(self,Slist):
self.visualist=[]
for i in Slist:
self.visualist.append(self.clist[i])
def reason(self):
self.reasoninglist=[]
for group in self.logicalgroups:
self.visualise(group)
if 1 in self.visualist:
self.reasoninglist.append('R')
else :
self.reasoninglist.append(0)
##########################################
def easy(self):
for index,unmarked in enumerate(self.logicalgroups):
if self.clist[unmarked[0]] == 0:
if self.plstate == 2 :
cmd="self.button%d.configure(text='X',background='red')"%unmarked[0]
exec(cmd)
self.clist[unmarked[0]]=2
self.plstate=1
if self.clist[unmarked[1]] == 0:
if self.plstate == 2 :
cmd="self.button%d.configure(text='X',background='red')"%unmarked[1]
exec(cmd)
self.clist[unmarked[1]]=2
self.plstate=1
if self.clist[unmarked[2]] == 0:
if self.plstate == 2 :
cmd="self.button%d.configure(text='X',background='red')"%unmarked[2]
exec(cmd)
self.clist[unmarked[2]]=2
self.plstate=1
##########################################
def statewin(self,val):
"""
Terminal Output of Who wins
"""
if self.svar == 0:
if val == 1 :
print 'PLAYER ONE WINS'
elif self.rvar.get() == 1:
print 'COMPUTER WINS'
else:
print 'PLAYER TWO WINS'
self.svar=1
def checkwin(self):
"""
Algorithm to check the winning Possiblities
"""
for subgroup in self.logicalgroups:
if self.clist[subgroup[0]] == 1:
if self.clist[subgroup[1]] == 1:
if self.clist[subgroup[2]] == 1:
if self.svar == 0:
self.winvar = 1
self.statewin(1)
self.fil(self.winvar)
if self.clist[subgroup[0]] == 2:
if self.clist[subgroup[1]] == 2:
if self.clist[subgroup[2]] == 2:
if self.svar ==0 :
self.winvar = 2
self.statewin(2)
self.fil(self.winvar)
self.draw()
def fil(self,IDN):
"""
To avoid Playing After a person Wins the game
"""
for index,ID in enumerate(self.clist):
if ID == 0 :
cbs="self.button%d.configure(text='--',background='white',state=DISABLED)"%index
exec(cbs)
self.clist[index] = 3
if IDN==1:
Player='PLAYER ONE'
elif self.rvar.get() == 1:
Player='COMPUTER'
else:
Player='PLAYER TWO'
###Display The Winner In a seperate Window
msg_box=Toplevel()
msg_box.title("Winner")
msg_box.geometry("350x50")
wd="Display=Label(msg_box,text='%s WINS',font=self.helv20)"%(Player)
exec(wd)
Display.pack()
def draw(self):
"""
To Declare a Draw
"""
if not 0 in self.clist:
if not 3 in self.clist:
if self.winvar == 0:
print 'MATCH DRAW'
msg_box=Toplevel()
msg_box.title("MATCH RESULT")
msg_box.geometry("300x50")
Display=Label(msg_box,text='MATCH DRAWN',font=self.helv20)
Display.pack()
self.dvar=1 ### Setting the draw flag
root = Tk()
root.title("TIC TAC TOE")
myapp = myApp(root)
myapp.action_button()
myapp.menu_button()
myapp.label()
root.mainloop()
From: https://bytes.com/topic/python/insights/790594-python-tkinter