我现在在我的国际象棋项目中使用python-chess。我想我已经找到了通过直接定义获取它的用法。例如chess.Board().piece_at(chess.B1),但是我想通过一个变量来获取它,有什么方法可以让我获取块类型吗?
例如source = 'g1'如何获取源码的块类型?
发布于 2019-04-12 19:44:01
你应该有权访问块对象,你可以像这样从块对象中获得块类型。(您可能还需要符号或颜色)
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)发布于 2021-03-25 12:45:40
假设您想在: F3上找到哪一块,您可以使用chess.parse_square()来获取任意正方形的索引,方法是传递: F3、A1、A2等等。
下面是我们如何在特定的方块中找到一块:
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)它将返回:n(在本例中,我们将骑士从g1移动到f3。)
发布于 2019-10-27 21:38:14
作为Play Chess with a WebCam的作者,我遇到了同样的问题。
中介绍了几个间接选项
https://python-chess.readthedocs.io/en/latest/core.html
下面的代码请参阅
https://github.com/WolfgangFahl/play-chess-with-a-webcam/blob/master/src/test_Board.py
使用按行/列访问,但您也可以使用core中提供的等级/文件间接地址进行任何其他映射。只要你的squareIndex在0..63的范围内,你就不会有问题。
chess.SQUARES[squareIndex]是如何获得可以用作board.piece_at(board.piece_at)输入的正方形的。
代码
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'输出
♜ ♞ ♝ ♛ ♚ ♝ ♞ ♜
♟ ♟ ♟ ♟ ♟ ♟ ♟ ♟
· · · · · · · ·
· · · · · · · ·
· · · · · · · ·
· · · · · · · ·
♙ ♙ ♙ ♙ ♙ ♙ ♙ ♙
♖ ♘ ♗ ♕ ♔ ♗ ♘ ♖
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 | whitehttps://stackoverflow.com/questions/55650138
复制相似问题