Combinations are represented by different colored pegs


Mastermind is a guessing game where one player picks a secret combination and then another player tries to guess it. The correctness of a guess is indicated by a combination of black pegs and white pegs, with the game proceeding until the correct combination is found, or some predetermined maximum number of guesses is reached.

In the actual game, combinations are represented by different colored pegs (red green, etc.) but for his program we will use six numeric digits (1 through 6). The input is then merely a four-digit number, which can be input as a single instruction, and then evaluated.

For this program, the computer will pick the combination, and then the user will be given opportunities to guess at that combination. The correctness will be represented with black pegs and white pegs.

Each black peg indicates that a digit has a correct value and is in the correct position. For example, if the second digits in both combinations is the same, then the player is awarded a black peg.

Each white peg indicates that a digit has a correct value, but is in an incorrect position. For example, if the player's first digit does not match the answer's first digit, but does match the third, he would be awarded a white peg.

Since the combinations only have four digits, then no more than four digits can be correct -- the total number of black and white pegs may not exceed four. And the particular case of four black pegs would indicate that the guess is completely correct.

Hint: The 0 digit was eliminated above to make it easier to avoid making two many pegs. It will not match any actual code digit.'

USER INTERFACE SPECIFICATION
This program is to support several games of Mastermind in sequence. Each new game would have a new combination to guess. Also, limit the number of attempts at each combination to twelve.

Please DO NOT seed the random number generator with your program. Grading will be very much accelerated if all submitted programs contain the same 'random' combinations, so the TAs will be able to 'guess' your answers more quickly. There may be some variation on which order you pick the four digits in your combination (i.e. forward or backwards), but I don't want the TAs to have to spend ten minutes playing any game just to see if it works.

SAMPLE OUTPUT
The following results are actually from the instructor's implementation. Note carefully how duplicated digits in the guess and the answer are handled, so that no guess receives too many white pegs. It is therefore recommended that you understand all of these outputs before you start writing the program.

Try to guess the combination: 5566
You earn 0 black pegs and 4 white pegs.
Try to guess the combination: 6655
You earn 4 black pegs and 0 white pegs.
You guessed my combination! Congratulations!

Play another game? (y/n) y
Try to guess the combination: 1234
You earn 0 black pegs and 1 white pegs.
Try to guess the combination: 2556
You earn 1 black pegs and 1 white pegs.
Try to guess the combination: 6365
You earn 1 black pegs and 1 white pegs.
Try to guess the combination: 6664
You earn 1 black pegs and 0 white pegs.
Try to guess the combination: 1114
You earn 1 black pegs and 1 white pegs.
Try to guess the combination: 1256
You earn 0 black pegs and 3 white pegs.
Try to guess the combination: 6566
You earn 2 black pegs and 0 white pegs.
Try to guess the combination: 6555
You earn 2 black pegs and 0 white pegs.
Try to guess the combination: 6533
You earn 2 black pegs and 0 white pegs.
Try to guess the combination: 6511
You earn 4 black pegs and 0 white pegs.
You guessed my combination! Congratulations!

Play another game? (y/n) y
Try to guess the combination: 1234
You earn 0 black pegs and 1 white pegs.
Try to guess the combination: 1556
You earn 1 black pegs and 1 white pegs.
Try to guess the combination: 2565
You earn 1 black pegs and 1 white pegs.
Try to guess the combination: 6553
You earn 0 black pegs and 3 white pegs.
Try to guess the combination: 3666
You earn 2 black pegs and 1 white pegs.
Try to guess the combination: 6365
You earn 2 black pegs and 2 white pegs.
Try to guess the combination: 5366
You earn 4 black pegs and 0 white pegs.
You guessed my combination! Congratulations!

Play another game? (y/n) n

PROGRAM SPECIFICATIONS
The assigned program must implement all of the following functions. Additional ones are permitted as desired -- these below are required. Future assignments will not detail the functions as below -- but will instead require the students to design their own function descriptions in advance to writing the program.

Main:
simulates multiple games of MasterMind, as many as a user desires
Inputs (from the keyboard):
yesNo (char) whether user wishes to play again
Calls: playMastermind

playMastermind:
simulates one complete game of Mastermind
computer picks a combination, and then lets user guess at them
The player is limited to twelve guesses.
Parameters: none!
Calls: rollDie, evaluate, nthDigit (if verifying appropraite input)

rollDie:
picks a random number up the size of a die. It does not need to have 6 sides
Parameter:
sides (input int) How many sides the simulated die has
Post-condition:
returns an integer value >= 1 and <= sides, with equal probability

evaluate:
evaluates a combination by comparing it with the answer
Correctness is indicated by black pegs (correct digit in correct position)
and white pegs (correct digit in incorrect position)
Parameters:
answer (input int) the correct combination
guess (input int) the current guess
black (output int) number of black pegs
white (output int) number of white pegs
pre-conditions:
answer and guess are both 4-digit numbers with no zero digits
post-conditions:
black and white are both > 0 and their sum is <= 4
Calls: nthDigit, clearNthDigit

nthDigit:
identified the n'th digit of a combination
whether digits count from left to right or right to left is unspecified
Parameters:
combination (input int) combination to examine
position (input int) which digit to examine
(returned) (output int) the value of the actual digit
pre-conditions:
combination has the appropriate number of digits, and
0 < position <= number of digits
post-condition:
0 <= returned digit <= 9 (single digit)

clearNthDigit:
ears the n'th digit of a combination to zero, so it will no longer match
digits must be counted in the same manner as nthDigit above.
parameters:
combination (in/out int) combination to modify
position (input int) which digit to set to 0
pre-condition:
same as those for nthDigit above
post-condition:
corresponding digit is set to zero
Calls: nthDigit (optional, depending on the implementation)

ALSO: Important Specification:
For full credit, there may be no global variables in your solution (aside from the given object cin and cout). Constants, however, may be (and should be) global.

Additional Commentary:
The requirement for the functions above is not merely an arbitrary demand just to force their presence. They have been chosen to simplify the program structure to allow for more rapid development.

'playMastermind' represents a clear major task that is part of this program. It has been separated from the main() function to allow for the possibility of reversing player roles (have the computer guess a number picked by the user) at some later date. It also allows for very easily playing only one game at a time during program development.

'evaluate' also has a well-defined purpose that can be summarized as a distinct task. It has also been separated out to allow the possibility of role reversal -- how a guess is evaluated is the same, no matter who is doing the guessing.

The 'nthDigit' and 'clearNthDigit' also represent small utility tasks that can be written once and then reused as needed. The 'evaluate' function in particular will have a lot of use for these in checking answers, and making sure that no peg counts twice.

'rollDie' is a very simple function, which can be called multiple times to create our random combination. It is representative of tiny utility operations that are often used many times over, but only need to be written once and reused.

IMPLEMENTATION AND TESTING GUIDELINES

Scaling Constants may be very helpful in developing this program. If you let constants govern the range of values each digit may have, you can control the total number of possible combinations.

For our 'production' version that will be turned in for a grade, there would be four digits in the range of 1-6. But that is 1296 different combinations, which could take a while to guess, especially if you do not know what the answer is.

But if the program would work in the same way with only the digits 1 or 2, there would only be 16 possible combinations, which very much reduces testing time. Also, this greatly increases the chances of duplicate digits in the answer to verify how well that part of the program works.

On the flip side, if you really want to challenge yourself with this program (after turning it in), you could see how long it would take you to guess numbers using all the digits from 1 to 9 (6561 possibilities)|

Request for Solution File

Ask an Expert for Answer!!
Basic Computer Science: Combinations are represented by different colored pegs
Reference No:- TGS088997

Expected delivery within 24 Hours