/*************************************************************************************************
* Program Name: Guess My Number Game *
* Author: Terry Weiss *
* Date Written: 1/12/2016 *
*************************************************************************************************
* This program runs the classic number guessing game. The computer guesses a number between *
* 1 and 100, and the player must then guess that number. The computer will tell the player if *
* the guess is high or low, or if it's correct. The player may also decide to choose a number *
* and have the computer try to guess it.
*************************************************************************************************/
#include <cstdlib>
#include <ctime>
#include <iostream>
enum mainOption { GUESSER = 1, CHOOSER_DUMB, CHOOSER_SMART, EXIT };
const int MIN_GUESS = 1;
const int MAX_GUESS = 100;
const std::string PLAY_AGAIN_MSG = "\nThat was fun! Let's play again!\n";
int mainMenu();
void playGuesser();
void playChooser(bool smart);
int main() {
std::srand(static_cast<unsigned int>(time(0)));
int option;
do {
option = mainMenu();
switch (option) {
case GUESSER:
playGuesser();
break;
case CHOOSER_DUMB:
playChooser(false);
break;
case CHOOSER_SMART:
playChooser(true);
break;
case EXIT:
std::cout << "We should do this again some time!\n\n";
break;
default:
std::cout << "**ERROR**: Invalid menu option {" << option << "}\n\n";
}
} while (option != EXIT);
return EXIT_SUCCESS;
}
int mainMenu() {
int option;
std::cout << " 1. Play as guesser\n"
<< " 2. Play as chooser\n"
<< " 3. Play as chooser (against smartest guesses)\n"
<< " 4. Exit\n";
do {
std::cout << "Please choose a game mode to begin: ";
std::cin >> option;
if (option < 1 || option > EXIT) {
std::cout << "Invalid option. ";
}
} while (option < 1 || option > EXIT);
return option;
}
void playGuesser() {
int number = (std::rand() % (MAX_GUESS - MIN_GUESS + 1)) + MIN_GUESS;
int guess, guesses = 0;
std::cout << "\nOkay, I'm ready!" << std::endl;
do { //Repeat until number is guessed correctly
std::cout << "Please pick an integer between " << MIN_GUESS << " and " << MAX_GUESS << ": ";
std::cin >> guess;
if (guess < MIN_GUESS || guess > MAX_GUESS) {
std::cout << "Sorry, invalid guess. ";
}
else {
guesses++;
if (guess < number) {
std::cout << "No, that's too low." << std::endl;
}
else if (guess > number) {
std::cout << "Sorry, that's too high." << std::endl;
}
else {
std::cout << "Congrats, you got it!! It only took " << guesses << " guess"
<< ((guesses == 1) ? "" : "es") << "." << std::endl; //pluralize "guesses"
}
}
} while (guess != number);
std::cout << PLAY_AGAIN_MSG;
}
void playChooser(bool smart) {
enum resultOption { HIGH = 1, LOW, CORRECT };
int guess, guesses = 0;
int min, max;
int result;
bool cheater = false;
std::cout << "\nAlrighty, pick a number between " << MIN_GUESS << " and " << MAX_GUESS
<< " but don't tell me!" << std::endl
<< "Hit any key when you're ready..." << std::endl;
std::cin.get();
min = MIN_GUESS;
max = MAX_GUESS;
do { //until the guess is right or it's determined the player is cheating
if (smart) {
guess = (max + min) / 2;
}
else {
guess = std::rand() % (max - min + 1) + min;
}
guesses++;
std::cout << " 1. Too high" << std::endl
<< " 2. Too low" << std::endl
<< " 3. Correct!" << std::endl;
do {
std::cout << "Is it " << guess << "? ";
std::cin >> result;
if (result < 1 || result > CORRECT) {
std::cout << "Invalid option. ";
}
} while (result < 1 || result > CORRECT);
if (result == CORRECT) {
std::cout << "Awesome! I got it right in " << guesses << " guesses." << std::endl;
}
else if (result == HIGH || result == LOW) {
if (max == min) { //If it's the only option left, it can't be wrong.
cheater = true;
}
else if (result == HIGH) {
if (guess == min) { //It can't be lower than the min.
cheater = true;
}
else {
max = guess - 1;
}
}
else if (result == LOW) {
if (guess == max) { //It can't be higher than the max.
cheater = true;
}
else {
min = guess + 1;
}
}
if (cheater) {
std::cout << "Hmm, it seems like someone likes to cheat ;)" << std::endl
<< "You had me going there for " << guesses << " guesses." << std::endl;
}
}
else {
std::cout << "**ERROR**: Invalid result. {" << result << "}" << std::endl;
}
} while (result != CORRECT && !cheater);
std::cout << PLAY_AGAIN_MSG;
}
Comments