/* 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 500 /* largest number to be guessed /* change to 100 to something small /* while developing */ void game(); int main() { int secret,guess,count,type,range,rangeCount,encourage,highScore,plays,play; time_t time1,time2; plays = 0; srand ( (int) time( NULL )); /* seed the random number generator */ /* comment above out while developing*/ /* the number guessed will then always be the same*/ game (); return 0; void game (); do { count = 0; encourage=0; secret = 225; /*rand ( ) % MAXNUM +1; /* a random number between 1 and MAXNUM */ if (plays == 0) { printf( "\nGuessing Game\n\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\n", MAXNUM ); } else { printf( "\n\n\nA new number has been selected.\n" ); printf( "Hint the number is between 1 and %d\n\n", MAXNUM ); } do { printf ( "Enter your guess: " ); scanf( "%d",&guess ); if (count == 0) { (void) time(&time1); } count++; if ((secret-guess)<=5 && (secret-guess)>=-5 && encourage==0 && guess!=secret) { printf("You're getting close!!\n"); encourage = 1; } 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 ((secret-guess)<=5 && (secret-guess)>=-5 && encourage==0 && guess!=secret) { printf("You're getting close!!\n"); encourage = 1; } 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 ((secret-guess)<=5 && (secret-guess)>=-5 && encourage==0 && guess!=secret) { printf("You're getting close!!\n"); encourage = 1; } if (guess > secret) { printf("Lower \n"); } else if (guess < secret) { printf("Higher \n"); } } while (guess != secret); printf("That's right\n"); } } (void) time(&time2); printf("\n\nIt took you %d attempts and %d seconds to guess the secret number\n", count, time2-time1); if (plays!=0) { if (highScore > time2-time1) { highScore=time2-time1; printf("Your new fastest time is: %d seconds", highScore); } else { printf("You did not beat your fastest time. Your current fastest time is: %d seconds", highScore); } } plays++; printf("\n\nDo you wish to try to beat your time?\n"); printf("To play again press 1, otherwise press 2: ");/ scanf( "%d",&play ); }while (play == 1); return 0; }