TopCoder problem "TextBoard" used in College Tour West China (Division I Level One)



Problem Statement

    

You are working on a text-based checkers program, and you need to write a module to display the state of the board to the user. The board has 8 rows labeled 1 to 8 from top to bottom, and 8 columns labeled A to H from left to right. The cells alternate colors black and white, and no two horizontally or vertically adjacent cells can have the same color. Cell A1 is black. Each piece on the board is either black or white, depending on the player who owns it. You want to draw the board by printing 8 rows of 8 characters where each character is one of the following:

  • '.' - an empty white cell.
  • '#' - an empty black cell.
  • 'W' - a cell with a white piece on it.
  • 'B' - a cell with a black piece on it.
For example, the initial state of the board would be printed like:
B.B.B.B.
.B.B.B.B
B.B.B.B.
.#.#.#.#
#.#.#.#.
.W.W.W.W
W.W.W.W.
.W.W.W.W

You are given a String[] pieces where each element describes the color and position of one piece on the board. Each element is formatted "<COLOR> <C><R>", where <COLOR> is either "WHITE" or "BLACK", <C> is the column ('A'-'H'), and <R> is the row ('1'-'8') (all quotes for clarity). Return a String[] containing exactly 8 elements, where the first element is the text representation of row 1, the second element is row 2, and so on.

 

Definition

    
Class:TextBoard
Method:draw
Parameters:String[]
Returns:String[]
Method signature:String[] draw(String[] pieces)
(be sure your method is public)
    
 

Notes

-Even though in a regular checkers game pieces are never placed on the white cells, your playing program may contain bugs so you want to print the pieces that landed up on the white cells too.
-Similarly you might get more than the usual 12 each of white or black pieces.
 

Constraints

-pieces will contain between 0 and 50 elements, inclusive.
-Each element of pieces will be formatted as described in the problem statement.
-No two pieces will be on the same cell.
 

Examples

0)
    
{"WHITE B2", "BLACK A3"}
Returns: 
{"#.#.#.#.",
".W.#.#.#",
"B.#.#.#.",
".#.#.#.#",
"#.#.#.#.",
".#.#.#.#",
"#.#.#.#.",
".#.#.#.#" }
A board with two pieces on it, a white one on B2 and a black one on A3.
1)
    
{"BLACK A1", "BLACK C1", "BLACK E1", "BLACK G1", "BLACK B2", "BLACK D2",
 "BLACK F2", "BLACK H2", "BLACK A3", "BLACK C3", "BLACK E3", "BLACK G3",
 "WHITE B6", "WHITE D6", "WHITE F6", "WHITE H6", "WHITE A7", "WHITE C7",
 "WHITE E7", "WHITE G7", "WHITE B8", "WHITE D8", "WHITE F8", "WHITE H8"}
Returns: 
{"B.B.B.B.",
".B.B.B.B",
"B.B.B.B.",
".#.#.#.#",
"#.#.#.#.",
".W.W.W.W",
"W.W.W.W.",
".W.W.W.W" }
The example from the problem statement.
2)
    
{"BLACK A1", "BLACK A2", "BLACK A3", "BLACK A4", "BLACK A5", "BLACK A6",
 "BLACK A7", "BLACK A8", "BLACK B1", "WHITE B2", "WHITE B3", "WHITE B4",
 "WHITE B5", "WHITE B6", "WHITE B7", "BLACK B8", "BLACK C1", "WHITE C2",
 "WHITE C7", "BLACK C8", "BLACK D1", "WHITE D2", "WHITE D7", "BLACK D8",
 "BLACK E1", "WHITE E2", "WHITE E7", "BLACK E8", "BLACK F1", "WHITE F2",
 "WHITE F7", "BLACK F8", "BLACK G1", "WHITE G2", "WHITE G3", "WHITE G4",
 "WHITE G5", "WHITE G6", "WHITE G7", "BLACK G8", "BLACK H1", "BLACK H2",
 "BLACK H3", "BLACK H4", "BLACK H5", "BLACK H6", "BLACK H7", "BLACK H8"}
Returns: 
{"BBBBBBBB",
"BWWWWWWB",
"BW#.#.WB",
"BW.#.#WB",
"BW#.#.WB",
"BW.#.#WB",
"BWWWWWWB",
"BBBBBBBB" }
Not really a valid situation.

Problem url:

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

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=12240&pm=9731

Writer:

slex

Testers:

PabloGilberto , Olexiy , ivan_metelsky

Problem categories:

String Manipulation