Python国际象棋棋子运动

时间:2020-02-22 21:44:55

标签: python oop chess pyprocessing

在使用Python进行的早期进行中的国际象棋游戏中,为国际象棋生成有效棋子时遇到了问题...我在Bishop中遇到了麻烦。 这是我的程序的一瞥...选择了右下角的白色主教,红色方块代表有效的动作...显然,主要问题是什么。

如果有障碍,我希望程序停止添加更多潜在动作,

enter image description here

^^不是重复项;我已经咨询了其他来源

主教课程:

class Bishop(Piece):
    def __init__(self, x, y, pl, im):
        Piece.__init__(self, x, y, pl, im) 

    def findAvailableMoves(self):
        for i in range(1, 8):
            for (dx, dy) in [(i, i), (i, -i), (-i, i), (-i, -i)]:
                if self.inBoundsPiece(self.cor.x + dx, self.cor.y + dy):
                    if board.board[self.cor.y + dy][self.cor.x + dx] == None:
                    self.potentialMoves.append((self.cor.y + dy, self.cor.x + dx))

class WBishop(Bishop):
    def __init__(self, x, y):
        Bishop.__init__(self, x, y, 1, wBishop)

class BBishop(Bishop):
    def __init__(self, x, y):
        Bishop.__init__(self, x, y, 2, bBishop)

1 个答案:

答案 0 :(得分:1)

我认为最简单的解决方案是重新排列循环的顺序,因此外循环沿四个方向循环,而内循环沿距离循环。然后在遇到阻塞块时停止内部循环。您也可以在搜索超出范围后停止内部循环。

@Mock