Exemplo n.º 1
0
def negamax(depth: int, board: chess.Board, move: chess.Move) -> float:
    if depth == 0:
        return evaluation.evaluate_board_state(
            board, str(move), [evaluation.TAKE_PIECES_STRATEGY])

    possible_moves = board.legal_moves
    best_move = -1

    for possible_move in possible_moves:
        move = chess.Move.from_uci(str(possible_move))
        board.push(move)

        for pos in possible_moves:
            best_move = max(best_move, -negamax(depth - 1, board, pos))

        board.pop()

        return best_move
Exemplo n.º 2
0
def min_max(depth : int, board : chess.Board, is_maximizing : bool, move : chess.Move) -> float:
    if depth == 0 :
        return -evaluation.evaluate_board_state(board, str(move),[evaluation.PAWN_ADVANCE_STRATEGY])
    possible_moves = board.legal_moves
    if is_maximizing :
        best_move = -1
        for possible_move in possible_moves :
            move = chess.Move.from_uci(str(possible_move))
            board.push(move)
            for pos in possible_moves :
                best_move = max(best_move, min_max(depth - 1, board, not is_maximizing, pos))
            board.pop()
        return best_move
    else :
        best_move = 1
        for possible_move in possible_moves :
            move = chess.Move.from_uci(str(possible_move))
            board.push(move)
            for pos in possible_moves :
                best_move = min(best_move, min_max(depth - 1, board, not is_maximizing, pos))
            board.pop()
        return best_move
def find_end_game(move_list):
    board = chess.Board()
    board = construction.construct_current_state(board, move_list)

    turns_to_endgame = 0

    while not board.is_game_over() and turns_to_endgame < 150:
        move_list.clear()
        max_gain = -1
        best_move = ""

        for move in board.legal_moves:
            current_gain = evaluation.evaluate_board_state(board, str(move))
            if max_gain <= current_gain:
                max_gain = current_gain
                best_move = str(move)

        move_list.insert(0, best_move)
        turns_to_endgame = turns_to_endgame + 1
        current_move = chess.Move.from_uci(best_move)
        board.push(current_move)

    return turns_to_endgame
def alpha_beta(depth: int, board: chess.Board, alpha: int, beta: int,
               is_maximizing: bool, move: chess.Move) -> float:
    if depth == 0:
        return -evaluatin.evaluate_board_state(
            board, str(move), [evaluatin.DEFEND_PIECES_STRATEGY])
    possible_moves = board.legal_moves
    if is_maximizing:
        best_move = -1
        for possible_move in possible_moves:
            move = chess.Move.from_uci(str(possible_move))
            board.push(move)
            for pos in possible_moves:
                best_move = max(
                    best_move,
                    alpha_beta(depth - 1, board, alpha, beta,
                               not is_maximizing, pos))
            board.pop()
            alpha = max(alpha, best_move)
            if beta <= alpha:
                return best_move
        return best_move
    else:
        best_move = 1
        for possible_move in possible_moves:
            move = chess.Move.from_uci(str(possible_move))
            board.push(move)
            for pos in possible_moves:
                best_move = min(
                    best_move,
                    alpha_beta(depth - 1, board, alpha, beta,
                               not is_maximizing, pos))
            board.pop()
            beta = min(beta, best_move)
            if beta <= alpha:
                return best_move
        return best_move
def mistake_checker(move_list, player, mistake_threshold):
    result_list = []
    index = 0

    if len(move_list) < 3:
        return None

    board = chess.Board()

    if player == 1:
        board = construct_current_state(board, move_list[:2])
        move_list.pop(0)
        move_list.pop(0)
        index = index + 3
    else:
        board = construct_current_state(board, move_list[:3])
        move_list.pop(0)
        move_list.pop(0)
        move_list.pop(0)
        index = index + 4

    while True:
        max_move_value_2 = -1
        max_move_value_1 = -1
        max_move_1 = ""
        board_copy = board.copy()
        total_gain = 0

        made_move_1 = move_list[0]
        made_move_value_1 = evaluation.evaluate_board_state(
            board_copy, move_list[0], [
                evaluation.ATTACK_PIECES_STRATEGY,
                evaluation.DEFEND_PIECES_STRATEGY,
                evaluation.KEEP_PIECES_STRATEGY
            ])
        move = chess.Move.from_uci(str(made_move_1))
        board_copy.push(move)
        made_move_value_2 = evaluation.evaluate_board_state(
            board_copy, move_list[1], [
                evaluation.ATTACK_PIECES_STRATEGY,
                evaluation.DEFEND_PIECES_STRATEGY,
                evaluation.KEEP_PIECES_STRATEGY
            ])

        board_copy = board.copy()
        #board_copy.pop()

        for move in board_copy.legal_moves:
            current_move_value = evaluation.evaluate_board_state(
                board_copy, str(move), [
                    evaluation.ATTACK_PIECES_STRATEGY,
                    evaluation.DEFEND_PIECES_STRATEGY,
                    evaluation.KEEP_PIECES_STRATEGY
                ])
            current_move = str(move)

            if current_move_value > max_move_value_1:
                max_move_value_1 = current_move_value
                max_move_1 = str(current_move)

        total_gain = max_move_value_1 - made_move_value_1
        move = chess.Move.from_uci(str(max_move_1))
        board_copy.push(move)

        for move in board_copy.legal_moves:
            current_move_value = evaluation.evaluate_board_state(
                board_copy, str(move), [
                    evaluation.ATTACK_PIECES_STRATEGY,
                    evaluation.DEFEND_PIECES_STRATEGY,
                    evaluation.KEEP_PIECES_STRATEGY
                ])

            if current_move_value > max_move_value_2:
                max_move_value_2 = current_move_value

        total_gain = total_gain - (made_move_value_2 - max_move_value_2)

        if made_move_value_1 <= max_move_value_1 - mistake_threshold:
            if made_move_1 != max_move_1:
                if made_move_value_2 >= max_move_value_2:
                    result_tuple = (made_move_1, index, total_gain)
                    result_list.append(result_tuple)

        if len(move_list) - 2 == 0:
            worst_mistake = find_worst_mistake(result_list)
            return worst_mistake
        else:
            board = construct_current_state(board.copy(), move_list[:2])
            move_list.pop(0)
            move_list.pop(0)
            index = index + 2
def generate_response_case2(fen: str, list: list) -> str:
    depth = 1
    fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
    board = chess.Board(fen)
    bad_move = mistake_move(list.copy(), len(list) % 2 + 1, 0)
    list_for_min = []
    list_for_alfa = []
    list_for_nega = []
    check1 = all_pieces_on_board(list.copy())
    for index in range(0, len(list)):
        if index == bad_move[1] and check1 == 0 and bad_move[0] is not None:
            board2 = board.copy()
            for p in range(0, index - 1):
                board2.push_uci(list[p])
            negamax_board = board2.copy()
            minmax_board = board2.copy()
            alphabeta_bord = board2.copy()
            i = 0
            while i < 6:
                with Pool(processes=4) as pool:
                    rminmax = pool.apply_async(minmax_algorithm, (
                        depth,
                        minmax_board,
                        True,
                    ))
                    rnegamax = pool.apply_async(negamax_algorithm, (
                        depth + 1,
                        negamax_board,
                    ))
                    ralfabeta = pool.apply_async(alphabeta_algorihm, (
                        depth,
                        alphabeta_bord,
                        True,
                    ))
                    movealfa, valuealfa = ralfabeta.get()
                    movemin, valuemin = rminmax.get()
                    movenega, valuenega = rnegamax.get()
                    list_for_alfa.append((str(movealfa), valuealfa))
                    list_for_min.append((str(movemin), valuemin))
                    list_for_nega.append((str(movenega), valuenega))
                    negamax_board.push(movenega)
                    minmax_board.push(movemin)
                    alphabeta_bord.push(movealfa)
                i += 1
    color = ["w", "b"]
    color_pos = 0
    poz = 0
    if "w" in fen:
        color_pos = 0
        poz = 0
    else:
        color_pos = 1
        poz = 1
    algorithms = [("MinMax", list_for_min, "PAWN_ADVANCE_STRATEGY"),
                  ("NegaMax", list_for_nega, "TAKE_PIECES_STRATEGY"),
                  ("AlfaBeta", list_for_alfa, "DEFEND_PIECES_STRATEGY")]
    list_moves = []
    list_all_moves = []
    list_all_variants = []
    list_strategies = [
        chess_functions.PAWN_ADVANCE_STRATEGY,
        chess_functions.DEFEND_PIECES_STRATEGY,
        chess_functions.TAKE_PIECES_STRATEGY,
        chess_functions.ATTACK_PIECES_STRATEGY,
        chess_functions.KEEP_PIECES_STRATEGY
    ]
    for index_for2 in range(0, len(list)):
        if index_for2 == bad_move[1] - 1:
            n = 0
            color_pos = poz
            for algorithm in algorithms:
                variants = output.create_json_variants(algorithm[0],
                                                       [algorithm[2]])
                n += 1
                m = 0
                for move_algorithm, score in algorithm[1]:
                    if color_pos % 2 == 0:
                        m += 1
                        information_move = output.create_json_information_moves(
                            color[color_pos] + move_algorithm, score)
                        #print("Info White",information_move)
                        #                       output.add_variants(list_moves,variants,[information_move])
                        if m == 1:
                            output.add_variants(list_moves, variants,
                                                [information_move])
                        else:
                            list_moves[-1]['moves'].append(information_move)

                    else:
                        m += 1
                        information_move = output.create_json_information_moves(
                            color[color_pos] + move_algorithm, score)
                        #list_moves[-1]['moves'].append(information_move)
                        if m == 1:
                            output.add_variants(list_moves, variants,
                                                [information_move])
                        else:
                            list_moves[-1]['moves'].append(information_move)
                        #print("Info Black",information_move)
                    color_pos = 1 - color_pos

                list_all_variants.append(variants)
                #print(list_all_variants)
                if n == 3:
                    d = output.create_json_moves(list_all_variants)
                    list_all_moves.append(
                        output.create_json_information_moves_with_variants(
                            color[poz] + list[index_for2],
                            chess_functions.evaluate_board_state(
                                board, list[index_for2], list_strategies),
                            list_moves))

        else:
            list_all_moves.append(
                output.create_json_information_moves(
                    color[poz] + list[index_for2],
                    chess_functions.evaluate_board_state(
                        board, list[index_for2], list_strategies)))
        poz = 1 - poz
        board.push_uci(list[index_for2])
        #d2=output.create_json_information_moves_with_variants(second_move,second_score,list_moves)
    #print(list_all_moves)
    dictionary = output.create_json_moves(list_all_moves)
    #dictionary={"moves":list_all_moves}
    json_main_part = output.create_second_json_main_body(fen, dictionary)
    output.create_json_output2(json_main_part)
    return convert_json_to_string('second_output.json')