TopCoder problem "RussianCheckers" used in SRM 416 (Division I Level Three)



Problem Statement

    

Russian Checkers is a game played on an 8x8 board of alternating dark and light squares. The square in the lower-left corner is dark, and only dark squares are used in the game. Columns are labelled 'a' to 'h' from left to right, and rows are labelled '1' to '8' from bottom to top. Each cell is denoted by its column followed by its row (for example, "a4" or "f8"). The following image shows an empty board:

There are two players taking alternate turns. One player controls the white pieces and the other controls the black. The white player sits at the bottom of the board and the black player sits at the top. Every piece is exactly in one of the two types: "man" and "king".

There are two types of moves - a simple move and a capturing move. The player can make a simple move in his turn only if there are no available capturing moves for him in this turn.

A simple move for a "man" consists of moving diagonally forward to an adjacent empty square. White pieces move forward by moving to higher numbered rows, and black pieces move forward by moving to lower numbered rows. The following image shows four possible simple moves:

A capturing move for a "man" consists of jumping over a diagonally adjacent opponent piece (this piece is considered to be captured) and landing in the empty square immediately beyond that piece. After landing, if another capture is possible from that new square, it must be performed as part of the same turn. This can happen multiple times within the same turn. If multiple captures are possible from a position, any one of them can be performed. When capturing, pieces can move in any of the four diagonal directions, and can change directions after each landing. After the end of a capturing move all captured pieces are removed from the board. It is not allowed to capture the same opponent piece more than once during the same turn. The image below shows two possible capturing moves - c3 to e5 to c7, and c3 to e5 to g3. Note that, for example, the capturing move c3 to e5 to g3 to e5 to c7 is not valid because it requires jumping two times over the piece at f4.

When a "man" reaches the far side of the board (row 8 for white pieces, and row 1 for black pieces), it is promoted to a "king". A "king" moves differently than a "man". A simple move for a "king" consists of moving one or more squares in any of the four diagonal directions. All the squares in the path, including the ending square, must be empty.

A capturing move for a "king" consists of moving zero or more squares diagonally, jumping over an opponent piece, and then moving zero or more additional squares along the same path. With the exception of the square containing the jumped opponent piece, all squares in the path must be empty, and the path must form a straight diagonal line. When the "king" moves zero or more additional squares after jumping over the opponent, it must stop at a square from which it can perform another capture if possible. If there are multiple such squares, any one of them can be chosen. If there are no such squares, it can stop at any empty square. The "king" must continue making captures within the same turn as long as possible captures exist. As with "men", all captured pieces are removed from the board after the end of move, and it is not allowed to capture the same opponent piece more than once during the same turn. The following image shows the three possible moves a white king can make in a single turn - a1 to e5 to c7, a1 to e5 to b8, and a1 to f6 to d8:

If a "man" reaches the far side of the board as the result of a capturing move, it becomes a "king" and must continue capturing if possible, but as a "king". The following image shows a white "man" jumping a black piece, becoming a "king", and continuing to capture as a "king":

Your task is to write the first part of an AI engine. Given the current configuration of the board, and the player with the current turn, you must determine all the possible moves for that player. The board will be given in the String[] board, the first element of which describes row 8 from left to right, the second element of which describes row 7 from left to right, etc. '.' characters denote empty cells, 'b' and 'w' denote black and white "men", respectively, and 'B' and 'W' denote black and white "kings", respectively. The current player is given in the String turn, and will be either "BLACK" or "WHITE" (quotes for clarity). Return a String[] containing all the possible moves for the current player in lexicographical order. Each move is described by the sequence of cells in which the piece lands, starting with the piece's original position. In a simple move, cells are separated by '-' characters, and in a capturing move, cells are separated by ':' characters. See examples for further clarification.

 

Definition

    
Class:RussianCheckers
Method:listMoves
Parameters:String[], String
Returns:String[]
Method signature:String[] listMoves(String[] board, String turn)
(be sure your method is public)
    
 

Notes

-It is possible that the position described by board can never occur in real life. For example in a real Russian checkers game there will never be more than 12 pieces for each side, but in our game it is possible.
-A String A is lexicographically less than a String B if A is a prefix of B, or if, for some character at index i, A[i] < B[i], and for all j < i, A[j] = B[j].
 

Constraints

-board will contain exactly 8 strings.
-Each element of board will contain exactly 8 characters long.
-Each character in board will be one of {'.', 'w', 'W', 'b', 'B'}.
-board will contain '.' in cells that correspond to light board cells.
-turn will be either "BLACK" or "WHITE" (quotes for clarity).
-The first element of board will not contain lowercase 'w'.
-The last element of board will not contain lowercase 'b'.
 

Examples

0)
    
{".b.b.b.b",
 "b.b.b.b.",
 ".b.b.b.b",
 "........",
 "........",
 "w.w.w.w.",
 ".w.w.w.w",
 "w.w.w.w."}
"WHITE"
Returns: {"a3-b4", "c3-b4", "c3-d4", "e3-d4", "e3-f4", "g3-f4", "g3-h4" }
This is the starting game position, and there are 7 distinct legal moves.
1)
    
{".......B",
 "......w.",
 "........",
 "........",
 ".......W",
 "........",
 "...W.W..",
 "........"}
"BLACK"
Returns: {"h8:c3:e1:g3", "h8:d4:g1" }
Note that the black "king" must capture the white piece at g7 and then continue capturing more pieces. There are two alternatives for the second piece it captures - it can capture either d2 or f2. If it captures d2, it must capture f2 next. If it captures f2, the turn is over. Note that cells in capturing moves are separated with ':'.
2)
    
{"........",
 "........",
 "...b....",
 "........",
 "...b.b..",
 "..w.....",
 ".....w..",
 "........"}
"WHITE"
Returns: {"c3:e5:c7", "c3:e5:g3" }
Note that simple moves for the piece at f2 are not allowed because capturing moves exist for the piece at c3.
3)
    
{"........",
 "..b.....",
 ".w......",
 "......B.",
 "........",
 "........",
 "........",
 "........"}
"WHITE"
Returns: {"b6:d8:h4" }
The last picture in the problem description. A white "man" jumps over a black piece, becoming a "king", and continues to capture as a "king".
4)
    
{"........",
 "......b.",
 "........",
 "........",
 "...W....",
 "........",
 ".b......",
 "........"}
"WHITE"
Returns: {"d4:a1", "d4:h8" }
Jumping over the same opponent piece more than once during one capturing move is prohibited.
5)
    
{"........",
 "..w.w...",
 "........",
 "....w...",
 "........",
 "..B.w...",
 "........",
 "........"}
"BLACK"
Returns: {"c3:f6:d8:b6:f2", "c3:f6:d8:b6:g1" }
But visiting the same free cell more than once during one capturing move is allowed. In this case, the black "king" visits cell d4 twice.
6)
    
{".......b",
 "....w.w.",
 "........",
 "....b...",
 ".w.w.w..",
 "........",
 "...w.w..",
 "........"}
"BLACK"
Returns: 
{"e5:c3:a5",
"e5:c3:e1:g3:d6:a3",
"e5:c3:e1:g3:d6:f8:h6",
"e5:c3:e1:h4:d8",
"e5:g3:e1:c3:a5",
"e5:g3:e1:c3:f6:d8",
"h8:f6:d8" }
In this situation, there are lots of choices for capturing moves.

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=9857

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=13507&pm=9857

Writer:

xOberon

Testers:

PabloGilberto , Olexiy , ivan_metelsky , zhuzeyuan

Problem categories:

Recursion, Simulation