tags:

views:

246

answers:

4

Hi,

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)

+2  A: 

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

Specifically, the valid moves for pychess: http://code.google.com/p/pychess/source/browse/lib/pychess/Utils/eval.py

WoLpH
Can't understand how. Seems docs are not very good there. Need some example...
Oleg Tarasenko
@WoLpH: The reference that you gave is for evaluation (scoring) of a board position, NOT for validation (checking for legal moves).
John Machin
@John: you are correct, it seems I am unable to find the right bit of code right now. But the answer from Dimo414 seems better anyway so I won't look further :)
WoLpH
@Oleg: You would be using the pychess.Utils.lutils.validator module.It takes a pychess.Utils.lutils.lboard object and a lmove one. Both which can be initialized from pure chess formats such as fen and algebraic notation.
Thomas Ahle
+1  A: 

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

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

dimo414
It's not easy to build own :(. Anyway it would be great to reuse something already created
Oleg Tarasenko
Seems pretty straightforward to me - identify type of piece, determine all paths for that type of piece from that position. Check if move is on valid path. Then confirm (except for knight) that there is no piece along that path, nor any friendly piece in the new location.
dimo414
Maybe, but if there is open python lib which solves the problem already, I would use it.
Oleg Tarasenko
Yeah I'd agree that it's straightforward. It's not like your trying to rate the usefulness of the move or something. It will probably be easier to just roll your own rather than dissecting an open-source program's documentation/source.
Cam
Perhaps, but I'd be willing to bet that in the 2+ hours since you asked your question, you could have rolled your own already. There's certainly something to be said for using pre-existing tools, but sometimes it's easier to reinvent the wheel than to make someone else's wheel work with your car.
dimo414
+3  A: 

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).

John Machin
+1. If possible the OP should store that kind of info as flags along with the board state.
Cam
There is also the threefold repetition rule and the fifty move rule to consider
gnibbler
@gnibbler: I did write "e.g.", not wanting to frighten the OP too much ;-)
John Machin
A: 

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.

jedi_coder