Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. //guessing.c
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6.  
  7.  
  8. int main(void)
  9. {
  10. printf("Hello, welcome to the guessing game\n");
  11.  
  12.  
  13.  
  14. char mode;
  15. srand(time(NULL));
  16. int NumToGuess; //random number
  17. int TotalGuesses;
  18. int found;
  19. int guess;
  20. int CharChecker = 0;
  21. int top;
  22. int bottom;
  23. char hly; //testing for high/low/correct
  24. int Quit = 0; //testing for trying a game again
  25. char QuitChecker;
  26.  
  27. //((top# - bottom) / 2) + bottom = guess
  28.  
  29. printf("Do you want to guess, or should I? (Type C for computer, U for you)\n");
  30.  
  31. while (Quit == 0){ //Unlimited loop. Progam will keep looping until closed
  32.  
  33.  
  34. while (CharChecker == 0){ //Checking for whether or not the character entered is valid. If not, it asks you again
  35.  
  36.  
  37. scanf("%c",&mode);
  38.  
  39.  
  40. if (mode == 'u' || mode == 'U'){ //testing for User
  41. CharChecker=1;
  42. printf("\nOk, I'm thinking of a number\n"); //testing input
  43.  
  44. NumToGuess = (rand() % 1000)+1;
  45. TotalGuesses = 0;
  46. found = 0;
  47.  
  48.  
  49. while(found==0){
  50.  
  51. //printf("\nNumber was %d\n",NumToGuess); //troubleshooting
  52.  
  53. printf("\nWhat is your guess?");
  54. scanf("%d",&guess);
  55. //printf("\nYour guess is %d\n",guess);//checking guess
  56. TotalGuesses++;
  57. if (guess == NumToGuess){
  58. found=1;
  59. printf("You win! You got it in %d guess(es). ", TotalGuesses);
  60.  
  61. if (TotalGuesses >10){
  62. printf("You could do better.");
  63. }else {
  64. printf("You know the secret to a binary search!");}
  65. }
  66. else if (guess >=1000||guess <=0){
  67. printf("Out of Range");
  68. }
  69. else if (guess < NumToGuess){
  70. printf("Too low!");
  71. }
  72. else if (guess > NumToGuess){
  73. printf("\nToo high!");}
  74. }
  75. }
  76. else if (mode == 'c'|| mode == 'C'){ //testing for Computer
  77. CharChecker=1;
  78. TotalGuesses=0;
  79. printf("Okay, is your number 500? (H for too high,L for too low,Y for yes) ->");
  80. found=0;
  81. guess=500;
  82. top=1000;
  83. bottom=0;
  84. while(found==0 && TotalGuesses<=10){
  85.  
  86. scanf("%c",&hly);
  87. if (hly == 'h'|| hly == 'H'){
  88. top=guess;
  89. guess = (((top-bottom)/2)+bottom);
  90. TotalGuesses++;
  91. printf("Okay, is your number %d? (H,L,Y) ->", guess);
  92. }else if (hly == 'l'|| hly == 'L'){
  93. bottom=guess;
  94. guess = (((top-bottom)/2)+bottom);
  95. TotalGuesses++;
  96. printf("Okay, is your number %d? (H,L,Y) ->", guess);
  97. }else if (hly == 'y'|| hly =='Y'){
  98. printf("dang, I'm good\n");
  99. found=1;
  100. }else if (hly == ' '|| hly == '\n'){
  101. }
  102. else {
  103. printf("\nInvalid input\n");
  104. printf("Type H for too high, L for too low or Y for yes\n");
  105.  
  106. }
  107. if (TotalGuesses >10){
  108. printf("There is no other possibility!");
  109. }
  110. }
  111.  
  112. }
  113. else if (mode == ' '|| mode == '\n'){
  114. }
  115. else {
  116. printf("\nInvalid input\n");
  117. printf("Type C for computer, U for you\n");
  118.  
  119. }}
  120.  
  121.  
  122.  
  123. if (found == 1){ //setting up for whether or not you want to play again
  124. //Using Repeat because it would repeat "Do you want to play again" line
  125. printf ("\nDo you want to play again? (Y/N) \n");
  126. scanf("%c",&QuitChecker);
  127. scanf("%c",&QuitChecker); //had a bug apparently where it would repeat the "Do you want to
  128. //play again" unless I put this scan line twice
  129.  
  130. if ((QuitChecker == 'y'|| QuitChecker == 'Y')&& QuitChecker != '\n'){
  131. printf("Do you want to guess, or should I? (Type C for computer, U for you)\n");
  132. CharChecker=0;
  133. } //taking input
  134. else if (QuitChecker == 'n'|| QuitChecker =='N'){
  135. exit(0);} //makes the game close, exiting the loop
  136. else if (QuitChecker == ' '|| '\n'){}
  137. else{
  138. printf("\nInvalid input. Y or N");
  139. }
  140.  
  141. }
  142. }
  143. return 0;
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement