When a player castles by moving a King two or more spaces, and it doesn't yet know it is a King, it doesn't know this is a castling move, and it doesn't complete the castle.
I have addressed this in the following manner. First of all, every space on ranks 1 and 8 are flagged. Whenever a piece moves, its origin and destination are both unflagged, and whenever a castling move is made, all spaces on the back rank for that player are unflagged. This guarantees that castling moves can be made only by previously unmoved pieces, and that each player can castle only once. When a King castles by moving two or more spaces, it moves to the c file or to the g file. That means the spaces in between are empty, and the Rook will be the a, b, or h file. It is assumed to be a castling move if the moving piece has the King bit set but does not have the Queen bit set, and if the other piece has the Rook bit set. In this situation, it seems likely that it is a castling move.
However, it is conceivable that the Rook is on the a file while the King is on b, and the piece moving to c is a Rook. I could check that the a piece is not a Rook. This would make the likelihood of it being a castling move stronger. Or it may be enough that either a is not a Rook or the moving piece is not a Rook. If a is not a Rook, b could not be the King and the moving piece could not be a Rook, since the King goes between the two Rooks. Likewise, if the horizontally moving piece is not a Rook or a Queen, it must be a King.
Doing it this way, there is still a chance that an actual castling move could be interpreted as a Queen move. Usually, though, the player has to move enough pieces out of the way before he can castle by moving the King to the c or g file. This should usually give enough time for the Queen to be identified.
I have addressed this in the following manner. First of all, every space on ranks 1 and 8 are flagged. Whenever a piece moves, its origin and destination are both unflagged, and whenever a castling move is made, all spaces on the back rank for that player are unflagged. This guarantees that castling moves can be made only by previously unmoved pieces, and that each player can castle only once. When a King castles by moving two or more spaces, it moves to the c file or to the g file. That means the spaces in between are empty, and the Rook will be the a, b, or h file. It is assumed to be a castling move if the moving piece has the King bit set but does not have the Queen bit set, and if the other piece has the Rook bit set. In this situation, it seems likely that it is a castling move.
However, it is conceivable that the Rook is on the a file while the King is on b, and the piece moving to c is a Rook. I could check that the a piece is not a Rook. This would make the likelihood of it being a castling move stronger. Or it may be enough that either a is not a Rook or the moving piece is not a Rook. If a is not a Rook, b could not be the King and the moving piece could not be a Rook, since the King goes between the two Rooks. Likewise, if the horizontally moving piece is not a Rook or a Queen, it must be a King.
Doing it this way, there is still a chance that an actual castling move could be interpreted as a Queen move. Usually, though, the player has to move enough pieces out of the way before he can castle by moving the King to the c or g file. This should usually give enough time for the Queen to be identified.