Problem Statement
a b c d e f g 6 . . . O . . . 5 . . . X . . . 4 . . . O . . . 3 . . . X . . . 2 . . . O . X . 1 . . X X O O XHere a '.' denotes an unoccupied square; the first player's stones and the second player's are represented by 'X' and 'O' respectively. In the example above, it's the second player's turn to drop a stone in one of the six valid positions: a1, b1, c2, e2, f3, and g2. The game ends when a player wins by placing at least four of his/her stones consecutively in at least one line, either horizontally, vertically, or diagonally. In the following example, the first player has successfully placed four stones in a line (d5-e4-f3-g2) thus winning the game.
a b c d e f g 6 . . . O . . . 5 . . . X . O . 4 . . O O X X O 3 . . O X O X X 2 O . X O X X X 1 O . X X O O XThe game may also end in a draw if the board is completely filled without anyone winning. After a game ends, the players are not allowed to drop more stones onto the board.
You will be given a
- "first player moves" - return this when it's the first player's turn to drop a stone.
- "second player moves" - return this when it's the second player's turn to drop a stone.
- "first player wins" - return this when the first player has won the game.
- "second player wins" - return this when the second player has won the game.
- "draw game" - return this when the board is full and neither player wins the game.
- "invalid" - return this when it's impossible to produce the given board without violating the rules (e.g. dropping stones in bad positions, playing out of turn, or dropping stones when the game has ended.)
Definition
- Class:
- ConnectFour
- Method:
- judge
- Parameters:
- String[]
- Returns:
- String
- Method signature:
- String judge(String[] board)
- (be sure your method is public)
Notes
- Connect Four on a 6x7 board has been solved independently by Victor Allis and James D. Allen. The first player can force a win with perfect play.
Constraints
- board will contain exactly 6 elements.
- Each element of board will have exactly 7 characters.
- Each character will be one of the 3 possible characters: '.', 'X', or 'O'.
Examples
{ ".......", ".......", ".......", ".......", ".......", "......." }
Returns: "first player moves"
Every game starts with an empty board where the first player may drop a stone ('X') in one of the seven columns.
{ ".......", ".......", ".......", "...X...", "...O...", "...X..." }
Returns: "second player moves"
After three turns, the second player should make a move again.
{ ".......", ".......", "X......", "OX.....", "XOXO...", "OXOX..." }
Returns: "first player wins"
The first player wins by connecting four stones in a diagonal line. There are many valid moving sequences that may lead to this end configuration.
{ ".......", ".......", ".X.....", ".X.....", ".X..XX.", "XOOOOOO" }
Returns: "second player wins"
The second player concludes the game by connecting a long line at the bottom.
{ "OOXOXOX", "XXXOXOO", "OXXOOXO", "XOOXXOO", "XXXOOXX", "XOOXXOO" }
Returns: "draw game"
The board is full, and neither player has managed to connect at least four stones in a line. The game therefore ends in a tie.
{ "...X...", ".......", ".......", ".......", "...X...", "...O..." }
Returns: "invalid"
No "floating" stone is allowed in a valid game, so there would never be an empty square beneath any stone.
{ ".......", ".......", ".......", ".......", "OOOO...", "XXXX..." }
Returns: "invalid"
The game ends when the first player wins. Neither player should continue thereafter.
{ "XXO.XOX", "OOX.OXX", "XXO.XXO", "OOXXOOO", "XXOOXOO", "OOXOOXX" }
Returns: "invalid"
At a glance the first player seems to have won the game when in fact there does not exist a valid sequence of moves.
{"XX.XX.O","OO.XOXX","XX.OXXO","OOOOOOX","XXXOXOO","OOOXOXX"}
Returns: "second player wins"
{"XXX.OXX","OOO.XXX","XXX.OXO","OOO.OOO","XXX.OOO","OOO.XXX"}
Returns: "first player moves"
{"XXOXOX.","OOXXXO.","XXOOXX.","OOXOOOX","XXOOOXO","OOXXXOO"}
Returns: "invalid"
{".......",".......",".......",".......","...X...","...O..."}
Returns: "invalid"
{".......",".......","...X...","...O...","...O...","...X..."}
Returns: "invalid"
{".......","...X...","..OX...","..OX...","..OX...","......."}
Returns: "invalid"
{"..O....","...X...","..O....","...X...","..OX...","..OX..."}
Returns: "invalid"
{".......","..O....","..O....","..OX...","..OX..X","..OX..X"}
Returns: "invalid"
{"OXOXOXO","XOXOXOX","OXOXOXO","XOXOXOX","OXOXOXO","XOXOXOX"}
Returns: "invalid"
{"OXOOOXX","XOXXXOX","OXOOOXO","XOXXXOX","OXOOOXO","XOXXXOX"}
Returns: "invalid"
{".......","..X....","..O....","..OX...","..OX...","..OX..."}
Returns: "invalid"
{".O.....",".O.....",".X.....",".O.....","OX.....","XX....."}
Returns: "invalid"
{".O.OXOX",".O.XOXO","XO.OOXX","XXXXOOO","XOOOXXX","XOXXOXO"}
Returns: "invalid"
{"OOXOXOX","O.....O","O.....X","X.....O","X.....X","XOXXOXO"}
Returns: "invalid"
{".......",".......",".......","..O....","..X....","XOXO..."}
Returns: "first player moves"
{"...O...",".OXOXO.","XOXOXOX","XOXOXOX","OXOXOXO","XOXOXOX"}
Returns: "second player wins"
{".......",".......","O.....X","OO...XO","OOO.XOO","XXXXXXX"}
Returns: "first player wins"
{"XO.OXOX","OX.XOXO","OO.OOXX","XXXXOOO","XOOOXXX","XOXXOXO"}
Returns: "first player wins"
{"XXOO.OO","XOXOXOO","XXXOOXX","OOOXXXO","XXXOOOX","XOOXXOX"}
Returns: "second player moves"
{"XXOOOOO","XOXOXOO","XXXOOXX","OOOXXXO","XXXOOOX","XOOXXOX"}
Returns: "second player wins"
{"XOOO.OO","OXXO.OX","XOXXOXO","XXXOXOX","XXOOXOO","OXOXOXX"}
Returns: "second player wins"
{".......",".......","..O....","..OX...","..OX...","..OX..X"}
Returns: "second player wins"
{".......",".......",".......",".......",".XXX..O","OXXXOOO"}
Returns: "second player moves"
{".......",".......",".......","......O",".XXX..O","OXXXOOO"}
Returns: "first player moves"
{".......","...X...","...X...","...O...","..OO...","..XX..."}
Returns: "second player moves"
{"OOOXOOO","XXXOXXX","OOOXOOO","XXXOXXX","OOOXOOO","XXXOXXX"}
Returns: "draw game"
{"XOOXOOO","OXOXOXX","XOXXXOO","OOXOXXX","XOXOXOO","OXOXOXX"}
Returns: "draw game"
{"OXOOOXO","XOXXXOX","OXOOOXO","XOXXXOX","OXOOOXO","XOXXXOX"}
Returns: "draw game"
{ ".......", "...X...", "..XXX..", "XXOOOXX", "XXOOOXX", "OOOXOOO" }
Returns: "invalid"
{ "X.X....", "O.X.X..", "O.X.X..", "X.O.O..", "X.O.O..", "X.O.O.." }
Returns: "second player moves"
{ "XX.....", "XO.....", "OO.....", "OO.....", "OX.....", "XX....." }
Returns: "invalid"
{ ".....XO", ".....XX", ".....OO", ".....OO", ".....OX", ".....XX" }
Returns: "first player moves"
{".......", ".......", "XXX....", "XXX....", "OOO....", "OOOX..." }
Returns: "invalid"
{"OOXOXOX", "XXXOXOO", "OXXOOXO", "XOOXXOO", "XXXOOXX", "XOOXXOO" }
Returns: "draw game"
{".......", ".......", ".......", "..O....", "..X....", "..X...." }
Returns: "invalid"
{"O....X.", "XX...XO", "OO...OX", "OX...OX", "XOOX.XX", "OXOOOXX" }
Returns: "invalid"
{".......", ".......", "XXX....", "OOO....", "OOO....", "XXX...." }
Returns: "invalid"
{".......", ".......", ".......", ".......", "X......", "O......" }
Returns: "invalid"
{".......", "...X...", "..XX...", ".XXO...", "XOOO...", "OOOX..." }
Returns: "invalid"
{"OOOOOOO", "XXXOXXX", "OOXOXOO", "OXOXOXO", "XXOXOXX", "XXOXOXX" }
Returns: "invalid"
{".......", ".......", "...O...", "...O...", "...X...", "...X..." }
Returns: "invalid"
{"....O..", ".XX.O..", ".OX.O..", ".XXOX..", ".OXOO..", "XXXOOXO" }
Returns: "invalid"
{".......", ".......", "O......", "O......", "X......", "X......" }
Returns: "invalid"
{".......", ".......", ".......", ".......", "OOOX...", "XXXXO.." }
Returns: "invalid"
{"...OOX.", "..OXXO.", ".OOOOX.", "XOXXXO.", "XXOXOXX", "OXOOXXO" }
Returns: "second player wins"
{".......", ".......", ".......", "O......", "X......", "X......" }
Returns: "invalid"
{".......", ".......", ".......", ".......", "...X...", "...O..." }
Returns: "invalid"
{".......", ".......", ".......", ".......", "OOO.OOO", "XXXXXXX" }
Returns: "first player wins"
{".......", ".......", "XX.....", "XXO.O..", "XXO.O..", "XXO.O.O" }
Returns: "invalid"
{".......", ".......", "XXX.O..", "XOOXOX.", "XXOXOO.", "XOXOOO." }
Returns: "invalid"
{"....OOO", "....OOO", "....OOO", "....XXX", "....XXX", "....XXX" }
Returns: "invalid"
{".......", ".......", "...X...", "..XO...", "OXOO...", "XXXXOO." }
Returns: "invalid"
{".......", "XO.....", "OXO....", "XOXO...", "XOXX...", "XOOOXX." }
Returns: "invalid"
{".......", ".......", "OO.....", "OX.....", "XX.....", "XO....." }
Returns: "first player moves"