-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
92 lines (72 loc) · 1.6 KB
/
main.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void game(int x);
int random();
int start();
int randomNumber;
int count;
int main(void)
{
printf("********************************\n");
printf("********************************\n");
printf(" THE GUESSING GAME \n");
printf("********************************\n");
printf("********************************\n\n");
printf("I have chosen a number between 0 and 20 which you must guess. Press 21 to quit\n\n\n");
count = 5;
random();
start();
return 0;
}
int start()
{
int guess;
printf("You have %d tries left.\n", count);
count--;
printf("Enter a guess: ");
scanf_s("%d", &guess);
if (guess > 20 || guess < 0)
{
printf("number must be between 0 and 20");
}
if (guess == 21)
{
exit(0);
}
game(guess);
}
/*Geneating a random number */
int random()
{
time_t t;
srand((unsigned)time(&t));
randomNumber = rand() % 21;
return randomNumber;
}
void game(int x)
{
if (x == randomNumber)
{
printf("Congratulations!! Yoou guessed it ! \n\n\n\n");
main();
}
if (count == 0)
{
printf("You have no more attempts. You loose! \n\n");
main();
}
while (x != randomNumber && count != 0)
{
if (x < randomNumber)
{
printf("Sorry %d is wrong. My number is more than that.\n\n", x);
}
if (x > randomNumber)
{
printf("Sorry %d is wrong. My number is less than that.\n\n", x);
}
start();
}
return;
}