有没有一种方法可以将python国际象棋棋盘转换为整数列表?

时间:2019-04-27 00:44:37

标签: python-3.x chess python-chess

我正在尝试创建一个下棋的神经网络,但首先,我需要将棋盘转换为整数列表。我正在将python-chess模块用于棋盘和游戏。我目前有一个国际象棋棋盘类,但是找不到将其转换为列表的方法。

我尝试使用chess_board.epd()方法,但是返回的格式格式很难转换。

这是我需要的代码:

board = chess.Board()  # Define board object
board.convert_to_int()  # Method I need

现在,通过.epd()方法,我得到了"rnbqkbnr/pppppppp/8/8/8/5P2/PPPPP1PP/RNBQKBNR b KQkq -"

如您所见,由于存在/8//5P2/,因此解析和转换为整数列表非常困难。

期望的输出是这样的(逐行):

[4, 2, 3, 5, 6, 3, 2, 4, 1, 1, 1, 1, 1, 1, 1, 1, ... -1, -1, -1, -1,-1, -1,-1, -1, -4, -2, -3, -5, -6, -3, -2, -4]

例如,这些可能是整数映射到peices的位置:

pawn - 1
knight - 2
bishop - 3
rook - 4
queen - 5
king - 6

白色可以是正整数,黑色可以是负整数。

4 个答案:

答案 0 :(得分:0)

我只是阅读了chess模块的文档,并根据需要创建了一个简单的抽象Class

import chess

class MyChess(chess.Board):

    mapped = {
        'P': 1,     # White Pawn
        'p': -1,    # Black Pawn
        'N': 2,     # White Knight
        'n': -2,    # Black Knight
        'B': 3,     # White Bishop
        'b': -3,    # Black Bishop
        'R': 4,     # White Rook
        'r': -4,    # Black Rook
        'Q': 5,     # White Queen
        'q': -5,    # Black Queen
        'K': 6,     # White King
        'k': -6     # Black King
        }

    def convert_to_int(self):
        epd_string = self.epd()
        list_int = []
        for i in epd_string:
            if i == " ":
                return list_int
            elif i != "/":
                if i in self.mapped:
                    list_int.append(self.mapped[i])
                else:
                    for counter in range(0, int(i)):
                        list_int.append(0)

注意:

大写字母表示白色,小写字母表示黑色。 而0表示棋盘上的空白。

答案 1 :(得分:0)

我做了一个函数,将Chess.Board对象转换为矩阵

def make_matrix(board): #type(board) == chess.Board()
    pgn = board.epd()
    foo = []  #Final board
    pieces = pgn.split(" ", 1)[0]
    rows = pieces.split("/")
    for row in rows:
        foo2 = []  #This is the row I make
        for thing in row:
            if thing.isdigit():
                for i in range(0, int(thing)):
                    foo2.append('.')
            else:
                foo2.append(thing)
        foo.append(foo2)
    return foo

这将返回矩阵或嵌套列表:

输出:

[['r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'], 
['p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'], 
['.', '.', '.', '.', '.', '.', '.', '.'], 
['.', '.', '.', '.', '.', '.', '.', '.'], 
['.', '.', '.', '.', '.', '.', '.', '.'], 
['.', '.', '.', '.', '.', '.', '.', '.'], 
['P', 'P', 'P', 'P', 'P', 'P', 'P', 'P'], 
['R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R']]

答案 2 :(得分:0)

这是使用unicode()方法的另一个版本:

def convert_to_int(board):
    indices = '♚♛♜♝♞♟⭘♙♘♗♖♕♔'
    unicode = board.unicode()
    return [
        [indices.index(c)-6 for c in row.split()]
        for row in unicode.split('\n')
    ]

答案 3 :(得分:-1)

我在Github上发布了问题,发现那里有一个更优雅的解决方案。是这样的:

>>> import chess
>>> board = chess.Board()
>>> [board.piece_type_at(sq) for sq in chess.SQUARES]
[4, 2, 3, 5, 6, 3, 2, 4, 1, 1, 1, 1, 1, 1, 1, 1, ...]

请注意,上述版本不包含底片,因此以下是改进版本:

def convert_to_int(board):
        l = [None] * 64
        for sq in chess.scan_reversed(board.occupied_co[chess.WHITE]):
            l[sq] = board.piece_type_at(sq)
        for sq in chess.scan_reversed(board.occupied_co[chess.BLACK]):
            l[sq] = -board.piece_type_at(sq)
        return [0 if v is None else v for v in l]

piece_type_list(chess.Board())

"""
Outpus:
[4, 2, 3, 5, 6, 3, 2, 4, 1, 1, 1, 1, 1, 1, 1, 1, None, None, None, None, None, None, None, None, None, None, None, None, 
None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None,
 -1, -1, -1, -1, -1, -1, -1, -1, -4, -2, -3, -5, -6, -3, -2, -4]
"""
相关问题