Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Colors : Y Yellow O Orange R Red B Blue G Green P - Purple Colors Y Yellow O

Colors :

Y – Yellow

O – Orange

R – Red

B – Blue

G – Green

P - Purple

Colors

Y – Yellow

O – Orange

R – Red

B – Blue

G – Green

P - Purple

You are to write a program to allow the user to play Mastermind.Mastermind is a game where the user tries to guess the color andorder of pegs that the computer has chosen. The peg colors areshown to the right. The computer will randomly choose the solutionpegs and store them in a string representing the colors (Forexample: YPBRP). The user will then try to guess the pegs. Aftereach guess the computer will tell the user how many colors theyguess correctly AND how many pegs were in the correct position.Using that information, the user would then make another guess.This continues until the user guesses all of the colors in thecorrect order.

There should be a constant NUM_OF_PEGS, this should be declaredabove main. Start the NUM_OF_PEGS at 3 when testing your program,but should be 5 when turned in.

The following functions should be written

makeSolution

Creates the solution string and returns it. The solution stringis made up of randomly chosen colors. The constant (NUM_OF_PEGS)will determine how many colors (pegs) are in the solution.

allowUserToGuess

Asks the user to type in their guess. It checks to make surethat the string they enter is the correct length. If it is not thecorrect length, asks the user to enter another guess. (You do NOThave to make sure that the colors are correct)

numberCorrectColorAndPosition

Determines how many pegs are in the correct position, meaninghow many colors are in the correct spot.

numberCorrectColor

Determines how many peg colors were guessed correctly. (This isprobably the most challenging function due to that fact that therecan be multiples of the same color)

directions

This function prints the directions. The statements are givenbelow, so you can copy them.

cout << "Master Mind" << endl;

cout << "The object of this game is to guessthe colored pegs that the " << endl;

cout << "computer has randomly chosen. Youwill guess the sequence by " << endl;

cout << "typing in the first letter of thecolor that you choose." << endl;

cout << "The color choices are:" <

cout <<"tY-YellowtO-OrangetR-RedtB-BluetG-GreentP-Purple";

cout << "If the computer had a solution ofBYRBP, and the user entered OBGRP";

cout << "The computer would tell the userthat they have 3 colors correct and";

cout << "1 color in the correct position.Using this information, the user";

cout << "would then guess again until theyget all of the colors correct and";

cout << "in the correct order.";

The main function should

  • Have it print “Programmed by … “ with your name (as seen onoutput)
  • Allow the program to make the solution(call a function)
  • In a loop
    • allow the user to guess (call a function)
    • Determine how many are correct and color that are correct (callfunctions)
    • Continue doing the loop until the user gets the correctsequence of colors
  • Continue with playing another game until the user typesQ

Sample output is given below

Hints

  • Test the functions (without playing the game) first – do nottry to make it play the game in main until all of the functions inthe class are working
  • Start the constant (NUM_OF_PEGS) at 3, it will make it easierto play and test.
  • When you start writing the main to play the game, make thesolution hard coded in (in other words, comment out the functioncall and set the solution to be a particular answer) After you getthat to work (and try it with different values), allow the code tomake the solution, but print it out, so you know what it is.
  • The sample output prints out the solution so you can see thatthe program is working correctly. When playing the REAL game, youwould comment out that line of code so the user couldn't see it,but you can leave it in when you turn it in.

Sample Output

Programmed By Lori Tetzner

Master Mind

The object of this game is to guess the colored pegs thatthe

computer has randomly chosen. You will guess the sequence by

typing in the first letter of the color that you choose.

The color choices are:

Y-Yellow

O-Orange

R-Red

B-Blue

G-Green

P-Purple

If the computer had a solution of BYRBP, and the user enteredOBGRP

The computer would tell the user that they have 3 colors correctand

1 color in the correct position. Using this information, theuser

would then guess again until they get all of the colors correctand

in the correct order.

Do you want to play Master Mind: y

I have chosen my color pegs

Solution is: YBYPO //only printed for you to see how gameworks

The solution has 5 pegs

Enter your guess: RBYGP

Your guess is: RBYGP

There are 3 peg colors correct and 2 pegs in the correctposition

Enter your guess: RBYOR

Your guess is: RBYOR

There are 3 peg colors correct and 2 pegs in the correctposition

Enter your guess: RBY

There are 5 pegs in the solution, your guess does not

have the correct number of pegs. Tryagain!!!

Enter your guess: RRRRR

Your guess is: RRRRR

There are 0 peg colors correct and 0 pegs in the correctposition

Enter your guess: BYOGR

Your guess is: BYOGR

There are 3 peg colors correct and 0 pegs in the correctposition

Enter your guess: BYOPY

Your guess is: BYOPY

There are 5 peg colors correct and 1 pegs in the correctposition

Enter your guess: OBYPY

Your guess is: OBYPY

There are 5 peg colors correct and 3 pegs in the correctposition

Enter your guess: PBYOY

There are 5 pegs in the solution, your guess does not

have the correct number of pegs. Tryagain!!!

Enter your guess: PBYOY

Your guess is: PBYOY

There are 5 peg colors correct and 2 pegs in the correctposition

Enter your guess: YBYPO

Your guess is: YBYPO

You Won!!!!!

The pegs that were chosen were YBYPO

Do you want to play another game of Master Mind? (Y/N): n

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Design Operation And Evaluation Of Mobile Communications

Authors: Gavriel Salvendy ,June Wei

1st Edition

3030770249, 978-3030770242

More Books

Students also viewed these Programming questions

Question

Describe effectiveness of reading at night?

Answered: 1 week ago

Question

find all matrices A (a) A = 13 (b) A + A = 213

Answered: 1 week ago

Question

Can partitioned join be used for r r.A s? Explain your answer

Answered: 1 week ago

Question

How does stock price maximization benefit society?

Answered: 1 week ago

Question

How are interest rates used to allocate capital among firms?

Answered: 1 week ago