Ask Computer Engineering Expert

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 ex, 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 ex, 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)|

Computer Engineering, Engineering

  • Category:- Computer Engineering
  • Reference No.:- M988997

Have any Question?


Related Questions in Computer Engineering

Does bmw have a guided missile corporate culture and

Does BMW have a guided missile corporate culture, and incubator corporate culture, a family corporate culture, or an Eiffel tower corporate culture?

Rebecca borrows 10000 at 18 compounded annually she pays

Rebecca borrows $10,000 at 18% compounded annually. She pays off the loan over a 5-year period with annual payments, starting at year 1. Each successive payment is $700 greater than the previous payment. (a) How much was ...

Jeff decides to start saving some money from this upcoming

Jeff decides to start saving some money from this upcoming month onwards. He decides to save only $500 at first, but each month he will increase the amount invested by $100. He will do it for 60 months (including the fir ...

Suppose you make 30 annual investments in a fund that pays

Suppose you make 30 annual investments in a fund that pays 6% compounded annually. If your first deposit is $7,500 and each successive deposit is 6% greater than the preceding deposit, how much will be in the fund immedi ...

Question -under what circumstances is it ethical if ever to

Question :- Under what circumstances is it ethical, if ever, to use consumer information in marketing research? Explain why you consider it ethical or unethical.

What are the differences between four types of economics

What are the differences between four types of economics evaluations and their differences with other two (budget impact analysis (BIA) and cost of illness (COI) studies)?

What type of economic system does norway have explain some

What type of economic system does Norway have? Explain some of the benefits of this system to the country and some of the drawbacks,

Among the who imf and wto which of these governmental

Among the WHO, IMF, and WTO, which of these governmental institutions do you feel has most profoundly shaped healthcare outcomes in low-income countries and why? Please support your reasons with examples and research/doc ...

A real estate developer will build two different types of

A real estate developer will build two different types of apartments in a residential area: one- bedroom apartments and two-bedroom apartments. In addition, the developer will build either a swimming pool or a tennis cou ...

Question what some of the reasons that evolutionary models

Question : What some of the reasons that evolutionary models are considered by many to be the best approach to software development. The response must be typed, single spaced, must be in times new roman font (size 12) an ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As