So I have two fens. One gotten before the move and one after. How can I get the played move by comparing these 2 fens to each other and returning the answer in uci or any other format. I am using the python-chess library so maybe there is a way to get the played move by comparing two different board objects as well?
Asked
Active
Viewed 740 times
1 Answers
2
I figured it out! I used the board objects that the chess python library makes and parsed those for the UCI of the move played.
nums = {1:"a", 2:"b", 3:"c", 4:"d", 5:"e", 6:"f", 7:"g", 8:"h"}
def get_uci(board1, board2, who_moved):
str_board = str(board1).split("\n")
str_board2 = str(board2).split("\n")
move = ""
flip = False
if who_moved == "w":
for i in range(8)[::-1]:
for x in range(15)[::-1]:
if str_board[i][x] != str_board2[i][x]:
if str_board[i][x] == "." and move == "":
flip = True
move+=str(nums.get(round(x/2)+1))+str(9-(i+1))
else:
for i in range(8):
for x in range(15):
if str_board[i][x] != str_board2[i][x]:
if str_board[i][x] == "." and move == "":
flip = True
move+=str(nums.get(round(x/2)+1))+str(9-(i+1))
if flip:
move = move[2]+move[3]+move[0]+move[1]
return move

Fancy129
- 49
- 3
-
Nice, great work! Does this also cover moves where King and Rock swap or when a pawn gets upgraded to a queen? – Martin S Mar 07 '23 at 14:34
-
@MartinS Hi! This was long ago and funnily enough I had this exact problem only a few days ago again! I decided to rewrite the code since this one is not very good and doesn't work for all cases. Please see the edited answer for an updated version that should work for most if not all cases. – Fanfer123 Mar 10 '23 at 11:42