-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtic_tac_toe.py
160 lines (133 loc) · 5.86 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
# Follow Us On Twitter @PY4ALL
from tkinter import *
from tkinter import ttk
import tkinter.messagebox as tmsg
import numpy as np
class MainWindow:
def __init__(self, master):
master.wm_iconbitmap("tic.ico")
master.title('Tic-Tac-Toe Game')
master.resizable(False, False)
master.configure(background='#D5F2DF')
self.turn = 'x'
self.w = Canvas(master, width=600, height=600, bg='#C5E9D2')
self.w.pack()
self.frame_control = ttk.Frame()
self.frame_control.pack()
# @ <-------- Buttons --------->
ttk.Button(master, text='Clear',
command=self.clear).pack()
ttk.Button(master, text='Exit',
command=self.exi).pack()
self.w.create_line(0, 200, 600, 200, fill="red")
self.w.create_line(0, 400, 600, 400, fill="red")
self.w.create_line(200, 0, 200, 600, fill="red")
self.w.create_line(400, 0, 400, 600, fill="red")
self.w.bind("<Button 1>", self.get_loc)
self.draw_loc = [[(35, 35), (35, 235), (35, 435)],
[(235, 35), (235, 235), (235, 435)],
[(435, 35), (435, 235), (435, 435)]]
self.game = np.array([[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])
self.draw_loc2 = [[(0, 0, 200, 200), (200, 0, 400, 200), (400, 0, 600, 200)],
[(0, 200, 200, 400), (200, 200, 400, 400),
(400, 200, 600, 400)],
[(0, 400, 200, 600), (200, 400, 400, 600), (400, 400, 600, 600)]]
self.x = PhotoImage(file="x.png")
self.o = PhotoImage(file="o.png")
self.game_end = False
def get_loc(self, event):
if not self.game[event.x//200][event.y//200] and not self.game_end:
if self.turn == 'x':
self.w.create_image(
self.draw_loc[event.x//200][event.y//200], anchor=NW, image=self.x, tags=('delete',))
self.turn = 'o'
self.game[event.x//200][event.y//200] = 1
else:
self.w.create_image(
self.draw_loc[event.x//200][event.y//200], anchor=NW, image=self.o, tags=('delete',))
self.turn = 'x'
self.game[event.x//200][event.y//200] = -1
self.game_end = self.check()
def check(self):
for count, row in enumerate(self.game.sum(0)):
if row == 3:
self.w.create_line(
0, 100+(count*200), 600, 100+(count*200), fill="#0D4EF6", tags=('delete',))
v = self.x_won()
tmsg.showinfo("Tic Tac Toe", "X Won\nO Lost")
return True
elif row == -3:
self.w.create_line(
0, 100+(count*200), 600, 100+(count*200), fill="#0D4EF6", tags=('delete',))
self.o_won()
tmsg.showinfo("Tic Tac Toe", "O Won\nX Lost")
return True
for count, col in enumerate(self.game.sum(1)):
if col == 3:
self.w.create_line(
100+(count*200), 0, 100+(count*200), 600, fill="#0D4EF6", tags=('delete',))
self.x_won()
tmsg.showinfo("Tic Tac Toe", "X Won\nO Lost")
return True
elif col == -3:
self.w.create_line(
100+(count*200), 0, 100+(count*200), 600, fill="#0D4EF6", tags=('delete',))
self.o_won()
tmsg.showinfo("Tic Tac Toe", "O Won\nX Lost")
return True
if sum(self.game.diagonal()) == 3:
self.w.create_line(
0, 0, 600, 600, fill="#0D4EF6", tags=('delete',))
self.x_won()
tmsg.showinfo("Tic Tac Toe", "X Won\nO Lost")
return True
elif sum(self.game.diagonal()) == -3:
self.w.create_line(
0, 0, 600, 600, fill="#0D4EF6", tags=('delete',))
self.o_won()
tmsg.showinfo("Tic Tac Toe", "O Won\nX Lost")
return True
if sum(np.fliplr(self.game).diagonal()) == 3:
self.w.create_line(
0, 600, 600, 0, fill="#0D4EF6", tags=('delete',))
self.x_won()
tmsg.showinfo("Tic Tac Toe", "X Won\nO Lost")
return True
elif sum(np.fliplr(self.game).diagonal()) == -3:
self.w.create_line(
0, 600, 600, 0, fill="#0D4EF6", tags=('delete',))
self.o_won()
tmsg.showinfo("Tic Tac Toe", "O Won\nX Lost")
return True
if len(np.where(self.game == 0)[0]) == 0:
self.w.create_text(300, 300, font='Times 40 bold',
text='Tie!', tags=('delete',), fill='#0D4EF6')
tmsg.showinfo(
"Tic Tac Toe", "The Match Tied.....\nTo Win Try again")
return True
return False
def clear(self):
self.w.delete('delete')
self.game = np.array([[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])
self.game_end = False
def exi(self):
msg = tmsg.askquestion(
"Confirm Exit.", "Do You Really want to Exit?\nIf YES click OK.")
if msg == 'yes':
quit()
def x_won(self):
self.w.create_text(300, 300, font='Times 40 bold',
text='X won!', tags=('delete',), fill='#0D4EF6')
def o_won(self):
self.w.create_text(300, 300, font='Times 40 bold',
text='O won!', tags=('delete',), fill='#0D4EF6')
def main():
root = Tk()
mainwindow = MainWindow(root)
root.mainloop()
if __name__ == "__main__":
main()