-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck.py
297 lines (270 loc) · 7.81 KB
/
check.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
import pickle
from tkinter import *
from tkinter import messagebox
import numpy as np
with open("W_param.pkl", 'rb') as f:
W = pickle.load(f)
with open("W2_param.pkl", 'rb') as f:
W2 = pickle.load(f)
with open("B_param.pkl", 'rb') as f:
B = pickle.load(f)
with open("B2_param.pkl", 'rb') as f:
B2 = pickle.load(f)
def forward(X):
hidden = np.matmul(X, W) + B
hidden = 1 / (1 + np.exp(-hidden))
result = np.matmul(hidden, W2) + B2
return result
def findBlank(myBoard):
print(myBoard)
predict = -100
index = 0
board = myBoard
for i in range(len(board)):
if(board[i]==0):
board[i] = 1
result = forward(board)
print(i, result)
#컴퓨터가 먼저 시 최솟값
#사람이 먼저 시 최댓값
if(predict < result):
predict = result
index = i
board[i]=0
print("computer : " + str(index) + ", " + str(predict))
return index
#Game
window=Tk()
window.title("AI Tic-Tac-Toe")
window.geometry("400x300")
lbl=Label(window,text="Tic-tac-toe Game",font=('Helvetica','15'))
lbl.grid(row=0,column=0)
lbl=Label(window,text="Player 1: X",font=('Helvetica','10'))
lbl.grid(row=1,column=0)
lbl=Label(window,text="Player 2: O",font=('Helvetica','10'))
lbl.grid(row=2,column=0)
turn=1; #For first person turn.
global board
global next
global first
next = 0
board = [0, 0, 0, 0, 0, 0, 0, 0, 0]
first = 1
def put(next):
if next==0:
clicked1()
elif next==1:
clicked2()
elif next==2:
clicked3()
elif next==3:
clicked4()
elif next==4:
clicked5()
elif next==5:
clicked6()
elif next==6:
clicked7()
elif next==7:
clicked8()
elif next==8:
clicked9()
def clicked1():
global turn
global next
global first
if btn1["text"]==" ": #For getting the text of a button
if turn==1:
turn =2;
board[0] = -1
btn1["text"]="X"
elif turn==2:
turn=1;
board[0] = 1
btn1["text"]="O"
check();
if turn==first:
put(findBlank(board))
def clicked2():
global turn
global next
global first
if btn2["text"]==" ":
if turn==1:
turn =2;
board[1] = -1
btn2["text"]="X"
elif turn==2:
turn=1;
board[1] = 1
btn2["text"]="O"
check();
if turn==first:
put(findBlank(board))
def clicked3():
global turn
global next
global first
if btn3["text"]==" ":
if turn==1:
turn =2;
board[2] = -1
btn3["text"]="X"
elif turn==2:
turn=1;
board[2] = 1
btn3["text"]="O"
check();
if turn==first:
put(findBlank(board))
def clicked4():
global turn
global next
global first
if btn4["text"]==" ":
if turn==1:
turn =2;
board[3] = -1
btn4["text"]="X"
elif turn==2:
turn=1;
board[3] = 1
btn4["text"]="O"
check();
if turn==first:
put(findBlank(board))
def clicked5():
global turn
global next
global first
if btn5["text"]==" ":
if turn==1:
turn =2;
board[4] = -1
btn5["text"]="X"
elif turn==2:
turn=1;
board[4] = 1
btn5["text"]="O"
check();
if turn==first:
put(findBlank(board))
def clicked6():
global turn
global next
global first
if btn6["text"]==" ":
if turn==1:
turn =2;
board[5] = -1
btn6["text"]="X"
elif turn==2:
turn=1;
board[5] = 1
btn6["text"]="O"
check();
if turn==first:
put(findBlank(board))
def clicked7():
global turn
global next
global first
if btn7["text"]==" ":
if turn==1:
turn =2;
board[6] = -1
btn7["text"]="X"
elif turn==2:
turn=1;
board[6] = 1
btn7["text"]="O"
check();
if turn==first:
put(findBlank(board))
def clicked8():
global turn
global next
global first
if btn8["text"]==" ":
if turn==1:
turn =2;
board[7] = -1
btn8["text"]="X"
elif turn==2:
turn=1;
board[7] = 1
btn8["text"]="O"
check();
if turn==first:
put(findBlank(board))
def clicked9():
global turn
global next
global first
if btn9["text"]==" ":
if turn==1:
turn =2;
board[8] = -1
btn9["text"]="X"
elif turn==2:
turn=1;
board[8] = 1
btn9["text"]="O"
check();
if turn==first:
put(findBlank(board))
flag=1;
def check():
global flag;
b1 = btn1["text"];
b2 = btn2["text"];
b3 = btn3["text"];
b4 = btn4["text"];
b5 = btn5["text"];
b6 = btn6["text"];
b7 = btn7["text"];
b8 = btn8["text"];
b9 = btn9["text"];
flag=flag+1;
if b1==b2 and b1==b3 and b1=="O" or b1==b2 and b1==b3 and b1=="X":
win(btn1["text"])
if b4==b5 and b4==b6 and b4=="O" or b4==b5 and b4==b6 and b4=="X":
win(btn4["text"]);
if b7==b8 and b7==b9 and b7=="O" or b7==b8 and b7==b9 and b7=="X":
win(btn7["text"]);
if b1==b4 and b1==b7 and b1=="O" or b1==b4 and b1==b7 and b1=="X":
win(btn1["text"]);
if b2==b5 and b2==b8 and b2=="O" or b2==b5 and b2==b8 and b2=="X":
win(btn2["text"]);
if b3==b6 and b3==b9 and b3=="O" or b3==b6 and b3==b9 and b3=="X":
win(btn3["text"]);
if b1==b5 and b1==b9 and b1=="O" or b1==b5 and b1==b9 and b1=="X":
win(btn1["text"]);
if b7==b5 and b7==b3 and b7=="O" or b7==b5 and b7==b3 and b7=="X":
win(btn7["text"]);
if flag ==10:
messagebox.showinfo("Tie", "Match Tied!!! Try again :)")
window.destroy()
def win(player):
ans = "Game complete " +player + " wins ";
messagebox.showinfo("Congratulations", ans)
window.destroy() # is used to close the program
btn1 = Button(window, text=" ",bg="yellow", fg="Black",width=3,height=1,font=('Helvetica','20'),command=clicked1)
btn1.grid(column=1, row=1)
btn2 = Button(window, text=" ",bg="yellow", fg="Black",width=3,height=1,font=('Helvetica','20'),command=clicked2)
btn2.grid(column=2, row=1)
btn3 = Button(window, text=" ",bg="yellow", fg="Black",width=3,height=1,font=('Helvetica','20'),command=clicked3)
btn3.grid(column=3, row=1)
btn4 = Button(window, text=" ",bg="yellow", fg="Black",width=3,height=1,font=('Helvetica','20'),command=clicked4)
btn4.grid(column=1, row=2)
btn5 = Button(window, text=" ",bg="yellow", fg="Black",width=3,height=1,font=('Helvetica','20'),command=clicked5)
btn5.grid(column=2, row=2)
btn6 = Button(window, text=" ",bg="yellow", fg="Black",width=3,height=1,font=('Helvetica','20'),command=clicked6)
btn6.grid(column=3, row=2)
btn7 = Button(window, text=" ",bg="yellow", fg="Black",width=3,height=1,font=('Helvetica','20'),command=clicked7)
btn7.grid(column=1, row=3)
btn8 = Button(window, text=" ",bg="yellow", fg="Black",width=3,height=1,font=('Helvetica','20'),command=clicked8)
btn8.grid(column=2, row=3)
btn9 = Button(window, text=" ",bg="yellow", fg="Black",width=3,height=1,font=('Helvetica','20'),command=clicked9)
btn9.grid(column=3, row=3)
window.mainloop()