def sgf_part_from_node(board, node_id): part = ';' node = board.tree[node_id] if node.action == NODE_BLACK: part += 'B[%s]' % coord_to_sgf(node.move, board.size) elif node.action == NODE_WHITE: part += 'W[%s]' % coord_to_sgf(node.move, board.size) elif node.action == NODE_EDIT: edits = defaultdict(list) for coord, color in node.edits.items(): edits[color].append(coord_to_sgf(int(coord), board.size)) ab = ''.join('[%s]' % c for c in edits[BLACK]) aw = ''.join('[%s]' % c for c in edits[WHITE]) ae = ''.join('[%s]' % c for c in edits[EMPTY]) if ab: part += 'AB%s' % ab if aw: part += 'AW%s' % aw if ae: part += 'AE%s' % ae part += _part_symbols(board, node, SYMBOL_TRIANGLE) part += _part_symbols(board, node, SYMBOL_SQUARE) part += _part_symbols(board, node, SYMBOL_CIRCLE) part += _part_labels(board, node) for child in node.children: p = sgf_part_from_node(board, child) if len(node.children) == 1: part += p else: part += '(' + p + ')' return part
def _part_symbols(board, node, symbol): symbols = ['[%s]' % coord_to_sgf(int(coord), board.size) for coord, sym in node.symbols.items() if sym == symbol] return (symbol + ''.join(symbols)) if symbols else ''
def _part_labels(board, node): labels = ['[%s:%s]' % (coord_to_sgf(int(coord), board.size), lbl) for coord, lbl in node.labels.items()] return ('LB' + ''.join(labels)) if labels else ''
def test_coord_to_sgf(): assert coord_to_sgf(coord2d(1, 1, 19), 19) == 'aa' assert coord_to_sgf(coord2d(3, 1, 19), 19) == 'ca' assert coord_to_sgf(coord2d(4, 4, 19), 19) == 'dd' assert coord_to_sgf(coord2d(4, 16, 19), 19) == 'dp' assert coord_to_sgf(coord2d(19, 19, 19), 19) == 'ss'