Question
/* Author: Danny Lee (dannylee@my.unt.edu) Date: 02/26/2023 Instructor: Dr. Shrestha Description: A program of a number guessing game where the player/user specifies the range of
/* Author: Danny Lee (dannylee@my.unt.edu)
Date: 02/26/2023
Instructor: Dr. Shrestha
Description: A program of a number guessing game where the player/user
specifies the range of numbers to be guessed and gives/deducts
the appropriated amount of points based on the user's guess.
*/
#include
#include
#include
#include
using namespace std;
enum MenuChoice
{
displayLeft = 1,
displayRight,
Guess,
Change,
Exit
};
int main()
{
// initialize random number generator with the current time
srand(time(0));
// initialize points for player
int points = 100;
// prompt to enter player's name
string playerName;
bool validName = false;
while (!validName)
{
cout << "Enter player name: ";
getline(cin, playerName);
validName = true;
for (int i = 0; i < playerName.length(); i++)
// Verify proper formatting of player name
{
if (!isalpha(playerName[i]) && !isspace(playerName[i]))
{
cout << "Invalid input. Please enter alphabets and spaces only." << endl;
validName = false;
break;
}
}
}
playerName[0] = toupper(playerName[0]);
for (int i = 1; i < playerName.length(); i++)
{
playerName[i] = tolower(playerName[i]);
}
// Create two random integers, 100 and 200, inclusive
int lowerBound = -1, upperBound = -1;
while (lowerBound >= upperBound)
{
lowerBound = rand() % 101 + 100;
upperBound = rand() % 101 + 100;
}
// Display bounds
cout << "The bounds are: " << lowerBound << " - " << upperBound << endl;
// Initialize game loop
bool exitGame = false;
while (!exitGame && points > 0)
{
// Display player's points balance
cout << endl
<< playerName << "'s points balance: " << points << endl;
// Display menu
cout << "Select an option from the menu:" << endl;
cout << "1. Display the left bound" << endl;
cout << "2. Display the right bound" << endl;
cout << "3. Guess a number between the bounds" << endl;
cout << "4. Change the random bounds" << endl;
cout << "5. Exit game" << endl;
// Display player's choice
int choice;
cin >> choice;
// Use switch-case block to proceed with player's choice
// Program enters a loop that allows user to make guesses. Prompts player to enter a guess.
// If the guess is correct program exits loop and gives point to player.
// Incorrect guess gives feedback in respect to range to player and negates points from player.
switch (choice)
{
case displayLeft:
if (lowerBound == -1)
{
cout << "The left bound is: " << lowerBound << endl;
cout << "Earn 1 point for correct guess, and lose 10 points for incorrect guess" << endl;
int guess;
cin >> guess;
if (guess == upperBound || guess <= lowerBound)
{
cout << "You guessed incorrectly! You lose 10 points." << endl;
points -= 10;
}
else
{
cout << "You guessed correctly! You win 1 point." << endl;
points += 1;
}
}
else
{
cout << "The left bound is already displayed." << endl;
}
break;
case displayRight:
if (upperBound == -1)
{
cout << "The right bound is: " << upperBound << endl;
cout << "Earn 1 point for correct guess, and lose 10 points for incorrect guess." << endl;
int guess;
cin >> guess;
if (guess == lowerBound || guess >= upperBound)
{
cout << "Invalid input. You must enter a number between " << lowerBound << " and " << upperBound - 1 << endl;
break;
}
if (guess == number)
{
cout << "You Won! Your guess was correct." << endl;
score += 1;
break;
}
else
{
cout << "You lose! The correct answer is " << number << endl;
score -= 10;
break;
}
}
else
{
cout << "Right bound is not set. Please set the bounds." << endl;
break;
}
case quit:
cout << "Congratulation! Your final score is " << score << endl;
return 0;
}
}
return 0;
}
Design an algorithm for this program of number guessing game:
DESIGN (ALGORITHM): On a piece of paper (or word processor), write down the algorithm, or sequence of steps, that you will use to solve the problem. You may think of this as a recipe for someone else to follow. Continue to refine your recipe until it is clear and deterministically solves the problem. Be sure to include the steps for prompting for input, performing calculations, and displaying output. You should attempt to solve the problem by hand first (using a calculator as needed) to work out what the answer should be for a few sets of inputs. Type these steps and calculations into a document (i.e., Word, text, or PDF) that will be submitted along with your source code. Note that if you do any work by hand, images (such as pictures) may be used, but they must be clear and easily readable. This document shall contain both the algorithm and any supporting hand-calculations you used in verifying your results.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started