Mastermind
Due Sunday, April 27th at 8pm
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 a number the user enters (between 4 and 9).
For example, if the
user chooses a maximum of 7, then the numbers could be anything from
1 to 7, 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 (also 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 prompts:
#s (4 - 9)
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" after the prompt.) Put each of
these prompts 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
Nubmer 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 maximum value (0 is
not allowed, nor does it make sense, so it serves as a decent flag for
this behavior.)
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"
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:
#s (4 - 9) 91000
#s (4 - 9) 4
Turns (1 - 40) 100000
Turns (1 - 40) 42
Turns (1 - 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!
#s (4 - 9) 10
Turns (1 - 40) 3
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. This is challenging, but a
good exercise. 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."