-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathai.py
160 lines (147 loc) · 4.88 KB
/
ai.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
import threading
import tictactoe
import random
import json
import tqdm
import os
def read_conf() -> dict:
inputfile = open("aiconf.txt")
input_line = inputfile.readline()
if input_line != "":
conf = json.loads(input_line)
else:
conf = {"amt_empty": 3, "train": 10000, "learn_player": 1}
write_conf(conf)
inputfile.close()
return conf
def read_poss() -> dict:
inputfile = open("poss.txt", "r")
input_line = inputfile.read()
if input_line != "":
poss = json.loads(input_line)
else:
poss = {"X": {}, "O": {}}
write_poss(poss)
inputfile.close()
return poss
def write_poss(poss: dict) -> None:
ouputfile = open("poss.txt", "w")
ouputfile.write(json.dumps(poss))
ouputfile.close()
def write_conf(conf: dict) -> None:
ouputfile = open("aiconf.txt", "w")
ouputfile.write(json.dumps(conf))
ouputfile.close()
def move(player: str, game: str, poss: dict) -> int:
"Uses the knowledge it has to make a decision regarding what move to play in the scenario 'game' if its symbol is 'player'"
if not game in poss[player].keys():
p = []
for i, s in enumerate(game):
if s == " ":
p.append(i)
return p[random.randint(0, len(p)-1)]
else:
return poss[player][game][random.randint(0, len(poss[player][game])-1)]
def train(amt: int, poss: dict, conf: dict) -> None:
"""
Trains the AI on 'amt' games. 'rand' is just cus threading was being annoying
"""
GAMES = amt
print(f"RUNNING {GAMES} TicTacToe Games")
target = 0
for i in tqdm.trange(GAMES):
thisgame = {"X": {}, "O": {}}
game = tictactoe.new_game()
if i % 2 == 0:
player = "X"
else:
player = "O"
while tictactoe.check_game(game) == "CONTINUE":
if not game in poss[player].keys():
spaces = []
for s_index, space in enumerate(game):
if space == " ":
for s in range(conf["amt_empty"]):
spaces.append(s_index)
poss[player][game] = spaces
thisgame[player][game] = poss[player][game][random.randint(0, len(poss[player][game])-1)]
game = tictactoe.move(game, thisgame[player][game], player)
if player == "X":
player = "O"
else:
player = "X"
winner = tictactoe.check_game(game)
if winner != "DRAW":
for k,v in thisgame[winner].items():
poss[winner][k].append(v)
poss[winner][k].append(v)
if winner == "X":
loser = "O"
else:
loser = "X"
for k,v in thisgame[loser].items():
if len(poss[loser][k]) > 1:
poss[loser][k].remove(v)
else:
for k,v in thisgame["X"].items():
poss["X"][k].append(v)
for k,v in thisgame["O"].items():
poss["O"][k].append(v)
write_poss(poss)
def remove() -> None:
f = open("poss.txt", "w")
f.write("")
f.close()
def game_result(moves: dict, player: str, result: str, poss: dict, conf: dict) -> None:
if player == "X":
ai = "O"
else:
ai = "X"
if result != ai and result != "DRAW": # AI LOST
for k,v in moves.items():
if not k in poss[ai].keys():
sp = []
for si, i in enumerate(k):
if i == " ":
for s in range(conf["amt_empty"]):
sp.append(si)
sp.remove(v)
poss[ai][k] = sp
else:
poss[ai][k].remove(v)
elif result == "DRAW": # AI DREW
for k,v in moves.items():
if not k in poss[ai].keys():
sp = []
for si, i in enumerate(k):
if i == " ":
for s in range(conf["amt_empty"]):
sp.append(si)
sp.append(v)
poss[ai][k] = sp
else:
poss[ai][k].append(v)
else: # AI WON
for k,v in moves.items():
if not k in poss[ai].keys():
sp = []
for si, i in enumerate(k):
if i == " ":
for s in range(conf["amt_empty"]):
sp.append(si)
sp.append(v)
sp.append(v)
poss[ai][k] = sp
else:
poss[ai][k].append(v)
poss[ai][k].append(v)
write_poss(poss)
def train_thread(threads: int, amt: int):
ts = []
for i in range(threads):
t = threading.Thread(target=train, args=[100], daemon=True)
ts.append(t)
for t in ts:
t.start()
t.join()
print("Created New Thread")