Question

In this project, you will write a complete program that allows the user to play a...

In this project, you will write a complete program that allows the user to play a game of Mastermind against the computer. A Mastermind game has the following steps:

1. The codebreaker is prompted to enter two integers: the code length n, and the range of digits m.

2. The codemaker selects a code: a random sequence of n digits, each of which is in the range [0,m-1].

3. The codebreaker is prompted to enter a guess, an n-digit sequence.

4. The codemaker responds by printing two values that indicate how close the guess is to the code. The first response value is the number of digits that are the right digit in the right location. The second response value is the number of digits that are the right digit in the wrong location. For example if the code is 1, 2, 3, 4, 5 and the guess is 5, 0, 3, 2, 6, the response would be 1, 2 because one digit (3) is the right digit in the right location, and two digits (2 and 5) are the right digits in the wrong locations. Note that no digit in the code or guess is counted more than once. If the code is 1, 2, 3, 4, 5 and the guess is 2, 1, 2, 2, 2, the response is 0, 2. If the code is 3, 2, 3, 3, 3 and the guess is 1, 3, 3, 4, 5, the response is 1, 1.

5. The codebreaker is prompted to continue entering guesses. The codebreaker wins if the correct code is guessed in ten or fewer guesses, and otherwise the codemaker wins.

I have a code that contains the following parts of the program:

1. Implement the class code which stores the code as a vector and contains

(a) the code class declaration,

(b) a constructor that is passed values n and m and initialize the code object,

(c) a function that initializes the code randomly,

(d) a function checkCorrect which is passed a guess as a parameter, i.e. another code object, and which returns the number of correct digits in the correct location,

(e) a function checkIncorrect which is passed a guess as a parameter (i.e. another code object and returns the number of correct digits in the incorrect location. No digit in the guess or the code should be counted more than once.

2. Implement a function main() which initializes a secrete code and prints out the result of calling checkCorrect and checkIncorrect for three sample guess codes ((5, 0, 3, 2, 6), (2, 1, 2, 2, 2), (1, 3, 3, 4, 5)). Please print the secrete code as well.

The code is the following:

#include "game.h"

#include <iostream>

#include <stdlib.h>

#include <vector>

#include <random>

#include<ctime>

using namespace std;

Game::Game(int y, int z)

// constructor that initalizes the secret code,length,range,

// and guesscode vector size

{

   length = y;

   range = z;

   generatecode();

   guesscode.resize(y);

}// end constructor

void Game::inputguess()

{

int tries = 1;

   cout << "you have 3 tries" << endl;

do

       // only allows 3 guesses by keeping track under variable tries;

       // fills the guess code vector;

       // prints how many numbers are in correct and incorrect position

       // through the function guesscodeverify

   {

       cout << "Enter guess "<<tries<<" with a space seperating each number " << endl;

       for (int i = 0; i < length; i++)

           cin >> guesscode[i];

       guesscodeverify(guesscode);

       tries += 1;

   } while (tries < 4);

}

void Game::generatecode()

// creates and displays the secret code

// usinr a random code generator

{

   mastercode.resize(length);

   guesscode.resize(length);

   cout << "secret code = ";

   srand((unsigned int)time(NULL));

for (int i = 0; i < length; i++)

   {

       mastercode[i] = rand() % range;

       cout << mastercode[i]<<" ";

   }

   cout << endl;

}// end generatecode

void Game::guesscodeverify(vector<int> temp)

// passes the guess vector to checkCorrect and checkIncorrect functions

// displays how many digits are in correct and incorrect position

{

   cout << "The number of digits in the correct position is " << checkCorrect(guesscode) << endl;

   cout << "The number of digits in the incorrect position is " << checkIncorrect(guesscode) << endl;

}// end guesscodegenerator

int Game::checkCorrect(vector<int>guesscode)

// returns how many digits are in the correct position

{

int count = 0;

for (int i = 0; i < length; i++)

   {

       // compares values at same position in vector 1 by 1 and if equal increase count

       if (guesscode[i] == mastercode[i])

           count++;

   }// end for

return count;

}// end checkCorrect

int Game::checkIncorrect(vector<int>guesscode)

// creates temporary vectors to pass the secret and guess vector to so the original

// values are not alterd and then passes the temporary vectors and returns how many

// digits are in the incorrect position

{

int count = 0;

   vector<int>guesscopy = guesscode; //temporary vector

   vector<int>mastercopy = mastercode; //temporary vetor

for (int i = 0; i < length; i++)

   {

       // loop for guesscopy vector digits

       for (int k = 0; k < length; k++)

       {

           // loop for mastercopy vector digits

           if (guesscopy[i] == mastercopy[k] && i == k && guesscopy[i] != 10)

           {

               // checks if the value in the guess vector is equal to the

               // mastercode vector value and if they are in the same position

               // and if it not equal to 10.

               // if the mastercopy and guesscopy vector value are the same

               // as well as the position then both values are replaced with

               // a 10 so that they are omitted from being included in the for

               // loop to check if the correct value is in the incorrect position

               mastercopy[k] = 10;

               guesscopy[i] = 10;

           }// end if

       }// end for(mastercopy)

   }// end for(guesscopy)

for (int i = 0; i < length; i++)

   {

       // loop for guesscopy vector digits

       for (int k = 0; k < length; k++)

       {

           // loop for mastercopy vector digits

           if (guesscopy[i] == mastercopy[k] && i != k && guesscopy[i] != 10)

           {

               // if a guesscopy vector value equal a master copy vector value

               // and the positions arent the same and the value is not equal to 10

               // then the values that match are both replaced with value 10

               // and the count is increased so that they can't be matched again

               // while going through the for loop

               count++;

               mastercopy[k] = 10;

               guesscopy[i] = 10;

           }// end if

       }//end for (mastercopy)

   }// end for (guesscopy)

return count;

}// end checkIncorrect

The header file game.h is the following:

#include <iostream>

#include <stdlib.h>

#include <vector>

using namespace std;

class Game

{

public:

//   construcor & member function for

//   checkIncorrect, checkCorrect

//   and guesscode generator

   Game(int y, int z);

int checkIncorrect(vector<int> guess);

int checkCorrect(vector<int> guess);

void guesscodeverify(vector<int> temp);

void inputguess();

private:

// private values and vectors

int length; // code length

int range; // range of digits

void generatecode();

   vector<int>mastercode;

   vector<int>guesscode;

};// end Game class

The main function is the following:

#include<iostream>

#include <vector>

#include "game.h"

using namespace std;

int main()

// includes temporary variables to passs to constructor and member functions

{

int length;

int range;

   cout << "enter the length that you want the master code to be" << endl;

   cin >> length;

   cout << "enter the range of number you want each number in the master code to fall within";

   cin >> range;

   Game game1(length, range);

// runs entire code including checkCorrect and checkIncorrect

   game1.inputguess();

return 0;

}

My Question is: Please Implement the class response which stores the response to a guess (number correct and number incorrect), and which includes:

(a) a constructor,

(b) functions to set and get the individual stored values within a response,

(c) an overloaded operator == that compares responses and returns true if they are equa (global),

(d) an overloaded operator << that prints a response (global).

I posted the code that I have until now for your reference.

0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
In this project, you will write a complete program that allows the user to play a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • I am trying to write this code which asks "Write a program that ask the user,...

    I am trying to write this code which asks "Write a program that ask the user, the question: What is a^b? //The program generates two signed integers and gives the user four attempts to get the correct answer //a=- , b = + //a= + , b = - " So far this what I wrote. I am not sure how to do this correctly. #include<iostream> #include<cstdlib> #include<ctime> using namespace std; int main() {    srand(time(0));    int guess,a,ans,b, k;...

  • Create a new program, WordGuessingGame. Using a while loop, create a game for the user. The...

    Create a new program, WordGuessingGame. Using a while loop, create a game for the user. The game requires the user to guess a secret word. The game does not end until the user guesses the word. After they win the game, they are prompted to choose to play again. The secret word that user must guess is "valentine". It is a case-sensitive analysis With each guess... If the guess does not have an equal number of characters, tell the user...

  • Here is the skeleton of the password checking program. We have the character counting function (you...

    Here is the skeleton of the password checking program. We have the character counting function (you will need to finish it), and a function to check if the password is good. Change the password algorithm so that (three of the four) two uppercase, two lowercase, two digits, or one other is present. Also change the required length of the password to 10. Bonus points for making the program loop until a good password is entered. You will need to read...

  • Write a c++ code into the given code  to find composite numbers from the given random number...

    Write a c++ code into the given code  to find composite numbers from the given random number list. The composite numbers is only counted once if there is a repeated number. I need to use this code and add on a code to find if the numbers generated is a composite function. Please help #include <cmath> #include <cstdlib> #include <ctime> #include <iostream> #include <vector> using namespace std; int main() {    srand(time(NULL)); int size_of_list = 0; // the number of random...

  • Hello, I am working on a C++ pick 5 lottery game that gives you the option...

    Hello, I am working on a C++ pick 5 lottery game that gives you the option to play over and over. I have everything working right except that every time the game runs it generates the same winning numbers. I know this is an srand or rand problem, (at least I think it is), but I can't figure out what my mistake is. I've tried moving srand out of the loop, but I'm still getting the same random numbers every...

  • Can you fix this program and run an output for me. I'm using C++ #include using...

    Can you fix this program and run an output for me. I'm using C++ #include using namespace std; //function to calculate number of unique digit in a number and retun it int countUniqueDigit(int input) {    int uniqueDigitCount = 0;    int storeDigit = 0;    int digit = 0;    while (input > 0) {        digit = 1 << (input % 10);        if (!(storeDigit & digit)) {            storeDigit |= digit;       ...

  • Write a C++ program to play the number guessing game with user as follows.

    Write a C++ program to play the numberguessing game with user as follows.The user determines a number between 1~100.The program has 4 tries to guess the number byproviding a range (low, high) on each try.The user tells the program whether the number isbelow, within, or above the range.The program announces a game loss after 4 incorrecttries.

  • I need help with this assignment. Please include comments throughout the program. Thanks Develop an algorithm...

    I need help with this assignment. Please include comments throughout the program. Thanks Develop an algorithm and write the program in java for a simple game of guessing at a secret five-digit code. When the user enters a guess at the code, the program returns two values: the number of digits in the guess that are in the correct position and the sum of those digits. For example, if the secret code is 13720, and the user guesses 83521, the...

  • please c++ with functions *Modify the Guessing Game Write the secret number to a file. Then...

    please c++ with functions *Modify the Guessing Game Write the secret number to a file. Then write each user guess and answer to the file. (on separate lines) Write the number of guesses to the file at the end of the game. After the game is finished, ask the user if they want to play again. If 'n' or 'N' don't play again, otherwise play again! NOTE: your file should have more than one game in it if the user...

  • #include<stdio.h> #include<stdlib.h> #include <time.h> int main() { /* first you have to let the computer generate...

    #include<stdio.h> #include<stdlib.h> #include <time.h> int main() { /* first you have to let the computer generate a random number. Then it has to declare how many tries the user has for the game loop. we then need the player to enter their guess. After every guess we have to give an output of how many numbers they have in the right location and how many they have the right number. The player will keep guessing until their 10 tries are...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT