Advertisement
ausbrumm96

Reverse guessing game

Feb 12th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <time.h>
  5.  
  6. #define TRUE 1
  7. #define FALSE 0
  8.  
  9. // Function: print_greeting
  10. // This function prints out the greeting for the game
  11. void print_greeting(int min, // Received: minimum possible guess value
  12. int max, // Received: maximum possible guess value
  13. int guesses);// Received: maximum number of computer guesses
  14.  
  15. // Function: user_wishes_to_continue
  16. // Returns: true(1) if user responds with y
  17. // Returns: false(0) if user responds with n
  18. int user_wishes_to_continue();
  19.  
  20. // Function: prompt_user
  21. // Tells the computer if it's guess is too H, L, or correct
  22. // char response Returned: the user's response to the guess
  23. char prompt_user(int guess); // Received: computer's guess
  24.  
  25. // Function: print_results
  26. // Prints the number of wins by the user and an ending message
  27. void print_results(int comp_wins, // Received: number of computer wins
  28. int user_wins);// Received: number of user wins
  29. // Function: play_game
  30. // play one game of the reverse guessing game
  31. // int(boolean) has_won Returned: result of game
  32. void play_game(int min, int max, int guesses);
  33.  
  34. int main()
  35. {
  36. srand(time(0)); // seeds the random number generator with
  37. // time - # seconds since some fixed point
  38.  
  39.  
  40. // 1. Print the program banner
  41. print_greeting(1, 1000, 5);
  42.  
  43.  
  44. // 2. Initialize the counter variables to 0
  45.  
  46. // 3. In a loop, play a game using the play_game function,
  47. // and increment the win count of the winner of the game
  48. // until the user does not wish to continue.
  49. play_game(1, 1000, 5);
  50. // 4. Print the results and ending message using the print_results function
  51. // 5. Return 0 to exit the program
  52. return 0;
  53. }
  54.  
  55. void print_greeting(int min, int max, int guesses)
  56. {
  57. printf("Welcome to the (reverse) guessing game.\n");
  58. printf("Choose a number between %d and %d inclusive\n", min, max);
  59. printf("and I will try to guess it in %d or fewer tries\n", guesses);
  60. }
  61.  
  62. int user_wishes_to_continue()
  63. {
  64. char response;
  65. do
  66. {
  67. printf("Do you wish to continue? (y/n) ");
  68. scanf(" %c", &response);
  69. response = tolower(response);
  70. if (response != 'y' && response != 'n')
  71. printf("Bad input. Try again.\n");
  72. }
  73. while (response != 'y' && response != 'n');
  74.  
  75.  
  76. return response == 'y';
  77. }
  78.  
  79. char prompt_user(int guess)
  80. { guess = 500;
  81. char response;
  82. printf("\nMy guess is %d.\n", guess);
  83. do
  84. {
  85. printf("Is my guess too (H)igh, too (L)ow, or (E)qual to your number? ");
  86. scanf(" %c", &response);
  87. response = toupper(response);
  88. if (response != 'H' && response != 'L' && response != 'E')
  89. printf("Bad input. Try again.\n");
  90.  
  91. } while (response != 'E');
  92. return response == 'E';
  93. }
  94. void print_results(int comp_wins, int user_wins)
  95. {
  96. printf("I won %d game(s).\n", comp_wins);
  97. printf("You won %d game(s).\n", user_wins);
  98. printf("Thank you for playing!");
  99. }
  100. void play_game(int min, int max, int guesses)
  101. {
  102. int low, // when guess is too low
  103. high, // when guess is too high
  104. num_guesses, // number of guesses taken by the computer
  105. guess,
  106. has_won;
  107. num_guesses = 0;
  108. char response;
  109. min = low;
  110. max = high;
  111. has_won = FALSE;
  112. response = prompt_user(guess);
  113.  
  114. while(response != 'E' && num_guesses !=5)
  115. {
  116. prompt_user(guess);
  117. if (response == 'H')
  118. {
  119. high = guess - 1 ;
  120. guess = (high + low)/2;
  121. prompt_user(guess);
  122. printf("My guess is %d.\n", guess);
  123. }
  124. else if (response == 'L')
  125. {
  126. low = guess + 1;
  127. guess = (high + low)/2;
  128. prompt_user(guess);
  129. printf("My guess is %d.\n", guess);
  130. }
  131.  
  132. }
  133.  
  134.  
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement