如何在数组中引用数组中的位置?

2024-10-17 10:25:41 发布

您现在位置:Python中文网/ 问答频道 /正文

在我的数组中,我需要引用数组中元素的位置。你知道吗

我目前正在用Python编写一个象棋程序。我需要这首曲子的起始位置。这种方法是我唯一能想到的方法。但是,位置是手动添加的(我给出了位置,如果我只更改了我给出的位置,而不是更改数组,则工件的位置会更改,即使数组没有更改),而不是使用其在数组中的位置。我刚刚开始学习Python,我的老师还没有教我这个。顺便说一下,Rook、Knight、Bishop等是我定义的类,它们的参数依次是初始位置、暂定最终位置和字母(大写代表白色,小写代表黑色)

board_pieces = [
            [Rook((0, 0), end, 'r'), Knight((1, 0), end, 'n'), Bishop((2, 0), end, 'b'), Queen((3, 0), end, 'q'), King((4, 0), end, 'k'), Bishop((5, 0), end, 'b'), Knight((6, 0), end, 'n'), Rook((7, 0), end, 'r')],
            [Pawn((0, 1), end, 'p'), Pawn((1, 1), end, 'p'), Pawn((2, 1), end, 'p'), Pawn((3, 1), end, 'p'), Pawn((4, 1), end, 'p'), Pawn((5, 1), end, 'p'), Pawn((6, 1), end, 'p'), Pawn((7, 1), end, 'p')],
            [None, None, None, None, None, None, None, None],
            [None, None, None, None, None, None, None, None],
            [None, None, None, None, None, None, None, None],
            [None, None, None, None, None, None, None, None],
            [Pawn((0, 6), end, 'P'), Pawn((1, 6), end, 'P'), Pawn((2, 6), end, 'P'), Pawn((3, 6), end, 'P'), Pawn((4, 6), end, 'P'), Pawn((5, 6), end, 'P'), Pawn((6, 6), end, 'P'), Pawn((7, 6), end, 'P')],
            [Rook((0, 7), end, 'R'), Knight((1, 7), end, 'N'), Bishop((2, 7), end, 'B'), Queen((3, 7), end, 'Q'), King((4, 7), end, 'K'), Bishop((5, 7), end, 'B'), Knight((6, 7), end, 'N'), Rook((7, 7), end, 'R')]]

我希望第一个参数引用数组上的实际位置,但我编写数组的方式不起作用。你知道吗


Tags: 方法程序none元素参数代表数组bishop