Ejemplo n.º 1
0
    def get_available_attacks(self, current_position, current_board):
        def check_if_offboard(available_attacks):

            for each_move in available_attacks:

                if each_move[0] > 7 or each_move[1] > 7 or each_move[
                        0] < 0 or each_move[1] < 0:

                    available_attacks.remove(each_move)

            return available_attacks

        i = current_position[0]
        j = current_position[1]

        available_attacks = []

        available_attacks.append([i - 1, j - 1])
        available_attacks.append([i - 1, j + 1])

        available_attacks = check_if_offboard(available_attacks)
        all_white_coords = board_evaluation.get_all_white_coords(current_board)

        available_attacks_final = [
            i for i in available_attacks if i not in all_white_coords
        ]

        #  print(available_attacks_final)

        return available_attacks_final
Ejemplo n.º 2
0
    def get_available_moves(self, current_position, current_board):

        available_moves = []

        available_move = []

        def check_if_offboard(avaialbe_moves):

            good_moves = []

            for each_move in avaialbe_moves:

                if each_move[0] < 8 and each_move[1] < 8 and each_move[
                        0] > -1 and each_move[1] > -1:

                    good_moves.append(each_move)

            return good_moves

        #print(current_position)

        if current_position[0] == 1:

            available_move.append(2)
            available_move.append(current_position[1])

            available_moves.append(available_move)

            available_move = []

            available_move.append(3)
            available_move.append(current_position[1])

            available_moves.append(available_move)

        else:

            available_move.append(current_position[0] + 1)
            available_move.append(current_position[1])

            available_moves.append(available_move)

        available_moves = check_if_offboard(available_moves)

        all_white_coords = board_evaluation.get_all_white_coords(current_board)
        all_black_coords = board_evaluation.get_all_black_coords(current_board)

        available_moves_1 = [
            i for i in available_moves if i not in all_white_coords
        ]
        available_moves_final = [
            i for i in available_moves_1 if i not in all_black_coords
        ]

        #  print(available_moves_final)

        return available_moves_final
Ejemplo n.º 3
0
    def get_available_moves(self, current_position, current_board):

        available_moves = []

        #available_move = []

        def check_if_offboard(avaialbe_moves):

            good_moves = []

            for each_move in avaialbe_moves:

                if each_move[0] < 8 and each_move[1] < 8 and each_move[
                        0] > -1 and each_move[1] > -1:

                    good_moves.append(each_move)

            return good_moves

        #print(current_position)

        all_white_coords = board_evaluation.get_all_white_coords(current_board)

        #all_black_attacks = board_evaluation.get_all_black_attacks(current_board)

        i = current_position[0]
        j = current_position[1]

        available_moves_dict = {
            "available_move0": [i + 1, j + 1],
            "available_move1": [i + 1, j - 1],
            "available_move2": [i + 1, j],
            "available_move3": [i - 1, j + 1],
            "available_move4": [i - 1, j - 1],
            "available_move5": [i - 1, j],
            "available_move6": [i, j + 1],
            "available_move7": [i, j - 1]
        }

        available_moves = list(available_moves_dict.values())

        available_moves = check_if_offboard(available_moves)

        available_moves_1 = [
            i for i in available_moves if i not in all_white_coords
        ]

        #available_moves_final = [i for i in available_moves_1 if i not in all_black_attacks]

        #print("White king can move to: " + str(available_moves_1))

        return available_moves_1
Ejemplo n.º 4
0
    def get_available_moves(self, current_position, current_board):
        def check_if_offboard(avaialbe_moves):

            good_moves = []

            for each_move in avaialbe_moves:

                if each_move[0] < 8 and each_move[1] < 8 and each_move[
                        0] > -1 and each_move[1] > -1:

                    good_moves.append(each_move)

            return good_moves

        all_white_coords = board_evaluation.get_all_white_coords(current_board)
        # all_black_coords = board_evaluation.get_all_black_coords(current_board)

        #print(current_position)

        i = current_position[0]
        j = current_position[1]

        available_moves_dict = {
            "available_move0": [i + 2, j + 1],
            "available_move1": [i + 2, j - 1],
            "available_move2": [i - 2, j - 1],
            "available_move3": [i - 2, j + 1],
            "available_move4": [i + 1, j + 2],
            "available_move5": [i + 1, j - 2],
            "available_move6": [i - 1, j - 2],
            "available_move7": [i - 1, j + 2]
        }

        available_moves = list(available_moves_dict.values())

        available_moves = check_if_offboard(available_moves)

        available_moves_final = [
            i for i in available_moves if i not in all_white_coords
        ]

        return available_moves_final
Ejemplo n.º 5
0
    def get_available_moves(self, current_position, current_board):

        available_moves = []
        available_move = []

        def check_if_offboard(avaialbe_moves):

            good_moves = []

            for each_move in avaialbe_moves:

                if each_move[0] < 8 and each_move[1] < 8 and each_move[
                        0] > -1 and each_move[1] > -1:

                    good_moves.append(each_move)

            return good_moves

        #print(current_position)

        i = current_position[0]
        j = current_position[1]

        all_white_coords = board_evaluation.get_all_white_coords(current_board)
        all_black_coords = board_evaluation.get_all_black_coords(current_board)

        #print(all_white_coords)
        #print(all_black_coords)

        x = i
        y = j + 1

        while (y < 8):

            available_move = [x, y]

            if available_move in all_black_coords:

                break

            elif available_move in all_white_coords:

                available_moves.append(available_move)

                break

            else:

                available_moves.append(available_move)

                y = y + 1

        x = i
        y = j - 1

        while (y >= 0):

            available_move = [x, y]

            if available_move in all_black_coords:

                break

            elif available_move in all_white_coords:

                available_moves.append(available_move)

                break

            else:

                available_moves.append(available_move)

                y = y - 1

        x = i + 1
        y = j

        while (x < 8):

            available_move = [x, y]

            if available_move in all_black_coords:

                break

            elif available_move in all_white_coords:

                available_moves.append(available_move)

                break

            else:

                available_moves.append(available_move)

                x = x + 1

        x = i - 1
        y = j

        while (x >= 0):

            available_move = [x, y]

            if available_move in all_black_coords:

                break

            elif available_move in all_white_coords:

                available_moves.append(available_move)

                break

            else:

                available_moves.append(available_move)

                x = x - 1

        # for y in range(len(current_board[j])):

        #     available_move = [i, y]

        #     available_moves.append(available_move)

        #     for m in range(len(available_moves)):

        #         if available_moves[m] == current_position:

        #             available_moves.pop(m)

        available_moves = check_if_offboard(available_moves)

        return available_moves
Ejemplo n.º 6
0
    def get_available_moves(self, current_position, current_board):

        available_moves = []

        available_move = []

        def check_if_offboard(avaialbe_moves):

            good_moves = []

            for each_move in avaialbe_moves:

                if each_move[0] < 8 and each_move[1] < 8 and each_move[
                        0] > -1 and each_move[1] > -1:

                    good_moves.append(each_move)

            return good_moves

        #print(current_position)

        i = current_position[0]
        j = current_position[1]

        all_white_coords = board_evaluation.get_all_white_coords(current_board)
        all_black_coords = board_evaluation.get_all_black_coords(current_board)

        x = i + 1
        y = j + 1

        while (x < 8 and y < 8):

            available_move = [x, y]

            if available_move in all_white_coords:

                break

            elif available_move in all_black_coords:

                available_moves.append(available_move)

                break

            else:

                available_moves.append(available_move)

                x = x + 1
                y = y + 1

        x = i - 1
        y = j + 1

        while (x >= 0 and y < 8):

            available_move = [x, y]

            if available_move in all_white_coords:

                break

            elif available_move in all_black_coords:

                available_moves.append(available_move)

                break

            else:

                available_moves.append(available_move)

                x = x - 1
                y = y + 1

        x = i - 1
        y = j - 1

        while (x >= 0 and y >= 0):

            available_move = [x, y]

            if available_move in all_white_coords:

                break

            elif available_move in all_black_coords:

                available_moves.append(available_move)

                break

            else:

                available_moves.append(available_move)

                x = x - 1
                y = y - 1

        x = i + 1
        y = j - 1

        while (x < 8 and y >= 0):

            available_move = [x, y]

            if available_move in all_white_coords:

                break

            elif available_move in all_black_coords:

                available_moves.append(available_move)

                break

            else:

                available_moves.append(available_move)

                x = x + 1
                y = y - 1

        available_moves = check_if_offboard(available_moves)

        return available_moves
Ejemplo n.º 7
0
def is_move_valid(move, board_coord, turn, current_board):

    len_check = 0
    space_check = 0
    first_check = 0
    second_check = 0

    if len(move) == 5:
        len_check = 1

        if move[2] == " ":

            space_check = 1

            move_start = str(move[0] + move[1])

            move_end = str(move[3] + move[4])

            for i in range(len(board_coord)):

                for j in range(len(board_coord[i])):

                    if move_start == board_coord[i][j]:
                        first_check = 1

            for i in range(len(board_coord)):

                for j in range(len(board_coord[i])):

                    if move_end == board_coord[i][j]:
                        second_check = 1

        else:
            space_check == 0

    else:
        len_check = 0

    if len_check == 0 or space_check == 0:

        return 0

    elif first_check == 0:

        return 1

    elif second_check == 0:

        return 2

    if len_check == 1 and space_check == 1 and first_check == 1 and second_check == 1:

        current_position = piece_from_coord(move, board_coord)

        if is_your_piece(current_position, turn, current_board):

            next_position = piece_to_coord(move, board_coord)

            current_piece = find_current_piece(current_position, current_board)

            next_piece = find_current_piece(next_position, current_board)

            print("next pos" + str(next_position))

            piece_moves = get_current_piece_moves(current_position,
                                                  current_board)

            piece_attacks = get_current_piece_attacks(current_position,
                                                      current_board)

            attacks_on_opp_pieces = []

            promoted_piece = current_piece

            promoted = 0

            all_black_attacks = board_evaluation.get_all_black_attacks(
                current_board)

            all_white_attacks = board_evaluation.get_all_white_attacks(
                current_board)

            white_king_coord = board_evaluation.get_white_king_coord(
                current_board)

            black_king_coord = board_evaluation.get_black_king_coord(
                current_board)

            all_white_coords = board_evaluation.get_all_white_coords(
                current_board)

            all_black_coords = board_evaluation.get_all_black_coords(
                current_board)

            print("Current piece moves: " + str(piece_moves))
            print("Current piece attacks: " + str(piece_attacks))

            if turn == "white":

                if current_piece == "wp" and next_position[
                        0] == 0 and next_piece == "00":

                    promoted_piece = promote(current_piece)

                    promoted = 1

            if turn == "black":

                if current_piece == "bp" and next_position[
                        0] == 7 and next_piece == "00":

                    promoted_piece = promote(current_piece)

                    promoted = 1

            with open('./game_database/game_flags.json', 'r') as flags:
                data = flags.read()

            game_flags = json.loads(data)

            en_passant_flag = game_flags['en_passant_flag']

            en_passant_flagged = game_flags['en_passant_flagged']

            castling = check_castling(white_king_coord, black_king_coord,
                                      game_flags, current_piece, next_position,
                                      all_black_attacks, all_white_attacks,
                                      all_black_coords, all_white_coords)

            new_board = current_board

            new_board = make_test_move(move, new_board, board_coord,
                                       en_passant_flagged, en_passant_flag)

            if promoted == 1:

                i = piece_to_coord(move, board_coord)[0]
                j = piece_to_coord(move, board_coord)[1]

                new_board[i][j] = fill_cell(new_board[i][j], promoted_piece)

            # for i in range(len(new_board)):

            #     print(new_board[i])
            #     print("\n")

            test_all_black_attacks = board_evaluation.get_all_black_attacks(
                current_board)

            test_all_white_attacks = board_evaluation.get_all_white_attacks(
                current_board)

            test_white_king_coord = board_evaluation.get_white_king_coord(
                current_board)

            test_black_king_coord = board_evaluation.get_black_king_coord(
                current_board)

            test_all_white_coords = board_evaluation.get_all_white_coords(
                current_board)

            test_all_black_coords = board_evaluation.get_all_black_coords(
                current_board)

            new_board = revert_test_move(move, new_board, board_coord,
                                         next_piece)

            # Loading game flags

            if turn == "white":

                # Checking en passant for white

                attacks_on_opp_pieces = [
                    i for i in piece_attacks if i in all_black_coords
                ]

                if current_piece == "wp":

                    if en_passant_flag in piece_attacks:

                        attacks_on_opp_pieces.append(en_passant_flag)

                # Check casling for white

            if turn == "black":

                # Checking en passant for white

                attacks_on_opp_pieces = [
                    i for i in piece_attacks if i in all_white_coords
                ]

                if current_piece == "bp":

                    if en_passant_flag in piece_attacks:

                        attacks_on_opp_pieces.append(en_passant_flag)

            print("All white attacks:  " + str(test_all_white_attacks))
            print("All white coords:  " + str(test_all_white_coords))

            print("All black attacks:  " + str(test_all_black_attacks))
            print("All black coords:  " + str(test_all_black_coords))

            print("White king coord  " + str(test_white_king_coord))
            print("Black king coord  " + str(test_black_king_coord))

            # for i in range(len(current_board)):

            #     print(current_board[i])
            #     print("\n")

            #!!!! Problem with castling

            if (turn == "white"
                    and (test_white_king_coord in test_all_black_attacks)):

                return 5

            elif (turn == "black"
                  and (test_black_king_coord in test_all_white_attacks)):

                return 6

            elif castling != "illegal" and castling != "none":

                game_flags['castling'] = castling

                add_en_passant_moves(game_flags, current_position,
                                     current_piece, next_position)

                check_castling_flags(current_piece, current_position,
                                     game_flags)

                with open("./game_database/game_flags.json", "w") as outfile:
                    json.dump(game_flags, outfile)

                return 3

            elif ((next_position not in piece_moves)
                  and (next_position not in attacks_on_opp_pieces)):

                return 7

            # elif ((next_position in piece_attacks) and is_your_piece(next_position, turn, current_board)):

            #     return 8

            # elif (current_piece == "wp") and next_position[0] = 7

            elif castling == "illegal":

                return 8

            else:

                game_flags['castling'] = castling

                add_en_passant_moves(game_flags, current_position,
                                     current_piece, next_position)

                check_castling_flags(current_piece, current_position,
                                     game_flags)

                with open("./game_database/game_flags.json", "w") as outfile:
                    json.dump(game_flags, outfile)

                return 3

        else:

            return 4