微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

遍历嵌套列表并跳过行或列

如何解决遍历嵌套列表并跳过行或列

我正在使用 python 3.7 和 pygame 作为我的第一个编程项目开发 Windows 国际象棋应用程序/游戏。我已经走了很远,但有一个问题我尝试解决了几个小时,但不知道如何解决

我有一个嵌套列表,用于存储棋子在棋盘上的位置,如下所示:

ppos = [
    ["br","bkn","bb","bq","bk","br"],["bp","bp","bp"],["e","e","e"],["wp","wp","wp"],["wr","wkn","wb","wq","wk","wr"],]

然后我有一个函数返回嵌套列表 ppos 中一块的颜色:

def get_piece_colour(piece):
    blackpieceslist = ["br","bp"]
    whitepieceslist = ["wr","wp"]

    if piece in blackpieceslist:
        colour = "black"
    elif piece in whitepieceslist:
        colour = "white"
    elif piece == "e":
        colour = "e"
    return colour

一个功能检查并突出显示棋子的所有可能移动,方法是在棋盘上每个可以移动到的方格上闪烁一个部分透明的绿色方块(表面)。

上面提到的带有工作示例白色 pawn 的函数

def highlight_possible_squares(from_row,from_col,piece):
    pygame.draw.rect(green_highlight,ALPHAGREEN,green_highlight.get_rect())

    # white pawn
    if piece == "wp":
        for r in range(0,8):
            for c in range(0,8):

                to_row = r
                to_col = c

                square_x = to_row * square_size
                square_y = to_col * square_size

                square_xy_tuple = (square_x,square_y)

                from_to_row_dif = from_row - to_row
                from_to_col_dif = from_col - to_col

                # if on starting row,squares that are one and two squares in front get highlighted
                if from_row == 6:
                    if from_to_row_dif == 1 and from_to_col_dif == 0 and get_piece_colour(ppos[r][c]) == "e":
                        highlight_squares_lst.append(square_xy_tuple)
                    elif from_to_row_dif == 2 and from_to_col_dif == 0 and get_piece_colour(ppos[r][c]) == "e":
                        highlight_squares_lst.append(square_xy_tuple)
                    # diagonal move to destroy other pieces
                    elif from_to_row_dif == 1 and from_to_col_dif == 1 and get_piece_colour(ppos[r][c]) == "black":
                        highlight_squares_lst.append(square_xy_tuple)
                    elif from_to_row_dif == 1 and from_to_col_dif == -1 and get_piece_colour(ppos[r][c]) == "black":
                        highlight_squares_lst.append(square_xy_tuple)

                # if not on starting row,pawn can only move one square in front
                elif from_row < 6:
                    if from_to_row_dif == 1 and from_to_col_dif == 0 and get_piece_colour(ppos[r][c]) == "e":
                        highlight_squares_lst.append(square_xy_tuple)
                    # diagonal move to destroy other pieces
                    elif from_to_row_dif == 1 and from_to_col_dif == 1 and get_piece_colour(ppos[r][c]) == "black":
                        highlight_squares_lst.append(square_xy_tuple)
                    elif from_to_row_dif == 1 and from_to_col_dif == -1 and get_piece_colour(ppos[r][c]) == "black":
                        highlight_squares_lst.append(square_xy_tuple)

        for t in highlight_squares_lst:
            board.blit(green_highlight,(t[1],t[0]))

现在我不知道该怎么做是车。车可以左右上下移动,但不能越过棋子移动到棋子后面的方格。

这是我目前所拥有的:

# white rook
if piece == "wr":

    for r in range(0,8):
        for c in range(0,8):

            square_x = r * square_size
            square_y = c * square_size

            square_xy_tuple = (square_x,square_y)

            if from_row == r and get_piece_colour(ppos[r][c]) == "e":
                highlight_squares_lst.append(square_xy_tuple)
            elif from_col == c and get_piece_colour(ppos[r][c]) == "e":
                highlight_squares_lst.append(square_xy_tuple)

    for t in highlight_squares_lst:
        board.blit(green_highlight,t[0]))

当我点击图片间的车时它的样子的图像示例:

highlight white rook moves

因此,在本例中,我的函数不应突出显示左侧白色棋子左侧的正方形、右侧白色棋子右侧的正方形以及黑色皇后和黑色棋子之间的正方形。

谁能告诉我怎么做?

谢谢。

附言我知道我的大部分代码都可以改进很多,但我是初学者,我正在努力随着时间的推移变得更好。

解决方法

部分感谢 shalom,部分通过研究,我想出了一个解决方案。

我尝试使用 4 个 for 循环在所有 4 个方向上进行,并在我击中颜色不是“e”的棋子/方块时打破它们。 真正起作用的是范围函数的阶段参数。

# white rook
    if piece == "wr":

        for c in range(from_col + 1,8):
            if get_piece_colour(ppos[from_row][c]) == "e":
                highlight_squares_lst.append((from_row * 100,c * 100))
            else:
                break

        for mc in range(from_col - 1,-1,-1):
            if get_piece_colour(ppos[from_row][mc]) == "e":
                highlight_squares_lst.append((from_row * 100,mc * 100))
            else:
                break

        for r in range(from_row + 1,8):
            if get_piece_colour(ppos[r][from_col]) == "e":
                highlight_squares_lst.append((r * 100,from_col * 100))
            else:
                break

        for mr in range(from_row - 1,-1):
            if get_piece_colour(ppos[mr][from_col]) == "e":
                highlight_squares_lst.append((mr * 100,from_col * 100))
            else:
                break

        for t in highlight_squares_lst:
            board.blit(green_highlight,(t[1],t[0]))

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。