首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Python-Chess中获取棋子的位置

在Python-Chess中获取棋子的位置
EN

Stack Overflow用户
提问于 2021-03-25 23:50:50
回答 1查看 320关注 0票数 0

我目前正在使用Python-Chess构建一个国际象棋游戏,并尝试使用SVG模块来生成棋盘的SVG图像。用于生成svg的参数之一是check (here),它是一个“要标记的方块,表示选中”。然而,从文档中,我找不到一种方法来找出玩家的国王在哪里。

我想要发生的是,每当board.is_check()我希望它用check=生成svg时,使用当前玩家的国王的当前位置。我该如何解决这个问题呢?我是否必须遍历每个方块并检查上面有什么块,直到我找到正确的国王?或者有没有我看不到的函数?如有任何帮助,我们将非常感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-04-01 01:06:07

这里有一个用于获取国王位置的函数:https://python-chess.readthedocs.io/en/latest/core.html#chess.BaseBoard.king

下面是一些示例代码:

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

https://stackoverflow.com/questions/66803080

复制
相关文章

相似问题

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