niklasf / python-chess

A chess library for Python, with move generation and validation, PGN parsing and writing, Polyglot opening book reading, Gaviota tablebase probing, Syzygy tablebase probing, and UCI/XBoard engine communication
https://python-chess.readthedocs.io/en/latest/
GNU General Public License v3.0
2.4k stars 521 forks source link

Cannot replay legal moves #15

Closed vhf closed 9 years ago

vhf commented 9 years ago

Hi,

Nice library you got there, I really like its ease of use. Unfortunately I'm having trouble with the push_san() function. Here is a sample of code :

from __future__ import print_function
import chess

board = chess.Bitboard("k7/pp6/8/8/8/8/8/3R3K w - - 0 1")

for move in board.legal_moves:
    pprint(move)
    try:
        board.push_san(str(move))
        board.fen()
        board.pop()
    except:
        print('Bad move')

For each legal move, it says "Bad move".

niklasf commented 9 years ago

Hi,

this is because board.push_san() strictly expects standard algebraic notation like 'Nf6' or 'e4' or 'O-O'. Standard algebraic notation depends on the position, for example a move like 'Re1' might sometimes have to be qualified with 'Rfe1'.

str(move) or move.uci() gets the move in position independent UCI notation, like 'g8f6' 'e2e4' or 'e1g1'.

What to do instead?

Best regards Niklas

vhf commented 9 years ago

Hi,

Many thanks for your answer. I'll close this issue.

Best -vhf