-
Notifications
You must be signed in to change notification settings - Fork 1
/
V1.1.py
301 lines (250 loc) · 11.4 KB
/
V1.1.py
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# -*- coding: utf-8 -*-
"""
Created on Mon Nov 25 11:45:23 2019
@author: Kushagra Indurkhya
"""
from tkinter import *
import tkinter.messagebox
import tkinter.ttk as ttk
from tkcolorpicker import askcolor
tk = Tk()
tk.configure(bg="firebrick3")
tk.title("Tic Tac Toe")
p = StringVar()
buttonc=Button(tk, text='Choose board', font='Times 10 bold', bg='yellow', fg='black', width=12, command=lambda:color())
buttonc.grid(row=2,column=0)
buttonc=Button(tk, text='Choose Text', font='Times 10 bold', bg='yellow', fg='black', width=12, command=lambda:colora())
buttonc.grid(row=2,column=1)
buttonc=Button(tk, text='Choose Tile', font='Times 10 bold', bg='yellow', fg='black', width=12, command=lambda:colort())
buttonc.grid(row=2,column=2)
player_name = Entry(tk, textvariable=p, bd=7,width=35,bg="white")
player_name.grid(row=1, column=1, columnspan=8)
def color():
colorb=askcolor((255, 255, 0),tk)[1]
tk.configure(bg=colorb)
label.configure(bg=colorb)
def colort():
colorb=askcolor((255, 255, 0),tk)[1]
button1.configure(bg=colorb)
button2.configure(bg=colorb)
button3.configure(bg=colorb)
button4.configure(bg=colorb)
button5.configure(bg=colorb)
button6.configure(bg=colorb)
button7.configure(bg=colorb)
button8.configure(bg=colorb)
button9.configure(bg=colorb)
def colora():
colorb=askcolor((255, 255, 0),tk)[1]
button1.configure(fg=colorb)
button2.configure(fg=colorb)
button3.configure(fg=colorb)
button4.configure(fg=colorb)
button5.configure(fg=colorb)
button6.configure(fg=colorb)
button7.configure(fg=colorb)
button8.configure(fg=colorb)
button9.configure(fg=colorb)
#checking for empty tiles
def moves_left(board):
for i in range(3):
for j in range(3):
if board[i][j] == ' ':
return True
return False
WIN_SCORE = 10
LOOSE_SCORE = -10
#checking if maximizer(computer) or minimizer(player) == winning or draw
def evaluate(board):
if board[0][0] == board[0][1] and board[0][1] == board[0][2] and board[0][0] == 'O': return LOOSE_SCORE
if board[1][0] == board[1][1] and board[1][1] == board[1][2] and board[1][0] == 'O': return LOOSE_SCORE
if board[2][0] == board[2][1] and board[2][1] == board[2][2] and board[2][0] == 'O': return LOOSE_SCORE
if board[0][0] == board[1][0] and board[1][0] == board[2][0] and board[0][0] == 'O': return LOOSE_SCORE
if board[0][1] == board[1][1] and board[1][1] == board[2][1] and board[0][1] == 'O': return LOOSE_SCORE
if board[0][2] == board[1][2] and board[1][2] == board[2][2] and board[0][2] == 'O': return LOOSE_SCORE
if board[0][0] == board[1][1] and board[1][1] == board[2][2] and board[0][0] == 'O': return LOOSE_SCORE
if board[2][0] == board[1][1] and board[1][1] == board[0][2] and board[2][0] == 'O': return LOOSE_SCORE
if board[0][0] == board[0][1] and board[0][1] == board[0][2] and board[0][0] == 'X': return WIN_SCORE
if board[1][0] == board[1][1] and board[1][1] == board[1][2] and board[1][0] == 'X': return WIN_SCORE
if board[2][0] == board[2][1] and board[2][1] == board[2][2] and board[2][0] == 'X': return WIN_SCORE
if board[0][0] == board[1][0] and board[1][0] == board[2][0] and board[0][0] == 'X': return WIN_SCORE
if board[0][1] == board[1][1] and board[1][1] == board[2][1] and board[0][1] == 'X': return WIN_SCORE
if board[0][2] == board[1][2] and board[1][2] == board[2][2] and board[0][2] == 'X': return WIN_SCORE
if board[0][0] == board[1][1] and board[1][1] == board[2][2] and board[0][0] == 'X': return WIN_SCORE
if board[2][0] == board[1][1] and board[1][1] == board[0][2] and board[2][0] == 'X': return WIN_SCORE
return 0
#minimax algorithm imp is bool variable to know if its maximiser's turn or not
def minimax(board , imp,alpha,beta):
score=evaluate(board)
if score == 10:
return score
if score == -10:
return score
# function was not called
if moves_left(board) == False:
return 0
if imp == True:
best=-1000
for i in range(3):
for j in range(3):
if board[i][j]==" ":
board[i][j]="X"
val=minimax(board,not imp,alpha,beta)
best=max(best,val)
alpha=max(best,alpha)
board[i][j]=" "
if alpha>=beta:
return best
else:
best= 1000
for i in range(3):
for j in range(3):
if board[i][j]==" ":
board[i][j]="O"
val=minimax(board,not imp,alpha,beta)
best=min(best,val)
beta=min(best,beta)
board[i][j]=" "
if alpha>=beta:
return best
return best
#finding best move for a given board position
def best_move(board):
bestval = 1000
moveval = None
row=-1
col=-1
for i in range(3):
for j in range(3):
if board[i][j]==' ':
board[i][j]='O'
moveval=minimax(board,True,-1000,1000)
board[i][j]=' '
if moveval < bestval:
row=i
col=j
bestval=moveval
return((3*row)+(col+1))
def disableButton():
button1.configure(state=DISABLED)
button2.configure(state=DISABLED)
button3.configure(state=DISABLED)
button4.configure(state=DISABLED)
button5.configure(state=DISABLED)
button6.configure(state=DISABLED)
button7.configure(state=DISABLED)
button8.configure(state=DISABLED)
button9.configure(state=DISABLED)
def btnClick(buttons):
global bclick,player_name, player,p,board,undo_p,undo_c
if buttons["text"] == " ":
buttons["text"] = "X"
undo_p=buttons
checkForWin()
board=[[button1['text'],button2['text'],button3['text']],[button4['text'],button5['text'],button6['text']],[button7['text'],button8['text'],button9['text']]]
mov= best_move(board)
if mov == 1:
button1["text"]="O"
undo_c=button1
if mov == 2:
button2["text"]="O"
undo_c=button2
if mov == 3:
button3["text"]="O"
undo_c=button3
if mov == 4:
button4["text"]="O"
undo_c=button4
if mov == 5:
button5["text"]="O"
undo_c=button5
if mov == 6:
button6["text"]="O"
undo_c=button6
if mov == 7:
button7["text"]="O"
undo_c=button7
if mov == 8:
button8["text"]="O"
undo_c=button8
if mov == 9:
button9["text"]="O"
undo_c=button9
checkForWin()
else:
tkinter.messagebox.showinfo("Tic-Tac-Toe", "Button already Clicked!")
def checkForWin():
if (button1['text'] == 'O' and button2['text'] == 'O' and button3['text'] == 'O' or
button4['text'] == 'O' and button5['text'] == 'O' and button6['text'] == 'O' or
button7['text'] == 'O' and button8['text'] == 'O' and button9['text'] == 'O' or
button1['text'] == 'O' and button5['text'] == 'O' and button9['text'] == 'O' or
button3['text'] == 'O' and button5['text'] == 'O' and button7['text'] == 'O' or
button1['text'] == 'O' and button4['text'] == 'O' and button7['text'] == 'O' or
button2['text'] == 'O' and button5['text'] == 'O' and button8['text'] == 'O' or
button3['text'] == 'O' and button6['text'] == 'O' and button9['text'] == 'O'):
disableButton()
tkinter.messagebox.showinfo(player_name.get()+" VS Computer", "Computer Wins")
elif(button1['text'] == 'X' and button2['text'] == 'X' and button3['text'] == 'X' or
button4['text'] == 'X' and button5['text'] == 'X' and button6['text'] == 'X' or
button7['text'] == 'X' and button8['text'] == 'X' and button9['text'] == 'X' or
button1['text'] == 'X' and button5['text'] == 'X' and button9['text'] == 'X' or
button3['text'] == 'X' and button5['text'] == 'X' and button7['text'] == 'X' or
button1['text'] == 'X' and button4['text'] == 'X' and button7['text'] == 'X' or
button2['text'] == 'X' and button5['text'] == 'X' and button8['text'] == 'X' or
button3['text'] == 'X' and button6['text'] == 'X' and button9['text'] == 'X'):
disableButton()
tkinter.messagebox.showinfo(player_name.get()+" VS Computer", p.get()+" Wins")
elif(moves_left([[button1['text'],button2['text'],button3['text']],[button4['text'],button5['text'],button6['text']],[button7['text'],button8['text'],button9['text']]])==False):
disableButton()
tkinter.messagebox.showinfo(player_name.get()+" VS Computer", "It's a draw")
def reset(buttons,buttonc):
buttons['text'] = ' '
buttonc['text'] = ' '
def reset_All():
button1['text'] = ' '
button2['text'] = ' '
button3['text'] = ' '
button4['text'] = ' '
button5['text'] = ' '
button6['text'] = ' '
button7['text'] = ' '
button8['text'] = ' '
button9['text'] = ' '
button1.configure(state=NORMAL)
button2.configure(state=NORMAL)
button3.configure(state=NORMAL)
button4.configure(state=NORMAL)
button5.configure(state=NORMAL)
button6.configure(state=NORMAL)
button7.configure(state=NORMAL)
button8.configure(state=NORMAL)
button9.configure(state=NORMAL)
buttons = StringVar()
buttonc = StringVar()
label = Label( tk, text="Name:", font='Times 20 bold', bg='firebrick3', fg='black', height=1, width=8)
label.grid(row=1, column=0)
button1 = Button(tk, text=" ", font='Times 20 bold', bg='NavajoWhite3', fg='black', height=4, width=8, command=lambda: btnClick(button1))
button1.grid(row=3, column=0)
button2 = Button(tk, text=' ', font='Times 20 bold', bg='NavajoWhite3', fg='black', height=4, width=8, command=lambda: btnClick(button2))
button2.grid(row=3, column=1)
button3 = Button(tk, text=' ',font='Times 20 bold', bg='NavajoWhite3', fg='black', height=4, width=8, command=lambda: btnClick(button3))
button3.grid(row=3, column=2)
button4 = Button(tk, text=' ', font='Times 20 bold', bg='NavajoWhite3', fg='black', height=4, width=8, command=lambda: btnClick(button4))
button4.grid(row=4, column=0)
button5 = Button(tk, text=' ', font='Times 20 bold', bg='NavajoWhite3', fg='black', height=4, width=8, command=lambda: btnClick(button5))
button5.grid(row=4, column=1)
button6 = Button(tk, text=' ', font='Times 20 bold', bg='NavajoWhite3', fg='black', height=4, width=8, command=lambda: btnClick(button6))
button6.grid(row=4, column=2)
button7 = Button(tk, text=' ', font='Times 20 bold', bg='NavajoWhite3', fg='black', height=4, width=8, command=lambda: btnClick(button7))
button7.grid(row=5, column=0)
button8 = Button(tk, text=' ', font='Times 20 bold', bg='NavajoWhite3', fg='black', height=4, width=8, command=lambda: btnClick(button8))
button8.grid(row=5, column=1)
button9 = Button(tk, text=' ', font='Times 20 bold', bg='NavajoWhite3', fg='black', height=4, width=8, command=lambda: btnClick(button9))
button9.grid(row=5, column=2)
button10=Button(tk, text="QUIT", bg="grey60",fg="black",command=tk.destroy,height=2,width=12)
button10.grid(row=6,column=0)
button12=Button(tk, text="UNDO", bg="grey60",fg="black",command=lambda:reset(undo_p,undo_c),height=2,width=12)
button12.grid(row=6,column=1)
button11=Button(tk, text="RESET", bg="grey60",fg="black",command=lambda:reset_All(),height=2,width=12)
button11.grid(row=6,column=2)
tk.mainloop()