00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <time.h>
00004
00005 #define MAX_NUMBER 100
00006
00007
00008 int secret_number;
00009
00010
00011 void initialize_number_generator(void);
00012 void choose_new_secret_number(void);
00013 void read_guesses(void);
00014
00015 int main(void)
00016 {
00017 char command;
00018
00019 printf("Guess the secret number between 1 and %d.\n\n",
00020 MAX_NUMBER);
00021 srand((unsigned) time(NULL));
00022 do {
00023 choose_new_secret_number();
00024 printf("A new number has been chosen.\n");
00025 read_guesses();
00026 printf("Play again? (Y/N) ");
00027 scanf(" %c", &command);
00028 printf("\n");
00029 } while (command == 'y' || command == 'Y');
00030
00031 return 0;
00032 }
00033
00034
00035
00036
00037
00038
00039
00040 void choose_new_secret_number(void)
00041 {
00042 secret_number = rand() % MAX_NUMBER + 1;
00043 }
00044
00045
00046
00047
00048
00049
00050
00051
00052 void read_guesses(void)
00053 {
00054 int guess, num_guesses = 0;
00055
00056 for (;;) {
00057 num_guesses++;
00058 printf("Enter guess: ");
00059 scanf("%d", &guess);
00060 if (guess == secret_number) {
00061 printf("You won in %d guesses!\n\n", num_guesses);
00062 return;
00063 } else if (guess < secret_number)
00064 printf("Too low; try again.\n");
00065 else
00066 printf("Too high; try again.\n");
00067 }
00068 }