Advertisement
Mr_kindle

NumberGuessinggame.c

Dec 13th, 2022
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.96 KB | Source Code | 0 0
  1. /*
  2.     Name: numberguessGame.c
  3.     Copyright:
  4.     Author:Mr.Kindle
  5.     Date: 13-12-22 12:56
  6.     Description: In this code first user guess a number between a given range and then computer guess that
  7.     number in few steps.After that computer guesses a number and user has to guess it in the less step than computer
  8.     in order to win the game.
  9.    
  10.     https://replit.com/@PrajjwalTripat2/NumberGuessGame#main.c
  11. */
  12.  
  13.  
  14.  
  15. #include<stdio.h>
  16. #define UPPERLIMIT 100
  17. #define LOWERLIMIT 0
  18. int main()
  19. {  
  20.     srand(time(0));
  21.     enum number{High,Low
  22.     };
  23.     enum guessed{Yes,No
  24.     };
  25.     int u_limit,l_limit;
  26.     int u_guess,c_guess;
  27.     int u_score,c_score;
  28.     int guesscount = 0;//to count the number of guesses
  29.     int guessed = No;//state that tells the number hasbeen guessed by computer or not
  30.     u_limit = UPPERLIMIT;
  31.     l_limit = LOWERLIMIT;
  32.     start:
  33.     printf("\nGuess a number: ");
  34.     scanf("%d",&u_guess);
  35.     if(u_guess<0 || u_guess>UPPERLIMIT)//checking constrants
  36.         {
  37.             printf("\nYOu guessed invalid number: Please guess between 1 & %d",u_limit);
  38.             goto start;
  39.         }
  40.    int i = 10;
  41.     while(guessed)
  42.         {  
  43.             /*Since each time the lower limit is incresing and upper limit is decreasing hence it may be possible
  44.             that both become equal. and if so then that number will be the number which user guessed*/
  45.             if(u_limit != l_limit)//if both limit will be equal then denominator will be zero so check it
  46.                 c_guess = rand()%(u_limit - l_limit) + l_limit;
  47.             else//found the user guessed number
  48.                 {
  49.                     printf("\nComputer guessed: %d",u_limit );//or l_limit coz both are same
  50.                     guesscount++;
  51.                     printf("\nCongrats! computer guessed correct number in %d guess: ",guesscount);
  52.                     guessed = Yes;//number hasbeen guessed so terminate the loop
  53.                     continue;//do nothing further
  54.                 }
  55.             guesscount++;
  56.             printf("\nComputer guessed: %d",c_guess);
  57.             switch(c_guess < u_guess?Low:High)
  58.                 {
  59.                     case Low:
  60.                                 l_limit = c_guess + 1;
  61.                                 break;
  62.                     case High: /*Two cases possible either computer guess more or equal*/
  63.                                 if(c_guess > u_guess)//if guess more
  64.                                     u_limit = c_guess - 1;
  65.                                 else//if guess equal
  66.                                     {
  67.                                         printf("\nWow! computer guessed correct number in %d guess: ",guesscount);
  68.                                         guessed = Yes;//number hasbeen guessed so terminate the loop
  69.                                     }
  70.                                 break;
  71.                    
  72.                 }
  73.            
  74.         }//while
  75.         c_score = guesscount;
  76.        
  77.     printf("\n\nNow its your turn to guess: ");
  78.     /*Initializing the limits*/
  79.     u_limit = UPPERLIMIT;
  80.     l_limit = LOWERLIMIT;
  81.     /*initializing the counter*/
  82.     guesscount = 0;
  83.     /*initializing the state of guessing*/
  84.     guessed = No;
  85.     /*Now computer will guess a number*/
  86.     c_guess = rand()%(u_limit - l_limit) + l_limit;
  87.     printf("\n\nComputre has guessed a number between 1 & %d",u_limit);
  88.    
  89.     printf("\nNow its your time to guess a number between 1 & %d inclusive: ",u_limit);
  90.     while(guessed)
  91.         {  
  92.             printf("\nGuess a number : ");
  93.             scanf("%d",&u_guess);
  94.             guesscount++;
  95.            
  96.             switch(u_guess < c_guess?Low:High)
  97.                 {
  98.                     case Low:
  99.                                 printf("\nGuessed number is too low, Please guess larger: ");
  100.                                 break;
  101.                     case High: /*Two cases possible either computer guess more or equal*/
  102.                                 if(u_guess > c_guess)//if guess more
  103.                                     printf("\nGuessed number is too high, Please guess smaller: ");
  104.                                 else//if guess equal
  105.                                     {
  106.                                         printf("\nCongrats! you guessed correct number in %d guess: ",guesscount);
  107.                                         guessed = Yes;//number hasbeen guessed so terminate the loop
  108.                                     }
  109.                                 break;
  110.                    
  111.                 }
  112.            
  113.         }//while
  114.         u_score = guesscount;
  115.        
  116.         printf("\n\nComputer Score: %d",c_score);
  117.         printf("\nYour Score: %d",u_score);
  118.        
  119.         if(c_score < u_score)
  120.             printf("\nSorry! You loose. Better luck next time: ");
  121.         else if(c_score == u_score)
  122.             printf("\nOhh! Its a draw: ");
  123.         else
  124.             printf("\nCongrats! You won. Its a party time: ");
  125.    
  126.  
  127. return 0;
  128. }//main
  129.  
  130.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement