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

This is in C++: For this assignment, you will write a program to play the game M

ID: 3812959 • Letter: T

Question

This is in C++:

For this assignment, you will write a program to play the game Mastermind. Please see https://en.wikipedia.orfi/wiki/Masterm¡nd (board game) for details of how to play the game. Specifications: 1. Your program should print out instructions for the game. 2. A game consists of trying to guess a secret randomly generated, 4-digit code where each digit is a number from 1 to 6. A player will have 12 guesses to guess the correct 4-digit code. 4. After each guess, your program must inform the player how many digits were guessed correctly in the correct location and how many digits were guessed correctly but in the wrong location. If the player guesses correctly, then your program should congratulate the player on successfully guessing the correct answer. 5. Your program must check that the user enters a valid guess, i.e., that it is exactly 4 digits and that each digit is a number from 1 to 6. 6. After finishing a game, your program should ask the user if they want to play again or quit the program. The program should play another game unless the user chose to quit the program. 7. Your program must include and properly use a function to check if the guess entered by the user is valid or not. The function should return true or false. 8. Your program must include and properly use a function to count the number of digits of the guess that are correct and in the correct position. This function must take as input the guess and the secret code to be guessed. The function should return the number of digits that are correct and in the correct location. 9. Your program must include and properly use a function to count the number of digits of the guess that are correct and in the incorrect position. This function must take as input the guess and the secret code to be guessed. The function should return the number of digits that are correct and in the incorrect location. 10. Your program should print out the secret answer at the beginning of each round for grading/debugging purposes.

Explanation / Answer

c++ code:

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

bool isvalid(int d)
{
   if(d < 1 or d>6)
   {
       return false;
   }
   else
   {
       return true;
   }
}

int f1(vector<int> digits, int d1, int d2, int d3, int d4)
{
   int c = 0;
   if( digits[0] == d1)
   {c++;}
   if( digits[1] == d2)
   {c++;}
   if(digits[2] == d3)
   {c++;}
   if(digits[3] == d4)
   {c++;}
   return c;
}

int f2( std::vector<int> digits, int d1, int d2, int d3, int d4)
{
int c = 0;
   if(find(digits.begin(), digits.end(),d1) != digits.end() and digits[0] != d1)
   {c++;}
   if(find(digits.begin(), digits.end(),d2) != digits.end() and digits[1] != d2)
   {c++;}
   if(find(digits.begin(), digits.end(),d3) != digits.end() and digits[2] != d3)
   {c++;}
   if(find(digits.begin(), digits.end(),d4) != digits.end() and digits[3] != d4)
   {c++;}
   return c;
}


int main()
{

   while(true)
   {
       cout << "Game Starts! ";
       vector<int> digits;
       cout << "Original number is ";
       for (int i = 0; i < 4; ++i)
       {
           int tmp = rand()%6 + 1;
           digits.push_back(tmp);
           cout << tmp;
       }
       cout << endl;


       int g = 0;
       while(g < 12)
       {
           g++;
           int d1 = 0;
           int d2 = 0;
           int d3 = 0;
           int d4 = 0;
           while(!(isvalid(d1)))
           {
           cout << "Enter guess of digit 1 in range 1 to 6" << endl; cin >> d1;  
           if(!(isvalid(d1)))
           {
               cout << "Invalid Input! ";
           }
       }
           while(!(isvalid(d2)))
           {
           cout << "Enter guess of digit 2 in range 1 to 6" << endl; cin >> d2;  
           if(!(isvalid(d2)))
           {
               cout << "Invalid Input! ";
           }
       }
           while(!(isvalid(d3)))
           {
           cout << "Enter guess of digit 3 in range 1 to 6" << endl; cin >> d3;  
           if(!(isvalid(d3)))
           {
               cout << "Invalid Input! ";
           }
       }
           while(!(isvalid(d4)))
           {
           cout << "Enter guess of digit 4 in range 1 to 6" << endl; cin >> d4;  
           if(!(isvalid(d4)))
           {
               cout << "Invalid Input! ";
           }
       }

       int correctdigits = f1(digits,d1,d2,d3,d4);
           if(correctdigits == 4)
           {
               cout << "Congrats all digits are guessed corectly ";
               break;
           }
           int corect_but_wrong_pos = f2(digits,d1,d2,d3,d4);
           cout << "You Correctly guessed " << correctdigits << endl;
           cout << "You Guessed " << corect_but_wrong_pos << " digits Correctly but wrong location"<< endl;
   }

   if(g == 12)
   {
       cout << "Sorry you lost the game! ";
   }

       int c;
       cout << "Enter 1 to play the game again and 2 to exit ";  
       cin >> c;
       if(c != 1)
       {
           exit(0);
       }

   }

   return 0;
}

Sample Output:

Game Starts!
Original number is 6655
Enter guess of digit 1 in range 1 to 6
6
Enter guess of digit 2 in range 1 to 6
6
Enter guess of digit 3 in range 1 to 6
5
Enter guess of digit 4 in range 1 to 6
5
Congrats all digits are guessed corectly
Enter 1 to play the game again and 2 to exit

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