Please help write a complete program that allows the user to play a game of Mast
ID: 3877279 • Letter: P
Question
Please help write a complete program that allows the user to play a game of Mastermind against the computer in DEV C++
(Each project should contain ALL source and user defined header files)
Project #1 In this project, you will write a complete program that allows the user to play a game of ter. A Masterminad game has the followring stepasi 1. The codemaker selects a code, a random sequence of 4 digits, each of which is in the range [0,5] 2. The codebreaker is prompted to enter a guess, a 4-digit sequence. 3. 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 secret code is (1,2, 3,4) and the guess is (4, 0,3,2), the response would be (1,2) because one digit (3) is the right digit in the right location, and two digits (2 and 4) are the right digits in the wrong locations. Note that no digit in the code or guess is counted more than once. If the secret code is (1,2,3, 4) and the guess is (2,1,2,2), the response is (0,2). If the secret code is (3, 2, 3,3) and the guess is (1,3,3, 4), the response is (1,1 4. 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 winsExplanation / Answer
code.h :------------------->>>>>>>>>>>>>>>
#ifndef CODE_H
#define CODE_H
#include<vector>
#include<cstdlib>
#include<ctime>
using namespace std;
//the class code to store secret or guess
class Code{
vector<int> code;
public:
Code();
void setCode(vector<int>);
void randomize();
int checkCorrect(Code);
int checkIncorrect(Code);
vector<int> getCode();
};
#endif
code.cp : :----------------->>>>>>>>>>>
#include<iostream>
#include "code.h"
Code::Code(){
code.reserve(4);
}
int Code::checkCorrect(Code ch){
int count = 0;
vector<int> guess;
guess.reserve(4);
guess = ch.getCode();
for(int i = 0;i<4;i++){
if(code[i] == guess[i]){
count++;
}
}
return count;
}
int Code::checkIncorrect(Code ch){
int count = 0;
vector<int> guess;
guess = ch.getCode();
for(int i = 0;i<4;i++){
for(int j=0;j<4;j++){
if(guess[i] == code[j] && i != j){
count++;
break;
}
}
}
return count;
}
void Code::setCode(vector<int> guess){
for(int i = 0;i<4;i++){
code.push_back(guess[i]);
}
}
vector<int> Code::getCode(){
return code;
}
void Code::randomize(){
srand(time(0));
int temp;
for(int i = 0;i<4;i++){
temp = rand()%5;
code.push_back(temp);
}
}
response.h : -------------------->>>>>>>>>>>>>>>
#ifndef RESPONSE_H
#define RESPONSE_H
//the class code to store secret or guess
class Response{
int correct;
int incorrect;
public:
Response();
void setCorrect(int);
void setIncorrect(int);
bool compare(Response);
void print();
int getCorrect();
int getIncorrect();
};
#endif
response.cpp : ------------------>>>>>>>>>>>
#include<iostream>
#include "response.h"
using namespace std;
Response::Response(){
correct = 0;
incorrect = 0;
}
int Response::getCorrect(){
return correct;
}
int Response::getIncorrect(){
return incorrect;
}
void Response::setCorrect(int c){
correct = c;
}
void Response::setIncorrect(int in){
incorrect = in;
}
bool Response::compare(Response r1){
if(r1.getCorrect() == correct && r1.getIncorrect() == incorrect){
return true;
}
return false;
}
void Response::print(){
cout<<"("<<correct<<" , "<<incorrect<<")";
}
mainGame.cpp : --------------------->>>>>>>>>>>>>>
#include "code.cpp"
#include "response.cpp"
class MasterMind{
Code secret;
public:
MasterMind(){
secret.randomize();
}
void printSecret(){
vector<int> secr = secret.getCode();
cout<<" Secret Code : ";
for(int i = 0;i<4;i++){
cout<<secr[i];
}
}
Code humanGuess(){
vector<int> guess;
guess.reserve(4);
string s1;
s1.reserve(4);
cout<<" enter the Four digit guess : ";
cin>>s1;
for(int i = 0;i<4;i++){
guess.push_back(((int)(s1[i] - '0')));
}
Code temp;
temp.setCode(guess);
return temp;
}
Response getResponse(Code guess){
Response temp;
temp.setCorrect(secret.checkCorrect(guess));
temp.setIncorrect(secret.checkIncorrect(guess));
return temp;
}
bool isSolved(Response r1){
int c = r1.getCorrect();
if(c == 4){
return true;
}
return false;
}
void playGame(){
secret.randomize();
Response res;
Code guess;
bool check;
int st = 0;
while(st <= 10){
guess = humanGuess();
res = getResponse(guess);
check = isSolved(res);
if(check){
break;
}
cout<<" You are Close to : ";
res.print();
st++;
}
if(st > 10){
cout<<" You Loose the Game : ";
}
else{
cout<<" You Win the Game :";
}
}
};
int main(){
MasterMind m1;
m1.playGame();
return 0;
}