-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
411 lines (335 loc) · 11.6 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
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#!/usr/bin/env python3
"""
main.py - Main script for running a Tic-Tac-Toe game between two players or against an AI.
This script imports required modules, initializes game variables, and then starts the main loop
which repeatedly prompts for user input until they choose to quit the game. Depending on
their choice, it either runs a new game or quits the program.
"""
import tensorflow as tf
import numpy as np
from keras import losses, optimizers
from keras.models import Sequential
from keras.layers import Dense
from keras.layers.advanced_activations import PReLU
from keras.models import model_from_json
from pathlib import Path
import random
np.random.seed(123)
board_state = np.array([0.0,0.0,0.0, 0.0,0.0,0.0, 0.0,0.0,0.0], dtype=np.float32)
player1 = 1
player2 = -1
empty_spot = 0
class GameData:
def __init__(self, winner, turns):
if (winner not in [-1, 0, 1]):
raise ValueError('Error: parameter "winner" must have a value of -1, 0, or 1. (Was ' + str(winner) + ')')
self.winner = winner
self.turns = turns
def train(games):
inputs = []
expected = []
samples = random.sample(games, 50000)
print("Sampled ", len(samples), " games of ", len(games))
model = Sequential()
model.add(Dense(8, input_dim=9, activation='sigmoid'))
model.add(Dense(9))
model.add(PReLU(weights=None, alpha_initializer="zero"))
model.compile(loss = losses.mean_absolute_error, optimizer=optimizers.Nadam())
print("Processing samples...")
for game in samples:
preceding_turn = np.array([0.0,0.0,0.0, 0.0,0.0,0.0, 0.0,0.0,0.0], dtype=np.float32)
for turn in game.turns:
delta = turn - preceding_turn
if any(i > 0 for i in delta):
# print(delta)
inputs.append(preceding_turn)
if game.winner == 1.0:
expected.append(10.0 * delta)
elif game.winner == -1:
expected.append(-1.0 * delta)
else:
expected.append(0.0 * delta)
preceding_turn = turn
print("Training model... len(inputs)=", len(inputs), "len(expected)=", len(expected))
model.fit(np.array(inputs, dtype=np.float32), np.array(expected, dtype=np.float32), epochs=15, batch_size=64)
return model
def importGameData():
with open('all-games.data') as f:
content = f.readlines()
print('Read ', len(content), ' lines of data from all-games.data.')
i = 0
games = []
while len(content) > i:
outcome = convertLetterToNumber(content[i])
line1 = content[i + 1]
line2 = content[i + 2]
line3 = content[i + 3]
i+=4
if not i%100000:
print('Line ', i, ' of ', len(content), ' read...')
turns = parseTurns(line1, line2, line3)
games.append(GameData(outcome, turns))
return games
def parseTurns(line1, line2, line3):
turns_row_1 = line1.split(',')
turns_row_2 = line2.split(',')
turns_row_3 = line3.split(',')
turns = []
for i in range(len(turns_row_1)-1):
s00 = convertLetterToNumber(turns_row_1[i][0:1])
s01 = convertLetterToNumber(turns_row_1[i][1:2])
s02 = convertLetterToNumber(turns_row_1[i][2:3])
s10 = convertLetterToNumber(turns_row_2[i][0:1])
s11 = convertLetterToNumber(turns_row_2[i][1:2])
s12 = convertLetterToNumber(turns_row_2[i][2:3])
s20 = convertLetterToNumber(turns_row_3[i][0:1])
s21 = convertLetterToNumber(turns_row_3[i][1:2])
s22 = convertLetterToNumber(turns_row_3[i][1:3])
turns.append(np.array([s00, s01, s02, s10, s11, s12, s20, s21, s22], dtype=np.float32))
return turns
def neuralnet_move(player1, board_state):
results = model.predict(np.array([board_state], dtype=np.float32))
#print("board_state = ", board_state, " results = ", results)
blank = getBlankSpotsArray(board_state)
for i in range(0, 9):
if i not in blank:
results[0, i] = np.finfo('float32').min
#print("after non-blanks removed: results = ", results)
return np.argmax(results)
def sign(player1, player2):
player1 = input("What team you want to be? X or O ")
while player1 not in ('x','X','o','O'):
print("Invalid Choice!")
player1 = input("What team you want to be? X or O ")
if player1 == 'x' or player1 == 'X':
print("Ok, X is yours!")
player1 = 1
player2 = -1
else:
print("Ok, O is yours!")
player1 = -1
player2 = 1
return player1, player2
def decide_turn():
"""
decide_turn() - A function to ask the user if they want to go first in the game.
This function uses a while loop and input() statement to prompt the user with a yes/no
question. Based on their response, it returns 1 if they want to go first or 0 otherwise.
"""
turn = None
while turn not in ('y','Y','n','N'):
turn = input("Do you want to go first? ")
if turn == 'y' or turn == 'Y':
return 1
elif turn == 'n' or turn == 'N':
return 0
else:
print("its an invalid choice.")
def draw(a):
"""
draw(a) - A function to display the current state of the game board.
This function takes an argument 'a' which is a list of 9 elements representing
the state of the Tic-Tac-Toe game board. It prints out a formatted version of
the game board on the terminal, with Xs for player 1's moves, Os for player 2's
moves, and empty spaces for unoccupied squares.
"""
#print(chr(27) + "[2J")
print()
print("\t ", convertNumberToLetter(a[0]), "|", convertNumberToLetter(a[1]), "|", convertNumberToLetter(a[2]))
print("\t", "-----------")
print("\t ", convertNumberToLetter(a[3]), "|", convertNumberToLetter(a[4]), "|", convertNumberToLetter(a[5]))
print("\t", "-----------")
print("\t ", convertNumberToLetter(a[6]), "|", convertNumberToLetter(a[7]), "|", convertNumberToLetter(a[8]), "\n")
def convertLetterToNumber(l):
"""
convertLetterToNumber(l) - A function to convert the game piece letters into numbers.
This function takes a single argument 'l', which can be either 'X', 'O' or any
other character. It returns 1.0 if l is equal to 'X', -1.0 if l is equal to 'O',
and 0.0 otherwise.
"""
if l in ('x','X'):
return 1.0
elif l in ("o", "O"):
return -1.0
else:
return 0.0
def convertNumberToLetter(n):
if n == 1.0:
return "X"
elif n == -1.0:
return "O"
else:
return " "
def congo_player1():
"""
congo_player1() - A function to display the win message for player 1.
This function simply prints out a victory message on the terminal screen
"""
print("Player 1 wins!")
def congo_player2():
"""
congo_player2() - A function to display the win message for player 2.
This function similar to above function, it prints out a victory message on the
terminal screen but this time for player 2
"""
print("Player 2 wins!")
def player1_first(player1, player2, board_state):
while check_for_winner(player1, player2, board_state) is None:
#move = player1_move(player1, board_state)
move = neuralnet_move(player1, board_state)
#print("player 1 takes ", move)
board_state[int(move)] = player1
#draw(board_state)
if check_for_winner(player1, player2, board_state) != None:
break
else:
pass
p_move = machine_move(player1, player2, board_state)
#print("player 2 took", p_move)
board_state[int(p_move)] = player2
#draw(board_state)
q = check_for_winner(player1, player2, board_state)
# if q == 1:
# congo_player1()
# elif q == -1:
# congo_player2()
# else:
# print("Its tie man...")
return q
def player2_first(player1, player2, new):
while not check_for_winner(player1, player2, new):
print("i'll take...")
p_move = machine_move(player1, player2, new)
print(p_move)
new[p_move] = player2
draw(new)
if check_for_winner(player1, player2, new) != None:
break
else:
pass
move = player1_move(player1, new)
new[int(move)] = player1
draw(new)
q = check_for_winner(player1, player2, new)
if q == 1:
congo_player1()
elif q == -1:
congo_player2()
else:
print("Cat's game...")
def check_for_winner(player1, player2, board_state):
winning_states = ((0,1,2),(3,4,5),(6,7,8),(0,3,6),(1,4,7),(2,5,8),(0,4,8),(2,4,6))
for ws in winning_states:
#print("board_state=", board_state, " board_state[ws[0]]=", board_state[ws[0]], " board_state[ws[1]]=", board_state[ws[1]], " board_state[ws[2]]=", board_state[ws[2]])
if board_state[ws[0]] == board_state[ws[1]] == board_state[ws[2]] != empty_spot:
winner = board_state[ws[0]]
if winner == player1:
return player1
elif winner == player2:
return player2
if empty_spot not in board_state:
return empty_spot
if empty_spot not in board_state:
return empty_spot
return None
def player1_move(player1, board_state):
a = input("where you want to move? ")
while True:
if a not in ('0','1','2','3','4','5','6','7','8'):
print("Sorry, invalid move")
a = input("where you want to move? ")
elif board_state[int(a)] != empty_spot:
print("Sorry, the place is already taken")
a = input("where you want to move? ")
else:
return int(a)
def machine_move(player1, player2, board_state):
#best = [4, 0, 2, 6, 8]
blank = getBlankSpotsArray(board_state)
for i in blank:
board_state[i] = player2
if check_for_winner(player1, player2, board_state) is 0:
return i
board_state[i] = empty_spot
for i in blank:
board_state[i] = player1
if check_for_winner(player1, player2, board_state) is 1:
return i
board_state[i] = empty_spot
return int(blank[random.randrange(len(blank))])
def getBlankSpotsArray(board_state):
blank = []
for i in range(0,9):
if board_state[i] == empty_spot:
blank.append(i)
return blank
def display_instruction():
print(chr(27) + "[2J")
""" Displays Game Instuructions. """
print(
"""
Welcome to the Game...
You will make your move known by entering a number, 0 - 8.
The will correspond to the board position as illustrated:
0 | 1 | 2
-----------
3 | 4 | 5
-----------
6 | 7 | 8
Prepare yourself, the ultimate battle is about to begin.....
""")
def loadModel():
json_file = open('model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
model = model_from_json(loaded_model_json)
# load weights into new model
model.load_weights("model.h5")
print("Loaded model from disk")
return model
def trainNewModel():
games = importGameData()
model = train(games)
saveNN = input("Save trained NN? (y/N)")
if saveNN == 'y' or saveNN == 'Y':
# serialize model to JSON
model_json = model.to_json()
with open("model.json", "w") as json_file:
json_file.write(model_json)
# serialize weights to HDF5
model.save_weights("model.h5")
print("Saved model to disk")
return model
def main(player1, player2, board_state):
# to re-enable user input
# player1, player2 = sign(player1, player2)
#b = decide_turn()
b = 1
if b == 1:
# print("Ok, you are first!")
# print("Lets get started, here's a new board!")
#draw(board_state)
return player1_first(player1, player2, board_state)
elif b == 0:
#print("Ok, I'll be the first!")
#print("So, lets start..")
#draw(board_state)
return player2_first(player1, player2, board_state)
else:
pass
model_exists = Path("model.json").is_file() and Path("model.h5").is_file()
if model_exists:
loadNN = input("Load previously trained NN? (Y/n)")
else:
loadNN = 'n'
if loadNN != 'n' and loadNN != 'N':
model = loadModel()
else:
model = trainNewModel()
results = np.array([0,0,0])
for i in range(0,3000):
board_state = np.array([0.0,0.0,0.0, 0.0,0.0,0.0, 0.0,0.0,0.0], dtype=np.float32)
winner = main(player1, player2, board_state)
results[winner + 1] += 1
print("results: [wins, ties, losses]")
print(results)