Python棋子运动

2024-06-01 23:19:36 发布

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

在Python中的一个早期进行中的国际象棋游戏中,我遇到了为国际象棋生成有效棋子的问题。。。我和主教有了麻烦。 这里是我的节目一瞥。。。右下角的白色主教被选中,红色方块代表有效的移动。。。很明显,主要问题是什么

如果有阻塞块,我希望我的程序停止添加更多潜在动作

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)

Tags: inselfforpieceinitdefbishopclass
1条回答
网友
1楼 · 发布于 2024-06-01 23:19:36

我认为最简单的解决方法是重新排列循环的顺序,这样外部循环通过四个方向,内部循环通过距离。当遇到阻塞件时,停止内环。一旦搜索超出范围,您还可以停止内部循环

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

相关问题 更多 >