【问题标题】:Iterating through nested list and skipping rows or columns遍历嵌套列表并跳过行或列
【发布时间】:2021-01-10 14:56:06
【问题描述】:

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

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

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

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

def get_piece_colour(piece):
    blackpieceslist = ["br", "bkn", "bb", "bk", "bq", "bp"]
    whitepieceslist = ["wr", "wkn", "wb", "wk", "wq", "wp"]

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

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

上述功能与工作示例白棋:

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[1], t[0]))

当我点击图片中间的车时它的外观示例:

所以在这个例子中,左边的白兵左边的方块,右边的白兵右边的方块以及黑皇后和黑兵之间的方块不应该被我的函数突出显示。

谁能告诉我怎么做?

谢谢。

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

【问题讨论】:

  • 我假设我们可以根据所选片段的来源计算高亮函数。选择棋子位置的车 (3,3)。我们有四个可以移动的方向。对于向上方向,我们可以增加 1 来控制 y 轴。例如,(3,4) == 'e' 绘制并增加然后控制 (3,5) 如果它不是 'e' 然后停止并继续控制其他方向,像这样。
  • 感谢您的建议,但我仍然无法正常工作。我运行一个从起始行到顶部的 for 循环,一个从起始行到底部的循环,一个从起始列到右侧,一个从起始列到左侧。如果片段颜色不是“e”,我还为每个 for 循环添加了中断,但不起作用。

标签: python-3.x pygame


【解决方案1】:

部分归功于 shalom,部分归功于研究,我想出了一个解决方案。

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

# 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, -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]))

【讨论】:

    猜你喜欢
    • 2019-08-22
    • 2015-05-16
    • 2015-12-06
    • 2021-09-19
    • 2021-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多