Пример #1
0
def main():
    # Initialize the board set as the opening position
    board_set: Set[str] = set()
    board_set.add(chess.Board().epd(en_passant="xfen"))

    # Find all boards that can be reached in num_half_turns moves
    num_half_turns = 1
    color_to_play = chess.WHITE
    for i in range(num_half_turns):
        next_turn_boards = populate_next_board_set(board_set,
                                                   not color_to_play)
        board_set |= set().union(*next_turn_boards.values())
        color_to_play = not color_to_play

    # Calculate and cache scores for all moves on all boards in the set
    for board_epd in tqdm(
            board_set,
            desc=f'Scoring moves from {len(board_set)} early-game board states',
            unit='boards'):
        board = chess.Board(board_epd)
        if board.king(chess.WHITE) and board.king(chess.BLACK):
            board.turn = not board.turn
            score = memo_calc_score(board=board)
            board.turn = not board.turn
            boards_in_cache.add(board.epd(en_passant="xfen"))
            for move in moves_without_opponent_pieces(
                    board) + pawn_capture_moves_on(board) + [
                        chess.Move.null()
                    ]:
                memo_calc_score(board=board, move=move, prev_turn_score=-score)

    # Store the cache as a json file
    with open('strangefish/score_cache.json', 'w') as file:
        json.dump({
            'cache': score_cache,
            'boards': list(boards_in_cache)
        }, file)

    # Shut down Stockfish
    try:
        engine.quit()
    except chess.engine.EngineTerminatedError:
        pass
Пример #2
0
def get_moves(board: chess.Board) -> List[chess.Move]:
    """ Accounts for the ability to castle through check. """
    return list(set(board.pseudo_legal_moves) | set(move for move in util.moves_without_opponent_pieces(board) if util.is_psuedo_legal_castle(board, move)))
Пример #3
0
def generate_moves_without_opponent_pieces(board: chess.Board) -> Iterable[chess.Move]:
    for move in moves_without_opponent_pieces(board):
        yield move
    for move in pawn_capture_moves_on(board):
        yield move
    yield chess.Move.null()