0

So I was just messing around with the stockfish library and am converting the moves to a PGN text format and errored parsing the move

Right now every move comes in this format "a1b2" and I'm assuming a promotion would be something like this "c7c8=Q" but I'm not sure as it could be "c8=Q" as well. Here's some of the code

best_w = stockfish.get_top_moves(3)
best_w = best_w[random.randint(0,2)]['Move']
fgn_w = getfgn(best_w)

best_b = stockfish.get_top_moves(3)
best_b = best_w[random.randint(0,2)]['Move']
fgn_b = getfgn(best_b)

I didn't have it before but I now have these try statements to print what comes back if it ever happens again

def getfgn(move):
  try:
    piece = stockfish.get_what_is_on_square(move[:2])
  except:
    print(f'Promotion? {move}')
    #piece = stockfish.get_what_is_on_square(move[2:2])?? 
  capt = stockfish.will_move_be_a_capture(move)
  try:
    sq1 = move[:2]
  except:
    print(move)
    #sq1 = move[2:2]??
  sq1 = sq1[:1]
  sq2 = move[2:]

So now it will return f'{sq2}' or f'{sq1}x{sq2}'
c7 or bxc7
But I'm pretty sure it probably returned =Q instead

LittleCoder
  • 391
  • 1
  • 13
  • *"and I'm assuming a promotion would be"*: why *assume* and not actually see what happens when you play a promotion move? – trincot May 31 '23 at 18:49
  • Because I don't have an interface to play it on, I was having the computer play itself so I could convert the moves to fgn. I did get it tho. It's the row it's on 8 and then the piece so 'd8q' as example – ReliableAirRepair Jun 01 '23 at 22:59
  • In case of promotion, UCI moves are like f7f8q – LittleCoder Jul 05 '23 at 10:15

1 Answers1

1

The stockfish Python package communicates with an instance of Stockfish Engine using the UCI Protocol, and the the protocol uses Long Algebraic Notation for moves, which means promotions are encoded as a sequence of 5 characters: two for the source square, two for the destination square, and one for the piece that the pawn is being promoted to.

This can be tested using this FEN position where the best move is to promote:

from stockfish import Stockfish

stockfish = Stockfish()
stockfish.set_fen_position("8/1KPk4/8/8/8/8/8/8 w - - 0 1")
print(stockfish.get_best_move())

Output:

c7c8q
Dogbert
  • 212,659
  • 41
  • 396
  • 397