Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

ID: 3663940 • Letter: I

Question

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;

}

Please implement the following:

1. 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).

Explanation / Answer

c)

#include <iostream>
#include <stdlib.h>
#include <cstdlib>
using namespace std;
long int NumberRange,CodeLength;

class CodeClass
{
long int Guess,ActualCode;
int NumberOfDigits=0;
int Produced;
int n=0;
public:
CodeClass(long int len,long int rng)
{


l: Produced=(rand() % rng); //First time number is generated
n=Produced;

do{//to check the number of digits in the sequence
++NumberOfDigits;
Produced/=10;

}while(Produced);

if(NumberOfDigits==len){//if generated and codebreaker chosen code has the same number of digits
cout << " Code maker Code is = " << n << endl;
cout << " Code breaker also GUESS the "<< len << " sequence number" << endl;
}
else
goto l;//if first time generated number do == to length then regenerate until == to Length

}

};

int main()
{

cout << "Hi Code Breaker Please enter the Range and Code length" << endl;
cin >> NumberRange >> CodeLength;
CodeClass c(CodeLength,NumberRange);
return 0;
}

d)

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:

Here is the code for the above said function:

int checkCorrect(Code c)
{
int countCorrects = 0;   //Initializes the count to 0.
for(int i = 0; i < n; i++)   //For each value in the vector.
   {
    if(codeVector.at(i) == c.getValueAtLocation(i))   //If the value in vector equals the value of vector of object c, at the same location,
     countCorrects++;       // then increment the counter.
   }
return countCorrects;    //Finally return count.
}

Here getValueAtLocation() is a member function of the class code, which returns the value in the vector at given location.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote