What does stockfish.py log as a move when promoting?

129 Views Asked by At

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

1

There are 1 best solutions below

0
Dogbert On

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