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

Mastermind Game C++ Help! Hello, I have the following code for the mastermind ga

ID: 638805 • Letter: M

Question

Mastermind Game C++ Help!

Hello, I have the following code for the mastermind game, but for some reason the checkCorrect and checkIncorrect are not giving me the right information. Can anyone help? Im not sure if theres a mistake here or if in another part of the code, so if this looks right to you, please let me know. Thank you.

class code

{

public:

int rightPos;

int wrongPos;

int r;

void randomNumber();

int checkCorrect(code c);

int checkIncorrect(code c);

vector<int> randCode;

vector<int> inputVect;

};

void code :: randomNumber()

{

srand(time(0));

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

{

randCode.push_back(4);

r= rand() % 6;

randCode[i] = r;

cout << randCode[i];

}

}

int code :: checkCorrect (code c)

{

for (int j=0; j <4; j++)

{

if (randCode[j] == c.inputVect[j])

rightPos++;

}

return rightPos;

}

int code :: checkIncorrect(code c)

{

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

{

if (randCode[i] != c.inputVect[i])

for (int j=0; j<4; j++)

if (randCode[j] == c.inputVect[i])

wrongPos++;

}

return wrongPos;

}

Explanation / Answer

Note:

Only thing need to be done is you need to enter the values for the inputVect.

The code provided is correct.

The program execution is depended on the calling of the functions by which object and the parameter object.

The below is the code with sample output by using two different objects.

Program code:

// MasterMind.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

#include <iostream>

#include <vector>

#include <time.h>

using namespace std;

class code

{

public:

    int rightPos;

    int wrongPos;

    int r;

    void randomNumber();

    int checkCorrect(code c);

    int checkIncorrect(code c);

    vector<int> randCode;

    vector<int> inputVect;

};

void code :: randomNumber()

{

    srand(time(0));

   

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

    {

         randCode.push_back(4);

         r= rand() % 6;

         randCode[i] = r;

         cout << randCode[i];     

    }

}

int code :: checkCorrect (code c)

{

    rightPos=0;

    for (int j=0; j <4; j++)

    {

         if (randCode[j] == c.inputVect[j])

         {       

             rightPos++;

         }

    }

    return rightPos;

}

int code :: checkIncorrect(code c)

{

    wrongPos=0;

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

    {

         if (randCode[i] != c.inputVect[i])

         {

             wrongPos++;

         }

    }

    return wrongPos;

}

int main()

{

    code mm;

    code cc;

    cc.randomNumber();

    cout<<endl;

    cout<<"Enter a 4 single digit guess number: "<<endl;

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

    {

         cout<<"Enter a single digit: ";

         mm.inputVect.push_back(4);

         cin>>mm.inputVect[i];    

    }

    cout<<"The random numbers are: ";

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

    {  

         cout << cc.randCode[i];       

    }

    cout<<endl;

    cout<<"The user numbers are: ";

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

    {  

         cout << mm.inputVect[i];      

    }

    cout<<endl;

    cout<<"Number of position values is correct: "<<cc.checkCorrect(mm)<<endl;

    cout<<"Number of position values is incorrect: "<<cc.checkIncorrect(mm)<<endl;

    cout<<endl;

    system("pause");

    return 0;

}

Sample Output:

1243

Enter a 4 single digit guess number:

Enter a single digit: 5

Enter a single digit: 6

Enter a single digit: 3

Enter a single digit: 1

The random numbers are: 1243

The user numbers are: 5631

Number of position values is correct: 0

Number of position values is incorrect: 4

Press any key to continue . . .

Sample Output:

4021

Enter a 4 single digit guess number:

Enter a single digit: 4

Enter a single digit: 5

Enter a single digit: 6

Enter a single digit: 2

The random numbers are: 4021

The user numbers are: 4562

Number of position values is correct: 1

Number of position values is incorrect: 3

Press any key to continue . . .