Play a text-based game of Hex. Input/Output Specification:
- Print the game directions on different lines as follows:
THE GAME OF HEX
Each turn, a player places a piece in an empty position using
algebraic notation (e.g. "b4"). The first player seeks to
connect the bottom row to the top row with a path of "*" pieces.
The second player seeks to connect the leftmost column to
the rightmost column with a path of "O" pieces. The command
"undo" will undo the previous move.
- Prompt the user with "Board size? " and read an integer board size
(and the following newline) from the input.
- Create a new HexState of the given size and create an empty Stack
of HexState objects for keeping a stack of previous states for undo
- While the state has no winner, iterate:
-- Print a newline and the state's toString representation.
-- Make a copy of the current state using the state's copy
-- Prompt the user with "Position? ", and read the next line of user
input, trimming it, and converting it to lowercase.
-- Suppose that this String input variable is called "pos". Next see
whether it's an invalid input String with the condition
"(!pos.matches("(undo)|([a-z]\\d+)"))". If this condition is met,
the input is invalid, and you should simply print "Invalid input."
-- Otherwise, if the input equals "undo", then the user has indicated
a desire to undo the previous move. If the undo stack is empty,
print "Cannot undo." Otherwise, replace the current state with
the state popped from the undo stack.
-- Otherwise, the input is an algebraic notation position for play.
If a call to play at the given position of the state returns true
(indicating a successful legal move), then push the state copy you
made earlier onto the undo stack. If it is an illegal play, then
print "Illegal play."
- After the game is over, print a newline and the final board String