Пример #1
0
def getOpenings (board):
    """ Return a tuple (move, weight, games, score) for each opening move
        in the given position. The weight is proportional to the probability
        that a move should be played. By convention, games is the number of
        times a move has been tried, and score the number of points it has
        scored (with 2 per victory and 1 per draw). However, opening books
        aren't required to keep this information. """

    default_path = os.path.join(addDataPrefix("pychess_book.bin"))
    path = conf.get("opening_file_entry", default_path) 

    openings = []
    if not os.path.isfile(path):
        return openings

    with open(path, "rb") as bookFile:
        key = board.hash
        # Find the first entry whose key is >= the position's hash
        bookFile.seek(0, os.SEEK_END)
        lo, hi = 0, bookFile.tell() // 16 - 1
        if hi < 0:
            return openings
        while lo < hi:
            mid = (lo + hi) // 2
            bookFile.seek(mid * 16)
            entry = bookFile.read(entrysize)
            if len(entry) != entrysize:
                return openings
            entry = BookEntry._make(entrystruct.unpack(entry))
            if entry.key < key:
                lo = mid + 1
            else:
                hi = mid

        bookFile.seek(lo * 16)
        while True:
            entry = bookFile.read(entrysize)
            if len(entry) != entrysize:
                return openings
            entry = BookEntry._make(entrystruct.unpack(entry))
            if entry.key != key:
                break
            mv = parsePolyglot(board, entry.move)
            openings.append( ( mv, entry.weight, entry.games, entry.score ) )
    return openings
Пример #2
0
def getOpenings(board):
    """ Return a tuple (move, weight, games, score) for each opening move
        in the given position. The weight is proportional to the probability
        that a move should be played. By convention, games is the number of
        times a move has been tried, and score the number of points it has
        scored (with 2 per victory and 1 per draw). However, opening books
        aren't required to keep this information. """

    default_path = os.path.join(addDataPrefix("pychess_book.bin"))
    path = conf.get("opening_file_entry", default_path)

    openings = []
    if not os.path.isfile(path):
        return openings

    with open(path, "rb") as bookFile:
        key = board.hash
        # Find the first entry whose key is >= the position's hash
        bookFile.seek(0, os.SEEK_END)
        lo, hi = 0, bookFile.tell() // 16 - 1
        if hi < 0:
            return openings
        while lo < hi:
            mid = (lo + hi) // 2
            bookFile.seek(mid * 16)
            entry = bookFile.read(entrysize)
            if len(entry) != entrysize:
                return openings
            entry = BookEntry._make(entrystruct.unpack(entry))
            if entry.key < key:
                lo = mid + 1
            else:
                hi = mid

        bookFile.seek(lo * 16)
        while True:
            entry = bookFile.read(entrysize)
            if len(entry) != entrysize:
                return openings
            entry = BookEntry._make(entrystruct.unpack(entry))
            if entry.key != key:
                break
            mv = parsePolyglot(board, entry.move)
            openings.append((mv, entry.weight, entry.games, entry.score))
    return openings
Пример #3
0
def getOpenings(board):
    """ Return a tuple (move, weight, learn) for each opening move
        in the given position. The weight is proportional to the probability
        that a move should be played. By convention, games is the number of
        times a move has been tried, and score the number of points it has
        scored (with 2 per victory and 1 per draw). However, opening books
        aren't required to keep this information. """

    openings = []
    if not bookfile:
        return openings

    with open(path, "rb") as bookFile:
        key = board.hash
        # Find the first entry whose key is >= the position's hash
        bookFile.seek(0, os.SEEK_END)
        low, high = 0, bookFile.tell() // 16 - 1
        if high < 0:
            return openings
        while low < high:
            mid = (low + high) // 2
            bookFile.seek(mid * 16)
            entry = bookFile.read(entrysize)
            if len(entry) != entrysize:
                return openings
            entry = BookEntry._make(entrystruct.unpack(entry))
            if entry.key < key:
                low = mid + 1
            else:
                high = mid

        bookFile.seek(low * 16)
        while True:
            entry = bookFile.read(entrysize)
            if len(entry) != entrysize:
                return openings
            entry = BookEntry._make(entrystruct.unpack(entry))
            if entry.key != key:
                break
            move = parsePolyglot(board, entry.move)
            openings.append((move, entry.weight, entry.learn))
    return openings
Пример #4
0
def getOpenings(board):
    """ Return a tuple (move, weight, learn) for each opening move
        in the given position. The weight is proportional to the probability
        that a move should be played. By convention, games is the number of
        times a move has been tried, and score the number of points it has
        scored (with 2 per victory and 1 per draw). However, opening books
        aren't required to keep this information. """

    openings = []
    if not bookfile:
        return openings

    with open(path, "rb") as bookFile:
        key = board.hash
        # Find the first entry whose key is >= the position's hash
        bookFile.seek(0, os.SEEK_END)
        low, high = 0, bookFile.tell() // 16 - 1
        if high < 0:
            return openings
        while low < high:
            mid = (low + high) // 2
            bookFile.seek(mid * 16)
            entry = bookFile.read(entrysize)
            if len(entry) != entrysize:
                return openings
            entry = BookEntry._make(entrystruct.unpack(entry))
            if entry.key < key:
                low = mid + 1
            else:
                high = mid

        bookFile.seek(low * 16)
        while True:
            entry = bookFile.read(entrysize)
            if len(entry) != entrysize:
                return openings
            entry = BookEntry._make(entrystruct.unpack(entry))
            if entry.key != key:
                break
            move = parsePolyglot(board, entry.move)
            openings.append((move, entry.weight, entry.learn))
    return openings