TopCoder problem "GomokuBoardChecker" used in SRM 261 (Division II Level Three)



Problem Statement

    

Gomoku, also called go-moku (Japanese: Gomoku Narabe, "five points") is a tic-tac-toe-like game played on a rectangular board divided into unit squares. There are two players, and each of them has an infinite supply of pieces of his color. They alternately place their pieces on the board in an attempt to place five (or more) of their own pieces in a row (horizontally, vertically, or diagonally). As soon as one of the players has reached this goal, the game ends and he is the winner. If and only if no player has reached the goal and the whole board is full, the game is a draw.

You will be given a String[] board containing a position in the game, with the letter 'X' used to denote pieces of one player, the letter 'O' (not zero!) the pieces of the other player and '.' (period) to denote empty spaces. (Note that you don't know whether the player that started the game used 'O's or 'X's.)

Your task is to write a function that decides:

  • whether this is a valid position (i.e., one that could arise if both players played according to the rules), and
  • whether a player already won.

If the position is not valid, return the string "INVALID". If the position is valid and represents a draw game, return the string "DRAW". If the position is valid and one of the players won, return "X WON" or "O WON" respectively. Otherwise, return the string "IN PROGRESS".

 

Definition

    
Class:GomokuBoardChecker
Method:check
Parameters:String[]
Returns:String
Method signature:String check(String[] board)
(be sure your method is public)
    
 

Notes

-Either of the players could have started the game.
-Each square of the board may contain at most one piece.
 

Constraints

-board will contain between 1 and 15 elements, inclusive.
-Each element of board will contain between 1 and 15 characters, inclusive.
-Each element of board will contain the same number of characters.
-Each element of board will contain only the characters 'X', 'O' and '.' .
 

Examples

0)
    
{"O.X..",
 ".OX..",
 "X.O..",
 "X..O.",
 "....O"}
Returns: "O WON"
Clearly, player "O" started the game and won.
1)
    
{"O.X..",
 ".OX..",
 "X.O..",
 "X..O.",
 "...XO"}
Returns: "O WON"
This time, player "X" started the game, but player "O" won.
2)
    
{"OOOOO",
 "OOOOO",
 "OOOOO",
 "OOOOO",
 "OOOOX"}
Returns: "INVALID"
This is an invalid position because players have to alternate placing the pieces.
3)
    
{"O...",
 "...X",
 "....",
 "...."}
Returns: "IN PROGRESS"
According to the problem statement, this is a game in progress (even though nobody can win on a 4x4 board).
4)
    
{"O.X.O.",
 ".OX.O.",
 "X.O.O.",
 "X..OO.",
 "..XXOX",
 "XXXXOO"}
Returns: "O WON"
5)
    
{"O.XX.O",
 ".OX..O",
 "X.O..O",
 "X..O.O",
 "..XXOO",
 "XXXX.O"}
Returns: "INVALID"
6)
    
{"XXXXO",
 "OOOOX",
 "XXXXO",
 "OOOOX",
 "XOXOX"}
Returns: "DRAW"

Problem url:

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

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=7995&pm=4652

Writer:

misof

Testers:

PabloGilberto , lbackstrom , brett1479 , Olexiy

Problem categories:

Brute Force, Simple Search, Iteration