-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
41 lines (39 loc) · 1.31 KB
/
utils.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
import numpy as np
from MCTS import MCTS
from games import TicTacToe
## Random vs Random
def play_game_random(game,player,state):
while True:
valid_moves=game.get_valid_moves(state)
if np.sum(valid_moves)==0:
return 0
action=np.random.choice(np.where(valid_moves==1)[0])
state=game.get_next_state(state,action,player)
value,terminated=game.get_value_and_terminated(state,action)
if terminated:
if value==1:
return player
else:
return 0
player=game.get_opponent(player)
## MCTS vs Random
def play_game_mcts(game,mcts,player,state):
while True:
if player==1:
valid_moves=game.get_valid_moves(state)
if np.sum(valid_moves)==0:
return 0
action=np.random.choice(np.where(valid_moves==1)[0])
else:
action_probs=mcts.search(state)
action=np.argmax(action_probs)
state=game.get_next_state(state,action,player)
value,terminated=game.get_value_and_terminated(state,action)
if terminated:
if value==1:
print(f"Player {player} wins")
return player
else:
print("Draw")
return 0
player=game.get_opponent(player)