首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >巨蟒棋子运动

巨蟒棋子运动
EN

Stack Overflow用户
提问于 2020-02-23 05:44:56
回答 1查看 841关注 0票数 0

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

如果有一个阻挡块,我希望我的程序停止添加更多的潜在移动,

^^不是复制品;我参考了其他来源

针对Bishops的类:

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-02-23 06:02:43

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

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60357122

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档