-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
58 lines (44 loc) · 1.33 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
import pyspiel
import numpy as np
import mcts as MCTS
from gui import GUI
gui = GUI()
gui.draw_lines()
def print_board(state):
if not state.is_terminal():
gui.update_board(str(state) + "\n")
print(state.observation_string())
print("-------------")
def get_human_action(state):
while True:
try:
legal_actions = state.legal_actions()
action = int(input(f"Enter you move {str(legal_actions)} : "))
if action in legal_actions:
return action
else:
print("Invalid move, Try again")
except ValueError:
print("Should be numeric value")
def play_game(game, human_player):
state = game.new_initial_state()
while not state.is_terminal():
print_board(state)
current_player = state.current_player()
if current_player == human_player:
action = get_human_action(state)
else:
action = MCTS.select_action(state)
state.apply_action(action)
print_board(state)
if state.returns()[human_player] > 0:
print("Human Wins!")
elif state.returns()[human_player] < 0:
print("AI Wins!")
else:
print("Draw")
def main():
game = pyspiel.load_game("tic_tac_toe")
human_player = 1
play_game(game, human_player)
main()