I'm working on a simple shell game program in C++. One of the requirements is that the user gets 4 guesses. If the user is incorrect 4 times, they're supposed to be told they're incorrect, the location of the ball, and return to the menu. However, when I'm running the code, it lets me guess for forever. I have the increment for the incorrectGuess variable, but I'm assuming there is a scope issue or something.
Help would be appreciated! Also, ignore my comments currently. That's just how I'm tracking the braces.
#include <iostream>
#include <array>
using namespace std;
void displayMenu();
void displayGameMenu ();
int main() { //mb
int userChoice;
int n;
do { //dob
displayMenu();
cin >> userChoice;
bool gameOver = false;
if (userChoice == 1) { //0b
displayGameMenu();
int incorrectGuess;
cin >> n;
if (n < 3) { //1b
cout << "The minimum number of cups is 3. Please enter the amount of cups you'd like to play with: " << endl;
cin >> n;
} //1e
else { //2b
bool cups[n];
cout << "The game is ready, and the ball is under one of the " << n << " cups. Guess which cup the ball is in: \n";
for (int i = 0; i < n; i++) { //3b
cout << "Cup " << i + 1 << "\n";
} //3e
cout << "Enter -1 to leave the game. ";
int ballLocation = rand()%(n-n+1) + 3;
cups[ballLocation] = true;
int userGuess;
cin >> userGuess;
do { //4b
if (userGuess == ballLocation) { //5b
cout << "Congrats! You win! What would you like to do now?\n";
gameOver = true;
}//5e
else if (userGuess != ballLocation && userGuess != -1) { //6b
incorrectGuess++;
cout << "Nope! Not it! Try again: \n";
for (int i = 0; i < n; i++) { //7b
cout << "Cup " << i + 1 << "\n";
} //7e
cout << "Enter -1 to leave the game. ";
cin >> userGuess;
ballLocation = rand()%(n-n+1) + 3;
continue;
} //6e
else if ((userGuess < n || userGuess > n) && userGuess != -1) { //8b
cout << "Invalid guess! Try again: ";
for (int i = 0; i < n; i++) { //9b
cout << "Cup " << i + 1 << "\n";
} //9e
cout << "Enter -1 to leave the game. ";
cin >> userGuess;
} //9e
else if (incorrectGuess == 4) { //10b
cout << "You're out of guesses! Nice try! The location of the ball was cup " << ballLocation << "!" << endl;
break;
}//10e
else if ( userGuess == -1) {//11b
cout << "Are you sure you wish to end the game? y/n \n";
string userConfirm;
cin >> userConfirm;
if (userConfirm == "y") {//12b
gameOver = true;
}//12e
else if (userConfirm == "n") {//13b
continue;
}//13e
else { //14b
cout << "Invalid response.. Enter y/n \n";
cin >> userConfirm;
continue;
}//14e
}//11e
}//4e
while (gameOver != true);
}//2b
}//0b
} //doe
while (userChoice != 2);
}
void displayMenu() {
cout << "Welcome to the Shell Game! Please choose one of the menu options below.\n";
cout << "1. Play the Game!\n";
cout << "2. Quit.\n";
cout << "Enter choice here: " << endl;
}
void displayGameMenu() {
cout << "Thank you for deciding to play the Shell Game. How many cups (3 is the minimum) would you like to play with? " << endl;
}
Aucun commentaire:
Enregistrer un commentaire