Esempio n. 1
0
def get_all_captures(board, color, is_sorted=False):
    """
    Get all captures a player can do.
    :param board:A board object.
    :param color:A string of the color of the player.
    :param is_sorted:A boolean of if sort.
    :return:A list of all captures.
    """
    all_captures = []
    length = board.get_length()
    for i in range(length):
        for j in range(length):
            if board.get(i, j):
                if board.get(i, j).color() == color:
                    if tools.get_captures(
                            board,
                            i,
                            j,
                    ):
                        all_captures.extend(tools.get_captures(
                            board,
                            i,
                            j,
                        ))
    return sort_captures(all_captures, is_sorted)
Esempio n. 2
0
def get_all_captures(board, color, is_sorted=False):
    """
    Get all captures a player can do.
    :param board:A board object.
    :param color:A string of the color of the player.
    :param is_sorted:A boolean of if sort.
    :return:A list of all captures.
    """

    all_captures_list = []
    length_int = board.get_length()
    for row in range(length_int):
        for col in range(length_int):
            if board.get(row, col):
                if board.get(row, col).color() == color:
                    if tools.get_captures(
                            board,
                            row,
                            col,
                    ):
                        all_captures_list.extend(
                            tools.get_captures(
                                board,
                                row,
                                col,
                            ))
    sorted_captures_list = sort_captures(all_captures_list, is_sorted)
    return sorted_captures_list
Esempio n. 3
0
def get_all_captures(board, color, is_sorted=False):
    """
    This function will give all the possible captures
    Values: int, string , boolean
    Returns: list
    """
    capture_lst = []
    board_length = board.get_length()
    for row in range(board_length):
        for col in range(board_length):

            piece = board.get(row, col)

            if piece != None:

                if piece.color() == color:
                    cap = tools.get_captures(board, row, col, is_sorted)
                    #                    position = deindexify(row, col)

                    if cap:

                        for p in cap:

                            captures = p

                            capture_lst.append(captures)

    return capture_lst
Esempio n. 4
0
def get_all_captures(board, color, is_sorted=False):
    """
    Write something about this function here.
    """
    captures = []
    piece = Piece(color)
    pieceK = Piece(color, True)
    for r in range(8):
        for c in range(8):
            if not board.is_free(r, c):
                if str(board.get(r, c)) is str(piece):
                    valid = tools.get_captures(board, r, c, True)
                    for lst in valid:
                        captures.append(lst)
                elif str(board.get(r, c)) is str(pieceK):
                    valid = tools.get_captures(board, r, c, True)
                    for lst in valid:
                        captures.append(lst)
    if is_sorted:
        sort_captures(captures, True)
    return captures
Esempio n. 5
0
def get_all_captures(board, color, is_sorted=False):
    """
Get the probability that all the pieces on the board can jump.
    use three for loop and two if
return sort_captures list
    """
    row = col = board.get_length()
    final_list = []
    for r in range(row):
        for c in range(col):
            piece = board.get(r, c)
            if piece:
                if piece.color() == color:
                    path_list = tools.get_captures(board, r, c, is_sorted)
                    for path in path_list:
                        final_list.append(path)
    return sort_captures(final_list, is_sorted)
Esempio n. 6
0
def get_all_captures(board, color, is_sorted=False):
    """
    This function returns a list of all possible captures for each piece of the 
    board (even if they were king)
    """
    #Create an empty list
    end_list = []
    for r in range(board.get_length()):
        for c in range(board.get_length()):
            if not board.is_free(r, c):
                p = board.get(r, c)
                if p.color() == color:
                    moves = tools.get_captures(board, r, c, is_sorted)
                    #If moves has something in it, extend it into the end list
                    if moves:
                        end_list.extend(moves)
    #Return the end list
    return (end_list)
Esempio n. 7
0
def heuristics(state):
    """
    This is the heuristics function. This function calculates these metrics:
        a. Normalized utility values from the number of pawn and king pieces 
            on the board. [0.32, -0.32]
        b. Normalized utility values from the number of captures could be made 
            by kings and pawns. [0.96, -0.96]
        c. Normalized utility values from the distances of pawns to become
            kings. [0.70, -0.70]
        d. Normalized utility values from the number of pieces on the safer
            places on the board. [0.19, -0.19]
    """
    board = state[0]
    turn = state[1]
    length = board.get_length()
    bp, wp = 0, 0
    bk, wk = 0, 0
    bc, wc = 0, 0
    bkd, wkd = 0, 0
    bsd, wsd = 0.0, 0.0
    for row in range(length):
        for col in range(length):
            piece = board.get(row, col)
            if piece:
                r = row if row > (length - (row + 1)) else (length - (row + 1))
                c = col if col > (length - (col + 1)) else (length - (col + 1))
                d = int(((r**2.0 + c**2.0)**0.5) / 2.0)
                if piece.color() == 'black':
                    bc += sum([len(v) for v in \
                               tools.get_captures(board, row, col)])
                    if piece.is_king():
                        bk += 1
                    else:
                        bp += 1
                        bkd += row + 1
                        bsd += d
                else:
                    wc += sum([len(v) for v in \
                               tools.get_captures(board, row, col)])
                    if piece.is_king():
                        wk += 1
                    else:
                        wp += 1
                        wkd += length - (row + 1)
                        wsd += d
    if turn == 'black':
        black_count_heuristics = \
                3.125 * (((bp + bk * 2.0) - (wp + wk * 2.0)) \
                    / 1.0 + ((bp + bk * 2.0) + (wp + wk * 2.0)))
        black_capture_heuristics = 1.0417 * ((bc - wc) / (1.0 + bc + wc))
        black_kingdist_heuristics = 1.429 * ((bkd - wkd) / (1.0 + bkd + wkd))
        black_safe_heuristics = 5.263 * ((bsd - wsd) / (1.0 + bsd + wsd))
        return black_count_heuristics + black_capture_heuristics \
                    + black_kingdist_heuristics + black_safe_heuristics
    else:
        white_count_heuristics = \
                3.125 * (((wp + wk * 2.0) - (bp + bk * 2.0)) \
                    / 1.0 + ((bp + bk * 2.0) + (wp + wk * 2.0)))
        white_capture_heuristics = 1.0416 * ((wc - bc) / (1.0 + bc + wc))
        white_kingdist_heuristics = 1.428 * ((wkd - bkd) / (1.0 + bkd + wkd))
        white_safe_heuristics = 5.263 * ((wsd - bsd) / (1.0 + bsd + wsd))
        return white_count_heuristics + white_capture_heuristics \
                    + white_kingdist_heuristics + white_safe_heuristics