-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathtic-tac-toe.py
232 lines (199 loc) · 6.6 KB
/
tic-tac-toe.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
import os.path
from tkinter import *
from games import minmax_decision, alpha_beta_player, random_player, TicTacToe
# "gen_state" can be used to generate a game state to apply the algorithm
from tests.test_games import gen_state
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
ttt = TicTacToe()
root = None
buttons = []
frames = []
x_pos = []
o_pos = []
count = 0
sym = ""
result = None
choices = None
def create_frames(root):
"""
This function creates the necessary structure of the game.
"""
frame1 = Frame(root)
frame2 = Frame(root)
frame3 = Frame(root)
frame4 = Frame(root)
create_buttons(frame1)
create_buttons(frame2)
create_buttons(frame3)
buttonExit = Button(
frame4, height=1, width=2,
text="Exit",
command=lambda: exit_game(root))
buttonExit.pack(side=LEFT)
frame4.pack(side=BOTTOM)
frame3.pack(side=BOTTOM)
frame2.pack(side=BOTTOM)
frame1.pack(side=BOTTOM)
frames.append(frame1)
frames.append(frame2)
frames.append(frame3)
for x in frames:
buttons_in_frame = []
for y in x.winfo_children():
buttons_in_frame.append(y)
buttons.append(buttons_in_frame)
buttonReset = Button(frame4, height=1, width=2,
text="Reset", command=lambda: reset_game())
buttonReset.pack(side=LEFT)
def create_buttons(frame):
"""
This function creates the buttons to be pressed/clicked during the game.
"""
button0 = Button(frame, height=2, width=2, text=" ",
command=lambda: on_click(button0))
button0.pack(side=LEFT)
button1 = Button(frame, height=2, width=2, text=" ",
command=lambda: on_click(button1))
button1.pack(side=LEFT)
button2 = Button(frame, height=2, width=2, text=" ",
command=lambda: on_click(button2))
button2.pack(side=LEFT)
# TODO: Add a choice option for the user.
def on_click(button):
"""
This function determines the action of any button.
"""
global ttt, choices, count, sym, result, x_pos, o_pos
if count % 2 == 0:
sym = "X"
else:
sym = "O"
count += 1
button.config(
text=sym,
state='disabled',
disabledforeground="red") # For cross
x, y = get_coordinates(button)
x += 1
y += 1
x_pos.append((x, y))
state = gen_state(to_move='O', x_positions=x_pos,
o_positions=o_pos)
try:
choice = choices.get()
if "Random" in choice:
a, b = random_player(ttt, state)
elif "Pro" in choice:
a, b = minmax_decision(state, ttt)
else:
a, b = alpha_beta_player(ttt, state)
except (ValueError, IndexError, TypeError) as e:
disable_game()
result.set("It's a draw :|")
return
if 1 <= a <= 3 and 1 <= b <= 3:
o_pos.append((a, b))
button_to_change = get_button(a - 1, b - 1)
if count % 2 == 0: # Used again, will become handy when user is given the choice of turn.
sym = "X"
else:
sym = "O"
count += 1
if check_victory(button):
result.set("You win :)")
disable_game()
else:
button_to_change.config(text=sym, state='disabled',
disabledforeground="black")
if check_victory(button_to_change):
result.set("You lose :(")
disable_game()
# TODO: Replace "check_victory" by "k_in_row" function.
def check_victory(button):
"""
This function checks various winning conditions of the game.
"""
# check if previous move caused a win on vertical line
global buttons
x, y = get_coordinates(button)
tt = button['text']
if buttons[0][y]['text'] == buttons[1][y]['text'] == buttons[2][y]['text'] != " ":
buttons[0][y].config(text="|" + tt + "|")
buttons[1][y].config(text="|" + tt + "|")
buttons[2][y].config(text="|" + tt + "|")
return True
# check if previous move caused a win on horizontal line
if buttons[x][0]['text'] == buttons[x][1]['text'] == buttons[x][2]['text'] != " ":
buttons[x][0].config(text="--" + tt + "--")
buttons[x][1].config(text="--" + tt + "--")
buttons[x][2].config(text="--" + tt + "--")
return True
# check if previous move was on the main diagonal and caused a win
if x == y and buttons[0][0]['text'] == buttons[1][1]['text'] == buttons[2][2]['text'] != " ":
buttons[0][0].config(text="\\" + tt + "\\")
buttons[1][1].config(text="\\" + tt + "\\")
buttons[2][2].config(text="\\" + tt + "\\")
return True
# check if previous move was on the secondary diagonal and caused a win
if x + y == 2 and buttons[0][2]['text'] == buttons[1][1]['text'] == buttons[2][0]['text'] != " ":
buttons[0][2].config(text="/" + tt + "/")
buttons[1][1].config(text="/" + tt + "/")
buttons[2][0].config(text="/" + tt + "/")
return True
return False
def get_coordinates(button):
"""
This function returns the coordinates of the button clicked.
"""
global buttons
for x in range(len(buttons)):
for y in range(len(buttons[x])):
if buttons[x][y] == button:
return x, y
def get_button(x, y):
"""
This function returns the button memory location corresponding to a coordinate.
"""
global buttons
return buttons[x][y]
def reset_game():
"""
This function will reset all the tiles to the initial null value.
"""
global x_pos, o_pos, frames, count
count = 0
x_pos = []
o_pos = []
result.set("Your Turn!")
for x in frames:
for y in x.winfo_children():
y.config(text=" ", state='normal')
def disable_game():
"""
This function deactivates the game after a win, loss or draw.
"""
global frames
for x in frames:
for y in x.winfo_children():
y.config(state='disabled')
def exit_game(root):
"""
This function will exit the game by killing the root.
"""
root.destroy()
if __name__ == "__main__":
global result, choices
root = Tk()
root.title("TicTacToe")
root.geometry("150x200") # Improved the window geometry
root.resizable(0, 0) # To remove the maximize window option
result = StringVar()
result.set("Your Turn!")
w = Label(root, textvariable=result)
w.pack(side=BOTTOM)
create_frames(root)
choices = StringVar(root)
choices.set("Vs Pro")
menu = OptionMenu(root, choices, "Vs Random", "Vs Pro", "Vs Legend")
menu.pack()
root.mainloop()