Programmer Puzzle: Encoding a chess board state throughout a game

前端 未结 30 1535
闹比i
闹比i 2021-01-29 17:16

Not strictly a question, more of a puzzle...

Over the years, I\'ve been involved in a few technical interviews of new employees. Other than asking the standard \"do you

30条回答
  •  遥遥无期
    2021-01-29 17:49

    The position on a board can be defined in 7 bits (0-63 , and 1 value specifying it is not on the board anymore). So for every piece on the board specify where it is located.

    32 pieces * 7 bits = 224 bits

    EDIT: as Cadrian pointed out... we also have the 'promoted pawn to queen' case. I suggest we add extra bits at the end to indicate which pawn have been promoted.

    So for every pawn which has been promoted we follow the 224 bits with 5 bits which indicate the index of the pawn which has been promoted, and 11111 if it is the end of the list.

    So minimal case (no promotions) is 224 bits + 5 (no promotions). For every promoted pawn add 5 bits.

    EDIT: As shaggy frog points out, we need yet another bit at the end to indicate whose turn it is ;^)

提交回复
热议问题