-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_ai.py
62 lines (47 loc) · 1.58 KB
/
train_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
from ai import AI
from game import Game, get_winner, get_player, X, O, is_over
from copy import deepcopy
def train_ai():
ai = AI()
for i in range(10000):
print(f"Playing training game {i + 1}")
game = Game()
last = {
X: {"board": None, "action": None},
O: {"board": None, "action": None}
}
while True:
board_before_action = deepcopy(game.board)
action = ai.choose_action(board_before_action)
player = get_player(board_before_action)
last[player]["board"] = board_before_action
last[player]["action"] = action
game.apply_action(action)
winner = get_winner(game.board)
update_ai(board_before_action, game, winner, ai, action, last)
if is_over(game.board) or winner:
break
for board, action in ai.q.items():
print(board)
print(action)
return ai
def update_ai(board_before_action, game, winner, ai, action, last):
board_after_action = deepcopy(game.board)
other_player = get_player(board_after_action)
if winner:
ai.update_q(board_before_action, action, board_after_action, 1)
ai.update_q(
last[other_player]["board"],
last[other_player]["action"],
board_after_action,
-1
)
return
if last[other_player]["board"]:
ai.update_q(
last[other_player]["board"],
last[other_player]["action"],
board_after_action,
0
)
return