我目前正在使用Python-Chess构建一个国际象棋游戏,并尝试使用SVG模块来生成棋盘的SVG图像。用于生成svg的参数之一是check (here),它是一个“要标记的方块,表示选中”。然而,从文档中,我找不到一种方法来找出玩家的国王在哪里。
我想要发生的是,每当board.is_check()我希望它用check=生成svg时,使用当前玩家的国王的当前位置。我该如何解决这个问题呢?我是否必须遍历每个方块并检查上面有什么块,直到我找到正确的国王?或者有没有我看不到的函数?如有任何帮助,我们将非常感谢!
发布于 2021-04-01 01:06:07
这里有一个用于获取国王位置的函数:https://python-chess.readthedocs.io/en/latest/core.html#chess.BaseBoard.king
下面是一些示例代码:
import chess
board = chess.Board()
# get the current square index of the white king
king_square_index = board.king(chess.WHITE)
# get the current square name of the white king
king_square_name = chess.square_name(king_square_index)
print(king_square_name) # e1
# Move white pawn to e4
board.push_san("e4")
# Move black pawn to e5
board.push_san("e5")
# Move white king to e2
board.push_san("Ke2")
# get the current square index of the white king
king_square_index = board.king(chess.WHITE)
# get the current square name of the white king
king_square_name = chess.square_name(king_square_index)
print(king_square_name) # e2https://stackoverflow.com/questions/66803080
复制相似问题