-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhuman_ui.py
83 lines (73 loc) · 2.39 KB
/
human_ui.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
from __future__ import print_function
from omok_env import OmokEnv
from mcts_puct import MCTS
import numpy as np
HISTORY = 2
BOARD_SIZE = 9 # 9x9 ~ 15x15
N_SIMUL = 50000 # number of simulations for 1 move
GAME = 1
COLUMN = {"a": 0, "b": 1, "c": 2,
"d": 3, "e": 4, "f": 5,
"g": 6, "h": 7, "i": 8,
"j": 9, "k": 10, "l": 11,
"m": 12, "n": 13, "o": 14}
class HumanAgent:
def get_action(self):
last_str = str(BOARD_SIZE)
for k, v in COLUMN.items():
if v == BOARD_SIZE - 1:
last_str += k
break
move_target = input('1a ~ {}: '.format(last_str)).strip().lower()
row = int(move_target[:1]) - 1
col = COLUMN[move_target[1:2]]
action = row * BOARD_SIZE + col
return action
class HumanUI:
def __init__(self):
self.human = HumanAgent()
self.ai = MCTS(BOARD_SIZE, HISTORY, N_SIMUL)
def get_action(self, state, board, idx):
if idx % 2 == 0:
action = self.human.get_action()
else:
action = self.ai.get_action(state, board)
return action
def main():
env = OmokEnv(BOARD_SIZE, HISTORY)
manager = HumanUI()
result = {'Black': 0, 'White': 0, 'Draw': 0}
for g in range(GAME):
print('########## Game: {} ##########'.format(g + 1))
state, board = env.reset()
done = False
idx = 0
while not done:
env.render()
# start simulations
action = manager.get_action(state, board, idx)
state, board, z, done = env.step(action)
idx += 1
if done:
if z == 1:
result['Black'] += 1
elif z == -1:
result['White'] += 1
else:
result['Draw'] += 1
# render & reset tree
env.render()
manager.ai.reset_tree()
# result
print('')
print('=' * 20, " {} Game End ".format(g + 1), '=' * 20)
blw, whw, drw = result['Black'], result['White'], result['Draw']
stat = (
'Black Win: {} White Win: {} Draw: {} Winrate: {:0.1f}%'.format(
blw, whw, drw,
1 / (1 + np.exp(whw / (g + 1)) / np.exp(blw / (g + 1))) * 100))
print(stat, '\n')
if __name__ == '__main__':
np.set_printoptions(suppress=True)
np.random.seed(42)
main()