Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
FEN to 2dArray in Python
#1
Hey, does someone have any idea, how i could convert an FEN input like this: [ bRbNbBbQbKbBbNbR/bpbpbpbpbpbpbpbp/8/8/8/8/wpwpwpwpwpwpwpwp/wRwNwBwQwKwBwNwR ]
to an array like this?
`
['bR', 'bN', 'bB', 'bQ', 'bK', 'bB', 'bN', 'bR'],
['bp', 'bp', 'bp', 'bp', 'bp', 'bp', 'bp', 'bp'],
['--', '--', '--', '--', '--', '--', '--', '--'],
['--', '--', '--', '--', '--', '--', '--', '--'],
['--', '--', '--', '--', '--', '--', '--', '--'],
['--', '--', '--', '--', '--', '--', '--', '--'],
['wp', 'wp', 'wp', 'wp', 'wp', 'wp', 'wp', 'wp'],
['wR', 'wN', 'wB', 'wQ', 'wK', 'wB', 'wN', 'wR']
`
I'm new to python and I found several examples to transform a 2d array to FEN notation, but i cannot find any way to do it vice versa. :(

Thank you for any help.
Reply
#2
This will convert what you have here but most likely will need significant modification to allow for all possible board configurations. At lest it's a start.. right?

board_fen = ['bRbNbBbQbKbBbNbR/bpbpbpbpbpbpbpbp/8/8/8/8/wpwpwpwpwpwpwpwp/wRwNwBwQwKwBwNwR']
split_board = board_fen [0].split ('/')
final_board = []
for row in split_board :
	if row [0] == '8' : 
		final_board.append (['--'] * 8)
	else :
		board_row = [(row [i: i+2]) for i in range (0, len (row), 2)]
		final_board.append (board_row)

for row in final_board :
	print (row)
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020