簡體   English   中英

如何在 python-chess 中獲得一塊

[英]How to get a piece in python-chess

我現在將 python-chess 用於我的國際象棋項目。 我想我已經找到了通過直接定義獲取它的用法。 例如chess.Board().piece_at(chess.B1)但我想通過一個變量來獲取它,有什么方法可以讓我獲取棋子類型。

例如source = 'g1'如何獲取源的片段類型?

您應該有權訪問 piece 對象,並且可以像這樣從 piece 對象獲取 piece 類型。 (您可能還需要符號或顏色)

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)

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

作為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

使用行/列訪問,但您也可以使用核心中提供的等級/文件間接訪問進行任何其他映射。 只要您最終得到 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

我真的沒有找到任何優雅的解決方案。 但我發現它也接受數字輸入,但格式特殊。

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()來獲取其符號。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM