0

I now use python-chess for my chess project. I think I have found the usage of getting it through direct definition. e.g. chess.Board().piece_at(chess.B1) but I want to get it through a variable, is there any way for me to get the piece type.

e.g.source = 'g1'how to get the piece type for source?

Michael Yang
  • 109
  • 1
  • 6

4 Answers4

2

suppose you want to find what piece at: F3 you can use chess.parse_square() to get the index of any square by passing: F3, A1, A2 and so on.

Here's how we will find a piece at specific square:

import chess
import chess.engine
import chess.polyglot

board = chess.Board()

opponent_move = board.parse_san('g1f3')
board.push(opponent_move)
print(board)

piece = board.piece_at(chess.parse_square('f3'))

print(piece)

its Gonna return: N (in this Case as we move our knight from g1 to f3.)

Abdul.Moqueet
  • 902
  • 12
  • 19
1

You should have access to piece object and you can get piece type from piece object like this. (You might need the symbol or color as well)

import chess
board = chess.Board()
piece = board.piece_at(chess.B1)
piece_type = piece.piece_type
piece_color = piece.color
piece_symbol = piece.symbol()

print(piece_type)
print(piece_symbol)
print(piece_color)
Moldovan Daniel
  • 1,521
  • 14
  • 23
  • Sorry, maybe I haven't expressed my question clearly. I was looking for a way to replace `chess.B1`. I want to get the information of a piece in a given square through variables. I wonder if I can store B1 into a variable and get different piece types as I change the variable. – Michael Yang Apr 12 '19 at 17:55
0

As the author of Play Chess with a WebCam i ran into the same problem.

There are several indirection options described in

https://python-chess.readthedocs.io/en/latest/core.html

The code below see

https://github.com/WolfgangFahl/play-chess-with-a-webcam/blob/master/src/test_Board.py

uses access by row/col but you could also do any other mapping using the rank/file indirections made available in core. As long as you end up with a squareIndex in the range from 0..63 you'll be fine.

chess.SQUARES[squareIndex]

is how you then get the square which you can use as input for board.piece_at(square)

code

def test_PieceAt():
    """
    see https://stackoverflow.com/questions/55650138/how-to-get-a-piece-in-python-chess
    see https://python-chess.readthedocs.io/en/latest/core.html """
    board = chess.Board()
    print (board.unicode())
    print(" square | row | col | type | piece | color | field")
    print("--------+-----+-----+------+-------+-------+------")
    for row in range(0,8):
      for col in range(0,8):
        squareIndex=row*8+col
        square=chess.SQUARES[squareIndex]
        piece = board.piece_at(square)
        fieldColor=(col+row)%2==1
        if piece is None:
           assert row in {2,3,4,5}
        else:
           print("%7d | %3d | %3d | %4d | %5s | %4s | %4s" % (square,row,col,piece.piece_type,piece.symbol(),"white" if piece.color else "black","black" if col%2!=row%2 else "white"))
           if row in {0,1}:
              assert piece.color==chess.WHITE
              # white symbols are upper case
              assert ord(piece.symbol())>ord('A') and ord(piece.symbol())<ord('Z')
           if row in {6,7}:
              assert piece.color==chess.BLACK
              # black symbols are lower case
              assert ord(piece.symbol())>ord('a') and ord(piece.symbol())<ord('z'

output

♜ ♞ ♝ ♛ ♚ ♝ ♞ ♜
♟ ♟ ♟ ♟ ♟ ♟ ♟ ♟
· · · · · · · ·
· · · · · · · ·
· · · · · · · ·
· · · · · · · ·
♙ ♙ ♙ ♙ ♙ ♙ ♙ ♙
♖ ♘ ♗ ♕ ♔ ♗ ♘ ♖
 square | row | col | type | piece | color | field
--------+-----+-----+------+-------+-------+------
      0 |   0 |   0 |    4 |     R | white | white
      1 |   0 |   1 |    2 |     N | white | black
      2 |   0 |   2 |    3 |     B | white | white
      3 |   0 |   3 |    5 |     Q | white | black
      4 |   0 |   4 |    6 |     K | white | white
      5 |   0 |   5 |    3 |     B | white | black
      6 |   0 |   6 |    2 |     N | white | white
      7 |   0 |   7 |    4 |     R | white | black
      8 |   1 |   0 |    1 |     P | white | black
      9 |   1 |   1 |    1 |     P | white | white
     10 |   1 |   2 |    1 |     P | white | black
     11 |   1 |   3 |    1 |     P | white | white
     12 |   1 |   4 |    1 |     P | white | black
     13 |   1 |   5 |    1 |     P | white | white
     14 |   1 |   6 |    1 |     P | white | black
     15 |   1 |   7 |    1 |     P | white | white
     48 |   6 |   0 |    1 |     p | black | white
     49 |   6 |   1 |    1 |     p | black | black
     50 |   6 |   2 |    1 |     p | black | white
     51 |   6 |   3 |    1 |     p | black | black
     52 |   6 |   4 |    1 |     p | black | white
     53 |   6 |   5 |    1 |     p | black | black
     54 |   6 |   6 |    1 |     p | black | white
     55 |   6 |   7 |    1 |     p | black | black
     56 |   7 |   0 |    4 |     r | black | black
     57 |   7 |   1 |    2 |     n | black | white
     58 |   7 |   2 |    3 |     b | black | black
     59 |   7 |   3 |    5 |     q | black | white
     60 |   7 |   4 |    6 |     k | black | black
     61 |   7 |   5 |    3 |     b | black | white
     62 |   7 |   6 |    2 |     n | black | black
     63 |   7 |   7 |    4 |     r | black | white
Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186
-2

I didn't really find any elegant solution for it. But I found that it also accepts the input of numbers but in a special format.

squares = [
            'A1', 'B1', 'C1', 'D1', 'E1', 'F1', 'G1', 'H1',
            'A2', 'B2', 'C2', 'D2', 'E2', 'F2', 'G2', 'H2',
            'A3', 'B3', 'C3', 'D3', 'E3', 'F3', 'G3', 'H3',
            'A4', 'B4', 'C4', 'D4', 'E4', 'F4', 'G4', 'H4',
            'A5', 'B5', 'C5', 'D5', 'E5', 'F5', 'G5', 'H5',
            'A6', 'B6', 'C6', 'D6', 'E6', 'F6', 'G6', 'H6',
            'A7', 'B7', 'C7', 'D7', 'E7', 'F7', 'G7', 'H7',
            'A8', 'B8', 'C8', 'D8', 'E8', 'F8', 'G8', 'H8',
        ]

This is the naming on the board, so you can use chess.Board().piece_at(squares.index(source.capitalize())).symbol() for getting its symbol.

JJJ
  • 32,902
  • 20
  • 89
  • 102
Michael Yang
  • 109
  • 1
  • 6