Question: I am working in visual studios and need help writting this in C++. I am in a entry level computer science class so I dont

I am working in visual studios and need help writting this in C++. I am in a entry level computer science class so I dont understand many functions other than the basic very basic ones.

Your task is to implement the classic board game Mastermind, a game of strategy and luck in which one player tries to crack a secret code set by another player.

In the game, the mastermind sets a secret code consisting of a sequence of colored pegs. The code is hidden from the player.

The player has a limited number of guesses to make. For each guess they must enter a complete sequence of pegs. If they guess the correct code, they win. If not, they get two types of feedback:

Black Result Pegs: The player gets one black peg for each peg of the code they get exactly correct.

White Result Pegs: The player gets one black peg for each peg of the code they get the correct color of, but not the correct location. Note this counts duplicates only if present in both codes: If the master code has two green pegs, and the guess has two green pegs in the wrong positions, the player gets two white result pegs, but if the player code has only one green peg, the player only gets one white result peg.

To make implementing the game easier, the mastermind will be played by the computer. In other words, the code should be selected randomly. (see extra credit for extensions)

Additionally, starter code has been provided, in three files: PegCode.h, PegCode.cpp, and application.cpp. Guess and master peg codes should be represented using the PegCode class.

You need to fill in:

The main part of the code that reads in a guess peg code, keeps track of how many rounds are left, and coordinates the calls to PegCodes member functions. Note: This must use the overloaded PegCode == operator somewhere.

The member functions getPegColorAt, getNumBlackResultPegs and getNumWhiteResultPegs. The latter two calculate the black/white pegs described above when passed in a guess code and called on the master code. Getting the white pegs is a bit challenging so the first part of the function has already been implemented for you.

Code to overload the == operator, which should test for exact equality of two PegCodes.

This is the application.cpp:

#include #include #include #include "PegCode.h" using namespace std; //The number of rounds the user gets to guess before they lose and the solution is revealed /// - Change this to make the game easier or harder const int NUMBER_OF_ROUNDS = 12; int main() { //YOUR CODE HERE return 0; } 

This is the PegCode.cpp :

#include #include #include "PegCode.h" using namespace std; PegCode::PegCode(PegColor peglist[], int length) { //Validate to ensure right number of pegs if (length != NUMBER_OF_PEGS) { cout << "Wrong length for pegcode!" << endl; exit(-1); } for (int i = 0; i < NUMBER_OF_PEGS; i++) { pegs[i] = peglist[i]; } //Validate to ensure no empty pegs on object construction for (int i = 0; i < NUMBER_OF_PEGS; i++) { if (pegs[i] == EMPTY) { cout << "Empty Peg in created pegCode!" << endl; exit(-1); } } } void PegCode::clearPegAt(int index) { pegs[index] = EMPTY; } void PegCode::print() { for (int i = 0; i < NUMBER_OF_PEGS; i++) { switch (pegs[i]) { case BLUE: cout << "Blue"; break; case RED: cout << "Red"; break; case YELLOW: cout << "Yellow"; break; case GREEN: cout << "Green"; break; case ORANGE: cout << "Orange"; break; case PURPLE: cout << "Purple"; break; case EMPTY: cout << "EMPTY"; break; default: cout << "Huh?"; break; } cout << " "; } cout << endl; } PegColor PegCode::getColorAt(int index) const { //YOUR CODE HERE return EMPTY;//Remove once implemented } int PegCode::getNumBlackResultPegs(PegCode guessCode) const { //YOUR CODE HERE return 0; //remove once implemented } int PegCode::getNumWhiteResultPegs(PegCode guessCode) const { //Don't want to count cases that are counted for black pegs // so clear them out of the guess code as they've already been // accounted for for (int i = 0; i < NUMBER_OF_PEGS; i++) { if (guessCode.getColorAt(i) == pegs[i]) { guessCode.clearPegAt(i); } } //YOUR CODE HERE //Hint: since the guessCode is passed by value, use // guessCode.clearPegAt() to make sure you don't double-count // colors return 0; //change this once implemented } //Add something to overload the == operator for PegCodes //YOUR CODE HERE 

This is the pegCode.h :

#ifndef PEGCODE_H #define PEGCODE_H using namespace std; //The number of pegs in Mastermind peg codes - Change this to make the game easier or harder const int NUMBER_OF_PEGS = 4; const int NUMBER_OF_COLORS = 6; enum PegColor { BLUE, RED, YELLOW, GREEN, ORANGE, PURPLE, EMPTY //should never appear in a "real" PegCode - might be useful as a temporary value }; class PegCode { public: /*Initializes the list of pegs*/ PegCode(PegColor peglist[], int length); /*Changes the PegColor at a specified index to EMPTY.*/ void clearPegAt(int index); /*Prints out the PegCode*/ void print(); /*Gets the PegColor at a specified index. index=0 should be the first peg. * The last "const" is there to indicate to C++ we are not modifying the object. */ PegColor getColorAt(int index) const; /* Gets the number of pegs exactly the same (color+ location) as other*/ int getNumBlackResultPegs(PegCode guessCode) const; /* Gets the number of pegs in guessCode the same color as this one, but not in the correct location*/ int getNumWhiteResultPegs(PegCode guessCode) const; //TODO: Add something here to overload the == operator private: PegColor pegs[NUMBER_OF_PEGS]; }; #endif 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!