-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
103 lines (92 loc) · 2.93 KB
/
__init__.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
"""
Interface for
https://github.com/shuyo/misc/blob/master/tricolour/tricolore.py
"""
from tricolore import Game, reverse, put, opposite, montecarlo_ucb
from enum import *
VERBOSE = False
OUTPUT_IMAGE = False
from renderer import Renderer
renderer = Renderer()
class MontecarloPlayer(object):
name = 'nishio:montecarlo'
def __init__(self, side):
self.game = Game()
if side == 'RED':
self.side = RED
elif side == 'BLUE':
self.side = BLUE
else:
raise AssertionError
self.side_s = side
self.opposite = opposite(self.side)
if OUTPUT_IMAGE:
renderer.draw(self.game.map)
def move(self, pos, color):
assert self.game.next == self.opposite
if color == 'WHITE':
color = reverse(self.opposite)
elif color == 'PASS':
if VERBOSE:
print 'move called:', pos, color
self.game.print_map()
self.game.switch_user()
return
else:
color = self.opposite
y, x = pos
put(self.game, (x, y), color)
if VERBOSE:
print 'move called:', pos, color
self.game.print_map()
self.game.switch_user()
if OUTPUT_IMAGE:
renderer.draw(self.game.map)
def nextmove(self):
#assert self.game.next == self.side
if self.game.next != self.side:
# opponent did pass
self.game.switch_user()
actions = self.game.get_possible_actions()
if not actions:
self.game.switch_user()
return ('PASS', None, None)
pos, color = montecarlo_ucb(actions, self.game)
put(self.game, pos, color)
self.game.switch_user()
x, y = pos
if color == self.side:
ret = ('MOVE', (y, x), self.side_s)
else:
ret = ('MOVE', (y, x), 'WHITE')
if VERBOSE:
print 'nextmove called, returns:', ret
self.game.print_map()
if OUTPUT_IMAGE:
renderer.draw(self.game.map)
return ret
class Human(MontecarloPlayer):
def nextmove(self):
#assert self.game.next == self.side
if self.game.next != self.side:
# opponent did pass
self.game.switch_user()
actions = self.game.get_possible_actions()
if not actions:
self.game.switch_user()
return ('PASS', None, None)
pos, color = montecarlo_ucb(actions, self.game)
self.game.print_map()
print pos, color
pos, color = input('(pos, color)>>>')
put(self.game, pos, color)
self.game.switch_user()
x, y = pos
if color == self.side:
ret = ('MOVE', (y, x), self.side_s)
else:
ret = ('MOVE', (y, x), 'WHITE')
if VERBOSE:
print 'nextmove called, returns:', ret
self.game.print_map()
return ret