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
  • 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...

  • 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...

  • 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...

  • 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.

  • Hello, Could you please explain how I would complete this program with input validation to ensure...

    Hello, Could you please explain how I would complete this program with input validation to ensure that an error message will not appear if the user enters something other than an integer that is not 1-100 and later if they enter anything other than yes and no? Here is the program: Write a program that plays the Hi-Lo guessing game with numbers. The program should pick a random number between 1 and 100 (inclusive), then repeatedly promt the user to...

  • Write a program to play "guess my number". The program should generate a random number between...

    Write a program to play "guess my number". The program should generate a random number between 0 and 100 and then prompt the user to guess the number. If the user guesses incorrectly the program gives the user a hint using the words 'higher' or 'lower' (as shown in the sample output). It prints a message 'Yes - it is' if the guessed number is same as the random number generated. ANSWER IN C LANGUAGE. ====================== Sample Input / Output:...

  • This program will implement a simple guessing game... Write a program that will generate a random...

    This program will implement a simple guessing game... Write a program that will generate a random number between 1 and 50 and then have the user guess the number. The program should tell the user whether they have guessed too high or too low and allow them to continue to guess until they get the number or enter a 0 to quit. When they guess the number it should tell them how many guesses it took. At the end, the...

  • Please write a C# program that where a player will play a guessing game with the...

    Please write a C# program that where a player will play a guessing game with the range of 1-100. Each time the play guesses a number, and it is not correct the program should indicate if the number is more or less than what the player guessed. The play should be able to guess until the correct number has been guessed. If an invalid number is entered, such as: 1.24 (real number), or asd (string). The program should tell user...

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
ADVERTISEMENT