-
Notifications
You must be signed in to change notification settings - Fork 0
/
guess.c
256 lines (236 loc) · 8.22 KB
/
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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int number_to_digits(int x)
{
return x%10;
}
void guess_easy()
{
int i,j,k,*generated_num,*guess_num,n,good=0,bad=0;
generated_num=(int*)malloc(4*sizeof(int));
guess_num=(int*)malloc(4*sizeof(int));
srand((unsigned)time(NULL));
A:generated_num[0]=rand()%9 +1;
generated_num[1]=rand()%10;
generated_num[2]=rand()%10;
generated_num[3]=rand()%10;
for(i=0;i<3;i++)
{
for(j=i+1;j<4;j++)
{
if(generated_num[i]==generated_num[j])
{
goto A;
}
}
}
printf("\t\tEnter your 4 digit number\n");
for(i=0;i<10;i++)
{
good=0;bad=0;
scanf("%d",&n);
for(j=3;j>=0;j--)
{
guess_num[j]=number_to_digits(n);
n=n/10;
}
for(j=0;j<4;j++)
{
if(generated_num[j]==guess_num[j])
good++;
}
for(k=0;k<4;k++)
{
for(j=0;j<4;j++)
{
if(generated_num[j]==guess_num[k] && j!=k)
bad++;
}
}
printf("%d GOOD , %d BAD \n",good,bad);
if(good==4)
{
printf("\nYou have guessed the number.GOOD JOB!!!\n");
}
}
printf("\nThe number was: \n");
for(i=0;i<4;i++)
printf("%d",generated_num[i]);
printf("\n");
}
void guess_medium()
{
int i,j,k;
long int n;
int *generated_num,*guess_num,good=0,bad=0;
generated_num=(int*)malloc(5*sizeof(int));
guess_num=(int*)malloc(5*sizeof(int));
srand((unsigned)time(NULL));
B:generated_num[0]=rand()%9 +1;
generated_num[1]=rand()%10;
generated_num[2]=rand()%10;
generated_num[3]=rand()%10;
generated_num[4]=rand()%10;;
for(i=0;i<4;i++)
{
for(j=i+1;j<5;j++)
{
if(generated_num[i]==generated_num[j])
goto B;
}
}
printf("\t\tEnter your 5 digit number\n");
for(i=0;i<10;i++)
{
good=0;bad=0;
scanf("%ld",&n);
for(j=4;j>=0;j--)
{
guess_num[j]=number_to_digits(n);
n=n/10;
}
for(j=0;j<5;j++)
{
if(generated_num[j]==guess_num[j])
good++;
}
for(k=0;k<4;k++)
{
for(j=0;j<5;j++)
{
if(generated_num[j]==guess_num[k] && j!=k)
bad++;
}
}
printf("%d GOOD , %d BAD \n",good,bad);
if(good==5)
{
printf("\nYou have guessed the number. GOOD JOB!!!\n");
}
}
printf("\nThe number was: \n");
for(i=0;i<5;i++)
printf("%d",generated_num[i]);
printf("\n");
}
void guess_hard()
{
int i,j,k;
long int n;
int *guess_num,*generated_num,good=0,bad=0;
generated_num=(int*)malloc(6*sizeof(int));
guess_num=(int*)malloc(6*sizeof(int));
srand((unsigned)time(NULL));
C:generated_num[0]=rand()%9 +1;
generated_num[1]=rand()%10;
generated_num[2]=rand()%10;
generated_num[3]=rand()%10;
generated_num[4]=rand()%10;
generated_num[5]=rand()%10;
for(i=0;i<5;i++)
{
for(j=i+1;j<6;j++)
{
if(generated_num[i]==generated_num[j])
{
goto C;
}
}
}
printf("\t\tEnter your 6 digit number\n");
for(i=0;i<10;i++)
{
good=0;bad=0;
scanf("%ld",&n);
for(j=5;j>=0;j--)
{
guess_num[j]=number_to_digits(n);
n=n/10;
}
for(j=0;j<6;j++)
{
if(generated_num[j]==guess_num[j])
good++;
}
for(k=0;k<5;k++)
{
for(j=0;j<6;j++)
{
if(generated_num[j]==guess_num[k] && j!=k)
bad++;
}
}
printf("%d GOOD , %d BAD \n",good,bad);
if(good==6)
{
printf("\nYou have guessed the number.GOOD JOB!!!\n");
}
}
printf("\nThe number was: \n");
for(i=0;i<6;i++)
printf("%d",generated_num[i]);
printf("\n");
}
void rules()
{
printf("\t\t\tHEY WORLD\n\tGuess the code is a code breaking game of logic.\n Your aim is to crack the code in the least number of attempts.\nYou can also challenge your friends and can compare the number of attempts.\n\n\tRULES:\n\t You get 10 attempts at breaking the code. If you cannot discover it by then, then you lose that game.\nBut that's alright.You can get a new code and try again.\nThere are three Levels to this game, each corresponding to a different level of difficulty.\nTo win, the number you guess must match the number generated by our friend, #Randomizer#!!.\n After each attempt you will be told how you went.\n\n\t-----> x GOOD : 'x' digit in your code are in the same position as the number you are supposed to guess.\n\t-----> y BAD : 'y' digits in your code are present in the number to be guessed, but not in the right position. \nTo make the game slightly simpler, the digits in a number will be unique.");
}
int g()
{
int choice;
/*main_menu_of_game:*/printf("\n\n\t\t\tWELCOME TO GUESS THE CODE\n");
A:printf("\n\t\t\t1.ABOUT THE GAME\n");
printf("\t\t\t2.PLAY!!\n");
printf("\t\t\t3.EXIT GAME\n");
printf("\t\t\tEnter your choice\n");
printf("\t\t\t");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
rules();
goto A;
break;
}
case 2:
{
int choice2=0;
play_menu:printf("1.EASY\n");
printf("2.MEDIUM\n");
printf("3.HARD\n");
printf("4.Go to main menu of the game\n");
printf("\nEnter your choice\n");
//code doesnt work from this point onwards
scanf("%d",&choice2);
switch(choice2)
{
case 1:
{
guess_easy();
goto play_menu;
break;
}
case 2:
{
guess_medium();
goto play_menu;
break;
}
case 3:
{
guess_hard();
goto play_menu;
break;
}
case 4:
{
goto A;
break;
}
}
}
}
return 0;
}