2
Mastermind game, written by: Stephen A. Ward,
5
Modified for BDS C by: Leor Zolman,
9
(and Visual C and GCC) by: Stefano Bodrato,
13
-- NOTE: Z88DK ports need to be extended to support shells, to be able to pass the following parameters --
15
Usage: mm [ -B ] [ -K ] [ -C# ] [ -P# ]
17
where: -B tells # of possible solutions before each guess
18
-C# sets number of different characters (e.g., "-c4" means A-D)
20
-P# sets number of positions in solution string
22
-K disables kibitzing (enabled by default.).
24
Thus, for example, the invokation
26
would simulate the game of "Bagels", where the layout is ten different
27
characters in three positions. I don't think "Bagels" allows repetitions,
28
though, so it isn't QUITE the same...
38
#define NPEGS 10 /* Max NPeg */
39
#define MCOLORS 26 /* Max NColors */
42
char Secret[NPEGS+2]; /* was CHAR */
43
char History[NHIST*NPEGS]; /* was CHAR */
46
int NColors, /* Number of colors */
47
NPeg; /* Number of pegs */
49
char KFlag, /* Kibitz flag */
50
BFlag; /* Debug flag */
53
main(int argc,char **argv)
61
NColors = 6; /* Number of colors */
62
NPeg = 4; /* Number of pegs */
63
KFlag = 1; /* Kibitz flag */
64
BFlag = 0; /* Debug flag */
66
for (i=1; i<argc; i++)
67
{ if (*(arg = argv[i]) == '-') switch (*++arg) {
68
case 'B': BFlag++; continue;
69
case 'K': KFlag = !KFlag; continue;
70
case 'C': NColors = atoi(++arg);
71
if (NColors > MCOLORS) NColors = MCOLORS;
73
case 'P': NPeg = atoi(++arg);
74
if (NPeg > NPEGS) NPeg = NPEGS;
76
default: printf("Illegal option %s\n",
79
{ printf("Usage: mm [ -b ] [ -k ] [ -c# ] [ -p# ]\n");
83
printf("Mastermind game:\n");
84
printf(" I have a secret string of %d letters ",NPeg);
85
printf("between A and %c.\n", 'A' + NColors - 1);
86
printf(" Object: find it in as few guesses as possible.\n");
87
printf(" For each guess, I will tell you how many\n");
88
printf(" Hits (right letter in the right place) and\n");
89
printf(" Misses (right letter in the wrong place)\n");
90
printf(" you had for that guess.\n");
91
printf(" Note: letters may appear more than once in my strings.\n");
93
//srand1("\nType any character to begin: ");
95
printf("\nType any character to begin: ");
99
for (i=0; i<NPeg; i++) Secret[i] = rand() % NColors;
100
printf("\n\nNew game!\n");
101
for (guesses=0;;guesses++)
103
printf("\n (%d possible secret symbols)\n", Check());
104
else if (KFlag && guesses && Check() == 1)
105
printf("\nYou should be able to figure it out now!\n");
106
if (!rguess("Your test symbol", trial = &History[NPeg*guesses]))
109
j = match(trial, Secret);
112
printf( (j>>4 ? "\t\t\t %d hit" : "\t\t\t no hit"), j>>4);
113
if ((j>>4) - 1) putchar('s');
115
printf ( (j & 0xf ? ", %d miss" : ", no miss"), j & 0xf);
116
if ((j & 0xf) - 1) printf("es");
120
if (j == (NPeg << 4))
121
{ printf("You got it in %d guesses!\n", ++guesses);
125
//printf("Average for %d game%c is %1d.%1d\n",
126
printf("Average for %d game%c is %d.%d\n",
127
ngames, (ngames != 1) ? 's' : 32,
128
i , ntries*100 /ngames %100);
131
printf("My secret symbol was ");
132
for (i=0; i<NPeg; i++) putchar('A' + Secret[i]);
140
{ register int i, score;
144
for (i=0; i<NColors; i++) temp[i] = 0;
145
for (i=0; i<NPeg; i++)
146
if ((j = aa[i]) != bb[i]) temp[j]++;
147
for (i=0; i<NPeg; i++)
148
{ if ((j = bb[i]) == aa[i]) score += 16;
149
else if (temp[j]-- > 0) score++; }
157
{ if (++tt[i] < NColors) return 1;
168
for (i=0; i<NPeg; i++) tt[i] = 0;
171
for (j=0; j<guesses; j++, hh += NPeg)
172
if (match(hh, tt) != Jots[j]) goto nope;
180
int rguess(char *prompt, char *where)
184
again: printf("%s: ", prompt);
185
i = strlen(gets(where));
186
//i = strlen(getl(where,4));
187
if (i == 0) return 0;
188
if (i > NPeg && !isspace(where[i])) {
189
printf("Too many letters\n"); goto again;
191
if (i < NPeg) { printf("Too few letters\n"); goto again; }
192
for (i=0; i<NPeg; i++) {
193
c = where[i] = toupper(where[i]) - 'A';
194
if (c < 0 || c >= NColors)
195
{ printf("Bad letter -- use A thru %c\n", 'A'+NColors-1);