Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python: Chess moves validation

Tags:

python

chess

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)

like image 759
Oleg Tarasenko Avatar asked May 18 '10 19:05

Oleg Tarasenko


2 Answers

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

like image 107
John Machin Avatar answered Sep 18 '22 12:09

John Machin


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!

like image 37
mofoe Avatar answered Sep 21 '22 12:09

mofoe