Create a working replica of the classic game MasterMind in C++. The idea of the
ID: 3733156 • Letter: C
Question
Create a working replica of the classic game MasterMind in C++. The idea of the game is for a player) to guess the secret code chosen by the computer. The code is a sequence of 4 colored pegs chosen from six colors available. The code-breaker makes a serie of pattern guesses - after each guess the code-maker provides feedback in the form of white or black pegs, the number of pegs that are of the right color and in the correct position are indicated with a black peg, and the number of pegs that are of the correct color but not in the correct position represented white pegs, please note that there is no correlation between the position of the pegs in the feedback area versus the pegs in the board. The code breaker has 10 opportunities to guess the secret code after what the code will be revealed and the program terminates indicating that the player lost.
BOARD PBEDB ACK 0 SOLUTI O N CONGRAT U LATIO NS YOU WONExplanation / Answer
Hello, I have a solution for you. Implemented everything as needed. Defined following things in this answer.
//code.cpp
#include<iostream>
#include<stdlib.h>
#include<time.h>
#define NUM_COLORS 4 //number of colors to be selected
#define TOTAL_NUM_COLORS 6 //total number of colors
using namespace std;
/*array to store all available colors*/
char pegs[]={'R','G','B','Y','O','V'};
int MAX_TRIES=10; //maximum tries
//method to check if a character c exist in a character array
bool isIn(char c, char array[NUM_COLORS]){
for(int i=0;i<NUM_COLORS;i++){
if(array[i]==c){
//found
return true;
}
}
//not found
return false;
}
//method to print the feedback
void printFeedback(char chosen_colors[NUM_COLORS] , char user_inputs[NUM_COLORS]){
cout<<" FEEDBACK: ";
for(int i=0;i<NUM_COLORS;i++){
if(user_inputs[i]==chosen_colors[i]){
//right color in right spot
cout<<"B ";
}else if(isIn(user_inputs[i],chosen_colors)){
//right color in wrong spot
cout<<"W ";
}else{
//wrong color
cout<<"_ ";
}
}
cout<<endl;
}
//method to check if all the colors have been solved
bool checkIfWin(char chosen_colors[NUM_COLORS],char user_inputs[NUM_COLORS]){
for(int i=0;i<NUM_COLORS;i++){
if(chosen_colors[i]!=user_inputs[i]){
//not solved
return false;
}
}
//solved
return true;
}
//method to print the solution at the end
void printSolution(char chosen_colors[NUM_COLORS]){
cout<<" SOLUTION: ";
for(int i=0;i<NUM_COLORS;i++){
cout<<chosen_colors[i]<<" ";
}
cout<<endl;
}
//method to print an intro message and description
void printIntro(){
cout<<"WELCOME TO THE GAME OF MASTERMIND!"<<endl;
cout<<"There are six colored pegs - Red, Green, Blue, Orange, Yellow and Violet"<<endl;
cout<<"4 colors are randomly selected from these colors (can contain duplicates)"<<endl;
cout<<"and you have to guess it in "<<MAX_TRIES<<" tries"<<endl;
cout<<" Input format: First letters of each colors"<<endl;
cout<<" Example: RGBY or R G B Y"<<endl;
}
int main(){
//seeding the random number generator
srand(time(NULL));
//creating an array of chosen colors, randomly
char chosen_colors[NUM_COLORS];
for(int i=0;i<NUM_COLORS;i++){
int randomIndex=rand()%TOTAL_NUM_COLORS;
chosen_colors[i]=pegs[randomIndex];
}
int tries=0;//number of attempts
bool gameOver=false;//keep the track of the game
char userInput[NUM_COLORS];//array to store user input
printIntro(); //printing intro message
//loop until the game is over
while(!gameOver){
cout<<" Enter your guesses: ";
for(int i=0;i<NUM_COLORS;i++){
char c;
cin>>c;
//converting to upper case
userInput[i]=toupper(c);
if(cin.peek()==' '){
//ignoring the space between chars, if any
cin.ignore(1);
}
}
//displaying the feedback
printFeedback(chosen_colors,userInput);
//checking if won
if(checkIfWin(chosen_colors,userInput)){
gameOver=true;
cout<<" Congratulations, you won..."<<endl;
//printing solution
printSolution(chosen_colors);
}
tries++;
if(tries==MAX_TRIES){
//attempts exhausted
gameOver=true;
cout<<" Game over! You couldn't 'guess it in maximum tries..."<<endl;
//printing solution
printSolution(chosen_colors);
}
}
return 0;
}
/*OUTPUT*/
WELCOME TO THE GAME OF MASTERMIND!
There are six colored pegs - Red, Green, Blue, Orange, Yellow and Violet
4 colors are randomly selected from these colors (can contain duplicates)
and you have to guess it in 10 tries
Input format: First letters of each colors
Example: RGBY or R G B Y
Enter your guesses: YRGB
FEEDBACK: W _ _ W
Enter your guesses: BVYY
FEEDBACK: B _ B B
Enter your guesses: BOYY
FEEDBACK: B B B B
Congratulations, you won...
SOLUTION: B O Y Y
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.