小编Hon*_*onn的帖子

更有效地检测支票(国际象棋)

我目前正在开发一个国际象棋引擎,该引擎目前正在运行,但需要很长时间才能生成棋步。由于必须生成许多移动,因此检查检测花费的时间最长。

在尝试了很多事情之后,我陷入了困境,并且无法真正弄清楚如何使其更有效率。这是我如何做到的:

要检查移动是否允许/合法,我会检查进行移动的一方是否在之后受到检查:

    def allowed_move(self, board, pos, move, colour):

    copy_board = copy.deepcopy(board)

    (x, y) = pos
    (new_x, new_y) = move

    if copy_board[x][y] == "k_w":
        self.white_king = (new_x, new_y)
        copy_board[new_x][new_y] = copy_board[x][y]
        copy_board[x][y] = " "
        self.in_check(colour, copy_board)
        self.white_king = (x, y)

    elif copy_board[x][y] == "k_b":
        self.black_king = (new_x, new_y)
        copy_board[new_x][new_y] = copy_board[x][y]
        copy_board[x][y] = " "
        self.in_check(colour, copy_board)
        self.black_king = (x, y)

    else:
        copy_board[new_x][new_y] = copy_board[x][y]
        copy_board[x][y] = " "
        self.in_check(colour, copy_board)


    if colour == "_w" and self.white_check:
        return False …
Run Code Online (Sandbox Code Playgroud)

python chess

10
推荐指数
2
解决办法
449
查看次数

标签 统计

chess ×1

python ×1