- // Update piece list
- pieceList[them][capture][index[capsq]] = pieceList[them][capture][pieceCount[them][capture]];
- index[pieceList[them][capture][index[capsq]]] = index[capsq];
+ // Update piece list, move the last piece at index[capsq] position
+ //
+ // WARNING: This is a not perfectly revresible operation. When we
+ // will reinsert the captured piece in undo_move() we will put it
+ // at the end of the list and not in its original place, it means
+ // index[] and pieceList[] are not guaranteed to be invariant to a
+ // do_move() + undo_move() sequence.
+ Square lastPieceSquare = pieceList[them][capture][pieceCount[them][capture]];
+ index[lastPieceSquare] = index[capsq];
+ pieceList[them][capture][index[lastPieceSquare]] = lastPieceSquare;
+ pieceList[them][capture][pieceCount[them][capture]] = SQ_NONE;