For this assignment, you will write a C++ program to play the game Mastermind. #include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>
using namespace std;
int main()
{
int size = 4;
int maxtries = 8;
int range = 6;
char lowest = 'a';
char highest = lowest + range -1;
string guess;
int number;
int correct;
int position;
/*srand(time(0));
string answer = "";
for (int i = 0; i < size; i++)
{
char ch = lowest + rand() % range;
answer += ch;
}*/
string answer = "abaa"; //For debugging
number = 1;
correct = 0;
position = 0;
while (number <= 8)
{
cout << "Guess #" << number << ": Enter 4 letters between the range of 'a' through 'f': ";
//cout << answer;
cout << "\n";
cin >> guess;
//if (number > 8)
//cout << "You went over the maximum allowed number of guesses\n";
if (guess == answer)
{
cout << "Right! It took you " << number << " move";
if (number != 1) cout << "s";
cout << "." << endl;
break;
cout << "You guessed right, this is my answer.\n";
cout << answer;
cout << "GAME OVER\n\n";
}
while (position < size)
{
if (size == position) correct++;
position++;
}
number++;
/*
cout << "Correct position: " << position << endl;
cout << "Correct letter: " << correct << endl;
*/
}
return 0;
}Your program will pick a random sequence of 4 letters and ask the user to guess them. After each guess, the program tells the user how many letters were right and in the right positions. For example, if the answer was "abcd" and the user guessed "adcb", the program would report that 2 letters were right. If the user guessed "acdc", the program would report that only 1 letter was right. The random letters can be anything between 'a' and 'f', and the user is only given up to 8 tries to get the whole sequence right.
I provide you with some code for things we have not learned in class yet. For your program to make up a word, you need to do a few things. At the top of your program, you should include the following lines to gain access to some library functions:
#include <ctime>
#include <cstdlib>
and within your program, you should create the following variables with the values given here:
int size = 4;
int maxtries = 8;
int range = 6;
char lowest = 'a';
char highest = lowest + range - 1;
These variables define the number of letters to be guessed (size), the number of guesses the user is allowed (maxtries), and the range of letters that can appear in the random string (lowest through highest, or 'a' through 'f', for a total of range possible characters). Your program should use these variables whenever those values are required. You will lose points if you fail to use them and use the constant values (4, 6, 8, 'a', 'f', etc.) instead.
To create a random string of characters, use the following code, which will create a string called answer of the appropriate length:
srand(time(0));
string answer = "";
for (int i = 0; i < size; i++) {
char ch = lowest + rand() % range;
answer += ch;
}
For debugging, you may find it helpful to comment out this code, and instead use something like:
string answer = "aaaa";
This will let you test with a known string, so the answers you get back from the program will be predictable. Later, you can enable the random code, but even then, you may find it helpful to print the answer to the screen at the beginning so you can tell if the answers your program gives you are correct. If you choose to do that, don't forget to set it back to normal before you submit your homeworkâyour finished solution must not print the answer to the screen and it must choose a new random answer each time the game is restarted.
Here is some pseudocode to help specify how your program should run. For this assignment, you may use other approaches without losing points, but following the pseudocode will be the best choice for most students:
* declare the variables and create a random answer using the code give above
* print out instructions to the user. the instructions should use the variables provided to tell the user how many guesses he/she can use, how many letters to enter, and the range of letters that can appear in the answer
* while the user has made less than maxtries guesses
o ask the user for a guess of size letters
o input the guess from the user and store it in a string variable. Don't forget to #include <string> at the top of your program
o if the guess has the wrong number of letters, print an error message and skip back to the top of the loop (use continue)
o start with position equal to zero, and correct equal to zero
o while position is less than size
+ if the guessed character at position is outside the range of allowed letters, print a warning
+ if the guessed letter at position matches the answer letter at position, increment correct
+ increment position
o if all of the letters were correct, tell the user how many guesses were required and quit
o print out the number of letters that were correct, as stored in correct
o increment the number of guesses
* print a message that the user lost by running out of turns
Examples
If the answer is "bdbc":
Welcome to Mastermind
You must guess a random sequence of 4 letters
from 'a' to 'f' within 8 tries. After each
guess, I'll tell you how many letters were right. To be
counted, you must guess the right letter in the right position.
Please enter your guess of 4 letters: aaaa
You got 0 letters right
Please enter your guess of 4 letters: bbbb
You got 2 letters right
Please enter your guess of 4 letters: cccc
You got 1 letter right
Please enter your guess of 4 letters: dddd
You got 1 letter right
Please enter your guess of 4 letters: bbcd
You got 1 letter right
Please enter your guess of 4 letters: cddd
You got 1 letter right
Please enter your guess of 4 letters: bdcc
You got 3 letters right
Please enter your guess of 4 letters: bdbc
You won in 8 tries!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>
using namespace std;
int main()
{
int size = 4;
int maxtries = 8;
int range = 6;
char lowest = 'a';
char highest = lowest + range -1;
string guess;
int number;
int correct;
int position;
/*srand(time(0));
string answer = "";
for (int i = 0; i < size; i++)
{
char ch = lowest + rand() % range;
answer += ch;
}*/
string answer = "abaa"; //For debugging
number = 1;
correct = 0;
position = 0;
while (number <= 8)
{
cout << "Guess #" << number << ": Enter 4 letters between the range of 'a' through 'f': ";
//cout << answer;
cout << "\n";
cin >> guess;
//if (number > 8)
//cout << "You went over the maximum allowed number of guesses\n";
if (guess == answer)
{
cout << "Right! It took you " << number << " move";
if (number != 1) cout << "s";
cout << "." << endl;
break;
cout << "You guessed right, this is my answer.\n";
cout << answer;
cout << "GAME OVER\n\n";
}
while (position < size)
{
if (size == position) correct++;
position++;
}
number++;
/*
cout << "Correct position: " << position << endl;
cout << "Correct letter: " << correct << endl;
*/
}
return 0;
}
|