I am trying to design a "game" where the user picks a number between 100 and 999 and the code will tell them how close their guess was to the actual random number it generated. This is basically mastermind but with numbers and only 3. I think I have everything I need in terms of the class and the functions but I can't seem to figure out how to actually simulate the game and have it run. I wrote a function that I believe should simulate the game and tried calling it in my main but it keeps telling me it is not defined in this scope. Any and all help is greatly appreciated. Here is my code:
string randGenNum;
string userGuess;
bool done = false;
int green = 0;
int yellow = 0;
do {
for (int i = 0; i < 3; i++) {
if (randGenNum[i] == userGuess[i]) {
green++;
randGenNum[i] = -7; // We are changing the number to a negative number to prevent duplicating. Because the user can't pick a negative number.
userGuess[i] = -3; // same here with the duplicating
}
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (randGenNum[i] == userGuess[j]) {
yellow++;
randGenNum[i] = -5; // same here with the duplicating
userGuess[j] = -4; // same here with the duplicating
}
}
}
done = true;
} while (!done);
cout << "Green: " << green << endl;
cout << "Yellow: " << yellow << endl;
cout << "Red: " << 3 - green - yellow << endl;
}
void MasterCode::letsPlay() {
bool winner = false; //checks whether the current guess is correct
int answer[3]; //stores the correct number
int guess[3]; //stores the user's guess
generateArray(generateANumber(), answer);
while (winner == false) {
enterAGuess(guess);
if (winner == true) {
cout << "Congratulations. You guessed the correct number" << endl;
}
}
}
void MasterCode::enterAGuess(int* array) {
char response;
int counter = 0;
cout << "Enter your guess: ";
while (counter < 3) {
cin.get(response);
}
}
void MasterCode::generateArray(int number, int* array) {
array[0] = number / 100;
number -= array[0] * 100;
array[1] = number / 10;
number -= array[1] * 10;
array[2] = number;
}