python:国际象棋走法验证

发布于 2024-09-02 04:45:53 字数 395 浏览 11 评论 0原文

有谁知道某处是否有免费的 python 国际象棋移动验证功能?

我需要什么。我有一个存储为字符串的图表,并移动候选者。我需要的是查看移动候选者是否对图表有效。

如果可能的话,我真的很想看看例子。


该字符串看起来像这样:

emememememememememememememememememembbememwpemememememememwpemembkememememememememememememememememwbembrememememwkemememememem

我知道这可能看起来很愚蠢,但我发现用这种方式编码位置是最简单的。对我来说,调动候选人就是另一个这样的职位(发生在下一步调动之后,我认为可以改变这种行为)

Does anybody know if there is a free python chess moves validation function available somewhere?

What I need. I have a diagram stored as a string, and move candidate. What I need is to see if move candidate is valid for the diagram.

Would be really interested to see examples, if possible.


The string looks this way:

ememememememememememememememememememembbememwpemememememememwpemembkememememememememememememememememwbembrememememwkemememememem

I understand it may seem stupid, but I find it the easiest to encode position this way. Move candidate for me is just another such position (which happened after next move, can change this behavior I think)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

2024-09-09 04:45:53

您缺少信息,例如轮到谁移动、每个国王是否曾经移动过(意味着不允许易位)、每个棋子的“en passant”状态。除此之外,使用不太复杂的板表示(例如描述的 10x12 元素数组)来编写自己的板表示是一个非常有指导意义的练习 此处(除非您将其线性化为 120 个元素的数组)。

You are missing information e.g. whose turn to move, whether each king has ever moved (means castling is not allowed), the "en passant" status of each pawn. That aside, it would be a very instructive exercise for you to write your own, using a not-very-complicated board representation like the 10x12-element array described here (except that you'd linearise it to a 120-element array).

仅此而已 2024-09-09 04:45:53

我知道这是一个相当老的问题,但我和我的兄弟正在寻找同样的东西,我们遇到了这个很棒的小 python 模块,名为 栗子

这是它的使用示例:

#!/usr/bin/python
from Chessnut import Game

chessgame = Game(fen="rnbq1rk1/ppppp1bp/5np1/5p2/2PP4/2NBPN2/PP3PPP/R1BQK2R b KQ - 4 6")
print chessgame  

print chessgame.get_moves()

# apply a move 
chessgame.apply_move(chessgame.get_moves()[1])

print chessgame

这里生成的输出:

rnbq1rk1/ppppp1bp/5np1/5p2/2PP4/2NBPN2/PP3PPP/R1BQK2R b KQ - 4 6
['b8a6', 'b8c6', 'd8e8', 'f8e8', 'f8f7', 'g8h8', 'g8f7', 'a7a6', 'a7a5', 'b7b6', 'b7b5', 'c7c6', 'c7c5', 'd7d6', 'd7d5', 'e7e6', 'e7e5', 'g7h8', 'g7h6', 'h7h6', 'h7h5', 'f6e8', 'f6d5', 'f6e4', 'f6g4', 'f6h5', 'g6g5', 'f5f4']
r1bq1rk1/ppppp1bp/2n2np1/5p2/2PP4/2NBPN2/PP3PPP/R1BQK2R w KQ - 5 7

太棒了! :)
感谢cgearhart

I know this is a rather old question, but my brother and me were looking for the same thing and we came across this awesome little python module called Chessnut.

Here is an example of its use:

#!/usr/bin/python
from Chessnut import Game

chessgame = Game(fen="rnbq1rk1/ppppp1bp/5np1/5p2/2PP4/2NBPN2/PP3PPP/R1BQK2R b KQ - 4 6")
print chessgame  

print chessgame.get_moves()

# apply a move 
chessgame.apply_move(chessgame.get_moves()[1])

print chessgame

and here the generated output:

rnbq1rk1/ppppp1bp/5np1/5p2/2PP4/2NBPN2/PP3PPP/R1BQK2R b KQ - 4 6
['b8a6', 'b8c6', 'd8e8', 'f8e8', 'f8f7', 'g8h8', 'g8f7', 'a7a6', 'a7a5', 'b7b6', 'b7b5', 'c7c6', 'c7c5', 'd7d6', 'd7d5', 'e7e6', 'e7e5', 'g7h8', 'g7h6', 'h7h6', 'h7h5', 'f6e8', 'f6d5', 'f6e4', 'f6g4', 'f6h5', 'g6g5', 'f5f4']
r1bq1rk1/ppppp1bp/2n2np1/5p2/2PP4/2NBPN2/PP3PPP/R1BQK2R w KQ - 5 7

Awesome! :)
Thanks cgearhart!

毁梦 2024-09-09 04:45:53

只需使用 Python Chess 程序之一的源代码,例如 PyChessPython Chess

具体来说,pychess 的有效动作:https://code.google.com/p/pychess/source/browse/lib/pychess/ Utils/lutils/validator.py

Just use the source of one of the Python Chess programs like PyChess or Python Chess

Specifically, the valid moves for pychess: https://code.google.com/p/pychess/source/browse/lib/pychess/Utils/lutils/validator.py

千笙结 2024-09-09 04:45:53

查看旁边的一些相关答案不会有什么坏处:国际象棋移动验证库https://stackoverflow.com/questions/1239913/smallest-chess-playing-program对我来说很突出。

虽然我个人赞成建立你自己的。

Wouldn't hurt to look at some of the related answers on the side: Chess move validation library and https://stackoverflow.com/questions/1239913/smallest-chess-playing-program stand out to me.

Though personally I'm in favor of building your own.

七七 2024-09-09 04:45:53

查看 ChessBoard

不幸的是它有一些缺点:

  • 它似乎被放弃了,因为一年多前在评论中报告的错误似乎没有得到修复
  • 代码并不真正符合 PEP-8
  • 一些方法非常丑陋和大,不是全部方法有文档字符串
  • ,没有单元测试,所以深入研究该代码可能是一个挑战(我已经尝试过至少两次但失败了),

好处是代码是 GPL,所以只要你愿意,你就可以使用它遵守该许可证。

Check out ChessBoard.

Unfortunately it has some drawbacks:

  • it seems to be abandoned, because the bugs reported more than one year ago in the comments don't seem to be fixed
  • the code is not really PEP-8 compliant
  • some methods are very ugly and big, not all methods have docstrings
  • there are no unit tests, so digging into that code might be a challenge (I've already tried it at least twice and failed)

The good thing is that the code is GPL so you can play with it as long as you stick to that license.

小女人ら 2024-09-09 04:45:53

我在这里做了一个带有移动验证的简单国际象棋实现:https://github.com/akulakov/pychess

验证逻辑位于每个棋子的“moves()”方法中,您可以通过生成完整的棋步列表并检查您的棋步是否存在来验证您自己的棋步。

I've made a simple chess implementation with move validation here: https://github.com/akulakov/pychess

Validation logic is in each piece's "moves()" method, and you can validate your own move by generating full list of moves and checking if your move is there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文