4 * ----------------------------------------------------------------------
5 * Copyright (c) 1993, 1994, 1995 Matthias Mutz
6 * Copyright (c) 1999 Michael Vanier and the Free Software Foundation
8 * GNU SHOGI is based on GNU CHESS
10 * Copyright (c) 1988, 1989, 1990 John Stanback
11 * Copyright (c) 1992 Free Software Foundation
13 * This file is part of GNU SHOGI.
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.
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
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 * ----------------------------------------------------------------------
35 #define MAX_PATTERN_DATA 5000
36 #define MAX_OPENING_SEQUENCE 20
37 #define MAX_PATTERN 200
39 small_short pattern_data[MAX_PATTERN_DATA];
41 /* minimal ShowMessage to avoid dependency on extraneous display code */
43 Dummy_ShowMessage(char *s)
47 static struct display dummydsp = {
48 .ShowMessage = Dummy_ShowMessage,
50 struct display *dsp = &dummydsp;
52 #define is_digit(c) (((c) >= '0') && ((c) <= '9'))
53 #define is_alpha(c) ((((c) >= 'a') && ((c) <= 'z')) \
54 || (((c) >= 'A') && ((c) <= 'Z')))
55 #define eos(s) ((*s == '\0') || (*s == '\n'))
58 /* skip blanks and comments in brackets */
63 while ((**s == ' ') || (**s == '|') || (**s == '['))
76 /* skip unsigned numbers */
89 ScanPiece(char **s, small_short *side,
90 small_short *piece, small_short *square)
94 /* determine promotion status */
96 isp = true, (*s)++; /* FIXME: split into two lines. */
100 /* determine side and piece */
101 for (c = 0; c < NO_PIECES; c++)
103 if ((isw = (**s == pxx[c])) || (**s == qxx[c]))
105 *piece = isp ? promoted[c] : unpromoted[c];
117 /* piece is captured */
119 *square = NO_SQUARES + *piece;
123 /* determine column */
124 for (c = 0; c < NO_COLS; c++)
137 for (r = 0; r < NO_ROWS; r++)
149 /* determine square */
150 *square = r * NO_COLS + c;
159 ScanPattern (char *s, short *pindex)
161 small_short side, piece, square;
162 skipbb(&s); /* skip blanks and comments */
166 pattern_data[(*pindex)++] = atoi(s);
170 pattern_data[(*pindex)++] = END_OF_LINKS;
175 if (ScanPiece(&s, &side, &piece, &square))
181 pattern_data[(*pindex)++] = piece;
182 pattern_data[(*pindex)++] = (side ? -square : square);
187 pattern_data[(*pindex)++] = END_OF_FIELDS;
193 ReadOpeningSequences (short *pindex, const char* patternfile)
197 short max_pattern = 0;
198 short max_opening_sequence = 0;
200 fd = fopen (patternfile, "r");
203 sprintf(s, "no pattern file '%s'", patternfile);
210 while (fgets (s, 256, fd) != NULL)
214 /* comment, skip line */
216 else if (is_alpha(*s))
218 if (max_opening_sequence++ > 0)
220 pattern_data[(*pindex)++] = END_OF_PATTERNS;
223 pattern_data[(*pindex)++] = ValueOfOpeningName(s);
227 if (ScanPattern(s, pindex))
229 dsp->ShowMessage("error in pattern sequence...");
239 pattern_data[(*pindex)++] = END_OF_PATTERNS;
240 pattern_data[(*pindex)++] = END_OF_SEQUENCES;
243 "Pattern: %d bytes for %d sequences with %d patterns.\n",
244 *pindex, max_opening_sequence, max_pattern);
252 WriteOpeningSequences (short pindex, const char* patternincfile)
256 short max_pattern = 0;
257 short max_opening_sequence = 0;
259 fd = fopen (patternincfile, "w");
260 fprintf(fd, "#define MAX_PATTERN_DATA %d\n\n", pindex);
261 fprintf(fd, "small_short pattern_data[MAX_PATTERN_DATA] =\n{\n");
265 fprintf(fd, " %d,\n", pattern_data[n++]);
272 while (pattern_data[n] != END_OF_LINKS)
274 fprintf(fd, "%d, ", pattern_data[n++]);
277 fprintf(fd, "%d, ", pattern_data[n++]);
282 fprintf(fd, "%d,", pattern_data[n++]);
284 while (pattern_data[n] != END_OF_FIELDS);
286 fprintf(fd, "%d,\n", pattern_data[n++]);
289 while (pattern_data[n] != END_OF_PATTERNS);
291 fprintf(fd, " %d,\n", pattern_data[n++]);
292 max_opening_sequence++;
294 while (pattern_data[n] != END_OF_SEQUENCES);
296 fprintf(fd, " %d\n}; \n", pattern_data[n++]);
297 fprintf(fd, "\n#define MAX_OPENING_SEQUENCE %d\n", max_opening_sequence);
298 fprintf(fd, "\n#define MAX_PATTERN %d\n", max_pattern);