-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem.py
102 lines (95 loc) · 3.61 KB
/
Problem.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
# Copyright (C) 2013 Alexey Vyskubov ([email protected])
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License version 2 as published by the
# Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# The license is currently available on the Internet at:
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
from Constants import Color
import sgflib
import random
class Problem(object):
def __init__(self, sgf_string):
# FIXME: exception handling
parser = sgflib.SGFParser(sgf_string)
collection = parser.parse()
self._cursor = collection.cursor()
node = self._cursor.node
data = node.data
# FIXME: support for other board sizes
assert(data['SZ'].data == ['19'])
# FIXME: handle failures below
assert(data['GM'].data == ['1'])
assert(data['FF'].data == ['4'])
if 'HA' in data.keys():
assert(data['HA'].data == ['0'])
self._setup = {}
self._setup[Color.B] =\
[(Problem._char_to_index(b[0]), Problem._char_to_index(b[1]))
for b in data['AB']]
self._setup[Color.W] =\
[(Problem._char_to_index(w[0]), Problem._char_to_index(w[1]))
for w in data['AW']]
self._wrong = False
self._over = False
def get_setup(self):
return self._setup
def is_over(self):
return self._over
def is_wrong(self):
return self._wrong
def get_reply(self, move_x, move_y):
self._check_if_wrong(move_x, move_y)
return self._reply
@staticmethod
def _char_to_index(ch):
assert(len(ch) == 1)
# FIXME: handle bad results
return ord(ch) - ord('a')
def _check_if_wrong(self, move_x, move_y):
self._reply = None
children = self._cursor.children
if len(children) < 0:
self._over = true
return
# var stands for "variation"
for var in xrange(len(children)):
move = children[var].data['B'].data[0]
var_x = Problem._char_to_index(move[0])
var_y = Problem._char_to_index(move[1])
if (var_x, var_y) == (move_x, move_y):
break
else: # variation is not found
self._wrong = True
self._over = True
return
node = self._cursor.next(var)
# TODO: other Uligo and GoGrinder compatible ways to detect the wrong
# variation
# TODO: variation marked as wrong only in the last move of the variation
if 'WV' in node.data.keys() or 'TR' in node.data.keys():
self._wrong = True
children = self._cursor.children
replies = len(children)
if replies == 0:
self._over = True
return
var = random.randint(0, replies - 1)
node = self._cursor.next(var)
reply = node.data['W'].data[0]
reply_x = Problem._char_to_index(reply[0])
reply_y = Problem._char_to_index(reply[1])
if len(self._cursor.children) == 0:
self._wrong = True
self._over = True
self._reply = (reply_x, reply_y)