-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
260 lines (187 loc) · 7.72 KB
/
main.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
from tkinter import *
root = Tk()
root.geometry("330x550")
root.title("Tic Tac Toe")
root.resizable(0,0)
frame1 = Frame(root)
frame1.pack()
titleLabel = Label(frame1 , text="Tic Tac Toe" , font=("Arial" , 26) , bg="orange" , width=16 )
titleLabel.grid(row=0 , column=0)
optionFrame = Frame(root , bg="grey")
optionFrame.pack()
frame2 = Frame(root , bg="yellow")
frame2.pack()
board = { 1:" " , 2:" " , 3:" ",
4:" " , 5:" " , 6:" ",
7:" " , 8:" " , 9:" " }
turn = "x"
game_end = False
mode = "singlePlayer"
def changeModeToSinglePlayer():
global mode
mode = "singlePlayer"
singlePlayerButton["bg"] = "lightgreen"
multiPlayerButton["bg"] = "lightgrey"
def changeModeToMultiplayer():
global mode
mode = "multiPlayer"
multiPlayerButton["bg"] = "lightgreen"
singlePlayerButton["bg"] = "lightgrey"
def updateBoard():
for key in board.keys():
buttons[key-1]["text"] = board[key]
def checkForWin(player):
# rows
if board[1] == board[2] and board[2] == board[3] and board[3] == player:
return True
elif board[4] == board[5] and board[5] == board[6] and board[6] == player:
return True
elif board[7] == board[8] and board[8] == board[9] and board[9] == player:
return True
# columns
elif board[1] == board[4] and board[4] == board[7] and board[7] == player:
return True
elif board[2] == board[5] and board[5] == board[8] and board[8] == player:
return True
elif board[3] == board[6] and board[6] == board[9] and board[9] == player:
return True
# diagonals
elif board[1] == board[5] and board[5] == board[9] and board[9] == player:
return True
elif board[3] == board[5] and board[5] == board[7] and board[7] == player:
return True
return False
def checkForDraw():
for i in board.keys():
if board[i] == " ":
return False
return True
def restartGame():
global game_end
game_end = False
for button in buttons:
button["text"] = " "
for i in board.keys():
board[i] = " "
titleLabel = Label(frame1 , text="Tic Tac Toe" , font=("Arial" , 30) , bg="orange" , width=15 )
titleLabel.grid(row=0 , column=0)
def minimax(board , isMaximizing):
if checkForWin("o"):
return 1
if checkForWin("x"):
return -1
if checkForDraw():
return 0
if isMaximizing:
bestScore = -100
for key in board.keys():
if board[key] == " ":
board[key] = "o"
score = minimax(board , False) # minimax
board[key] = " "
if score > bestScore :
bestScore = score
return bestScore
else:
bestScore = 100
for key in board.keys():
if board[key] == " ":
board[key] = "x"
score = minimax(board , True) # minimax
board[key] = " "
if score < bestScore :
bestScore = score
return bestScore
def playComputer():
bestScore = -100
bestMove = 0
for key in board.keys():
if board[key] == " ":
board[key] = "o"
score = minimax(board , False) # minimax
board[key] = " "
if score > bestScore :
bestScore = score
bestMove = key
board[bestMove] = "o"
# Function to play
def play(event):
global turn,game_end
if game_end:
return
button = event.widget
buttonText = str(button)
clicked = buttonText[-1]
if clicked == "n" :
clicked = 1
else :
clicked = int(clicked)
if button["text"] == " ":
if turn == "x" :
board[clicked] = turn
if checkForWin(turn):
winningLabel = Label(frame1 , text=f"{turn} wins the game", bg="orange", font=("Arial" , 26),width=16 )
winningLabel.grid(row = 0 , column=0 , columnspan=3)
game_end = True
turn = "o"
updateBoard()
if mode == "singlePlayer":
playComputer()
if checkForWin(turn):
winningLabel = Label(frame1 , text=f"{turn} wins the game", bg="orange", font=("Arial" , 26),width=16 )
winningLabel.grid(row = 0 , column=0 , columnspan=3)
game_end = True
turn = "x"
updateBoard()
else:
board[clicked] = turn
updateBoard()
if checkForWin(turn):
winningLabel = Label(frame1 , text=f"{turn} wins the game" , bg="orange", font=("Arial" , 26),width=16)
winningLabel.grid(row = 0 , column=0 , columnspan=3)
game_end = True
turn = "x"
if checkForDraw():
drawLabel = Label(frame1 , text=f"Game Draw" , bg="orange", font=("Arial" , 26), width = 16)
drawLabel.grid(row = 0 , column=0 , columnspan=3)
# ------ UI --------
# Change Mode options
singlePlayerButton = Button(optionFrame , text="SinglePlayer" , width=13 , height=1 , font=("Arial" , 15) , bg="lightgrey" , relief=RAISED , borderwidth=5 , command=changeModeToSinglePlayer)
singlePlayerButton.grid(row=0 , column=0 , columnspan=1 , sticky=NW)
multiPlayerButton = Button(optionFrame , text="Multiplayer" , width=13 , height=1 , font=("Arial" , 15) , bg="lightgrey" , relief=RAISED , borderwidth=5 , command=changeModeToMultiplayer )
multiPlayerButton.grid(row=0 , column=1 , columnspan=1 , sticky=NW)
# Tic Tac Toe Board
# First row
button1 = Button(frame2 , text= " " , width=4 , height=2 , font=("Arial" , 30) , bg="yellow" , relief=RAISED , borderwidth=5)
button1.grid(row = 0 , column=0)
button1.bind("<Button-1>" , play)
button2 = Button(frame2 , text= " " , width=4 , height=2 , font=("Arial" , 30), bg="yellow" , relief=RAISED , borderwidth=5 )
button2.grid(row = 0 , column=1)
button2.bind("<Button-1>" , play)
button3 = Button(frame2 , text= " " , width=4 , height=2 , font=("Arial" , 30), bg="yellow" , relief=RAISED , borderwidth=5 )
button3.grid(row = 0 , column=2)
button3.bind("<Button-1>" , play)
# second row
button4 = Button(frame2 , text= " " , width=4 , height=2 , font=("Arial" , 30), bg="yellow" , relief=RAISED , borderwidth=5 )
button4.grid(row = 1 , column=0)
button4.bind("<Button-1>" , play)
button5 = Button(frame2 , text= " " , width=4 , height=2 , font=("Arial" , 30), bg="yellow" , relief=RAISED , borderwidth=5 )
button5.grid(row = 1 , column=1)
button5.bind("<Button-1>" , play)
button6 = Button(frame2 , text= " " , width=4 , height=2 , font=("Arial" , 30), bg="yellow" , relief=RAISED , borderwidth=5 )
button6.grid(row = 1 , column=2)
button6.bind("<Button-1>" , play)
# third row
button7 = Button(frame2 , text= " " , width=4 , height=2 , font=("Arial" , 30) , bg="yellow" , relief=RAISED , borderwidth=5)
button7.grid(row = 2 , column=0)
button7.bind("<Button-1>" , play)
button8 = Button(frame2 , text= " " , width=4 , height=2 , font=("Arial" , 30), bg="yellow" , relief=RAISED , borderwidth=5 )
button8.grid(row = 2 , column=1)
button8.bind("<Button-1>" , play)
button9 = Button(frame2 , text= " " , width=4 , height=2 , font=("Arial" , 30) , bg="yellow" , relief=RAISED , borderwidth=5)
button9.grid(row = 2 , column=2)
button9.bind("<Button-1>" , play)
restartButton = Button(frame2 , text="Restart Game" , width=19 , height=1 , font=("Arial" , 20) , bg="Green" , relief=RAISED , borderwidth=5 , command=restartGame )
restartButton.grid(row=4 , column=0 , columnspan=3)
buttons = [button1 , button2 , button3 , button4 , button5 , button6 , button7 , button8, button9]
root.mainloop()