4 * ----------------------------------------------------------------------
5 * Copyright (c) 1993, 1994, 1995 Matthias Mutz
6 * Copyright (c) 1999 Michael Vanier and the Free Software Foundation
7 * Copyright (c) 2008, 2013, 2014 Yann Dirson and the Free Software Foundation
9 * GNU SHOGI is based on GNU CHESS
11 * Copyright (c) 1988, 1989, 1990 John Stanback
12 * Copyright (c) 1992 Free Software Foundation
14 * This file is part of GNU SHOGI.
16 * GNU Shogi is free software; you can redistribute it and/or modify it
17 * under the terms of the GNU General Public License as published by the
18 * Free Software Foundation; either version 3 of the License,
19 * or (at your option) any later version.
21 * GNU Shogi is distributed in the hope that it will be useful, but WITHOUT
22 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
26 * You should have received a copy of the GNU General Public License along
27 * with GNU Shogi; see the file COPYING. If not, see
28 * <http://www.gnu.org/licenses/>.
29 * ----------------------------------------------------------------------
36 #define MAX_PATTERN_DATA 5000
37 #define MAX_OPENING_SEQUENCE 20
38 #define MAX_PATTERN 200
42 small_short pattern_data[MAX_PATTERN_DATA];
44 /* minimal ShowMessage to avoid dependency on extraneous display code */
46 Dummy_ShowMessage(char *format, ...)
53 static struct display dummydsp = {
54 .ShowMessage = Dummy_ShowMessage,
56 struct display *dsp = &dummydsp;
58 #define is_digit(c) (((c) >= '0') && ((c) <= '9'))
59 #define is_alpha(c) ((((c) >= 'a') && ((c) <= 'z')) \
60 || (((c) >= 'A') && ((c) <= 'Z')))
61 #define eos(s) ((*s == '\0') || (*s == '\n'))
64 /* skip blanks and comments in brackets */
69 while ((**s == ' ') || (**s == '|') || (**s == '['))
82 /* skip unsigned numbers */
95 ScanPiece(char **s, small_short *side,
96 small_short *piece, small_short *square)
100 /* determine promotion status */
102 isp = true, (*s)++; /* FIXME: split into two lines. */
106 /* determine side and piece */
107 for (c = 0; c < NO_PIECES; c++)
109 if ((isw = (**s == pxx[c])) || (**s == qxx[c]))
111 *piece = isp ? promoted[c] : unpromoted[c];
123 /* piece is captured */
125 *square = NO_SQUARES + *piece;
129 /* determine column */
130 for (c = 0; c < NO_COLS; c++)
132 if (**s == COL_NAME(c))
143 for (r = 0; r < NO_ROWS; r++)
145 if (**s == ROW_NAME(r))
155 /* determine square */
156 *square = r * NO_COLS + c;
165 ScanPattern (char *s, short *pindex)
167 small_short side, piece, square;
168 skipbb(&s); /* skip blanks and comments */
172 pattern_data[(*pindex)++] = atoi(s);
176 pattern_data[(*pindex)++] = END_OF_LINKS;
181 if (ScanPiece(&s, &side, &piece, &square))
187 pattern_data[(*pindex)++] = piece;
188 pattern_data[(*pindex)++] = (side ? -square : square);
193 pattern_data[(*pindex)++] = END_OF_FIELDS;
199 ReadOpeningSequences (short *pindex, const char* patternfile)
203 short max_pattern = 0;
204 short max_opening_sequence = 0;
206 fd = fopen (patternfile, "r");
209 sprintf(s, "no pattern file '%s'", patternfile);
216 while (fgets (s, 256, fd) != NULL)
220 /* comment, skip line */
222 else if (is_alpha(*s))
224 if (max_opening_sequence++ > 0)
226 pattern_data[(*pindex)++] = END_OF_PATTERNS;
229 pattern_data[(*pindex)++] = ValueOfOpeningName(s);
233 if (ScanPattern(s, pindex))
235 dsp->ShowMessage("error in pattern sequence...");
245 pattern_data[(*pindex)++] = END_OF_PATTERNS;
246 pattern_data[(*pindex)++] = END_OF_SEQUENCES;
249 "Pattern: %d bytes for %d sequences with %d patterns.\n",
250 *pindex, max_opening_sequence, max_pattern);
258 WriteOpeningSequences (short pindex, const char* patternincfile)
262 short max_pattern = 0;
263 short max_opening_sequence = 0;
265 fd = fopen (patternincfile, "w");
266 fprintf(fd, "#define MAX_PATTERN_DATA %d\n\n", pindex);
267 fprintf(fd, "small_short pattern_data[MAX_PATTERN_DATA] =\n{\n");
271 fprintf(fd, " %d,\n", pattern_data[n++]);
278 while (pattern_data[n] != END_OF_LINKS)
280 fprintf(fd, "%d, ", pattern_data[n++]);
283 fprintf(fd, "%d, ", pattern_data[n++]);
288 fprintf(fd, "%d,", pattern_data[n++]);
290 while (pattern_data[n] != END_OF_FIELDS);
292 fprintf(fd, "%d,\n", pattern_data[n++]);
295 while (pattern_data[n] != END_OF_PATTERNS);
297 fprintf(fd, " %d,\n", pattern_data[n++]);
298 max_opening_sequence++;
300 while (pattern_data[n] != END_OF_SEQUENCES);
302 fprintf(fd, " %d\n}; \n", pattern_data[n++]);
303 fprintf(fd, "\n#define MAX_OPENING_SEQUENCE %d\n", max_opening_sequence);
304 fprintf(fd, "\n#define MAX_PATTERN %d\n", max_pattern);