-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTic_Tac_Toe_Tkinter.py
More file actions
149 lines (144 loc) · 4.4 KB
/
Tic_Tac_Toe_Tkinter.py
File metadata and controls
149 lines (144 loc) · 4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
from tkinter import *
turn = 1
def clickedbtn1():
global turn
if turn % 2!=0:
btn1['text']='X'
else:
btn1['text']='O'
turn=turn+1
winner()
def clickedbtn2():
global turn
if turn % 2 != 0:
btn2['text'] = 'X'
else:
btn2['text'] = 'O'
turn = turn + 1
winner()
def clickedbtn3():
global turn
if turn % 2 != 0:
btn3['text'] = 'X'
else:
btn3['text'] = 'O'
turn = turn + 1
winner()
def clickedbtn4():
global turn
if turn % 2 != 0:
btn4['text'] = 'X'
else:
btn4['text'] = 'O'
turn = turn + 1
winner()
def clickedbtn5():
global turn
if turn % 2 != 0:
btn5['text'] = 'X'
else:
btn5['text'] = 'O'
turn = turn + 1
winner()
def clickedbtn6():
global turn
if turn % 2 != 0:
btn6['text'] = 'X'
else:
btn6['text'] = 'O'
turn = turn + 1
winner()
def clickedbtn7():
global turn
if turn % 2 != 0:
btn7['text'] = 'X'
else:
btn7['text'] = 'O'
turn = turn + 1
winner()
def clickedbtn8():
global turn
if turn % 2 != 0:
btn8['text'] = 'X'
else:
btn8['text'] = 'O'
turn = turn + 1
winner()
def clickedbtn9():
global turn
if turn % 2 != 0:
btn9['text'] = 'X'
else:
btn9['text'] = 'O'
turn = turn + 1
winner()
window = Tk()
window.title('tic tac toe')
window.geometry("150x150")
entryText=StringVar(window)
btn1 = Button(window, text = " ",command=clickedbtn1,height=1, width=5)
btn1.grid(row = 0, column = 0)
btn2 = Button(window, text = " ",command=clickedbtn2,height=1, width=5)
btn2.grid(row = 0, column = 1)
btn3 = Button(window, text = " ",command=clickedbtn3,height=1, width=5)
btn3.grid(row = 0, column = 2)
btn4 = Button(window, text = " ",command=clickedbtn4,height=1, width=5)
btn4.grid(row = 1, column = 0)
btn5 = Button(window, text = " ",command=clickedbtn5,height=1, width=5)
btn5.grid(row = 1, column = 1)
btn6 = Button(window, text = " ",command=clickedbtn6,height=1, width=5)
btn6.grid(row = 1, column = 2)
btn7 = Button(window, text = " ",command=clickedbtn7,height=1, width=5)
btn7.grid(row = 2, column = 0)
btn8 = Button(window, text = " ",command=clickedbtn8,height=1, width=5)
btn8.grid(row = 2, column = 1)
btn9 = Button(window,text = " ",command=clickedbtn9,height=1, width=5)
btn9.grid(row = 2,column =2)
def winner():
if btn1['text'] == btn2['text'] and btn2['text'] == btn3['text']:
if btn1['text'] == 'X':
entryText.set('Player 1 has won!')
elif btn1['text'] == 'O':
entryText.set('Player 2 has won!')
if btn4['text'] == btn5['text'] and btn5['text'] == btn6['text']:
if btn4['text'] == 'X':
entryText.set('Player 1 has won!')
elif btn4['text'] == 'O':
entryText.set('Player 2 has won!')
if btn7['text'] == btn8['text'] and btn8['text'] == btn9['text']:
if btn7['text'] == 'X':
entryText.set('Player 1 has won!')
elif btn7['text'] == 'O':
entryText.set('Player 2 has won!')
if btn1['text'] == btn4['text'] and btn4['text'] == btn7['text']:
if btn1['text'] == 'X':
entryText.set('Player 1 has won!')
elif btn1['text'] == 'O':
entryText.set('Player 2 has won!')
if btn2['text'] == btn5['text'] and btn5['text'] == btn8['text']:
if btn2['text'] == 'X':
entryText.set('Player 1 has won!')
elif btn2['text'] == 'O':
entryText.set('Player 2 has won!')
if btn3['text'] == btn6['text'] and btn6['text'] == btn9['text']:
if btn3['text'] == 'X':
entryText.set('Player 1 has won!')
elif btn3['text'] == 'O':
entryText.set('Player 2 has won!')
if btn1['text'] == btn5['text'] and btn5['text'] == btn9['text']:
if btn1['text'] == 'X':
entryText.set('Player 1 has won!')
elif btn1['text'] == 'O':
entryText.set('Player 2 has won!')
if btn3['text'] == btn5['text'] and btn5['text'] == btn7['text']:
if btn3['text'] == 'X':
entryText.set('Player 1 has won!')
elif btn3['text'] == 'O':
entryText.set('Player 2 has won!')
if turn>9:
entryText.set('Tie')
lblwinner = Label(window,text = "Result")
lblwinner.grid(row =3, column =0)
txtans = Entry(window,textvariable=entryText)
txtans.grid(row = 3,column = 1)
window.mainloop()