-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGuessTheNumber.c
132 lines (116 loc) · 2.93 KB
/
GuessTheNumber.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#ifdef _WIN32
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <string.h>
#endif
#ifdef linux
#include <stdio.h>
#include <ncurses.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <string.h>
#endif
int menu();
int main()
{
int num, menu_state1=0, menu_state2=0, loop=1;
char* state1_options[2];
char* state2_options[3];
// menu screen one
state1_options[0] = "start";
state1_options[1] = "select mode";
// menu screen two
state2_options[0] = "easy";
state2_options[1] = "medium";
state2_options[2] = "hard";
// Seed value for random number
srand((unsigned) time(0));
printf("Welcome to the Game!\n");
printf("To quit press q\n");
while(loop)
{
menu_state1 = menu(2, state1_options);
switch (menu_state1)
{
case 0:
loop = 0;
break;
case 1:
menu_state2 = menu(3, state2_options);
break;
}
}
// Set the dificulty level based on the menu_state2
int tries = menu_state2 == 1 ? 100 : (menu_state2 == 2 ? 50 : 10);
// generate the random number
int random = rand() % (tries*10);
printf("\nEnter a number between %d\n", tries*10-1);
printf("Enter a number to start the game:- ");
for (size_t i = 0; i < tries; i++)
{
scanf(" %d", &num);
printf("You entered %d\n", num);
if (num > random)
printf("Lower!\n");
else if (num < random)
printf("Higher!\n");
else {
printf("\nCongratulations You WON!!!!!\n");
exit(EXIT_SUCCESS);
}
}
return 0;
}
int menu(int no_of_options, char* options[])
{
// two charachers to read arrow keys
char menu_ch1, menu_ch2;
// "a H" for KEY_UP
// "a P" for KEY_DOWN
// "a K" for KEY_LEFT
// "a M" for KEY_RIGHT
// lookup on Grey codes for more info
int option=1;
system("clear");
while(1)
{
for (int i=0; i<no_of_options; i++)
{
if (i == option)
printf("* ");
else printf(" ");
printf("%s\n", options[i]);
}
menu_ch1 = getch();
if (menu_ch1 == -32)
{
menu_ch2 = getch();
switch(menu_ch2)
{
case 'H': // UP
option--;
break;
case 'P': // DOWN
option++;
break;
case 'K': // LEFT
option--;
break;
case 'M': // RIGHT
option++;
break;
}
option%=no_of_options;
}
if (menu_ch1 == 'q')
exit(EXIT_SUCCESS);
else if (menu_ch1 == 13)
return option;
//printf("\e[1;1H\e[2J");
system("clear");
}
}