Question

Specification Write a program that allows the user to play number guessing games. Playing a Guessing...

Specification Write a program that allows the user to play number guessing games. Playing a Guessing Game Use rand() function from the Standard C Library to generate a random number between 1 and 100 (inclusive). Prompt the user to enter a guess. Loop until the user guesses the random number or enters a sentinel value (-1) to give up. Print an error message if the user enters a number that is not between 1 and 100. Print an error message if the user enters a wrong guess more than once. Duplicate wrong guesses count as only one wrong guess. After five wrong guesses, the user is given help (higher or lower messages). After a game has been completed prompt the user to see if they want to play again. The user is allowed to play at most four games. Post Game Playing Processing Print the following prior to exiting the program. number of games played number of games won winning percentage Required Manifest (Named) Constants Your program must define the following manifest constants prior to the main() method and they should be used through out your code. const int MIN_NUMBER = 1; const int MAX_NUMBER = 100; const int EXIT_VALUE = -1; const int MAX_GAMES = 4; const int HINT_THRESHOLD = 5; Help Generating Random Numbers At the top of the file. #include #include #include At the top of the main() function call the srand() function to seed the random number generator. int main(int, char**) { srand(time(NULL)); // seeds the random number generator ... } To get a random number that's between 1 and 100. int random_number = rand() % MAX_NUMBER + 1; // rand() returns a number between 0 and some large number // % operator used to get the number beween 0 and MAX_NUMBER

Example Game

Assume the computer generated random number is 34 for game #1, 42 for game #2, and 99 for game #3.

*** You are playing the CSC100 Guessing Game ***

Enter a number between 1 and 100 (-1 to give up): 3

nope...

Enter a number between 1 and 100 (-1 to give up): 101

101 is too big...

Enter a number between 1 and 100 (-1 to give up): 21

nope...

Enter a number between 1 and 100 (-1 to give up): 0

0 is too small...

Enter a number between 1 and 100 (-1 to give up): 33

nope...

Enter a number between 1 and 100 (-1 to give up): 50

nope...

Enter a number between 1 and 100 (-1 to give up): 21

what part of nope don't you understand?

Enter a number between 1 and 100 (-1 to give up): 41

nope...

Enter a number between 1 and 100 (-1 to give up): 27

nope...higher

Enter a number between 1 and 100 (-1 to give up): 57

nope...lower

Enter a number between 1 and 100 (-1 to give up): 34

*** GOT IT *** it took you 8 guesses

Do you want to play again? (y/n): y

Enter a number between 1 and 100 (-1 to give up): 99

nope...

Enter a number between 1 and 100 (-1 to give up): -1

*** QUITTER ***

Do you want to play again? (y/n): y

Enter a number between 1 and 100 (-1 to give up): 21

nope...

Enter a number between 1 and 100 (-1 to give up): 99

*** GOT IT *** it took you 2 guesses Do you want to play again? (y/n): n Thanks for playing the CSC100 guessing game. You played 3 games and won 2 of them. Your winning percentage was 66.7%.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>
const int MIN_NUMBER = 1;
const int MAX_NUMBER = 100;
const int EXIT_VALUE = -1;
const int MAX_GAMES = 4;
const int HINT_THRESHOLD = 5;
int main()
{
   printf("*** You are playing the CSC100 Guessing Game ***\n");
   float winCount = 0;
   float gameCount = 0;
   char playAgagin;
  
   do{
       int num, guess, tries = 0;
   srand(time(0)); /* seed random number generator */
   num = rand() % MAX_NUMBER + MIN_NUMBER; /* random number between 1 and 100 */
   do
   {
   printf("Enter a number between 1 and 100 (-1 to give up): ");
   scanf("%d", &guess);
   tries++;
   //if user want to quit
           if(guess == -1){
               printf("*** QUITTER ***\n");
               break;
           }
           //if user enter more than 100
           else if (guess>100)
   {
   printf("%d is too big...\n",guess);
   tries--;
   }
   //if user enter less than 1
   else if (guess<1)
   {
   printf("%d is too small...\n",guess);
   tries--;
   }
   //if the user guess wrong
           else if (guess != num && tries<=HINT_THRESHOLD)
   {
   printf("nope...\n");
   }
   //if the user guesses wrong and the tries count more than 5 that is hint_thresold
   else if (guess > num && tries>HINT_THRESHOLD)
   {
   printf("nope...lower\n");
   }
   else if (guess < num && tries>HINT_THRESHOLD)
   {
   printf("nope...higher\n");
   }
   //if he guess right
   else
   {
   printf("*** GOT IT *** it took you %d guesses\n", tries);
   winCount++;
   }
          
   }while (guess != num);
   gameCount++;
   if(gameCount==MAX_GAMES) break;
   //if the user wants to play again
   printf("Do you want to play again? (y/n):");
   scanf(" %c", &playAgagin);
   }while ((playAgagin == 'y'||playAgagin == 'Y'));
   printf("Thanks for playing the CSC100 guessing game. You played %d games and won %d of them. Your winning percentage was %.1f%.",(int)gameCount,(int)winCount,(winCount/gameCount)*100);
return 0;
}

Add a comment
Know the answer?
Add Answer to:
Specification Write a program that allows the user to play number guessing games. Playing a Guessing...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Required in JAVA language Write a program that enables a user to play number guessing games....

    Required in JAVA language Write a program that enables a user to play number guessing games. The following is a nutshell description of a number guessing game. + a random number is generated + loop prompting the user to enter guesses until the user guesses the number or hits the maximum number of allowed guesses or enters the "quit" sentinel value The rest of this specification documents number guessing games in more detail. The documentation uses manifest constants that can...

  • Write a program that allows a user to play a guessing game. Pick a random number...

    Write a program that allows a user to play a guessing game. Pick a random number in between 1 and 100, and then prompt the user for a guess. For their first guess, if it’s not correct, respond to the user that their guess was “hot.” For all subsequent guesses, respond that the user was “hot”if their new guess is strictly closer to the secret number than their old guess and respond with“cold”, otherwise. Continue getting guesses from the user...

  • Write a JAVA program that plays a number guessing game with the user. A sample run...

    Write a JAVA program that plays a number guessing game with the user. A sample run for the game follows. User input is shown in boldface in the sample run. Welcome to the game of Guess It! I will choose a number between 1 and 100. You will try to guess that number. If your guess wrong, I will tell you if you guessed too high or too low. You have 6 tries to get the number. OK, I am...

  • please c++ with functions *Modify the Guessing Game Write the secret number to a file. Then...

    please c++ with functions *Modify the Guessing Game Write the secret number to a file. Then write each user guess and answer to the file. (on separate lines) Write the number of guesses to the file at the end of the game. After the game is finished, ask the user if they want to play again. If 'n' or 'N' don't play again, otherwise play again! NOTE: your file should have more than one game in it if the user...

  • Lab #6 Number Guessing Game – Loops and Nested Loops Requirements: Design, develop, test, and submit a program for a Number Guessing game. The program will select a random number between 0 and 100, al...

    Lab #6 Number Guessing Game – Loops and Nested Loops Requirements: Design, develop, test, and submit a program for a Number Guessing game. The program will select a random number between 0 and 100, allow the user to try up to 10 times to guess the number, and tell the user if each guess is too high, too low, or correct. The actual game portion must be a function...you can use more than one function in your program. The amount...

  • Write a Java application program that plays a number guessing game with the user. In the...

    Write a Java application program that plays a number guessing game with the user. In the starter code that you are given to help you begin the project, you will find the following lines of code: Random generator = args.length == 0 ? new Random() :                    new Random(Integer.parseInt(args[0])); int secret = generator.nextInt(100); You must keep these lines of code in your program. If you delete these lines, or if you change thse lines in any way, then your program...

  • Number Guessing Game Games Write program in C++. For this game, the computer will select a...

    Number Guessing Game Games Write program in C++. For this game, the computer will select a random number between 1 and 100 (inclusive). The computer will then ask the (human) player to guess the number the computer has selected. After the player’s guess is input to the computer, the computer will output one of three responses, depending on the relationship of the number the player guessed to the number the computer selected: “Your guess was too low.” “Your guess was...

  • python question Question 1 Write a Python program to play two games. The program should be...

    python question Question 1 Write a Python program to play two games. The program should be menu driven and should use different functions to play each game. Call the file containing your program fun games.py. Your program should include the following functions: • function guess The Number which implements the number guessing game. This game is played against the computer and outputs the result of the game (win/lose) as well as number of guesses the user made to during the...

  • How can I do this program complete with input validation so that the computer prompts the...

    How can I do this program complete with input validation so that the computer prompts the user to enter a number 0 through 100 if he/she guesses anything lower than 0, a floating point number, or other characters? And later, prompts the user to enter yes or no if he/she enters anything else when the program prompts the user to play again? Write a program that plays the Hi-Lo guessing game with numbers. The program should pick a random number...

  • Write a C++ program to play the number guessing game with user as follows.

    Write a C++ program to play the numberguessing game with user as follows.The user determines a number between 1~100.The program has 4 tries to guess the number byproviding a range (low, high) on each try.The user tells the program whether the number isbelow, within, or above the range.The program announces a game loss after 4 incorrecttries.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT