-
Notifications
You must be signed in to change notification settings - Fork 0
/
play.py
75 lines (63 loc) · 2.38 KB
/
play.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Alphago reproduction IA course project, ENSEIRB-MATMECA
# COUTHOUIS Fabien - HACHE Louis - Heuillet Alexandre
#############################################################
import time
from players.randomPlayer import RandomPlayer
from players.mctsPlayer import MCTSPlayer
from players.alphazeroPlayer import AlphaZeroPlayer
from goban import Goban
def play(player1, player2, goban, verbose=True):
# Black begins
next_player, other_player = (
player1, player2) if player1.color == goban._BLACK else (player2, player1)
nbmoves = 1
# total real time for each player
totalTime = {p.getPlayerName(): 0 for p in [next_player, other_player]}
if verbose:
print(next_player, other_player)
while not goban.is_game_over():
if verbose:
print("Referee Board:")
print(goban)
print("Before move", nbmoves)
# print("Legal Moves: ", goban.get_legal_actions())
nbmoves += 1
currentTime = time.time()
x, y, color = next_player.getPlayerMove()
totalTime[next_player.getPlayerName()] += time.time() - currentTime
if verbose:
print(next_player.color,
next_player.getPlayerName(), "plays " + str((x, y)))
if not goban.is_valid_move((x, y, color)):
print(other_player, next_player, next_player.color)
print("Problem: illegal move")
break
goban.play((x, y, next_player.color))
other_player.playOpponentMove(x, y)
# Invert players
next_player, other_player = other_player, next_player
if verbose:
print(goban)
print("The game is over")
nbblacks, nbwhites = goban.get_score()
winner = goban.get_winner()
if verbose:
print("Time:", totalTime)
print("Winner: ", end="")
if winner == goban._WHITE:
print("WHITE")
elif winner == goban._BLACK:
print("BLACK")
else:
print("DEUCE")
print("Final is: ", nbwhites, "whites and ", nbblacks, "blacks")
return winner
if __name__ == "__main__":
goban = Goban()
model_path = "saves/test1/best/model" # not well trained
player1, player2 = AlphaZeroPlayer(
goban._WHITE, goban.get_board_size(), model_path), MCTSPlayer(goban._BLACK, goban.get_board_size())
play(player1, player2, goban)