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

Below is a discussion of the game \"Mastermind\" that should be implemented usin

ID: 3662944 • Letter: B

Question

Below is a discussion of the game "Mastermind" that should be implemented using C++. I need help in implementing one class in this code. Below are the rules of the game just so you get an idea about it:

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.

Here goes my question:

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.

Please do part e only

Below is the code that I have until now (essentially parts a,b,c and d):

//

// proj1parta.cpp

//

//

// Created by Saif Abu Hashish on 1/23/16.

//

//

#include <vector>

using namespace std;

class Code{

private:

vector<int> codeVec;

int maxVal;

int numDigits;

public:

Code(int n, int m);

void initialize();

bool checkCorrect(const Code &another);

bool checkInCorrect(const Code &another);

};

Code::Code(int n, int m){

numDigits = n;

maxVal = m;

}

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

}

int main()

{

  

cout << "Hi Code Breaker Please enter the Range and Code length" << endl;

cin >> NumberRange >> CodeLength;

CodeClass c(CodeLength,NumberRange);

return 0;

}

Explanation / Answer

#include <bits/stdc++.h>
using namespace std;

class Code{
private:
vector<int> codeVec;
int maxVal;
int numDigits;
public:
Code(int n, int m);
void initialize();
int checkCorrect(vector<int> vect);
int checkInCorrect(vector<int> vect);
int getValueAtLocation(int n);
};

Code::Code(int n, int m){
numDigits = n;
maxVal = m;
}

void Code::initialize(){
int i = 0;
while (i < numDigits){
int val = rand() % maxVal;
if (find(codeVec.begin(), codeVec.end(), val) == codeVec.end()){
codeVec.push_back(val);
i += 1;
}
}
}

int Code::getValueAtLocation(int n){
return codeVec[n];
}

int Code::checkCorrect(vector<int> vect){
int count = 0;
for (int i = 0; i < numDigits; i++){
if (vect[i] == codeVec[i])
count += 1;
}
return count;
}

int Code::checkInCorrect(vector<int> vect){
int count;
for (int i = 0; i < vect.size(); i++){
int pos = find(codeVec.begin(), codeVec.end(), vect[i]) - codeVec.begin();
if (pos < codeVec.size()){
if (pos != i)
count += 1;
}
}
return count;
}


int main(){
srand (time(NULL));
int NumberRange,CodeLength;
cout << "Hi Code Breaker Please enter the Range and Code length : ";
cin >> NumberRange >> CodeLength;
Code c(CodeLength,NumberRange);
c.initialize();

int total_guess = 0;
int n;
while (true){
vector<int> vect;
cout << "Enter the guess please : ";
for (int i = 0; i < CodeLength; i++){
cin >> n;
if (find(vect.begin(), vect.end(), n) == vect.end())
vect[i] = n;
else
vect[i] = -1;
}
int corr = c.checkCorrect(vect);
int in_corr = c.checkInCorrect(vect);
cout << "Two response are : " << corr << " " << in_corr << endl;
if (corr == CodeLength){
cout << "Code Breaker Won" << endl;
break;
}
else if (total_guess > 10){
cout << "Code Maker Won" << endl;
break;
}
total_guess += 1;
}
return 0;
}

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