1+ from  tkinter  import  * 
2+ from  tkinter  import  messagebox 
3+ 
4+ root  =  Tk ()
5+ root .title ('Tic Tac Toe Game' )
6+ 
7+ # Player 1 [X] starts first, Player 2 [O] continues 
8+ clicked  =  True 
9+ count  =  0 
10+ 
11+ # To disable all the buttons when someone has won the game 
12+ def  disableButtons ():
13+     button1 .config (state = DISABLED )
14+     button2 .config (state = DISABLED )
15+     button3 .config (state = DISABLED )
16+ 
17+     button4 .config (state = DISABLED )
18+     button5 .config (state = DISABLED )
19+     button6 .config (state = DISABLED )
20+ 
21+     button7 .config (state = DISABLED )
22+     button8 .config (state = DISABLED )
23+     button9 .config (state = DISABLED )
24+ 
25+ # To check whether did anyone won the game and restart the game when someone won the game  
26+ def  checkWinner ():
27+     global  winner 
28+     winner  =  False 
29+ 
30+     # Player 1 [X] winning patterns 
31+     if  button1 ["text" ] ==  "X"  and  button2 ["text" ] ==  "X"  and  button3 ["text" ] ==  "X" :
32+         button1 .config (bg = "#80ffaa" )                                        #[X][X][X] 
33+         button2 .config (bg = "#80ffaa" )                                        #[O][O][ ] 
34+         button3 .config (bg = "#80ffaa" )                                        #[ ][ ][ ] 
35+         winner  =  True 
36+         messagebox .showinfo ("Tic Tac Toe" , "Player 1 is the Winner!" )
37+         disableButtons 
38+         start ()
39+ 
40+     elif  button4 ["text" ] ==  "X"  and  button5 ["text" ] ==  "X"  and  button6 ["text" ] ==  "X" :
41+         button4 .config (bg = "#80ffaa" )                                        #[O][O][ ] 
42+         button5 .config (bg = "#80ffaa" )                                        #[X][X][X] 
43+         button6 .config (bg = "#80ffaa" )                                        #[ ][ ][ ] 
44+         winner  =  True 
45+         messagebox .showinfo ("Tic Tac Toe" , "Player 1 is the Winner!" )
46+         disableButtons 
47+         start ()
48+ 
49+     elif  button7 ["text" ] ==  "X"  and  button8 ["text" ] ==  "X"  and  button9 ["text" ] ==  "X" :
50+         button7 .config (bg = "#80ffaa" )                                        #[ ][ ][ ] 
51+         button8 .config (bg = "#80ffaa" )                                        #[O][O][ ] 
52+         button9 .config (bg = "#80ffaa" )                                        #[X][X][X] 
53+         winner  =  True 
54+         messagebox .showinfo ("Tic Tac Toe" , "Player 1 is the Winner!" )
55+         disableButtons 
56+         start ()
57+ 
58+     elif  button1 ["text" ] ==  "X"  and  button4 ["text" ] ==  "X"  and  button7 ["text" ] ==  "X" :
59+         button1 .config (bg = "#80ffaa" )                                        #[X][O][ ] 
60+         button4 .config (bg = "#80ffaa" )                                        #[X][O][ ] 
61+         button7 .config (bg = "#80ffaa" )                                        #[X][ ][ ] 
62+         winner  =  True 
63+         messagebox .showinfo ("Tic Tac Toe" , "Player 1 is the Winner!" )
64+         disableButtons 
65+         start ()
66+ 
67+     elif  button2 ["text" ] ==  "X"  and  button5 ["text" ] ==  "X"  and  button8 ["text" ] ==  "X" :
68+         button2 .config (bg = "#80ffaa" )                                        #[O][X][ ] 
69+         button5 .config (bg = "#80ffaa" )                                        #[O][X][ ] 
70+         button8 .config (bg = "#80ffaa" )                                        #[ ][X][ ] 
71+         winner  =  True 
72+         messagebox .showinfo ("Tic Tac Toe" , "Player 1 is the Winner!" )
73+         disableButtons 
74+         start ()
75+ 
76+     elif  button3 ["text" ] ==  "X"  and  button6 ["text" ] ==  "X"  and  button9 ["text" ] ==  "X" :
77+         button3 .config (bg = "#80ffaa" )                                        #[ ][O][X] 
78+         button6 .config (bg = "#80ffaa" )                                        #[ ][O][X] 
79+         button9 .config (bg = "#80ffaa" )                                        #[ ][ ][X] 
80+         winner  =  True 
81+         messagebox .showinfo ("Tic Tac Toe" , "Player 1 is the Winner!" )
82+         disableButtons 
83+         start ()
84+ 
85+     elif  button1 ["text" ] ==  "X"  and  button5 ["text" ] ==  "X"  and  button9 ["text" ] ==  "X" :
86+         button1 .config (bg = "#80ffaa" )                                        #[X][O][ ] 
87+         button5 .config (bg = "#80ffaa" )                                        #[ ][X][ ] 
88+         button9 .config (bg = "#80ffaa" )                                        #[ ][O][X] 
89+         winner  =  True 
90+         messagebox .showinfo ("Tic Tac Toe" , "Player 1 is the Winner!" )
91+         disableButtons 
92+         start ()
93+ 
94+     elif  button3 ["text" ] ==  "X"  and  button5 ["text" ] ==  "X"  and  button7 ["text" ] ==  "X" :
95+         button3 .config (bg = "#80ffaa" )                                        #[ ][O][X] 
96+         button5 .config (bg = "#80ffaa" )                                        #[ ][X][ ] 
97+         button7 .config (bg = "#80ffaa" )                                        #[X][O][ ] 
98+         winner  =  True 
99+         messagebox .showinfo ("Tic Tac Toe" , "Player 1 is the Winner!" )
100+         disableButtons 
101+         start ()
102+ 
103+     # Player 2 [O] winning patterns 
104+     elif  button1 ["text" ] ==  "O"  and  button2 ["text" ] ==  "O"  and  button3 ["text" ] ==  "O" :
105+         button1 .config (bg = "#80ffaa" )                                        #[O][O][O] 
106+         button2 .config (bg = "#80ffaa" )                                        #[X][X][ ] 
107+         button3 .config (bg = "#80ffaa" )                                        #[X][ ][ ] 
108+         winner  =  True 
109+         messagebox .showinfo ("Tic Tac Toe" , "Player 2 is the Winner!" )
110+         disableButtons 
111+         start ()
112+ 
113+     elif  button4 ["text" ] ==  "O"  and  button5 ["text" ] ==  "O"  and  button6 ["text" ] ==  "O" :
114+         button4 .config (bg = "#80ffaa" )                                        #[X][X][ ] 
115+         button5 .config (bg = "#80ffaa" )                                        #[O][O][O] 
116+         button6 .config (bg = "#80ffaa" )                                        #[X][ ][ ] 
117+         winner  =  True 
118+         messagebox .showinfo ("Tic Tac Toe" , "Player 2 is the Winner!" )
119+         disableButtons 
120+         start ()
121+ 
122+     elif  button7 ["text" ] ==  "O"  and  button8 ["text" ] ==  "O"  and  button9 ["text" ] ==  "O" :
123+         button7 .config (bg = "#80ffaa" )                                        #[X][ ][ ] 
124+         button8 .config (bg = "#80ffaa" )                                        #[X][X][ ] 
125+         button9 .config (bg = "#80ffaa" )                                        #[O][O][O] 
126+         winner  =  True 
127+         messagebox .showinfo ("Tic Tac Toe" , "Player 2 is the Winner!" )
128+         disableButtons 
129+         start ()
130+ 
131+     elif  button1 ["text" ] ==  "O"  and  button4 ["text" ] ==  "O"  and  button7 ["text" ] ==  "O" :
132+         button1 .config (bg = "#80ffaa" )                                        #[O][X][X] 
133+         button4 .config (bg = "#80ffaa" )                                        #[O][X][ ] 
134+         button7 .config (bg = "#80ffaa" )                                        #[O][ ][ ] 
135+         winner  =  True 
136+         messagebox .showinfo ("Tic Tac Toe" , "Player 2 is the Winner!" )
137+         disableButtons 
138+         start ()
139+ 
140+     elif  button2 ["text" ] ==  "O"  and  button5 ["text" ] ==  "O"  and  button8 ["text" ] ==  "O" :
141+         button2 .config (bg = "#80ffaa" )                                        #[X][O][X] 
142+         button5 .config (bg = "#80ffaa" )                                        #[X][O][ ] 
143+         button8 .config (bg = "#80ffaa" )                                        #[ ][O][ ] 
144+         winner  =  True 
145+         messagebox .showinfo ("Tic Tac Toe" , "Player 2 is the Winner!" )
146+         disableButtons 
147+         start ()
148+ 
149+     elif  button3 ["text" ] ==  "O"  and  button6 ["text" ] ==  "O"  and  button9 ["text" ] ==  "O" :
150+         button3 .config (bg = "#80ffaa" )                                        #[X][X][O] 
151+         button6 .config (bg = "#80ffaa" )                                        #[ ][X][O] 
152+         button9 .config (bg = "#80ffaa" )                                        #[ ][ ][O] 
153+         winner  =  True 
154+         messagebox .showinfo ("Tic Tac Toe" , "Player 2 is the Winner!" )
155+         disableButtons 
156+         start ()
157+ 
158+     elif  button1 ["text" ] ==  "O"  and  button5 ["text" ] ==  "O"  and  button9 ["text" ] ==  "O" :
159+         button1 .config (bg = "#80ffaa" )                                        #[O][X][X] 
160+         button5 .config (bg = "#80ffaa" )                                        #[ ][O][ ] 
161+         button9 .config (bg = "#80ffaa" )                                        #[ ][X][O] 
162+         winner  =  True 
163+         messagebox .showinfo ("Tic Tac Toe" , "Player 2 is the Winner!" )
164+         disableButtons 
165+         start ()
166+ 
167+     elif  button3 ["text" ] ==  "O"  and  button5 ["text" ] ==  "O"  and  button7 ["text" ] ==  "O" :
168+         button3 .config (bg = "#80ffaa" )                                        #[X][X][O] 
169+         button5 .config (bg = "#80ffaa" )                                        #[ ][O][ ] 
170+         button7 .config (bg = "#80ffaa" )                                        #[O][X][ ] 
171+         winner  =  True 
172+         messagebox .showinfo ("Tic Tac Toe" , "Player 2 is the Winner!" )
173+         disableButtons 
174+         start ()
175+ 
176+ # To check whether the game is a draw 
177+ def  checkDraw ():
178+     global  count , winner 
179+ 
180+     if  count  ==  9  and  winner  ==  False :
181+         messagebox .showerror ("Tic Tac Toe" , "Draw, play again!" )
182+         start ()
183+ 
184+ # To determine the buttons that Player 1 or Player 2 has clicked on 
185+ def  buttonClicked (button ):
186+     global  clicked , count 
187+ 
188+     if  button ["text" ] ==  " "  and  clicked  ==  True :
189+         button ["text" ] =  "X" 
190+         clicked  =  False 
191+         count  +=  1 
192+         checkWinner ()
193+         checkDraw ()
194+     elif  button ["text" ] ==  " "  and  clicked  ==  False :
195+         button ["text" ] =  "O" 
196+         clicked  =  True 
197+         count  +=  1 
198+         checkWinner ()
199+         checkDraw ()
200+     else :
201+         messagebox .showerror ("Tic Tac Toe" , "Please select another box." )
202+ 
203+ # To start or restart the game 
204+ def  start ():
205+     global  button1 , button2 , button3 , button4 , button5 , button6 , button7 , button8 , button9 
206+     global  clicked , count 
207+     clicked  =  True 
208+     count  =  0 
209+ 
210+     # Building the buttons for the game 
211+     button1  =  Button (root , text = " " , font = ("Helvetica, 20" ), height = 3 , width = 7 , bg = "SystemButtonFace" , command = lambda : buttonClicked (button1 ))
212+     button2  =  Button (root , text = " " , font = ("Helvetica, 20" ), height = 3 , width = 7 , bg = "SystemButtonFace" , command = lambda : buttonClicked (button2 ))
213+     button3  =  Button (root , text = " " , font = ("Helvetica, 20" ), height = 3 , width = 7 , bg = "SystemButtonFace" , command = lambda : buttonClicked (button3 ))
214+ 
215+     button4  =  Button (root , text = " " , font = ("Helvetica, 20" ), height = 3 , width = 7 , bg = "SystemButtonFace" , command = lambda : buttonClicked (button4 ))
216+     button5  =  Button (root , text = " " , font = ("Helvetica, 20" ), height = 3 , width = 7 , bg = "SystemButtonFace" , command = lambda : buttonClicked (button5 ))
217+     button6  =  Button (root , text = " " , font = ("Helvetica, 20" ), height = 3 , width = 7 , bg = "SystemButtonFace" , command = lambda : buttonClicked (button6 ))
218+ 
219+     button7  =  Button (root , text = " " , font = ("Helvetica, 20" ), height = 3 , width = 7 , bg = "SystemButtonFace" , command = lambda : buttonClicked (button7 ))
220+     button8  =  Button (root , text = " " , font = ("Helvetica, 20" ), height = 3 , width = 7 , bg = "SystemButtonFace" , command = lambda : buttonClicked (button8 ))
221+     button9  =  Button (root , text = " " , font = ("Helvetica, 20" ), height = 3 , width = 7 , bg = "SystemButtonFace" , command = lambda : buttonClicked (button9 ))
222+ 
223+     # Arranging the buttons on the screen for the game 
224+     button1 .grid (row = 0 , column = 0 )
225+     button2 .grid (row = 0 , column = 1 )
226+     button3 .grid (row = 0 , column = 2 )
227+ 
228+     button4 .grid (row = 1 , column = 0 )
229+     button5 .grid (row = 1 , column = 1 )
230+     button6 .grid (row = 1 , column = 2 )
231+ 
232+     button7 .grid (row = 2 , column = 0 )
233+     button8 .grid (row = 2 , column = 1 )
234+     button9 .grid (row = 2 , column = 2 )
235+ 
236+ # Create game menu 
237+ gameMenu  =  Menu (root )
238+ root .config (menu  =  gameMenu )
239+ 
240+ # Create game options menu 
241+ optionMenu  =  Menu (gameMenu , tearoff = False )
242+ gameMenu .add_cascade (label = "Options" , menu = optionMenu )
243+ optionMenu .add_command (label = "Restart Game" , command = start )
244+ 
245+ 
246+ start ()
247+ root .mainloop ()
0 commit comments