コード例 #1
0
def walkthroughSgf(sgf_contents, sgf_file_path, sgf_file_name,
                   output_file_path, completed_dir, board_size, ownership):
    sgf = gomill.sgf.Sgf_game.from_string(sgf_contents)
    try:
        if sgf.get_size() != board_size:
            print('boardsize not %d, ignoring' % board_size)
            return
        goBoard = GoBoard.GoBoard(board_size)
        if sgf.get_handicap() != None and sgf.get_handicap() != 0:
            print 'handicap not zero, ignoring (' + str(
                sgf.get_handicap()) + ')'
            return
    except:
        print "Error getting handicap. Ignoring this file"
        return
    moveIdx = 0

    #here we attempt to use gnugo to finish the game and get the final ownership.
    #we check the score gnugo gives with the score written in the file, if this score is off
    #by more than difference_threshold then we skip the file. gnugo is often off by 1 because
    #of chinese scoring.
    black_ownership, white_ownership = None, None
    if ownership:
        black_ownership, white_ownership = finish_games.finish_sgf_and_get_ownership(
            sgf_file_path,
            sgf_file_name,
            completed_dir,
            board_size,
            difference_threshold=6,
            year_lowerbound=0
        )  #set year_lowerbound will ignore all games before given year
        if black_ownership is None or white_ownership is None:
            print "Unable to get final ownership for %s" % sgf_file_path
            return

    #all samples from this sgf will be written to this file
    output_file = open(output_file_path, 'wb')

    for it in sgf.main_sequence_iter():
        (color, move) = it.get_move()
        if color != None and move != None:
            (row, col) = move
            addToDataFile(output_file, color, move, goBoard, ownership,
                          black_ownership, white_ownership)
            try:
                goBoard.applyMove(color, (row, col))
            except:
                print "exception caught at move %d" % (moveIdx)
                print "Ignoring the rest of this file"
                output_file.close()
                return
            moveIdx = moveIdx + 1
    output_file.close()
def walkthroughSgf(countOnly, datafile, sgfContents):
    sgf = gomill.sgf.Sgf_game.from_string(sgfContents)
    # print sgf
    if sgf.get_size() != 19:
        print('boardsize not 19, ignoring')
        return
    goBoard = GoBoard.GoBoard(19)
    doneFirstMove = False
    if sgf.get_handicap() != None and sgf.get_handicap() != 0:
        #print 'handicap not zero, ignoring (' + str( sgf.get_handicap() ) + ')'
        #handicappoints = gomill.handicap_layout.handicap_points( sgf.get_handicap(), 19 )
        numhandicap = sgf.get_handicap()
        #print sgf.get_root().get_setup_stones()
        #sys.exit(-1)
        #for move in getHandicapPoints( numhandicap ):
        for set in sgf.get_root().get_setup_stones():
            #print set
            for move in set:
                #print move
                goBoard.applyMove('b', move)
        #sys.exit(-1)
#        print( 'handicap: ' + str(numhandicap) )
        doneFirstMove = True
        #sys.exit(-1)
    # first, count number of moves...
    if countOnly:
        numMoves = 0
        countDoneFirstMove = doneFirstMove
        for it in sgf.main_sequence_iter():
            (color, move) = it.get_move()
            if color != None and move != None:
                #(row,col) = move
                if countDoneFirstMove:
                    numMoves = numMoves + 1
                    #addToDataFile( datafile, color, move, goBoard )
                countDoneFirstMove = True
        return numMoves
    #writeFileHeader( datafile, numMoves, 7, 19, 'int', 1 )
    moveIdx = 0
    for it in sgf.main_sequence_iter():
        (color, move) = it.get_move()
        if color != None and move != None:
            (row, col) = move
            if doneFirstMove:
                addToDataFile(datafile, color, move, goBoard)
            goBoard.applyMove(color, (row, col))
            moveIdx = moveIdx + 1
            doneFirstMove = True
def walkthroughSgf( countOnly, datafile, sgfContents ):
    sgf = gomill.sgf.Sgf_game.from_string( sgfContents )
    # print sgf
    if sgf.get_size() != 19:
        print( 'boardsize not 19, ignoring' )
        return
    goBoard = GoBoard.GoBoard(19)
    doneFirstMove = False
    if sgf.get_handicap() != None and sgf.get_handicap() != 0:
        #print 'handicap not zero, ignoring (' + str( sgf.get_handicap() ) + ')'
        #handicappoints = gomill.handicap_layout.handicap_points( sgf.get_handicap(), 19 )
        numhandicap = sgf.get_handicap()
        #print sgf.get_root().get_setup_stones()
        #sys.exit(-1)
        #for move in getHandicapPoints( numhandicap ):
        for set in sgf.get_root().get_setup_stones():
            #print set
            for move in set:
                #print move
                goBoard.applyMove( 'b', move )
        #sys.exit(-1)
#        print( 'handicap: ' + str(numhandicap) )
        doneFirstMove = True
        #sys.exit(-1)
    # first, count number of moves...
    if countOnly:
        numMoves = 0
        countDoneFirstMove = doneFirstMove
        for it in sgf.main_sequence_iter():
            (color,move) = it.get_move()
            if color != None and move != None:
                #(row,col) = move
                if countDoneFirstMove:
                    numMoves = numMoves + 1
                    #addToDataFile( datafile, color, move, goBoard )
                countDoneFirstMove = True
        return numMoves
    #writeFileHeader( datafile, numMoves, 7, 19, 'int', 1 )
    moveIdx = 0
    for it in sgf.main_sequence_iter():
        (color,move) = it.get_move()
        if color != None and move != None:
            (row,col) = move
            if doneFirstMove:
                addToDataFile( datafile, color, move, goBoard )
            goBoard.applyMove( color, (row,col) )
            moveIdx = moveIdx + 1
            doneFirstMove = True
コード例 #4
0
def walkthroughSgf(datafile, sgfContents):
    sgf = gomill.sgf.Sgf_game.from_string(sgfContents)
    # print sgf
    if sgf.get_size() != 19:
        print('boardsize not 19, ignoring')
        return
    goBoard = GoBoard.GoBoard(19)
    doneFirstMove = False
    if sgf.get_handicap() != None and sgf.get_handicap() != 0:
        #print 'handicap not zero, ignoring (' + str( sgf.get_handicap() ) + ')'
        #handicappoints = gomill.handicap_layout.handicap_points( sgf.get_handicap(), 19 )
        numhandicap = sgf.get_handicap()
        #print sgf.get_root().get_setup_stones()
        #sys.exit(-1)
        #for move in getHandicapPoints( numhandicap ):
        for set in sgf.get_root().get_setup_stones():
            #print set
            for move in set:
                #print move
                goBoard.applyMove('b', move)
        #sys.exit(-1)


#        print( 'handicap: ' + str(numhandicap) )
        doneFirstMove = True
        #sys.exit(-1)
    moveIdx = 0
    for it in sgf.main_sequence_iter():
        (color, move) = it.get_move()
        #        print( 'color ' + str(color) )
        #        print( move )
        if color != None:
            if move == None:
                print("pass")
                move = (19, 0)
            (row, col) = move
            if doneFirstMove and datafile != None:
                addToDataFile(datafile, color, move, goBoard)
            #print 'applying move ' + str( moveIdx )
            if row < 19:
                goBoard.applyMove(color, (row, col))
            #print goBoard
            moveIdx = moveIdx + 1
            doneFirstMove = True
コード例 #5
0
def walkthroughSgf( sgfContents , sgfFilepath, output_file_path):
    sgf = gomill.sgf.Sgf_game.from_string( sgfContents )
    try:
        if sgf.get_size() != BOARD_SIZE:
            print ('boardsize not %d, ignoring' %BOARD_SIZE )
            return
        goBoard = GoBoard.GoBoard(BOARD_SIZE)
        if sgf.get_handicap() != None and sgf.get_handicap() != 0:
            print 'handicap not zero, ignoring (' + str( sgf.get_handicap() ) + ')'
            return
    except:
        print "Error getting handicap. Ignoring this file"
        return
    moveIdx = 0

    #here we attempt to use gnugo to finish the game and get the final ownership.
    #we check the score gnugo gives with the score written in the file, if this score is off
    #by more than difference_treshold then we skip the file. gnugo is often off by 1 because
    #of chinese scoring.
    black_ownership, white_ownership = None, None
    if OWNERSHIP_TARGET:
        black_ownership, white_ownership = finish_games.finish_sgf_and_get_ownership(sgfFilepath, 
            BOARD_SIZE, difference_treshold = 6, year_lowerbound = 0) #set year_lowerbound will ignore all games before given year
        if black_ownership is None or white_ownership is None:
            print "Unable to get final ownership for %s" %sgfFilepath
            return

    #all samples from this sgf will be written to this file
    output_file = open(output_file_path , 'wb')

    for it in sgf.main_sequence_iter():
        (color,move) = it.get_move()
        if color != None and move != None:
            (row,col) = move
            addToDataFile( output_file, color, move, goBoard, black_ownership, white_ownership )
            try:
                goBoard.applyMove( color, (row,col) )
            except:
                print "exception caught at move %d" %(moveIdx)
                print "Ignoring the rest of this file"
                output_file.close()
                return
            moveIdx = moveIdx + 1
    output_file.close()
コード例 #6
0
def walkthroughSgf( datafile, sgfContents ):
    sgf = gomill.sgf.Sgf_game.from_string( sgfContents )
    # print sgf
    if sgf.get_size() != 19:
        print( 'boardsize not 19, ignoring' )
        return
    goBoard = GoBoard.GoBoard(19)
    doneFirstMove = False
    if sgf.get_handicap() != None and sgf.get_handicap() != 0:
        #print 'handicap not zero, ignoring (' + str( sgf.get_handicap() ) + ')'
        #handicappoints = gomill.handicap_layout.handicap_points( sgf.get_handicap(), 19 )
        numhandicap = sgf.get_handicap()
        #print sgf.get_root().get_setup_stones()
        #sys.exit(-1)
        #for move in getHandicapPoints( numhandicap ):
        for set in sgf.get_root().get_setup_stones():
            #print set
            for move in set:
                #print move
                goBoard.applyMove( 'b', move )
        #sys.exit(-1)
#        print( 'handicap: ' + str(numhandicap) )
        doneFirstMove = True
        #sys.exit(-1)
    moveIdx = 0
    for it in sgf.main_sequence_iter():
        (color,move) = it.get_move()
#        print( 'color ' + str(color) )
#        print( move )
        if color != None and move != None:
            (row,col) = move
            if doneFirstMove and datafile != None:
                addToDataFile( datafile, color, move, goBoard )
            #print 'applying move ' + str( moveIdx )
            goBoard.applyMove( color, (row,col) )
            #print goBoard
            moveIdx = moveIdx + 1
            doneFirstMove = True