Draughts Move Judge
MediumOnline Assessment
List every legal move for one side in a position of English draughts (checkers).
Board. board is 8 strings of 8 characters, row 0 being rank 8 (the top) and column 0 being file a. w and b are white and black men, W and B are kings, . is an empty square. A square is named by its file and rank, so board[7][0] is a1. White moves toward rank 8, black toward rank 1.
Moves. All movement is diagonal.
- A man moves one square forward diagonally (two choices) onto an empty square. A king moves one square in any of the four diagonal directions.
- A jump passes over an adjacent enemy piece (man or king) and lands on the empty square directly behind it; the jumped piece is captured. Men jump only forward, kings in all four directions. A piece cannot be jumped twice in one move, and a jump cannot land on an occupied square.
- Capturing is mandatory: if the player has any jump available, every legal move is a capture and plain moves are not legal.
- A capturing move must keep jumping: after each jump, if the same piece can jump again it must, and the move ends only when no further jump is possible. When there is a choice, either jump may be taken (the longest sequence is not required). The square the piece started on counts as empty during the move.
- If a man lands on the far rank (rank 8 for white, rank 1 for black) during a capture, it becomes a king and the move ends there.
Input: board and player ("w" or "b"), the side to move.
Output: all legal moves as strings, sorted in ascending order. A plain move is "c3-d4", a capture lists the start square followed by each landing square: "c3xe5xg7". If the player has no legal move, return an empty list.
Example 1
- Input:
- [["........","........","...b.b..","........","...b....","..w...w.","........","........"],"w"]
- Output:
- ["c3xe5xc7","c3xe5xg7"]
- Explanation
- White to move with men on c3 and g3. The man on c3 can jump the black man on d4 and land on e5, so capturing is mandatory and no plain move (such as g3-f4) is legal. From e5 the move must continue: it can jump f6 and land on g7, or jump d6 and land on c7. Both complete sequences are listed; stopping on e5 is not allowed.
Example 2
- Input:
- [["........","..b.b...","........","..b.b...","...W....","w.......","........","........"],"w"]
- Output:
- ["d4xb6xd8xf6xd4","d4xf6xd8xb6xd4"]
- Explanation
- White king d4 is surrounded by a ring of black men on e5, e7, c7 and c5. The capture can go clockwise: d4 over e5 to f6, over e7 to d8, over c7 to b6, over c5 back to d4, the king's own starting square, which is empty again during the move. Going the other way round gives the second sequence. The white man on a3 has plain moves, but they are not legal because a capture exists.
Example 3
- Input:
- [["........","..b.b...",".w......","..b.....","........","........","........","......w."],"w"]
- Output:
- ["b6xd8"]
- Explanation
- The white man b6 jumps the black man on c7 and lands on d8, the last rank, so it is crowned and the move ends there even though a king on d8 could jump e7. The black man on c5 sits behind the white man's direction of travel, and men cannot capture backwards, so that is not a capture. The man on g1 has plain moves, but they are not legal while a capture exists.
Constraints
- board is 8 strings of 8 characters from
.wWbB, row 0 is rank 8; 0 to 24 pieces in total, every piece sits on a dark square (a1, c1, b2, ... in standard coloring) - player is
"w"or"b"