-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTic-Tac-Toe.py
317 lines (249 loc) · 11.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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
"""
Tic Tac Toe : Compete against a bot that learnt from you!
=========================================================
Tic Tac Toe is is a paper-and-pencil game for two players,
X and O, who take turns marking the spaces in a 3×3 grid.
The player who succeeds in placing three of their marks
in a horizontal, vertical, or diagonal row wins the game.
Board :-
========
1 | 2 | 3
---------
4 | 5 | 6
---------
7 | 8 | 9
Constants :-
============
win_conditions : List of all possible winning situations
PATH : Directory, for freeze mode
Variables :-
============
TRAIN_ITERATIONS: Number of iterations for training set
train : Flag for training mode
True: Yes, False: No
PLAYER_TURN : Flag for who's turn it is.
True: Player 1, False: Player 2
chance : Stores move number
selected : List of all selected moves
Even indices for Player 1, Odd indices for Player 2
Methods :-
==========
init_buttons : Connect all python methods with .ui file
disp : Places the appropriate move image (cross/ circle),
adds move to selected and checks current board state
check : Analyzes current board state for outcome
play_move : Finds the most winning move based on prior knowledge
test_lose : Helper function for play_move to play a move if next move
stops a lose or is a guaranteed win
train_data : Enters training mode where the bot is allowed to play
with itself for N_ITERATIONS finding new moves
store_data : Stores the current set of moves with outcome to dataset.csv
reset_data : Clears all data history making the AI dumb
disable : Temporarily lock down all buttons after successful outcome
reset : Resets current board and re-initializes variables
Dependencies :-
===============
dataset.csv : Labelled history of moves played
GUI.ui : ui format of GUI used loaded in variable "ui"
cross.jpg : Player 1 move
circle.jpg : Player 2 move
def.jps : White tile of same size as cross/circle
icon : Window icon for the application
"""
import sys
import csv
import random
from PyQt5 import QtWidgets, QtGui, QtCore, uic
win_conditions = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [1, 4, 7],
[2, 5, 8], [3, 6, 9], [1, 5, 9], [3, 5, 7]]
train = False # Play mode
PATH = 'data\\' # PATH for dependencies
def reset():
global chance, selected
chance = 0 # Move number
selected = [] # Moves played
ui.pushButton_1.setEnabled(True)
ui.pushButton_2.setEnabled(True)
ui.pushButton_3.setEnabled(True)
ui.pushButton_4.setEnabled(True)
ui.pushButton_5.setEnabled(True)
ui.pushButton_6.setEnabled(True)
ui.pushButton_7.setEnabled(True)
ui.pushButton_8.setEnabled(True)
ui.pushButton_9.setEnabled(True)
ui.pushButton_1.setIcon(QtGui.QIcon(PATH+'def.jpg'))
ui.pushButton_2.setIcon(QtGui.QIcon(PATH+'def.jpg'))
ui.pushButton_3.setIcon(QtGui.QIcon(PATH+'def.jpg'))
ui.pushButton_4.setIcon(QtGui.QIcon(PATH+'def.jpg'))
ui.pushButton_5.setIcon(QtGui.QIcon(PATH+'def.jpg'))
ui.pushButton_6.setIcon(QtGui.QIcon(PATH+'def.jpg'))
ui.pushButton_7.setIcon(QtGui.QIcon(PATH+'def.jpg'))
ui.pushButton_8.setIcon(QtGui.QIcon(PATH+'def.jpg'))
ui.pushButton_9.setIcon(QtGui.QIcon(PATH+'def.jpg'))
ui.pushButton_1.setIconSize(QtCore.QSize(175, 175))
ui.pushButton_2.setIconSize(QtCore.QSize(175, 175))
ui.pushButton_3.setIconSize(QtCore.QSize(175, 175))
ui.pushButton_4.setIconSize(QtCore.QSize(175, 175))
ui.pushButton_5.setIconSize(QtCore.QSize(175, 175))
ui.pushButton_6.setIconSize(QtCore.QSize(175, 175))
ui.pushButton_7.setIconSize(QtCore.QSize(175, 175))
ui.pushButton_8.setIconSize(QtCore.QSize(175, 175))
ui.pushButton_9.setIconSize(QtCore.QSize(175, 175))
ui.label_1.setText("Your Turn")
def disable():
global train, PLAYER_TURN
if not train: # Not in training phase
ui.pushButton_1.setEnabled(False)
ui.pushButton_2.setEnabled(False)
ui.pushButton_3.setEnabled(False)
ui.pushButton_4.setEnabled(False)
ui.pushButton_5.setEnabled(False)
ui.pushButton_6.setEnabled(False)
ui.pushButton_7.setEnabled(False)
ui.pushButton_8.setEnabled(False)
ui.pushButton_9.setEnabled(False)
else:
PLAYER_TURN = True
def disp(self, select):
global chance
if chance % 2 == 0: # Player 1
self.setIcon(QtGui.QIcon(PATH+'cross.jpg'))
else: # Player 2
self.setIcon(QtGui.QIcon(PATH+'circle.jpg'))
self.setIconSize(QtCore.QSize(175, 180))
chance += 1
selected.append(select)
check()
def check():
global selected, win_conditions, chance
player_1 = [selected[i] for i in range(0, len(selected), 2)] # Player 1 moves
player_2 = [selected[i+1] for i in range(0, len(selected)-1, 2)] # Player 2 moves
for x in win_conditions:
if set(player_1) >= set(x):
ui.label_1.setText("Player 1 Wins!")
store_data("L") # Bot loses
disable()
return
elif set(player_2) >= set(x):
ui.label_1.setText("Player 2 Wins!")
store_data("W") # Bot wins
disable()
return
if len(selected) == 9:
ui.label_1.setText("Draw!")
store_data("D") # Draw situation
return
if chance % 2 == 1: # Bot to play
x = play_move()
exec("disp(ui.pushButton_%d, %d)" % (x, x))
def play_move():
global selected, win_conditions, chance, PATH
player_1 = [selected[i] for i in range(0, len(selected), 2)]
player_2 = [selected[i + 1] for i in range(0, len(selected) - 1, 2)]
k = test_lose(player_2, player_1) # Find Win Move
if k == 99:
k = test_lose(player_1, player_2) # Find Force Move
if k == 99:
# No win/force move, find best move from dataset
avail = [x for x in [1, 2, 3, 4, 5, 6, 7, 8, 9] if x not in selected]
move_res = {x: 0 for x in avail} # dictionary with all moves and outcome probability
with open('data/dataset.csv', mode='r') as csv_file:
csv_reader = csv.reader(csv_file)
line_count = 0
for row in csv_reader:
if line_count == 0: # Ignore header
line_count += 1
continue
temp = [x for x in row[:-1] if int(x) not in selected]
while '0' in temp: # Remove blank moves
del temp[-1]
if len(temp) > 0:
if row[chance] == temp[0]:
if row[-1] == 'W':
move_res[int(temp[0])] += 1
elif row[-1] == 'D':
move_res[int(temp[0])] += 0.5
elif row[-1] == 'L':
move_res[int(temp[0])] -= 1
else:
move_res[int(random.choice(temp))] += 0.5
else:
move_res[random.choice(avail)] -= 0.5
if chance % 2 == 0: # Player 1
move_avail = [x for x in move_res
if move_res[x] == move_res[max(move_res, key=move_res.get)]] # max
else: # Player 2
move_avail = [x for x in move_res
if move_res[x] == move_res[min(move_res, key=move_res.get)]] # min
x = random.choice(move_avail)
# x = max(move_res, key=move_res.get)
else:
x = k
else:
x = k
return x
def test_lose(player_1, player_2):
global selected
for x in win_conditions:
if len(set(x)-set(player_1)) < 2 and list(set(x) - set(player_1))[0] not in player_2: # Check if next move is win/ force
return list(set(x) - set(player_1))[0]
return 99
def store_data(outcome):
global selected
with open('data/dataset.csv', mode='a', newline='') as csv_file:
csv_writer = csv.writer(csv_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
for i in range(9 - len(selected)):
selected.append(0)
csv_writer.writerow(
[str(selected[0]), str(selected[1]), str(selected[2]), str(selected[3]), str(selected[4]),
str(selected[5]), str(selected[6]), str(selected[7]), str(selected[8]), outcome])
def train_data():
global train, PLAYER_TURN
train_iterations = 200 # Variable for number of train sets
reset() # Reset board for any moves
train = True # Set train flag
msg = QtWidgets.QMessageBox()
msg.setText("Training data, please wait few minutes till you get next prompt...")
msg.exec()
for _ in range(train_iterations):
PLAYER_TURN = False
for _ in range(5):
if not PLAYER_TURN:
move = play_move()
# exec("disp(ui.pushButton_%d, %d)" % (x, x))
__ = getattr(disp, f'ui.pushButton_{move}, {move}')
reset()
msg.setText("Trained Successfully!")
msg.exec()
train = False # Reset train flag
def reset_data():
with open('data/dataset.csv', mode='w', newline='') as csv_file:
csv_writer = csv.writer(csv_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
csv_writer.writerow(["chance1", "chance2", "chance3", "chance4", "chance5",
"chance6", "chance7", "chance8", "chance9", "outcome"])
msg = QtWidgets.QMessageBox()
msg.setText("Reset complete, enjoy your free wins!")
msg.exec()
def init_buttons():
reset()
ui.pushButton_1.clicked.connect(lambda x: disp(ui.pushButton_1, 1) if 1 not in selected else 0)
ui.pushButton_2.clicked.connect(lambda x: disp(ui.pushButton_2, 2) if 2 not in selected else 0)
ui.pushButton_3.clicked.connect(lambda x: disp(ui.pushButton_3, 3) if 3 not in selected else 0)
ui.pushButton_4.clicked.connect(lambda x: disp(ui.pushButton_4, 4) if 4 not in selected else 0)
ui.pushButton_5.clicked.connect(lambda x: disp(ui.pushButton_5, 5) if 5 not in selected else 0)
ui.pushButton_6.clicked.connect(lambda x: disp(ui.pushButton_6, 6) if 6 not in selected else 0)
ui.pushButton_7.clicked.connect(lambda x: disp(ui.pushButton_7, 7) if 7 not in selected else 0)
ui.pushButton_8.clicked.connect(lambda x: disp(ui.pushButton_8, 8) if 8 not in selected else 0)
ui.pushButton_9.clicked.connect(lambda x: disp(ui.pushButton_9, 9) if 9 not in selected else 0)
ui.pushButton_new.clicked.connect(reset)
ui.pushButton_train.clicked.connect(train_data)
ui.pushButton_reset.clicked.connect(reset_data)
if __name__ == '__main__':
if getattr(sys, 'frozen', False):
PATH = sys._MEIPASS+'\\data\\'
app = QtWidgets.QApplication([])
app.setWindowIcon(QtGui.QIcon('icon.jpg'))
ui = uic.loadUi(PATH+"GUI.ui")
init_buttons()
ui.show()
app.exec()