PROBLEM STATEMENT
In the board game "Super Mastermind", there is a player and a coder. 5 colored
pegs are placed in a specific order by the coder and hidden from the player.
The player has to guess what color each of the pegs is. After each guess, the
coder tells the player how many of the pegs in this guess are "correct and in
the right place" and how many others are "correct but in the wrong place". The
player continues guessing until the guess is exactly the same as the hidden
solution (and the player wins), or the player is out of guesses (and the coder
wins).
The response given by the coder is determined by specific rules. First, all
pegs in the guess that are the same color as the peg in the equivalent position
of the solution count as a "correct and in the right place" peg. Each
remaining peg in the guess (discounting those that were counted as "correct and
in the right place") is then compared to each remaining peg in the solution
(also discounting those that were counted as "correct and in the right place").
Each match that is found counts as a "correct but in the wrong place" peg -
and that pair of pegs (one in the solutions, one in the guess) is removed from
remaining pegs to compare. No peg in the solution or guess will EVER be
counted as a member of more than one match of either type.
Each peg will be one of 8 possible colors - which will be represented by a
single letter in the guesses and solution. The colors (and their one-letter
representations) are Red ('R'), Green ('G'), Orange ('O'), Yellow ('Y'), Blue
('E'), Brown ('N'), White ('W'), or Black('K'). Colors can be repeated in the
solution.
Create a class Mastermind that contains a method solution that, when given a
String[] that represents guesses in the game Mastermind, returns either the
solution if it can be determined, "unknown" (quotes for clarity only) if the
solution can not be determined with the information at hand, or "invalid"
(quotes for clarity only) if the information present could not possibly
represent a valid solution. If it does return the solution, it should be
returned as a string 5 characters in length with each peg's one-letter color in
its correct place (the first letter representing the first peg, the second
representing the second, etc...).
DEFINITION
Class: Mastermind
Method: solution
Parameters: String[]
Returns: String
Method signature (be sure your method is public): String solution(String[]
guesses);
Each element in guesses will be in the form of "CCCCC R W" (quotes for clarity
only) - where each C is the letter representing the color of the peg in that
position (the first letter representing the first peg, the second representing
the second, etc...), and R and W are each single digits representing the number
of "correct and in the right place" guesses and "correct and in the wrong
place" guesses, respectively.
NOTES
- For those who know the Mastermind games: this is actually "Super Mastermind"
with the "Repeat Colors" optional rule, without the "Blanks" optional rule, and
(potentially) up to 50 guesses. Read the statement carefully, do not assume
standard Mastermind rules.
- Unlike normal Mastermind games, guesses may include more guesses after a
fully correct guess (that is, a guess that results in a response of 5 "correct
and in the right place"), or even multiple fully-correct guesses. If there are
no contradictions between all the guesses, these still may represent valid
games as far as this problem is concerned.
TopCoder will ensure the validity of the inputs. Inputs are valid if all of
the following criteria are met:
- String[] guesses will have at least 1 element, and at most 50 elements.
- Each element in String[] guesses will be exactly 9 characters in length.
- The first 5 characters will each be one of: 'R', 'G', 'O', 'Y', 'E', 'N',
'W', or 'K'.
- The 6th and 8th characters will be spaces.
- The 7th and 9th characters will each be numbers between '0' and '5', inclusive.
EXAMPLES
In each of these examples, quotes ('"'), commas (','), and curly braces ('{'
and '}') are included for clarity only. They are never part of the input or
output.
Example #1:
guesses = {"RYWKG 5 0"}
returns: "RYWKG"
Since the response from this one guess says that all 5 pegs are the correct
color and in the correct place, the guess was correct.
Example #2:
guesses = {"RRRRR 1 0", "NRRRR 0 1", "GYWKG 4 0"}
returns: "RYWKG"
Since the second guess tells you that none of its 4 red pegs are in the right
place, but the first tells you that one of its 5 is, you can deduce that the
first peg is red. Knowing this, you know the first peg in the third guess was
wrong - and since the 4 others are correct, you know all 5 pegs.
Double-checking, you can determine that "RYWKG" fits the responses to all three
guesses.
Example #3:
guesses = {"RRRRR 0 0", "WWWWW 0 0", "GGGGG 0 0", "NNNNN 0 0", "OOOOO 0 0",
"EEEEE 0 0", "YYYYY 0 0"}
returns: "KKKKK"
Since these 7 guesses tell you that there are no red, white, green, brown,
orange, blue, or yellow pegs (respectively), you know the answer must be all
black pegs ("KKKKK").
Example #4:
guess = {"RWYRW 4 0"}
returns: "unknown"
Since you can't tell which of the 4 pegs are in the correct places, you don't
know which one is wrong, and can not determine the exact solution.
Example #5:
guesses = {"RRRRR 5 0", "WWWWW 1 0"}
returns: "invalid"
Since this says there are 5 red pegs (from the first guess) AND 1 white peg
(from the second guess) - you know this can't be true, since there are only 5
pegs. The coder must be cheating and the game is invalid.
Example #6:
guesses = {"RRRRR 0 1"}
returns: "invalid"
Since this claims one of these red pegs is correct, but in the wrong place -
meaning one of the solution's pegs is red. However, it also claims none of
these 5 red pegs are in the right place - which is impossible, since if there
is a red peg in the solutions, one of the red pegs in the guess must be right.
This game is "invalid".
Example #7:
guesses = {"RYOKG 4 2"}
returns: "invalid"
Since this response describes 4 pegs that are correct and in the right place
and 2 others that are correct and in the wrong place, and there are only 5
pegs, this is obviously an invalid response.
Example #8:
guesses = {"YKWYE 2 0", "EKNOK 1 1", "YKGEE 1 2", "KRYKN 1 1", "KKRRG 0 1",
"YRNEG 1 2", "WWERO 0 3", "GOKYY 1 1", "RGERN 0 2", "WYRYG 1 1"}
returns: "EROYE"