我现在在我的国际象棋项目中使用python-chess。
我想我已经找到了通过直接定义获取它的用法。
例如chess.Board().piece_at(chess.B1)
,但我想通过变量获取它,我有什么办法获取样片类型。
例如source = 'g1'
如何获取来源的作品类型?
答案 0 :(得分:0)
您应该有权访问计件对象,并且您可以像这样从计件对象获取计件类型。 (您可能还需要符号或颜色)
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)
答案 1 :(得分:0)
我真的没有找到任何优雅的解决方案。但是我发现它也接受数字输入,但格式特殊。
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',
]
这是黑板上的命名,因此您可以使用chess.Board().piece_at(squares.index(source.capitalize())).symbol()
以获得其符号。
答案 2 :(得分:0)
假设你想在以下位置找到什么片段: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。)
答案 3 :(得分:-1)
与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中提供的rank / file间接进行任何其他映射。只要您得到的SquareIndex范围为0..63,就可以了。
chess.SQUARES[squareIndex]
然后是如何获得平方并用作board.piece_at(square)的输入
代码
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 | white