Thursday, December 30, 2010

New Year Spl: "GUESS it RIGHT" - The Game

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>


/* This program plays simple guessing game . The computer picks a
   random number from 0 to 100 , and the user tries to guess the number  */

   const int totchan = 7;
   void main()
   {
     int number ;                   // computers rabdom number
     int guess;                     // the users guess number
     int chances=0,score=0,chanscor;// chanscor stores score for 1 successful chance
     char ans;
     do
     {
       clrscr();
       chances=score=0;
       printf("\n\t\t\t\t Welcome to the HIGH/LOW GAME ");
       printf("\n\t\t\t\t I will pick a random number from 0 to 100 ");
       printf("\n\t\t\t\t You must try to guess the number ");
       randomize();
       number =(int)(rand()%100);
       chanscor=100/totchan;

       do
       {
      printf("\n\t What is your GUESS ?? ");
      scanf("%d",&guess);

      if((guess<0) || (guess>100))
      {
         printf(" Sorry but your guess  %d  must be from 0 to 100 ");
      }
      else if (guess < number )
      {
         printf(" %d is LOW . Try a higher number  ",guess);
      }
      else if(guess>number)
      {
         printf("%d is HIGH . Try a lower number ",guess);
      }
      else
      {
         printf("%d is correct . Congratulations !!!!  ");
         score = chanscor*(totchan-chances);
         printf("\n\t Your score is %d \n",score);
         break;
      }
      chances++;
      if(guess!=number)
        printf("\n\n Now you have %d chances left \n",totchan-chances);
      if(chances == totchan)
      {
         printf("\n\n Only %d chances are allowed . Better luck next time",totchan);
         printf("\n The actual number was %d \n",number);
         break;
      }
    }while(guess!=number);

    printf("\n\n Thank You for playing this game !!!!!!!!! ");
    printf("\n\n Want to play it again ?(y/n)..");
    fflush(stdin);
    scanf("%c",&ans);
      }while(ans=='y' || ans=='Y');
  }


No comments:

Post a Comment