def show_sgf_file(pathname, move_number): f = open(pathname, "rb") sgf_src = f.read() f.close() try: sgf_game = sgf.Sgf_game.from_bytes(sgf_src) except ValueError: raise Exception("bad sgf file") try: board, plays = sgf_moves.get_setup_and_moves(sgf_game) except ValueError as e: raise Exception(str(e)) if move_number is not None: move_number = max(0, move_number-1) plays = plays[:move_number] for colour, move in plays: if move is None: continue row, col = move try: board.play(row, col, colour) except ValueError: raise Exception("illegal move in sgf file") print(ascii_boards.render_board(board)) print()
def compare_boards(b1, b2): """Check whether two boards have the same position. returns a pair (position_is_the_same, message) """ if b1.side != b2.side: raise ValueError("size is different: %s, %s" % (b1.side, b2.side)) differences = [] for row, col in b1.board_points: if b1.get(row, col) != b2.get(row, col): differences.append((row, col)) if not differences: return True, None msg = "boards differ at %s" % " ".join(map(format_vertex, differences)) try: msg += "\n%s\n%s" % (ascii_boards.render_board(b1), ascii_boards.render_board(b2)) except Exception: pass return False, msg
def coerce(board, diagram): try: return board, ascii_boards.interpret_diagram(diagram, board.side) except ValueError: return ascii_boards.render_board(board), diagram
root_node = game.get_root() b_player = root_node.get("PB") w_player = root_node.get("PW") bcurrent = boards.Board(19) board = np.full((board_size, board_size), '.') states = ['19\n'] for i, node in enumerate(game.get_main_sequence()): move = node.get_move() if (move[1] is not None): bcurrent.play(move[1][0], move[1][1], move[0]) player, stonePos = node.get_move() # states.append(f'\n{player} {18-stonePos[0]} {stonePos[1]}\n') state = ascii_boards.render_board(bcurrent) state = ''.join(re.findall('[.o#][ \n]', state)) state = re.sub('#', 'b', state) state = re.sub('o', 'w', state) states.append(state) del states[1:-1] with open( os.path.join(destination, os.path.basename(filename).replace('sgf', 'txt')), 'w') as f: f.writelines(states) # np.savetxt(filename.split('.')[0], np.hstack(tuple(board_seg)), fmt="%s")