Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Name: numberguessGame.c
- Copyright:
- Author:Mr.Kindle
- Date: 13-12-22 12:56
- Description: In this code first user guess a number between a given range and then computer guess that
- number in few steps.After that computer guesses a number and user has to guess it in the less step than computer
- in order to win the game.
- https://replit.com/@PrajjwalTripat2/NumberGuessGame#main.c
- */
- #include<stdio.h>
- #define UPPERLIMIT 100
- #define LOWERLIMIT 0
- int main()
- {
- srand(time(0));
- enum number{High,Low
- };
- enum guessed{Yes,No
- };
- int u_limit,l_limit;
- int u_guess,c_guess;
- int u_score,c_score;
- int guesscount = 0;//to count the number of guesses
- int guessed = No;//state that tells the number hasbeen guessed by computer or not
- u_limit = UPPERLIMIT;
- l_limit = LOWERLIMIT;
- start:
- printf("\nGuess a number: ");
- scanf("%d",&u_guess);
- if(u_guess<0 || u_guess>UPPERLIMIT)//checking constrants
- {
- printf("\nYOu guessed invalid number: Please guess between 1 & %d",u_limit);
- goto start;
- }
- int i = 10;
- while(guessed)
- {
- /*Since each time the lower limit is incresing and upper limit is decreasing hence it may be possible
- that both become equal. and if so then that number will be the number which user guessed*/
- if(u_limit != l_limit)//if both limit will be equal then denominator will be zero so check it
- c_guess = rand()%(u_limit - l_limit) + l_limit;
- else//found the user guessed number
- {
- printf("\nComputer guessed: %d",u_limit );//or l_limit coz both are same
- guesscount++;
- printf("\nCongrats! computer guessed correct number in %d guess: ",guesscount);
- guessed = Yes;//number hasbeen guessed so terminate the loop
- continue;//do nothing further
- }
- guesscount++;
- printf("\nComputer guessed: %d",c_guess);
- switch(c_guess < u_guess?Low:High)
- {
- case Low:
- l_limit = c_guess + 1;
- break;
- case High: /*Two cases possible either computer guess more or equal*/
- if(c_guess > u_guess)//if guess more
- u_limit = c_guess - 1;
- else//if guess equal
- {
- printf("\nWow! computer guessed correct number in %d guess: ",guesscount);
- guessed = Yes;//number hasbeen guessed so terminate the loop
- }
- break;
- }
- }//while
- c_score = guesscount;
- printf("\n\nNow its your turn to guess: ");
- /*Initializing the limits*/
- u_limit = UPPERLIMIT;
- l_limit = LOWERLIMIT;
- /*initializing the counter*/
- guesscount = 0;
- /*initializing the state of guessing*/
- guessed = No;
- /*Now computer will guess a number*/
- c_guess = rand()%(u_limit - l_limit) + l_limit;
- printf("\n\nComputre has guessed a number between 1 & %d",u_limit);
- printf("\nNow its your time to guess a number between 1 & %d inclusive: ",u_limit);
- while(guessed)
- {
- printf("\nGuess a number : ");
- scanf("%d",&u_guess);
- guesscount++;
- switch(u_guess < c_guess?Low:High)
- {
- case Low:
- printf("\nGuessed number is too low, Please guess larger: ");
- break;
- case High: /*Two cases possible either computer guess more or equal*/
- if(u_guess > c_guess)//if guess more
- printf("\nGuessed number is too high, Please guess smaller: ");
- else//if guess equal
- {
- printf("\nCongrats! you guessed correct number in %d guess: ",guesscount);
- guessed = Yes;//number hasbeen guessed so terminate the loop
- }
- break;
- }
- }//while
- u_score = guesscount;
- printf("\n\nComputer Score: %d",c_score);
- printf("\nYour Score: %d",u_score);
- if(c_score < u_score)
- printf("\nSorry! You loose. Better luck next time: ");
- else if(c_score == u_score)
- printf("\nOhh! Its a draw: ");
- else
- printf("\nCongrats! You won. Its a party time: ");
- return 0;
- }//main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement