I am trying to create a neural network to play chess, but first, I need to convert the chess board to a list of integers. I am using the python-chess module for the chess board and game. I currently have a chess board class, but cannot find a method to convert it into a list.
I have tried to use the chess_board.epd()
method, but that returns a formatting scheme that is hard to convert.
Here is the code that I need:
board = chess.Board() # Define board object
board.convert_to_int() # Method I need
Right now, with the .epd()
method I get "rnbqkbnr/pppppppp/8/8/8/5P2/PPPPP1PP/RNBQKBNR b KQkq -"
As you can see, it is very difficult to parse and convert to a list of integers as there are /8/
's and /5P2/
.
The expected output is something like this (goes row by row):
[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]
For example, these can be what integers map to the peices:
pawn - 1
knight - 2
bishop - 3
rook - 4
queen - 5
king - 6
And white can be positive integers and black can be negative integers.
Here's another version, using the unicode() method:
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')
]
I just read the documntation of the chess
module and created a simple abstract Class
as you want.
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)
Note:
Uppercase letters are for white pieces and lowercase letters are for black pieces. And 0 are for empty spaces in the chess board.
I made a function to convert the chess.Board object into a matrix
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
This returns a matrix or nested lists:
Output:
[['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']]
I have posted the question on Github , and I found I got a more elegant solution there. It was the following:
>>> import chess
>>> board = chess.Board()
>>> [board.piece_type_at(sq) for sq in chess.SQUARES] # Get Type of piece
[4, 2, 3, 5, 6, 3, 2, 4, 1, 1, 1, 1, 1, 1, 1, 1, ...]
Note the above version does not include negatives, so here is the imporved version:
def convert_to_int(board):
l = [None] * 64
for sq in chess.scan_reversed(board.occupied_co[chess.WHITE]): # Check if white
l[sq] = board.piece_type_at(sq)
for sq in chess.scan_reversed(board.occupied_co[chess.BLACK]): # Check if 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]
"""
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.