34053d8d43039fae00da2c8e94d65ba9c9c9e844
[gnushogi.git] / gnushogi / makepattern.c
1 /*
2  * FILE: makepattern.c
3  *
4  * ----------------------------------------------------------------------
5  * Copyright (c) 1993, 1994, 1995 Matthias Mutz
6  * Copyright (c) 1999 Michael Vanier and the Free Software Foundation
7  *
8  * GNU SHOGI is based on GNU CHESS
9  *
10  * Copyright (c) 1988, 1989, 1990 John Stanback
11  * Copyright (c) 1992 Free Software Foundation
12  *
13  * This file is part of GNU SHOGI.
14  *
15  * GNU Shogi is free software; you can redistribute it and/or modify it
16  * under the terms of the GNU General Public License as published by the
17  * Free Software Foundation; either version 3 of the License,
18  * or (at your option) any later version.
19  *
20  * GNU Shogi is distributed in the hope that it will be useful, but WITHOUT
21  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
22  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
23  * for more details.
24  *
25  * You should have received a copy of the GNU General Public License along
26  * with GNU Shogi; see the file COPYING. If not, see
27  * <http://www.gnu.org/licenses/>.
28  * ----------------------------------------------------------------------
29  *
30  */
31
32 #include "gnushogi.h"
33 #include "pattern.h"
34
35 #define MAX_PATTERN_DATA     5000
36 #define MAX_OPENING_SEQUENCE 20
37 #define MAX_PATTERN          200
38
39 char *patternfile = PATTERNFILE;
40 small_short pattern_data[MAX_PATTERN_DATA];
41
42 /* minimal ShowMessage to avoid dependency on extraneous display code */
43 void
44 ShowMessage(char *s)
45 {
46     printf("%s\n", s);
47 }
48
49 #define is_digit(c) (((c) >= '0') && ((c) <= '9'))
50 #define is_alpha(c) ((((c) >= 'a') && ((c) <= 'z')) \
51     || (((c) >= 'A') && ((c) <= 'Z')))
52 #define eos(s)      ((*s == '\0') || (*s == '\n'))
53
54
55 /* skip blanks and comments in brackets */
56
57 static void
58 skipbb(char **s)
59 {
60     while ((**s == ' ') || (**s == '|') || (**s == '['))
61     {
62         if (**s == '[')
63         {
64             while (**s != ']')
65                 (*s)++;
66         }
67
68         (*s)++;
69     }
70 }
71
72
73 /* skip unsigned numbers */
74
75 static void
76 skipi(char **s)
77 {
78     while (is_digit(**s))
79         (*s)++;
80
81     skipbb(s);
82 }
83
84
85 static short
86 ScanPiece(char **s, small_short *side,
87           small_short *piece, small_short *square)
88 {
89     short isp, isw, c, r;
90
91     /* determine promotion status */
92     if (**s == '+')
93         isp = true, (*s)++;  /* FIXME: split into two lines. */
94     else
95         isp = false;
96
97     /* determine side and piece */
98     for (c = 0; c < NO_PIECES; c++)
99     {
100         if ((isw = (**s == pxx[c])) || (**s == qxx[c]))
101         {
102             *piece = isp ? promoted[c] : unpromoted[c];
103             *side  = isw;
104             (*s)++;
105             break;
106         }
107     }
108
109     if (c == NO_PIECES)
110         return 1;
111
112     if (**s == '*')
113     {
114         /* piece is captured */
115         (*s)++;
116         *square = NO_SQUARES + *piece;
117     }
118     else
119     {
120         /* determine column */
121         for (c = 0; c < NO_COLS; c++)
122         {
123             if (**s == cxx[c])
124             {
125                 (*s)++;
126                 break;
127             }
128         }
129
130         if (c >= NO_COLS)
131             return 1;
132
133         /* determine row */
134         for (r = 0; r < NO_ROWS; r++)
135         {
136             if (**s == rxx[r])
137             {
138                 (*s)++;
139                 break;
140             }
141         }
142
143         if (r >= NO_ROWS)
144             return 1;
145
146         /* determine square */
147         *square = r * NO_COLS + c;
148     }
149
150     skipbb(s);
151     return 0;
152 }
153
154
155 static short
156 ScanPattern (char *s, short *pindex)
157 {
158     small_short side, piece, square;
159     skipbb(&s); /* skip blanks and comments */
160
161     while (is_digit(*s))
162     {
163         pattern_data[(*pindex)++] = atoi(s);
164         skipi(&s);
165     }
166
167     pattern_data[(*pindex)++] = END_OF_LINKS;
168     skipbb(&s);
169
170     while (!eos(s))
171     {
172         if (ScanPiece(&s, &side, &piece, &square))
173         {
174             return 1;
175         }
176         else
177         {
178             pattern_data[(*pindex)++] = piece;
179             pattern_data[(*pindex)++] = (side ? -square : square);
180         }
181
182     }
183
184     pattern_data[(*pindex)++] = END_OF_FIELDS;
185     return 0;
186 }
187
188
189 void
190 ReadOpeningSequences (short *pindex)
191
192 {
193     FILE *fd;
194     char s[256];
195     short max_pattern = 0;
196     short max_opening_sequence = 0;
197
198     if ((fd = fopen (patternfile, "r")) == NULL)
199         fd = fopen ("gnushogi.pat", "r");
200
201     if (fd != NULL)
202     {
203         *pindex = 0;
204
205         while (fgets (s, 256, fd) != NULL)
206         {
207             if (*s == '#')
208             {
209                 /* comment, skip line */
210             }
211             else if (is_alpha(*s))
212             {
213                 if (max_opening_sequence++ > 0)
214                 {
215                     pattern_data[(*pindex)++] = END_OF_PATTERNS;
216                 }
217
218                 pattern_data[(*pindex)++] = ValueOfOpeningName(s);
219             }
220             else
221             {
222                 if (ScanPattern(s, pindex))
223                 {
224                     ShowMessage("error in pattern sequence...");
225                     exit(1);
226                 }
227                 else
228                 {
229                     max_pattern++;
230                 }
231             }
232         }
233
234         pattern_data[(*pindex)++] = END_OF_PATTERNS;
235         pattern_data[(*pindex)++] = END_OF_SEQUENCES;
236
237         sprintf(s,
238                 "Pattern: %d bytes for %d sequences with %d patterns.\n",
239                 *pindex, max_opening_sequence, max_pattern);
240         ShowMessage(s);
241
242         fclose(fd);
243     } else {
244         sprintf(s, "no pattern file '%s'", patternfile);
245         ShowMessage(s);
246     }
247 }
248
249
250 void
251 WriteOpeningSequences (short pindex)
252 {
253     FILE *fd;
254     short n = 0;
255     short max_pattern = 0;
256     short max_opening_sequence = 0;
257
258     fd = fopen ("pattern.inc", "w");
259     fprintf(fd, "#define MAX_PATTERN_DATA %d\n\n", pindex);
260     fprintf(fd, "small_short pattern_data[MAX_PATTERN_DATA] =\n{\n");
261
262     do
263     {
264         fprintf(fd, "  %d,\n", pattern_data[n++]);
265
266         do
267         {
268             fprintf(fd, "    ");
269
270             /* write links */
271             while (pattern_data[n] != END_OF_LINKS)
272             {
273                 fprintf(fd, "%d, ", pattern_data[n++]);
274             };
275
276             fprintf(fd, "%d, ", pattern_data[n++]);
277
278             /* write pattern */
279             do
280             {
281                 fprintf(fd, "%d,", pattern_data[n++]);
282             }
283             while (pattern_data[n] != END_OF_FIELDS);
284
285             fprintf(fd, "%d,\n", pattern_data[n++]);
286             max_pattern++;
287         }
288         while (pattern_data[n] != END_OF_PATTERNS);
289
290         fprintf(fd, "    %d,\n", pattern_data[n++]);
291         max_opening_sequence++;
292     }
293     while (pattern_data[n] != END_OF_SEQUENCES);
294
295     fprintf(fd, "  %d\n}; \n", pattern_data[n++]);
296     fprintf(fd, "\n#define MAX_OPENING_SEQUENCE %d\n", max_opening_sequence);
297     fprintf(fd, "\n#define MAX_PATTERN %d\n", max_pattern);
298     fclose(fd);
299 }
300
301