-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mastermind.c
530 lines (481 loc) · 12.6 KB
/
Mastermind.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
extern void sleep(int seconds);
void simulateGame(void);
void howToPlay(void);
void selectDifficultyLevel(void);
void playGame(void);
void computeFeedback(char codeString[], char guessString[]);
#define NOVICE 1
#define STANDARD 2
#define EXPERT 3
#define CODE_MAX_LENGTH 10
#define MAX_ATTEMPTS 10
#define NOVICE_ATTEMPTS 10
#define STANDARD_ATTEMPTS 20
#define EXPERT_ATTEMPTS 30
#define NOVICE_CODE_LEN 4
#define STANDARD_CODE_LEN 5
#define EXPERT_CODE_LEN 6
#define TRUE 1
#define FALSE 0
int difficultyLevel = NOVICE;
int secretLength = 4;
int numAttempts = NOVICE_ATTEMPTS;
typedef struct
{
int colors[4];
int black;
int white;
} guess;
int randomInteger(int low, int high)
{
int k;
double d;
d = (double)rand()/(1.0 + (double)RAND_MAX);
k = (int) (d*(high - low + 1));
return (low + k);
}
void simulateGame()
{
char code[4] = {'R','O','V','G'};
printf("Try guessing \n");
sleep(5);
printf("GVPY \n");
sleep(2);
printf("Sorry, your guess GVRP is not a match (B: %d, W: %d) \n", 0, 2);
printf("Interpret B and W\n");
printf("Try guessing \n");
sleep(5);
printf("ROGV \n");
sleep(2);
printf("Sorry, your guess ROGV is not a match (B: %d, W: %d) \n", 2, 2);
printf("Try guessing \n");
sleep(5);
printf("ROVG \n");
sleep(2);
printf("YAY, your guess ROVG is a match (B: %d, W: %d) \n", 4, 0);
return;
}
void howToPlay()
{
printf("\nHOW TO PLAY:\nThe computer creates a code which "
"has to be quessed by the user.\nEach guess is made by the "
"user by typing first letter of the colors, for example, "
"G R Y P.\nOnce code is typed, the code maker (computer) "
"provides feedback by displaying m B and n W,\nwhere m and n "
"can be a positive integer <= number of positions (for example 1 B, 2 W).\n"
"B is displayed for each color from the guess which is correct "
"in both color and position.\nW indicates the existence of a correct color, "
"but placed in the wrong position.\nOnce feedback is provided, "
"another guess is made by the user;\nguesses and feedback continue "
"to alternate until either the code breaker guesses correctly,\n"
"or twelve (or ten, or eight) incorrect guesses are made, depending "
"on the difficulty level.\n\n");
}
void selectDifficultyLevel()
{
int dl = 0;
printf("Choose Difficulty Level\n");
printf("1 for Novice\n2 for Standard and \n3 for Expert\n");
scanf("%d",&dl);
while(dl!= NOVICE && dl!= STANDARD && dl!= EXPERT)
{
printf("Your input is invalid. Try again...");
scanf("%d",&dl);
}
if(dl == NOVICE)
{
printf("The difficulty level is set to Novice\n"
"The colours available are: R O G Y P V.\n"
"The secret code length is 4. You're good to go!\n\n");
secretLength = NOVICE_CODE_LEN;
numAttempts = NOVICE_ATTEMPTS;
difficultyLevel = NOVICE;
}
if(dl == STANDARD)
{
printf("The difficulty level is set to Standard\n"
"The colours available are: R O G Y P V.\n"
"The secret code length is 5. You're good to go!\n\n");
secretLength = STANDARD_CODE_LEN;
numAttempts = STANDARD_ATTEMPTS;
difficultyLevel = STANDARD;
}
else if(dl == EXPERT)
{
printf("The difficulty level is set to Expert\n"
"The colours available are: R O G Y P V.\n"
"The secret code length is 6. Brace yourself, champion!\n");
secretLength = EXPERT_CODE_LEN;
numAttempts = EXPERT_ATTEMPTS;
difficultyLevel = EXPERT;
}
}
void aboutGame()
{
printf("\nABOUT THE GAME:\nMaster Mind is a code-breaking game "
"for two players - a code maker and a code breaker.\n"
"With four pegs and six colors, there are 6^4 = 1296 different "
"patterns (allowing duplicate colors).\nVarying the number of "
"colors and the number of holes results in\na spectrum of "
"Mastermind games of different levels of difficulty.\n"
"In this program, computer plays the role of code maker and "
"user plays the role of code breaker.\nCode maker (computer) "
"creates a code consisting of 4 or more colors at specific "
"positions,\nbut does not reveal the code to code breaker "
"(user).\nCode breaker tries to guess the pattern, in both "
"order and color, within 'x'turns,\nwhere x is determined "
"based on the difficulty level.\n\n");
printf("To get a feel, try to simulate the game -- press option 5\n\n");
}
void computeFeedback(char codeString[], char guessString[])
{
int min, sum, cp=0, c=0, i;
int l = strlen(codeString);
char code[CODE_MAX_LENGTH], string[CODE_MAX_LENGTH];
strcpy(code,codeString);
strcpy(string,guessString);
for (i=0;i<=(l-1);i++)
{
if (string[i]==code[i])
cp++;
}
printf("\tB: %d\n", cp);
int R1=0,R2=0,O1=0,O2=0,G1=0,G2=0,Y1=0,Y2=0,P1=0,P2=0,V1=0,V2=0;
for (i=0;i<=(l-1);i++)
{
if (string[i]=='R') R1++;
if (string[i]=='O') O1++;
if (string[i]=='G') G1++;
if (string[i]=='Y') Y1++;
if (string[i]=='P') P1++;
if (string[i]=='V') V1++;
if (code[i]=='R') R2++;
if (code[i]=='O') O2++;
if (code[i]=='G') G2++;
if (code[i]=='Y') Y2++;
if (code[i]=='P') P2++;
if (code[i]=='V') V2++;
}
sum=0;
min=R1;
if (R2<min) min=R2;
sum=sum+min;
min=O1;
if (O2<min) min=O2;
sum=sum+min;
min=G1;
if (G2<min) min=G2;
sum=sum+min;
min=Y1;
if (Y2<min) min=Y2;
sum=sum+min;
min=P1;
if (P2<min) min=P2;
sum=sum+min;
min=V1;
if (V2<min) min=V2;
sum=sum+min;
c=sum-cp;
printf("\tW: %d\n", c);
c=0;
cp=0;
}
void playGame()
{
// Loop until User quits, Runs out of tries or Cracks the code
char flag = TRUE, v = FALSE;
char codeString[CODE_MAX_LENGTH];
char guessString[CODE_MAX_LENGTH];
int turn = 1;
int len = 0;
int l = 0;
if(difficultyLevel == NOVICE)
l = NOVICE_CODE_LEN;
else if(difficultyLevel == STANDARD)
l = STANDARD_CODE_LEN;
else if(difficultyLevel == EXPERT)
l = EXPERT_CODE_LEN;
else
l = NOVICE_CODE_LEN;
for(int i = 0; i < CODE_MAX_LENGTH; i++)
{
guessString[i] = '\0';
codeString[i] = '\0';
}
//Valid color codes are R, O, G, Y, P, V
for(int i = 0; i < l ; i++)
{
int random = randomInteger(1, 6);
switch(random) {
case 1:
codeString[i] = 'R';
break;
case 2:
codeString[i] = 'O';
break;
case 3:
codeString[i] = 'G';
break;
case 4:
codeString[i] = 'Y';
break;
case 5:
codeString[i] = 'P';
break;
case 6:
codeString[i] = 'V';
break;
default:
codeString[i] = 'R';
break;
}
}
//printf("%s\n",codeString);
printf("You are %s User \n", (difficultyLevel == 1) ? "NOVICE" :
(difficultyLevel == 2) ? ("STANDARD") : ("EXPERT"));
printf("You have %d Attempts\n", numAttempts);
while(flag == TRUE)
{
if(turn > numAttempts)
{
printf("Tough Luck. You are out of turns.\n");
printf("The secret code was %s\n",codeString);
return;
}
printf("This is your %d of %d attempts\n", turn, numAttempts);
turn++;
printf("Enter the guess string containing R, O, G, Y, P, V only\n");
scanf("%s", guessString);
if(strcmp(guessString, codeString) == 0)
{
printf("CONGRATULATIONS!! You Won the game\n");
return;
}
if(strcmp(guessString, "QUIT") == 0)
{
printf("You have decided to QUIT this game. Try your luck next time... \n");
printf("The secret code was %s\n",codeString);
return;
}
len = strlen(guessString);
if(difficultyLevel == NOVICE && len != NOVICE_CODE_LEN ||
difficultyLevel == STANDARD && len != STANDARD_CODE_LEN ||
difficultyLevel == EXPERT && len != EXPERT_CODE_LEN)
{
printf("Level %d does not match code length entered - %d. Try again \n",
difficultyLevel, len);
continue;
}
//printf("Setting V to FALSE\n");
v = FALSE;
for(int i = 0; i < len; i++)
{
//printf("guessString[%d] = %c\n", i, guessString[i]);
if(guessString[i] != 'R' && guessString[i] != 'O' && guessString[i] != 'G'
&& guessString[i] != 'Y' && guessString[i] != 'P' && guessString[i] != 'V')
{
printf("You have entered invalid color code %c at position %d\n",
guessString[i], i);
printf("Setting V to TRUE\n");
v = TRUE;
//break;
}
}
if(v == TRUE)
{
printf("Due to invalid character continue with new input\n");
continue;
}
//Feedback computation
computeFeedback(codeString, guessString);
}
}
void valuation(int *col1, int *col2, int *wp, int *bp)
{
int color[6][2];
int c;
int white;
int black;
for(c=0; c<6; c++)
color[c][0]=color[c][1]=0;
for(c=0; c<4; c++)
{
color[col1[c]][0]++;
color[col2[c]][1]++;
}
white=0;
for(c=0; c<6; c++)
white+=color[c][(color[c][0]>color[c][1] ? 1 : 0)];
black=0;
for(c=0; c<4; c++)
black+=(col1[c]==col2[c] ? 1 : 0);
white-=black;
*wp=white;
*bp=black;
}
int search(int *colors, int depth, guess *gp, int guesses)
{
int guess;
int white, black;
int c;
if(depth==4)
{
for(guess=0; guess<guesses; guess++)
{
valuation(colors, gp[guess].colors, &white, &black);
if(!(white==gp[guess].white && black==gp[guess].black))
break;
}
return (guess<guesses ? 0 : 1);
}
for(c=0; c<6; c++)
{
colors[depth]=c;
if(search(colors, depth+1, gp, guesses))
return 1;
}
return 0;
}
char *my_strchr(char a[],char ch)
{
char *p = NULL;
char *s = a;
while(*s!='\0' && p==NULL)
{
if(*s==ch)
p=s;
s++;
}
return p;
}
int indexof(char *string, char character)
{
char *p = my_strchr(string,character);
if(p)
{
int i = p - string;
return i;
}
else
return -1;
}
void codeMaker()
{
srand((unsigned)time(NULL));
char secret_code[4];
char *valid_colours = "ROGYPV";
char *valid = valid_colours;
//printf("%s",valid_colours);
printf("You have chosen to be code maker!\n");
printf("You have to make a secret code and the computer will guess the code in miniumum number of tries.\n");
printf("The length of the secret code should be 4. The valid colours are R, O, G, Y, P and V.\n");
printf("You can create a secret code with a combination of these colours, with or without repition.\n");
printf("Enter the code: ");
scanf("%s",secret_code);
printf("---------------------\n");
printf(" turn : code : W/B \n");
printf("---------------------\n");
int toguess[4];
int c;
int done=0;
guess *gp=NULL, *cur;
int guesses=0;
int len = strlen(secret_code);
while(len != 4)
{
printf("The secret code length is invalid.\nEnter a code of length 4.");
scanf("%s",secret_code);
printf("%ld",strlen(secret_code));
len = strlen(secret_code);
}
for(c=0; c<4; c++)
{
while(my_strchr(valid_colours,secret_code[c])==NULL)
{
//fprintf(stderr,"Color no. %d (value %d) is out of range\n", c, toguess[c]);
//exit(-1);
printf("Color no. %d (value %c) is not valid. Enter code again.\n",(c+1),secret_code[c]);
scanf("%s",secret_code);
}
}
for(c=0; c<4; c++)
{
toguess[c] = indexof(valid_colours,secret_code[c]);
}
while(!done)
{
guesses++;
gp=(guess *)realloc((void *)gp, guesses*sizeof(guess));
cur=gp+(guesses-1);
search(cur->colors, 0, gp, guesses-1);
valuation(cur->colors, toguess, &(cur->white), &(cur->black));
printf("#%d\t ",guesses);
for(c=0; c<4; c++)
{
char colour1;
int colour_pos = cur->colors[c];
switch(colour_pos)
{
case 0: colour1 = 'R';
break;
case 1: colour1 = 'O';
break;
case 2: colour1 = 'G';
break;
case 3: colour1 = 'Y';
break;
case 4: colour1 = 'P';
break;
case 5: colour1 = 'V';
break;
}
printf(" %c",colour1);
}
printf(" %d %d\n",cur->white, cur->black);
done=(cur->black==4 ? 1 : 0);
}
}
int main()
{
int choice = 0;
char flag = TRUE;
while(flag == TRUE)
{
printf("\n\n\t\t\t\tWELCOME TO MASTERMIND!\n\t\t\t\t"
"THE CODE BREAKING GAME!\n Choose one of the following:"
"\n1.Play the game as Code Breaker\n2.Be the Code Maker\n3.Difficulty\n4.About the Game\n"
"5.How to Play\n6.Simulate the game\n7.Quit\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
playGame();
break;
case 2:
codeMaker();
break;
case 3:
selectDifficultyLevel();
break;
case 4:
aboutGame();
break;
case 5:
howToPlay();
break;
case 6:
simulateGame();
break;
case 7:
flag = FALSE;
break;
default:
printf("Invalid input. Try again.");
}
}
return 0;
}