def process_gametree()

in pachi_py/pachi/tools/sgf2gtp.py [0:0]


def process_gametree(gametree, fout):
    # cursor for tree traversal
    c = gametree.cursor()
    # first node is the header
    header = c.node

    handicap = get_atr(header, 'HA')
    board_size = int(get_atr(header, 'SZ') or 19)
    komi = get_atr(header, 'KM')
    player_next, player_other = "B", "W"
    setup_black = get_setup(header, 'AB')
    setup_white = get_setup(header, 'AW')

    print >>fout, "boardsize", board_size
    print >>fout, "clear_board"
    if komi:
        print >>fout, "komi", komi
    if handicap and handicap != '0':
        print >>fout, "fixed_handicap", handicap
        player_next, player_other = player_other, player_next
    if setup_black:
	for item in setup_black:
	    x, y = item
	    if x >= 'i':
		x = chr(ord(x)+1)
	    y = str(col2num(y, board_size))
	    print >>fout, "play B", x+y
    if setup_white:
	for item in setup_white:
	    x, y = item
	    if x >= 'i':
		x = chr(ord(x)+1)
	    y = str(col2num(y, board_size))
	    print >>fout, "play W", x+y

    def print_game_step(coord):
        if is_pass_move(coord, board_size):
            print >>fout, "play", player_next, "pass"
        else:
            x, y = coord
            # The reason for this incredibly weird thing is that
            # the GTP protocol excludes `i` in the coordinates
            # (do you see any purpose in this??)
            if x >= 'i':
                x = chr(ord(x)+1)
            y = str(col2num(y, board_size))
            print >>fout, "play", player_next, x+y

    movenum = 0
    # walk the game tree forward
    while 1:
        # sgf2gtp.pl ignores n = 0
        if c.atEnd or (args['n'] and movenum >= args['n']):
            break
        c.next()
        movenum += 1

        coord = get_atr(c.node, player_next)
        if coord != None:
            print_game_step(coord)
        else:
            # MAYBE white started?
            # or one of the players plays two time in a row
            player_next, player_other = player_other, player_next
            coord = get_atr(c.node, player_next)
            if coord != None:
                print_game_step(coord)
            else:
                # TODO handle weird sgf files better
                raise UnknownNode

        player_next, player_other = player_other, player_next

    if args['g']:
        print >>fout, "genmove", player_next