Statistics

Problem Statement for "ConnectX"

Problem Statement

PROBLEM STATEMENT:

Connect X is a two player game based on Connect Four.  Each player takes turns
dropping checkers down the columns of a rectangular board.  When a player
matches X checkers horizontally, vertically, or diagonally on the board, that
player wins the game.  (X is an int between 2 and 10 inclusive)

Jim and Bob, two amateur Connect X players, decide to play a single game of
Connect X one afternoon.  Not being very good players, they have no idea what
the outcome of their game is.  Given the configuration of the board at the end
of their game, it is your job to determine the outcome.  There are four
possible outcomes - Jim is the winner, Bob is the winner, there's a tie, or
neither player is a winner.

If Jim is the winner, that means that Jim has matched X (or more) of his own
checkers horizontally, vertically, or diagonally on the board, while Bob has
not.  If Bob is the winner, that means that Bob has matched X (or more) of his
own checkers horizontally, vertically, or diagonally on the board, while Jim
has not.  If there is a tie, that means that both players have matched X (ore
more) of their own checkers in a winning fashion.  Keep in mind that Jim and
Bob are not good players, and that they may have continued playing after one of
them had already technically won - however, for the purposes of this problem,
the only thing that matters is the configuration of the board when they stop
playing, so count the situation where both players are winners according to the
given configuration as a tie.

Implement a class ConnectX, which contains a method outcome.  This method will
return an int representing the outcome for a game of Connect X.  The possible
return values are:

1 - Jim wins
2 - Bob wins
3 - There's a tie
4 - Neither player wins

The method will be passed two parameters:

String[] board - the board configuration at the end of the game.

Note: Each element of board will represent a row of the board configuration.
Valid characters within each element are '+', which represents an empty slot,
'1' (one), which represents one of Jim's checkers, and '2', which represents
one of Bob's checkers.  The 0th element of board will represent the top row,
and each successive element will represent the row below the previous element.
For example, if board is {"+++", "+1+", "212"}, the board configuration will
look like this:

+++
+1+
212

int x - the number of checkers that must be matched for a player to win.

TopCoder will ensure the validity of the inputs.  Inputs are valid if all of
the following criteria are met:
* Each element of board will contain the same number of characters.
* Each element of board will only contain the following characters: '+', '1',
and '2'.
* board will contain between 1 and 10 elements inclusive.
* Each element of board will contain between 1 and 10 characters inclusive.
* x will be between 2 and 10 inclusive.
* board will contain no floating pieces - all pieces will either be in the
bottom row, or be directly above another piece.

NOTES
* x is not limited by the dimensions of the board - for example, if the board
is 3 rows by 3 columns large, 4 is a valid value for x.
* Do not assume that there will be an equal number of each player's checkers on
the board - Jim and Bob often cheat, so one of them may have taken multiple
turns before letting the other take his turn.

DEFINITION

Class name: ConnectX
Method name: outcome
Parameters: String[], int
Returns: int

The method signature is: int outcome(String[] board, int x);
Be sure your method is public.

EXAMPLES:

---------
Example 1
---------

board: { "+++++", "+++++", "1++++", "22212", "11221" }
x: 3

board configuration:

+++++
+++++
1++++
22212
11221

Bob wins this game, because he has three checkers in a row (horizontally, in
the second row up from the bottom).
Return value: 2

---------
Example 2
---------

board: { "+++++", "+++++", "1++++", "12212", "11221" }
x: 3

board configuration:

+++++
+++++
1++++
12212
11221

Jim wins this game, because he has three checkers in a row (vertically, in the
leftmost column).
Return value: 1

---------
Example 3
---------

board: { "+++++", "+++++", "12+++", "12212", "11221" }
x: 3

board configuration:

+++++
+++++
121++
12212
11221

There is a tie.  Jim has three in a row (vertically, in the leftmost column),
and Bob has three in a row as well (diagonally, starting from the piece at the
second column from the left, third row from the bottom).  Jim also has another
three in a row (diagonally starting from the piece at the third column from the
left, third row from the bottom) - even though Jim has two winning lines on the
board and Bob only has one, it's still a tie since both players have matched
three in a row.
Return value: 3

---------
Example 4
---------

board: { "+++++", "+++++", "1+2++", "12212", "11221" }
x: 4

board configuration:

+++++
+++++
1+2++
12212
11221

There is no winner - neither player has 4 checkers in a row.
Return value: 4

Definition

Class:
ConnectX
Method:
outcome
Parameters:
String[], int
Returns:
int
Method signature:
int outcome(String[] param0, int param1)
(be sure your method is public)

Constraints

    Examples


      This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
      This problem was used for: