summaryrefslogtreecommitdiff
path: root/tsumego
blob: 5317e9ad997474b02698552483f5e89872473879 (plain)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#!/usr/bin/env python3

import sys
import sgf
import string
import random
from random import randint
import os

class Board:

    board = {}
    xs = 0
    xe = 19
    ys = 0
    ye = 19
    to_play = "B"
    finished = False
    last_move = ""

    def to_move(self):
        if self.to_play == "B":
            return "\u25ef"
        return "\u2b24"

    def board_symbol(self, x, y):
        if x+y == self.last_move:
            pre = ""
        else:
            pre = ""
        if x+y in self.board:
            if self.board[x+y] == "W":
                if x=="a":
                    return " "+pre+"\u2b24 "
                else:
                    return "─"+pre+"\u2b24 "
            if self.board[x+y] == "B":
                if x=="a":
                    return " "+pre+"\u25ef "
                else:
                    return "─"+pre+"\u25ef "
        if (y=="d") or (y=="p") or (y=="j"):
            if (x=="d") or (x=="p") or (x=="j"):
                return "─*─"
        if (x=="a"):
            if (y=="s"):
                return " └─"
            if (y=="a"):
                return " ┌─"
            return " ├─"
        if (x=="s"):
            if (y=="s"):
                return "─┘ "
            if (y=="a"):
                return "─┐ "
            return "─┤ "
        if (y=="s"):
            return "─┴─"
        if (y=="a"):
            return "─┬─"
        return "─┼─"

    def l2n(self,l):
        return str(string.ascii_lowercase[0:19].index(l)+1)

    def n2l(self,n):
        return string.ascii_lowercase[n-1]

    def check_liberties(self,x,y,c,checked):
        k = self.n2l(x)+self.n2l(y)
        if k in checked:
            return 0
        checked.append(k)
        if not k in self.board:
            return 1
        if self.board[k] == c:
            return self.liberties(x, y, c, checked)
        return 0

    def liberties(self,x,y,c,checked):
        lib = 0
        if x > 1:
            lib += self.check_liberties(x-1, y, c, checked)
        if y > 1:
            lib += self.check_liberties(x, y-1, c, checked)
        if x < 19:
            lib += self.check_liberties(x+1, y, c, checked)
        if y < 19:
            lib += self.check_liberties(x, y+1, c, checked)
        return lib

    def process_captures(self,c):
        captured = []
        for x in range(1,20):
            for y in range(1,20):
                k = self.n2l(x)+self.n2l(y)
                if not k in self.board:
                    continue
                if self.board[k] == c:
                    if self.liberties(x,y,c,[]) == 0:
                        captured.append(k)
        for capture in captured:
            del self.board[capture]

    def add_node(self,node):
        for key in node.properties.keys():
            val = node.properties[key]
            if key == "AB":
                for pos in val:
                    self.board[pos] = "B"
            if key == "AW":
                for pos in val:
                    self.board[pos] = "W"
            if key == "B":
                for pos in val:
                    self.board[pos] = "B"
                self.process_captures("W")
                self.to_play = "W"
                self.last_move = pos
            if key == "W":
                for pos in val:
                    self.board[pos] = "W"
                self.process_captures("B")
                self.to_play = "B"
                self.last_move = pos
            if key == "PL":
                if val[0] == "1" or val[0] == "b" or val[0] == "B":
                    self.to_play = "B"
                if val[0] == "2" or val[0] == "w" or val[0] == "W":
                    self.to_play = "W"
            if key == "C":
                print("---> "+val[0])
            if key == "DI":
                print("Level "+val[0])

    def is_move(self,node):
        for key in node.properties.keys():
            val = node.properties[key]
            if key == "B" or key == "W":
                return True
        return False

    def add_next(self,nmoves):

        nm = 0

        while 1:

            # Decide which node comes next
            if not self.current_node.variations:
                n = self.current_node.next
                if not self.current_node.next:
                    self.finished = True
            else:
                i = randint(0,len(self.current_node.variations)-1)
                n = self.current_node.variations[i]

            if self.finished:
                print("Finished!")
                return

            if self.is_move(n):
                if nm == nmoves:
                    return
                nm += 1

            self.add_node(n)
            self.current_node = n

    def __init__(self,root):
        self.current_node = root
        self.add_node(root)

    def set_node(self,root):
        self.current_node = root
        self.add_node(root)

    def fit_board(self):
        self.ys = 0
        self.ye = 19
        self.xs = 0
        self.xe = 19

        for y in string.ascii_lowercase[0:18]:
            blank = 1
            for x in string.ascii_lowercase[0:18]:
                if x+y in self.board:
                    blank = 0
                    break
            if not blank:
                break
            self.ys += 1

        for y in string.ascii_lowercase[18:0:-1]:
            blank = 1
            for x in string.ascii_lowercase[0:18]:
                if x+y in self.board:
                    blank = 0
                    break
            if not blank:
                break
            self.ye -= 1

        for x in string.ascii_lowercase[0:18]:
            blank = 1
            for y in string.ascii_lowercase[0:18]:
                if x+y in self.board:
                    blank = 0
                    break
            if not blank:
                break
            self.xs += 1

        for x in string.ascii_lowercase[18:0:-1]:
            blank = 1
            for y in string.ascii_lowercase[0:18]:
                if x+y in self.board:
                    blank = 0
                    break
            if not blank:
                break
            self.xe -= 1

        if self.xs > 0:
            self.xs -= 1
        if self.ys > 0:
            self.ys -= 1
        if self.xe < 19:
            self.xe += 1
        if self.ye < 19:
            self.ye += 1

    def header(self):
        print("     ",end="")
        for x in string.ascii_lowercase[self.xs:self.xe]:
            print(x+"  ",end="")
        print("")

    def show(self):
        self.header()
        for y in string.ascii_lowercase[self.ys:self.ye]:
            print(self.l2n(y).rjust(3)+" ",end="")
            for x in string.ascii_lowercase[self.xs:self.xe]:
                print(self.board_symbol(x,y),end="")
            print(" "+self.l2n(y))
        self.header()

    def find_variation(self,move):

        # Is it in one of the variations?
        for var in self.current_node.variations:
            if not self.to_play in var.properties:
                print("No move in this variation!")
                print(var.properties)
            if var.properties[self.to_play][0] == move:
                return var

        # Is it just the next move?
        if self.current_node.next.properties[self.to_play][0] == move:
            return self.current_node.next

        print("Not found!")

def n2l(n):
    return string.ascii_lowercase[n-1]


def coord_to_sgf(a):
    n2 = ''.join([i for i in a if i.isdigit()])
    if not n2:
        return
    numbers = int(n2)
    letters = ''.join([i for i in a if not i.isdigit()])
    if not letters:
        return
    return letters+n2l(numbers)

if len(sys.argv) < 2:
    dn = "/home/taw/tsumego/Chikun_v1/"
    fn = dn+random.choice(os.listdir(dn))
else:
    fn = sys.argv[1]

with open(fn) as f:
    c = sgf.parse(f.read())

i=randint(0,len(c.children)-1)
print(fn+" puzzle "+str(i))

puzzle = c.children[i]
board = Board(puzzle.root)
board.add_next(0)
board.fit_board()
board.show()
print(board.to_move()+" to move.")

while not board.finished:

    ans = input("-> ")
    move = coord_to_sgf(ans)

    var = board.find_variation(move)
    if not var:
        print("Incorrect.")
        exit(0)

    board.set_node(var)
    board.add_next(1)
    board.show()