Questio n 2 Mastermind is a 2-player boardgame in which one player (the “Codemaster”) prepares a code of four colours from a set of six, the “Codebreaker” then repeatedly tries to guess the code and...

1 answer below »


Questio
n

2


Mastermind is a 2-player boardgame in which one player (the “Codemaster”) prepares a code of four colours from a set of six, the “Codebreaker” then repeatedly tries to guess the code and receives feedback from the Codemaster. The feedback given by the Codemaster is in the form of white and black pegs where each white peg represents a correct colour in the wrong position, and a black peg represents a correct colour in the correct position. For this question you will create a class Mastermind that acts as the Codemaster in a variant of this game. In this variant the passcode, instead of being of length 4, can be any length greater than 0. Your Mastermind should have a constructor with two integer parameters, the first being the length of the code for this game, and the second being the number of guesses the “Codebreaker” has to guess the code. You may assume the codeLength and guessLimit being passed to your constructor are greater than zero. Each character in the passcode in our game will be one of the characters in the set (A, B, C, D, E, F).




Consider the following Mastermind class definition (mastermind.h).


struct Mastermind { int codeLength; int guessLimit;


char* code;


Mastermind(int codeLength, int guessLimit); ˜Mastermind();


void playGame();


};


You must implement the three declared methods above so that your executable built with the main function provided by


us in a3q2.cc matches the output of the provided executable. Specification notes:


• playGame should print out the welcome message and read from stdin until you’ve received codeLength valid


characters.


• When reading the characters from the user, you must accept upper and lowercase characters from the set (A, B, C, D, E, F)


as valid characters, and ignore any other input.


• After each guess you should print out the number of black and white pegs the user is to receive, in the form “Xb, Yw” where X and Y are the number of black and white pegs respectively. Then if the game has not been won or lost you should print out the next guess message, which includes the number of guesses left and reads in another guess.


• If the code is guessed before the guess limit is reached you should print out the win message “You won in guessCount


guesses!”


• If the code is not guessed before the guess limit is reached you should print the loss message “You lost! The password


was: ” and the value of the password followed by a newline.


• The number of black pegs given for each correct guess is equal to the number of characters the user has in the correct


position in the code.






Page 3 of 8







• The number of white pegs given for each guess is equal to the number of characters the user has that are in the code but in the wrong position. However the number of black and white pegs can never exceed the size of the code. This means that if a code has only one A and the user guesses 2 A’s in the wrong position, they receive only one white peg for A. Additionally, if the code has only one A and the user has guessed 3 A’s, two in the wrong position and one in the correct position, the user only receives the single black peg for the A’s and no white pegs.


Consider the following sample execution of the program run with command-line arguments 4 3, indicating codeLength and guessLimit respectively.


Welcome to Mastermind! Please enter your first guess:


AAAA


1b,0w


2 guesses left. Enter guess:


BCDE


0b,3w


1 guesses left. Enter guess:


ADDA


2b,0w


You lost! The password was: ADEB


Would you like to play again? (Y/N):


N


Note that the lines in blue denote user input, and are not output by your program. You will be provided a simple test harness a3q2.cc, the .args and .in files that reproduce the example above. The main function provided takes the two command-line arguments corresponding to codeLength, and guessLimit, constructs a Mastermind object with those values, sets the variable code to be a password randomly generated by the functions defined in password.cc, and starts the game by calling the method playGame.
To ensure the “randomness” factor still allows for testing, please do not
change

th
e

a3q2.cc and password[.h|.cc] files.





Deliverables:


Full implementation of the Mastermind class in C++.Your zip archive should contain at minimum mastermind.h, mastermind.cc, the unchanged files a3q2.cc, password.h, password.cc, and your Makefile. Your Makefile must create an executable named a3q2. Note that the executable name is case-sensitive. Any additional classes (if created) must each reside in their own .h and .cc files.

Answered 3 days AfterJun 23, 2021

Answer To: Questio n 2 Mastermind is a 2-player boardgame in which one player (the “Codemaster”) prepares a...

Ali Asgar answered on Jun 26 2021
145 Votes
a3q2.cc
#include "mastermind.h"
#include "password.h"
#include
#include "password.cc"
#include "mastermind.cc
"
using namespace std;
int main(int argc, char** argv) {
if (argc!=3) {
cout << "Incorrect usage, should be: " << endl;
cout << argv[0] << " codeLength guessLimit" << endl;
return 1;
}
if (argc==3) {
mastermind mm {atoi(argv[1]),atoi(argv[2])};
//cout << mm.codeLength < //cout << mm.guessLimit < char passwd[atoi(argv[1])];
setPassword(passwd, mm.codeLength);
mm.code=passwd;
char choice;
while (mm.playGame()) {
     setPassword(mm.code, mm.codeLength);
}
}

return 0;
}
makefile
g++ -c a3q2.cc
g++ -o a3q2 a3q2.o
mastermind.cc
#include
mastermind::mastermind(int codeLength, int guessLimit)
{    this->codeLength=codeLength;
    this->guessLimit=guessLimit;
}
//mastermind::~mastermind() { delete [] code; }
bool mastermind::playGame()
{    char guessedCode[this->codeLength];
    int B=0,W=0;        //The Pegs.
    //Check array for finding White Pegs.
    int chkCode[this->codeLength],chkGuess[this->codeLength];
    char * pass = this->code;
    printf("\n\nWelcome to Master-Mind\nEnter a %d character long "
        "guess...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here