-
Notifications
You must be signed in to change notification settings - Fork 0
/
10IC Number Guess.c
61 lines (50 loc) · 1.28 KB
/
10IC Number Guess.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_NUMBER 100
int secret_number;
void initialize_number_generator(void);
void choose_new_secret_number(void);
void read_guesses(void);
int main(void)
{
char command;
printf("Guess the secret number between 1 and %d.\n\n", MAX_NUMBER);
initialize_number_generator();
do {
choose_new_secret_number();
printf("A new number has been chosen.\n");
read_guesses();
printf("Play again? (Y/N): ");
scanf(" %c", &command);
printf("\n");
} while (command == 'Y' || command == 'y');
return 0;
}
void initialize_number_generator(void)
{
srand((unsigned) time(NULL));
}
void choose_new_secret_number(void)
{
secret_number = rand() % MAX_NUMBER + 1;
}
void read_guesses(void)
{
int guess, num_guesses = 0;
for (;;)
{
num_guesses++;
printf("Enter guess: ");
scanf("%d", &guess);
if (guess == secret_number)
{
printf("You won in %d guesses!\n\n", num_guesses);
return;
}
else if (guess < secret_number)
printf("Too low, try again.\n");
else
printf("Too high, try again.\n");
}
}