starting_board = [
    [8, 9, 10, 11, 12, 10, 9, 8],
    [7, 7, 7, 7, 7, 7, 7, 7],
    [0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0],
    [1, 1, 1, 1, 1, 1, 1, 1],
    [2, 3, 4, 5, 6, 4, 3, 2]
]

def is_w_king_attacked(board, w_king_pos):
    ki, kj = w_king_pos

    # Pawn attacks (black pawns are '7')
    if ki > 1:
        if kj > 0 and board[ki - 1][kj - 1] == 7:
            return True
        if kj < 7 and board[ki - 1][kj + 1] == 7:
            return True

    # Knight attacks (9 is a Black Knight)
    knight_moves = [(-2, 1), (-2, -1), (-1, 2), (1, 2), (2, 1), (2, -1), (1, -2), (-1, -2)]
    for di, dj in knight_moves:
        ni, nj = ki + di, kj + dj
        if 0 <= ni < 8 and 0 <= nj < 8 and board[ni][nj] == 9:
            return True

    # Rook/Queen (10 is a Rook, 11 is a Queen)
    cros = [(-1, 0), (1, 0), (0, 1), (0, -1)]
    for id, jd in cros:
        i, j = ki, kj
        while True:
            i, j = i + id, j + jd
            if not (0 <= i < 8 and 0 <= j < 8):
                break
            x = board[i][j]
            if x == 0:
                continue
            if x < 7:
                break
            if x == 10 or x == 11:
                return True
            break

    # Bishop/Queen (8 is a Bishop, 11 is a Queen)
    diag = [(1, 1), (-1, 1), (-1, -1), (1, -1)]
    for id, jd in diag:
        i, j = ki, kj
        while True:
            i, j = i + id, j + jd
            if not (0 <= i < 8 and 0 <= j < 8):
                break
            x = board[i][j]
            if x == 0:
                continue
            if x < 7:
                break
            if x == 8 or x == 11:
                return True
            break
    return False


def would_attack_w_king_else_add_move(board, w_king_pos, i, j, ni, nj, legal_moves):
    save = board[ni][nj]
    board[ni][nj] = board[i][j]
    if is_w_king_attacked(board, w_king_pos):
        board[i][j] = board[ni][nj]
        board[ni][nj] = save
    else:
        board[i][j] = board[ni][nj]
        board[ni][nj] = save
        legal_moves.append((i, j, ni, nj))


def legal_moves_white(board, w_king_pos, left_rook_moved, right_rook_moved, king_moved, last_move):
    legal_moves = []
    for i in range(8):
        for j in range(8):
            x = board[i][j]
            if x == 0 or x > 6:
                continue
            if x == 1: # Pawns
                if board[i-1][j] == 0:
                    would_attack_w_king_else_add_move(board, w_king_pos, i, j, i - 1, j, legal_moves)
                if i == 6 and board[i-1][j] == 0 and board[i-2][j] == 0:
                    would_attack_w_king_else_add_move(board, w_king_pos, i, j, i - 2, j, legal_moves)
                if j > 0 and board[i-1][j-1] > 6:
                    would_attack_w_king_else_add_move(board, w_king_pos, i, j, i - 1, j - 1, legal_moves)
                if j < 7 and board[i-1][j+1] > 6:
                    would_attack_w_king_else_add_move(board, w_king_pos, i, j, i - 1, j + 1, legal_moves)
                if last_move:
                    last_from, last_to = last_move
                    if board[last_to[0]][last_to[1]] == 7:
                        if last_to[0] == i and abs(last_to[1] - j) == 1:
                            if last_from[0] == i - 2 and last_from[1] == last_to[1]:
                                would_attack_w_king_else_add_move(board, w_king_pos, i, j, i - 1, last_to[1], legal_moves)
            elif x == 2 or x == 5: # Rock, Queen
                cros = [(-1, 0), (1, 0), (0, 1), (0, -1)]
                for id, jd in cros:
                    ci, cj = i, j
                    while True:
                        ci, cj = ci + id, cj + jd
                        if not (0 <= ci < 8 and 0 <= cj < 8):
                            break
                        x = board[ci][cj]
                        if x == 0:
                            would_attack_w_king_else_add_move(board, w_king_pos, i, j, ci, cj, legal_moves)
                            continue
                        if x < 7:
                            break
                        else:
                            would_attack_w_king_else_add_move(board, w_king_pos, i, j, ci, cj, legal_moves)
                            break
            elif x == 4 or x == 5: # Bishop Queen
                diag = [(1, 1), (-1, 1), (-1, -1), (1, -1)]
                for id, jd in diag:
                    ci, cj = i, j
                    while True:
                        ci, cj = ci + id, cj + jd
                        if not (0 <= ci < 8 and 0 <= cj < 8):
                            break
                        x = board[ci][cj]
                        if x == 0:
                            would_attack_w_king_else_add_move(board, w_king_pos, i, j, ci, cj, legal_moves)
                            continue
                        if x < 7:
                            break
                        else:
                            would_attack_w_king_else_add_move(board, w_king_pos, i, j, ci, cj, legal_moves)
                            break
            elif x == 6: # King
                one_step = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]
                for id, jd in one_step:
                    ci, cj = i + id, j + jd
                    if 0 <= ci < 8 and 0 <= cj < 8 and (board[ci][cj] > 6 or board[ci][cj] == 0):
                        would_attack_w_king_else_add_move(board, w_king_pos, i, j, ci, cj, legal_moves)

            elif x == 3: # Knight
                knight_moves = [(-2, 1), (-2, -1), (-1, 2), (1, 2), (2, 1), (2, -1), (1, -2), (-1, -2)]
                for di, dj in knight_moves:
                    ni, nj = i + di, j + dj

                    if 0 <= ni < 8 and 0 <= nj < 8 and (board[ni][nj] > 6 or board[ni][nj] == 0):
                        would_attack_w_king_else_add_move(board, w_king_pos, i, j, ni, nj, legal_moves)


    # Castling
    if not king_moved:
        # Right castling (King-side)
        if not right_rook_moved and board[7][5] == 0 and board[7][6] == 0:
            if all(not is_w_king_attacked(board, (7, i)) for i in [4, 5, 6]):
                legal_moves.append((7, 4, 7, 6))  # King moves
                legal_moves.append((7, 7, 7, 5))  # Rook moves

        # Left castling (Queen-side)
        if not left_rook_moved and board[7][1] == 0 and board[7][2] == 0 and board[7][3] == 0:
            if all(not is_w_king_attacked(board, (7, i)) for i in [4, 3, 2, 1]):
                legal_moves.append((7, 4, 7, 2))  # King moves
                legal_moves.append((7, 0, 7, 3))  # Rook moves
    return legal_moves

Given this code generate the corresponding functions for the black peaces, namly is_b_king_attacked, would_attack_b_king_else_add_move and legal_moves_black