Mastermind
Professor I. M. Sosmart, famed scholar and researcher, has developed
a taste for logic puzzles. His current tastes have settled on the
game Mastermind, having picked up an old board-game version of the
puzzle at a thrift shop. Sadly, the real-world version of the game
requires another person to play, and Professor Sosmart's young son
Noah T. Somart does not share his father's fascination with the game.
Some online versions exist, but none of them quite match up with what
Professor Sosmart wants. So he has asked you to implement a version
of the puzzle, just for him.
In this assignment you will implement a slightly simplified version of
the game
Mastermind. If you are not familiar with this game, please click
the link above for a Java-based demonstration of it.
In this version of Mastermind, the computer will pick four (4) random
numbers between 1 and 9, inclusive. No single number may be chosen
twice, so groups like 1, 1, 3, 6 are not allowed. The user
then has a number of turns (specified by the user, up to a
maximum of 40) to guess the numbers in the correct order. In each
turn the user guesses 4 numbers, and your program must inform them how
many are correct, and how many are the right numbers but in the wrong
spot.
A few examples:
- Numbers 4, 2, 6, 1, Guess 4, 6, 3, 5:
1 correct (#4 in position 1), 1 in wrong spot (#6 in position 2 instead of 3)
- Numbers 4, 2, 6, 1, Guess 4, 1, 6, 3:
2 correct (#4 and #6), 1 in wrong spot (#1)
- Numbers 4, 2, 6, 1, Guess 4, 2, 6, 5:
3 correct (#4, #2, and #6), 0 in wrong spot
If the user guesses the correct sequence in the alloted number of
turns, then they win. If not, they lose.
Input / Output
To begin with, prompt the user for the maximum value and number of
turns. Use the following prompt:
Turns (1 - 40)
(Note that you are to read in the input on the same line as the
prompt, so do not print an "endl" or "\n" after the prompt.) Put this
prompt in a loop to ensure that the correct input is eventually
provided. (See example 1 for more info.)
For each turn, print out "Turn N" where N is the current turn,
beginning with 1. Follow this by a prompt for the current guess,
"What is your guess: ". After reading in the guess and calculating
the relevant scores, print them back out as "Number correct X" and
"Number in wrong spot Y". Follow this with an extra blank line. Thus
for each turn, something like:
Turn 4
What is your guess:
4 1 3 2
Number correct 0
Number in wrong spot 4
will be printed out.
Finally, when the game ends, either print out "You win!" or " No turns
remain, you lose!" If the user lost, print out "The numbers were: "
and the numbers, with a space between each number.
Also, to enable us to grade faster, we must have a way of checking for
a specific pattern of numbers. To allow for this, please ensure that
the numbers are 1, 2, 3, 4 if the user chooses 0 as the
number of turns (0 is not allowed, nor does it make sense, so it
serves as a decent flag for this behavior.) If the user picks this,
assume 10 turns.
Two grading samples are here (save these to your hw2 directory in
your local account):
mastermind1.in mastermind1.out
mastermind2.in mastermind2.out
And here is a binary version of the solution so you can see what
your program should behave like (save this and type "chmod +x mastermind"
before running it.):
mastermind
To test these, run the following command lines:
> ./mastermind < mastermind1.in | diff - mastermind1.out
> ./mastermind < mastermind2.in | diff - mastermind2.out
If these produce ANY output at all, either your I/O is incorrect, or you
haven't quite programmed the game correctly.
Please submit as "mastermind.cc"
Hints
- Start with a function int num_correct(int a1[], int a2[])
that takes in the computer's secret (array a1) and the user's guess
(array a2) and returns the number of elements that are the same
(at the same index in both arrays).
- Write a function bool in_array(int a1[], int element) that
returns true if the element is in array a1
- Write a function int num_wrong_spot(int a1[], int a2[]) that
takes in the computer's secret and the user's guess and returns the
number of ints that are the same but in the wrong spot. This is much
easier to do using the in_array function, but do think about
what you are writing.
Notes
If there are any inconsistencies between what is stated on this page
and the examples provided, the implied behavior of the examples is to
be considered correct. Any questions about this should be directed to
the course mailing list.
As always, commenting and style will be a significant portion of your
grade on this assignment, so start early, finish early, and devote
some time to making your code readable and understandable.
Two sample games follow:
Turns (0 - 40) 100000
Turns (0 - 40) 42
Turns (0 - 40) 10
Turn 1
What is your guess: 1 2 3 4
Number correct 0
Number in wrong spot 4
Turn 2
What is your guess: 2 3 4 1
Number correct 1
Number in wrong spot 3
Turn 3
What is your guess: 2 4 3 1
Number correct 0
Number in wrong spot 4
Turn 4
What is your guess: 3 4 1 2
Number correct 2
Number in wrong spot 2
Turn 5
What is your guess: 3 4 2 1
Number correct 0
Number in wrong spot 4
Turn 6
What is your guess: 4 1 2 3
Number correct 1
Number in wrong spot 3
Turn 7
What is your guess: 4 3 1 2
Number correct 4
Number in wrong spot 0
You win!
Turns (0 - 40) 0
Turn 1
What is your guess: 1 2 3 4
Number correct 1
Number in wrong spot 2
Turn 2
What is your guess: 1 3 4 5
Number correct 0
Number in wrong spot 4
Turn 3
What is your guess: 5 6 7 8
Number correct 0
Number in wrong spot 1
No turns remain, you lose!
The numbers were: 3 1 5 4
Optional Bonus Credit You may earn up to 20% bonus credit by
removing the "no repeats" restriction on the numbers that are
generated. This is challenging, but a good exercise. Note that the
challenge is not the generation of these numbers (which is actually
easier) but producing correct responses to user guesses. If you
choose to attempt this, make sure you get the simple version done
first, and keep a copy of the simple version to submit in case you do
not finish the bonus problem. Submit this version as "bonus.cc."