~z88dk-team/z88dk-pkg/trunk

« back to all changes in this revision

Viewing changes to examples/console/mm.c

  • Committer: Bazaar Package Importer
  • Author(s): Luca Falavigna
  • Date: 2008-02-12 08:23:49 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080212082349-wgijt44scmgje90o
Tags: 1.7.ds1-1ubuntu1
* Merge from Debian unstable. Remaining Ubuntu changes:
  - build z88dk and z88dk-bin binary packages for lpia too
  - update Maintainer field as per spec

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 /*
 
2
   Mastermind game, written by: Stephen A. Ward,
 
3
                                January, 1980
 
4
 
 
5
   Modified for BDS C by:       Leor Zolman,
 
6
                                February, 1980
 
7
 
 
8
   Modified for Z88DK
 
9
   (and Visual C and GCC) by:   Stefano Bodrato,
 
10
                                March, 2004
 
11
 
 
12
                                
 
13
   -- NOTE:  Z88DK ports need to be extended to support shells, to be able to pass the following parameters --
 
14
 
 
15
   Usage:  mm [ -B ] [ -K ] [ -C# ] [ -P# ]
 
16
 
 
17
   where:  -B  tells # of possible solutions before each guess
 
18
           -C# sets number of different characters (e.g., "-c4" means A-D)
 
19
                (defaults to 6)
 
20
           -P# sets number of positions in solution string
 
21
                (defaults to 4)
 
22
           -K  disables kibitzing (enabled by default.). 
 
23
                
 
24
    Thus, for example, the invokation
 
25
           mm -C10 -P3
 
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...
 
29
 
 
30
*/
 
31
 
 
32
#include <stdlib.h>
 
33
#include <string.h>
 
34
#include <ctype.h>
 
35
#include <stdio.h>
 
36
//#include <time.h>
 
37
 
 
38
#define NPEGS   10              /* Max NPeg             */
 
39
#define MCOLORS 26              /* Max NColors          */
 
40
#define NHIST 100
 
41
 
 
42
char    Secret[NPEGS+2];        /* was CHAR */
 
43
char    History[NHIST*NPEGS];   /* was CHAR */
 
44
int     Jots[NHIST];
 
45
int     guesses;
 
46
int     NColors,                /* Number of colors     */
 
47
        NPeg;                   /* Number of pegs       */
 
48
 
 
49
char    KFlag,                  /* Kibitz flag          */
 
50
        BFlag;          /* Debug flag           */
 
51
 
 
52
 
 
53
main(int argc,char **argv)
 
54
//char **argv;
 
55
 {
 
56
        register int i,j;
 
57
        int ngames, ntries;
 
58
        char *trial, *arg;
 
59
 
 
60
        ngames = ntries = 0;
 
61
        NColors = 6;            /* Number of colors     */
 
62
        NPeg = 4;               /* Number of pegs       */
 
63
        KFlag = 1;              /* Kibitz flag          */
 
64
        BFlag = 0;              /* Debug flag           */
 
65
 
 
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;
 
72
                                continue;
 
73
                case 'P':       NPeg = atoi(++arg);
 
74
                                if (NPeg > NPEGS) NPeg = NPEGS;
 
75
                                continue;
 
76
                default:        printf("Illegal option %s\n",
 
77
                                        argv[i]); exit(-1); }
 
78
           else
 
79
                { printf("Usage: mm [ -b ] [ -k ] [ -c# ] [ -p# ]\n");
 
80
                  exit(-1); }}
 
81
 
 
82
        printf("%c",12);
 
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");
 
92
 
 
93
        //srand1("\nType any character to begin: ");
 
94
        //srand(time(NULL));
 
95
        printf("\nType any character to begin: ");
 
96
        getchar();
 
97
 
 
98
Game:
 
99
        for (i=0; i<NPeg; i++) Secret[i] = rand() % NColors;
 
100
        printf("\n\nNew game!\n");
 
101
        for (guesses=0;;guesses++)
 
102
         { if (BFlag)
 
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]))
 
107
                break;
 
108
 
 
109
           j = match(trial, Secret);
 
110
           Jots[guesses] = j;
 
111
 
 
112
           printf( (j>>4 ? "\t\t\t %d hit" : "\t\t\t no hit"), j>>4);
 
113
           if ((j>>4) - 1) putchar('s');
 
114
 
 
115
           printf ( (j & 0xf ? ", %d miss" : ", no miss"), j & 0xf);
 
116
           if ((j & 0xf) - 1) printf("es");
 
117
 
 
118
           putchar('\n');
 
119
 
 
120
           if (j == (NPeg << 4))
 
121
                { printf("You got it in %d guesses!\n", ++guesses); 
 
122
                  ntries += guesses;
 
123
                  ngames++;
 
124
                  i = ntries/ngames;
 
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);
 
129
                  goto Game; }}
 
130
        Secret[NPeg] = 0;
 
131
        printf("My secret symbol was ");
 
132
        for (i=0; i<NPeg; i++) putchar('A' + Secret[i]);
 
133
        printf("\n");
 
134
        goto Game;
 
135
 }
 
136
 
 
137
int match(aa, bb)
 
138
 char *aa;
 
139
 char *bb;
 
140
 {      register int i, score;
 
141
        char j;
 
142
        int temp[MCOLORS];
 
143
        score = 0;
 
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++; }
 
150
        return score; }
 
151
 
 
152
int incr(tt)
 
153
 char *tt;
 
154
 {      register int i;
 
155
        i = 0;
 
156
        while (i < NPeg)
 
157
         { if (++tt[i] < NColors) return 1;
 
158
           tt[i] = 0; i++; }
 
159
        return 0; }
 
160
 
 
161
 
 
162
int Check()
 
163
 {      char tt[NPEGS];
 
164
        char *hh;
 
165
        register int i, j;
 
166
        int count;
 
167
        count = 0;
 
168
        for (i=0; i<NPeg; i++) tt[i] = 0;
 
169
        do {
 
170
                hh = &(History[0]);
 
171
                for (j=0; j<guesses; j++, hh += NPeg)
 
172
                        if (match(hh, tt) != Jots[j]) goto nope;
 
173
                count++;
 
174
nope:           i = i;
 
175
           } while (incr(tt));
 
176
        return count; 
 
177
 }
 
178
 
 
179
 
 
180
int rguess(char *prompt, char *where)
 
181
 {      
 
182
 register int i, c;
 
183
 
 
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;
 
190
         }
 
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);
 
196
                  goto again; }
 
197
         }
 
198
        return 1;
 
199
}