0

我现在将 python-chess 用于我的国际象棋项目。我想我已经找到了通过直接定义来获取它的用法。例如chess.Board().piece_at(chess.B1),但我想通过一个变量来获得它,有什么方法可以让我获得片段类型。

例如source = 'g1',如何获取源的片段类型?

4

4 回答 4

1

您应该可以访问片段对象,并且可以像这样从片段对象中获取片段类型。(您可能还需要符号或颜色)

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)
于 2019-04-12T11:44:00.600 回答
1

假设您想在 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。)

于 2021-03-25T04:45:40.030 回答
0

作为使用网络摄像头下棋的作者,我遇到了同样的问题。

有几个间接选项描述

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

下面的代码见

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

使用按行/列访问,但您也可以使用核心中提供的排名/文件间接进行任何其他映射。只要你最终得到一个在 0..63 范围内的 squareIndex,你就可以了。

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
于 2019-10-27T13:38:14.090 回答
-2

我真的没有找到任何优雅的解决方案。但我发现它也接受数字输入,但格式特殊。

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() 用于获取其符号。

于 2019-05-14T09:46:50.530 回答