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 522 forks source link

Legal moves for single square giving wrong output #770

Closed Garrett2532 closed 3 years ago

Garrett2532 commented 3 years ago

Hello I am having trouble with the following code

import chess

board =chess.Board()
cfile = 3                                      #file is the A in A1 (0-7) to be done with buttons later 
rank = 1                                       #rank is the 1 in A1 (0-7)

print(board)                                   #print ascii version of current board state

spot  = chess.square(cfile, rank)

legalMoves = (list(board.generate_legal_moves(chess.square(cfile,rank))))
print(chess.square_name(spot))                #converts from file and rank to name like A1 B5 
print("has legal moves")
print(legalMoves)

It gives an output like

r n b q k b n r
p p p p p p p p
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
P P P P P P P P
R N B Q K B N R
d2
has legal moves
[Move.from_uci('b1c3'), Move.from_uci('b1a3')]

It is giving me moves from b1 but giving the proper square d2 this happens with other squares as well. Its probally a simple fix but I have been stuck on this for hours

Thanks

niklasf commented 3 years ago

generate_legal_moves(from_mask=...) takes a bitboard (i.e. a set of squares), not a single square. generate_legal_moves(from_mask=chess.BB_SQUARE[spot]) should work.

Garrett2532 commented 3 years ago

This worked! Thank you very much!