Skip to content

Is it possible to get a piece location by its name and color? #1069

Closed Answered by niklasf
HarshilPatel007 asked this question in Q&A
Discussion options

You must be logged in to vote
import chess

board = chess.Board()

for sq in board.pieces(chess.ROOK, chess.WHITE):
    print(chess.square_name(sq), chess.square_file(sq), 7 - chess.square_rank(sq))

Would print:

a1 0 7
h1 7 7

7 - chess.square_rank(sq) because ranks increase as you go up the board, whereas the coordinates you mention in your question are flipped.

Replies: 1 comment 5 replies

Comment options

You must be logged in to vote
5 replies
@HarshilPatel007
Comment options

@niklasf
Comment options

@HarshilPatel007
Comment options

@niklasf
Comment options

@HarshilPatel007
Comment options

Answer selected by HarshilPatel007
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants