PROBLEM STATEMENT
You and some friends are playing Three Man and you cannot seem to keep score
without getting in severe arguments about the rules. Thus you decide to write
a program to determine the winner. The winner is the player with the fewest
number of points at the end of the game.
Three Man is played with 2 dice and goes as follows:
1. N players (numbered 0 through N-1) sit in the circle and start with 0 points.
2. Play starts with player 0 and proceeds in the circle, (player 0, 1, ... N-1,
0, ...).
3. Player 0 is the original 3-man and the original current player.
4. The current player rolls the dice:
* If either of the dice or the sum of the dice is 3:
* If the current player is not 3-man, the 3-man gets 1 point.
* If the current player is the 3-man, the current player picks
a new 3-man. See below for picking rules.
* If the sum of the dice is 7, the person to left of the current
player gets 1 point.
* If the sum of the dice is 11, the person to the right of the
current player gets 1 point.
(The player to the left is the player whose number is 1 less,
or player N-1 if the current player is 0. The player to the right is
the player whose number is 1 more, or player 0 if the current player
is N-1.)
* If the sum of the dice is 5, it's a social and everyone gets
1 point.
* If doubles are rolled (both dice show the same number):
* The current player picks another player, who then rolls the dice
and gets the number of points equivalent to the sum of the dice.
See below for rules on picking.
5. If the roll resulted in no one getting points, and no picking of a 3-man,
the current player passes the dice to the next player. Otherwise the
current player keeps the dice. Play goes back to step 4.
For this problem, you are to create a program to simulate play. The program
takes four parameters:
* int[] rolls1 - The values of the rolls of the first die.
* int[] rolls2 - The values of the rolls of the second die.
* int[] pickValues - Used during simulation to simulate picking. When
a player picks someone else for rolling for points or for
the next 3-man, assume they pick the next value in
this int[]. That is, the first time someone is picked,
pickValues[0] is picked, the second time, pickValues[1] is
picked. When there are no more pickValues left, wrap
around
and start at 0 again.
* int N - The number of people playing.
The program should simulate the game, keep track of the number of points each
player has, and return the number of the player who has the lowest number of
points at the end of the game.
DEFINITION
Class: ThreeMan
Method: whoWins
Parameters: int[] int[] int[] int
Returns: int
Method signature (be sure your method is public): int whoWins(int[] rolls1,
int[] rolls2, int[] pickValues, int N);
NOTES
* If a dice roll (other than a roll immediately after doubles) corresponds
to multiple events, all events occur. So, for example, if someone rolls
a 3 and a 2, the 3-man gets a point and then all players get a point
(so the 3-man gets 2 points total on the roll).
* The first time the dice are rolled, rolls1[0] and rolls2[0] are the
values rolled. The second time, rolls1[1] and rolls2[1], etc...
* The game ends when the dice have been rolled as many times as elements
in rolls1 and rolls2.
* During a roll after doubles (when a player is rolling to see how many
points to get), the outcome affects only the player rolling. For
example, if he rolls a 5, it does not count as a social.
* If more than 1 player ties for the low score, return the player with
this low score with the lowest player number.
* If double 3's are rolled and the current player is not the 3-man,
the 3-man gets just 1 point (and then the current player goes on to
pick someone to roll for points, which may be the 3-man)
* If double 3's are rolled and the current player is the 3-man,
first the current player picks the new 3-man, then he picks the player
to roll for points (so access pickValues in that order).
* If doubles are rolled on the last roll (no more elements in rolls1 and
rolls2), the game ends without a picked player rolling for points.
TopCoder will ensure the validity of the inputs. Inputs are valid if all of
the following criteria are met:
* rolls1 and rolls2 have the same length.
* rolls1 and rolls2 each contain between 1 and 50 elements, inclusive.
* Each element of rolls1 and rolls2 is between 1 and 6, inclusive.
* pickValues contains between 1 and 50 elements, inclusive.
* Each element of pickValues is between 0 and N-1, inclusive.
* N is between 2 and 10, inclusive.
EXAMPLES
1)
rolls1: {1, 2, 2, 4, 5, 3, 2, 4}
rolls2: {4, 1, 5, 5, 5, 2, 3, 4}
pickValues: {0, 2, 1, 2, 0}
N: 3
returns: 1
Originally, it is player 0's turn and player 0 is the 3-man.
Player 0 rolls a 1+4 = 5, so everyone gets 1 point.
The scores are player 0 with 1, player 1 with 1, and player 2 with 1. From
here on, the scores are represented as a list: (1, 1, 1)
Player 0 rolls a 2+1 = 3, so he picks a new 3-man. However, pickValues[0] is
0, so he picks himself.
points = (1, 1, 1).
Player 0 rolls a 5+2, so the player to the left gets a point (player 2).
points = (1, 1, 2).
Player 0 rolls a 4+5=9, which is nothing, so play passes to player 1.
Player 1 rolls a 5+5=10, doubles, so he picks player pickValues[1] = 2 to roll
to get points.
Player 2 rolls a 3+2, and gets 5 points.
points = (1, 1, 7)
Player 1 continues rolling and rolls a 2+3=5. So everyone gets a point and the
3-man gets a point. (The 3-man gets two points).
points = (3, 2, 8)
Player 1 rolls doubles, but the game ends because there are no more elements
left.
Thus, the final points are (3, 2, 8) and the method returns 1 because player 1
had the least number of points.
2)
rolls1: {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
rolls2: {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
pickValues: {1}
N: 5
returns: 0
The final scores are: (0, 12, 0, 0, 0)
There is a tie for the winner, and 0 is the lowest player number tied for the
low score.
3)
rolls1: {4, 2, 3, 2, 4, 5, 1, 5, 2, 4, 3}
rolls2: {6, 2, 1, 2, 3, 4, 1, 5, 4, 2, 3}
pickValues: {2, 1}
N: 4
returns: 0
The final scores are: (0, 7, 14, 0)
4)
rolls1: {1, 3, 3, 5, 1, 1, 3, 2, 5, 1, 6, 6, 2}
rolls2: {5, 1, 1, 1, 5, 4, 3, 2, 1, 4, 5, 2, 5}
pickValues: {1, 3, 0}
N: 4
returns: 2
The final scores are: (6, 7, 2, 2)