Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <time.h>
- #define TRUE 1
- #define FALSE 0
- // Function: print_greeting
- // This function prints out the greeting for the game
- void print_greeting(int min, // Received: minimum possible guess value
- int max, // Received: maximum possible guess value
- int guesses);// Received: maximum number of computer guesses
- // Function: user_wishes_to_continue
- // Returns: true(1) if user responds with y
- // Returns: false(0) if user responds with n
- int user_wishes_to_continue();
- // Function: prompt_user
- // Tells the computer if it's guess is too H, L, or correct
- // char response Returned: the user's response to the guess
- char prompt_user(int guess); // Received: computer's guess
- // Function: print_results
- // Prints the number of wins by the user and an ending message
- void print_results(int comp_wins, // Received: number of computer wins
- int user_wins);// Received: number of user wins
- // Function: play_game
- // play one game of the reverse guessing game
- // int(boolean) has_won Returned: result of game
- void play_game(int min, int max, int guesses);
- int main()
- {
- srand(time(0)); // seeds the random number generator with
- // time - # seconds since some fixed point
- // 1. Print the program banner
- print_greeting(1, 1000, 5);
- // 2. Initialize the counter variables to 0
- // 3. In a loop, play a game using the play_game function,
- // and increment the win count of the winner of the game
- // until the user does not wish to continue.
- play_game(1, 1000, 5);
- // 4. Print the results and ending message using the print_results function
- // 5. Return 0 to exit the program
- return 0;
- }
- void print_greeting(int min, int max, int guesses)
- {
- printf("Welcome to the (reverse) guessing game.\n");
- printf("Choose a number between %d and %d inclusive\n", min, max);
- printf("and I will try to guess it in %d or fewer tries\n", guesses);
- }
- int user_wishes_to_continue()
- {
- char response;
- do
- {
- printf("Do you wish to continue? (y/n) ");
- scanf(" %c", &response);
- response = tolower(response);
- if (response != 'y' && response != 'n')
- printf("Bad input. Try again.\n");
- }
- while (response != 'y' && response != 'n');
- return response == 'y';
- }
- char prompt_user(int guess)
- { guess = 500;
- char response;
- printf("\nMy guess is %d.\n", guess);
- do
- {
- printf("Is my guess too (H)igh, too (L)ow, or (E)qual to your number? ");
- scanf(" %c", &response);
- response = toupper(response);
- if (response != 'H' && response != 'L' && response != 'E')
- printf("Bad input. Try again.\n");
- } while (response != 'E');
- return response == 'E';
- }
- void print_results(int comp_wins, int user_wins)
- {
- printf("I won %d game(s).\n", comp_wins);
- printf("You won %d game(s).\n", user_wins);
- printf("Thank you for playing!");
- }
- void play_game(int min, int max, int guesses)
- {
- int low, // when guess is too low
- high, // when guess is too high
- num_guesses, // number of guesses taken by the computer
- guess,
- has_won;
- num_guesses = 0;
- char response;
- min = low;
- max = high;
- has_won = FALSE;
- response = prompt_user(guess);
- while(response != 'E' && num_guesses !=5)
- {
- prompt_user(guess);
- if (response == 'H')
- {
- high = guess - 1 ;
- guess = (high + low)/2;
- prompt_user(guess);
- printf("My guess is %d.\n", guess);
- }
- else if (response == 'L')
- {
- low = guess + 1;
- guess = (high + low)/2;
- prompt_user(guess);
- printf("My guess is %d.\n", guess);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement