Skip to content

Instantly share code, notes, and snippets.

@dsmith
Created October 11, 2014 01:44
Show Gist options
  • Save dsmith/fbeac0e469da1d52ee94 to your computer and use it in GitHub Desktop.
Save dsmith/fbeac0e469da1d52ee94 to your computer and use it in GitHub Desktop.
numGuess.cpp
/***********************************************************
* Author: Brian G. Smith
* Date Created: 10/09/14
* Last Modification Date: 10/09/14
* Filename: numGuess.cpp
*
* Overview: This is a two player number guessing game. It outputs to the user to enter a secret number,
* and inputs the users secret number into a variable. It then loops and outputs to the user how many guesses they have,
* how many guesses they have left, tells them whether they are too high or low/correct, and changes the range of numbers
* to reflect the effect that the user's newest guess had on the range of valid numbers. The program then tells the second
* user whether they guessed the number correctly or ran out of guesses. If they are successful the program will tell them so,
* otherwise the program should tell them what the secret number was and how close their guess was.
* Finally, it should determine whether or not the user wants to play again.
*
* Input:
* Number of coupons earned.
*
* Output:
* Outputs the number of coupons entered. The program also outputs the
* number of candy bars and gumballs purchased followed by the remaining number of coupons.
*
*************************************************************/
#include <iostream>
using namespace std;
/**************************************************************
*
* Entry: None
*
* Exit: Returns '0' to end the function, and subsequently the
* program.
*
* Purpose: The main function is a special c++ function that is
* initiated first, regardless of where the function is located in
* the code.
*
****************************************************************/
int main()
{
int number; //Player one's secret number.
int guess; //Player two's guess.
int tries = 5;
int min = 1;
int max = 10;
// dsmith - The closest guess needs to start
// off as the max so you can have a good base
// to start with.
int closestGuess = max;
char again;
do
{
cout << "Player one, please enter a number between one and ten: " << endl;
cin >> number;
cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
do
{
cout << "Player two, please guess a number between " << min << " and " << max << " ." << " You have " << tries << " tries." << endl;
cin >> guess;
if(guess == number)
{
cout << "That is correct!" << endl;
// dsmith - Need to exit the while loop because the
// player has already guessed the correct number.
break;
}
else if(guess < number)
{
tries--;
// dsmith - If player two runs out of tries,
// you need to present a new message and exit
// the while loop
if(tries == 0) {
cout << "Player two, womp womp, the number was " << number << endl;
cout << "Player two, your closest guess was " << closestGuess << endl;
break;
} else {
// dsmith - We need to capture the closest guess.
// Note how we are using the absolute value to determine
// if we get a better guess this time round.
if(abs(guess - number) < closestGuess) {
closestGuess = guess;
}
cout << "The number is higher!" << endl;
}
}
else if(guess > number)
{
tries--;
if(tries == 0) {
cout << "Player two, womp womp, the number was " << number << endl;
cout << "Player two, your closest guess was " << closestGuess << endl;
break;
} else {
if(abs(guess - number) < closestGuess) {
closestGuess = guess;
}
cout << "The number is lower!" << endl;
}
}
} while(tries > 0 || guess == number);
cout << "Would you like to play again? y/n" << endl;
cin >> again;
} while(again == 'y');
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment