/* Guessing Game SE1SA5 - Computer Tutorial Week beginning 23/10/06 */ #define _CRT_SECURE_NO_DEPRECATE 1 #include #include /*library with the function rand */ #include /* library with time in */ #define MAXNUM 100 /* largest number to be guessed /* change to 100 to something small /* while developing */ int main() { int secret,guess,count,type,range,rangeCount; count = 0; srand ( (int) time( NULL )); /* seed the random number generator */ /* comment above out while developing*/ /* the number guessed will then always be the same*/ secret = rand ( ) % MAXNUM +1; /* a random number between 1 and MAXNUM */; printf( "Guessing Game\n" ); printf( "You have to keep entering numbers until you guess the secret number\n" ); printf( "Hint the number is between 1 and %d\n", MAXNUM ); do { printf ( "Enter your guess: " ); scanf( "%d",&guess ); count++; if (guess > secret) { printf("Lower \n"); } else if (guess < secret) { printf("Higher \n"); } } while ((guess != secret)&&(count < 7)); /* loop back */ if (guess == secret) { printf( "That's right\n"); } else { if ((secret%2) == 0) { printf("Hint: The number is an even number!\n"); } else { printf("Hint: The number is an odd number!\n"); } do { printf ( "Enter your guess: " ); scanf( "%d",&guess ); count++; if (guess > secret) { printf("Lower \n"); } else if (guess < secret) { printf("Higher \n"); } } while ((guess != secret) &&(count < 11)); /* loop back */ if (guess == secret) { printf( "That's right\n"); } else { range = (rand()%50)+10; rangeCount = secret-range; if (rangeCount%10 != 0) { do {rangeCount ++; } while (rangeCount%10 != 0); } else {} if (rangeCount <= 0) { printf("The secret number is located somewhere between 1 and 50\n"); } else if (rangeCount+50 > MAXNUM) { printf("The secret number is located somewhere between %d and &d\n", MAXNUM-50, MAXNUM); } else { printf("The secret number is located somewhere between &d and %d\n",rangeCount, rangeCount+50); } } do { printf ( "Enter your guess: " ); scanf( "%d",&guess ); count++; if (guess > secret) { printf("Lower \n"); } else if (guess < secret) { printf("Higher \n"); } } printf("That's right\n"); printf("It took you %d attempts to guess the secret number", count); } return 0; }