4 * ----------------------------------------------------------------------
6 * Copyright (c) 2012 Free Software Foundation
8 * GNU SHOGI is based on GNU CHESS
10 * This file is part of GNU SHOGI.
12 * GNU Shogi is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 3 of the License,
15 * or (at your option) any later version.
17 * GNU Shogi is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * You should have received a copy of the GNU General Public License along
23 * with GNU Shogi; see the file COPYING. If not, see
24 * <http://www.gnu.org/licenses/>.
25 * ----------------------------------------------------------------------
32 #define MAX_PATTERN_DATA 5000
33 #define MAX_OPENING_SEQUENCE 20
34 #define MAX_PATTERN 200
36 char *patternfile = PATTERNFILE;
38 #define is_digit(c) (((c) >= '0') && ((c) <= '9'))
39 #define is_alpha(c) ((((c) >= 'a') && ((c) <= 'z')) \
40 || (((c) >= 'A') && ((c) <= 'Z')))
41 #define eos(s) ((*s == '\0') || (*s == '\n'))
44 /* skip blanks and comments in brackets */
49 while ((**s == ' ') || (**s == '|') || (**s == '['))
62 /* skip unsigned numbers */
75 ScanPiece(char **s, small_short *side,
76 small_short *piece, small_short *square)
80 /* determine promotion status */
82 isp = true, (*s)++; /* FIXME: split into two lines. */
86 /* determine side and piece */
87 for (c = 0; c < NO_PIECES; c++)
89 if ((isw = (**s == pxx[c])) || (**s == qxx[c]))
91 *piece = isp ? promoted[c] : unpromoted[c];
103 /* piece is captured */
105 *square = NO_SQUARES + *piece;
109 /* determine column */
110 for (c = 0; c < NO_COLS; c++)
123 for (r = 0; r < NO_ROWS; r++)
135 /* determine square */
136 *square = r * NO_COLS + c;
145 ScanPattern (char *s, short *pindex)
147 small_short side, piece, square;
148 skipbb(&s); /* skip blanks and comments */
152 pattern_data[(*pindex)++] = atoi(s);
156 pattern_data[(*pindex)++] = END_OF_LINKS;
161 if (ScanPiece(&s, &side, &piece, &square))
167 pattern_data[(*pindex)++] = piece;
168 pattern_data[(*pindex)++] = (side ? -square : square);
173 pattern_data[(*pindex)++] = END_OF_FIELDS;
179 ReadOpeningSequences (short *pindex)
184 short max_pattern = 0;
185 short max_opening_sequence = 0;
187 if ((fd = fopen (patternfile, "r")) == NULL)
188 fd = fopen ("gnushogi.pat", "r");
194 while (fgets (s, 256, fd) != NULL)
198 /* comment, skip line */
200 else if (is_alpha(*s))
202 if (max_opening_sequence++ > 0)
204 pattern_data[(*pindex)++] = END_OF_PATTERNS;
207 pattern_data[(*pindex)++] = ValueOfOpeningName(s);
211 if (ScanPattern(s, pindex))
213 ShowMessage("error in pattern sequence...");
223 pattern_data[(*pindex)++] = END_OF_PATTERNS;
224 pattern_data[(*pindex)++] = END_OF_SEQUENCES;
229 "Pattern: %d bytes for %d sequences with %d patterns.\n",
230 *pindex, max_opening_sequence, max_pattern);
237 sprintf(s, "no pattern file '%s'", patternfile);
244 WriteOpeningSequences (short pindex)
248 short max_pattern = 0;
249 short max_opening_sequence = 0;
251 fd = fopen ("pattern.inc", "w");
252 fprintf(fd, "#define MAX_PATTERN_DATA %d\n\n", pindex);
253 fprintf(fd, "small_short pattern_data[MAX_PATTERN_DATA] =\n{\n");
257 fprintf(fd, " %d,\n", pattern_data[n++]);
264 while (pattern_data[n] != END_OF_LINKS)
266 fprintf(fd, "%d, ", pattern_data[n++]);
269 fprintf(fd, "%d, ", pattern_data[n++]);
274 fprintf(fd, "%d,", pattern_data[n++]);
276 while (pattern_data[n] != END_OF_FIELDS);
278 fprintf(fd, "%d,\n", pattern_data[n++]);
281 while (pattern_data[n] != END_OF_PATTERNS);
283 fprintf(fd, " %d,\n", pattern_data[n++]);
284 max_opening_sequence++;
286 while (pattern_data[n] != END_OF_SEQUENCES);
288 fprintf(fd, " %d\n}; \n", pattern_data[n++]);
289 fprintf(fd, "\n#define MAX_OPENING_SEQUENCE %d\n", max_opening_sequence);
290 fprintf(fd, "\n#define MAX_PATTERN %d\n", max_pattern);