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
40 static char *patternfile
= PATTERNFILE
;
41 small_short pattern_data
[MAX_PATTERN_DATA
];
43 /* minimal ShowMessage to avoid dependency on extraneous display code */
50 #define is_digit(c) (((c) >= '0') && ((c) <= '9'))
51 #define is_alpha(c) ((((c) >= 'a') && ((c) <= 'z')) \
52 || (((c) >= 'A') && ((c) <= 'Z')))
53 #define eos(s) ((*s == '\0') || (*s == '\n'))
56 /* skip blanks and comments in brackets */
61 while ((**s
== ' ') || (**s
== '|') || (**s
== '['))
74 /* skip unsigned numbers */
87 ScanPiece(char **s
, small_short
*side
,
88 small_short
*piece
, small_short
*square
)
92 /* determine promotion status */
94 isp
= true, (*s
)++; /* FIXME: split into two lines. */
98 /* determine side and piece */
99 for (c
= 0; c
< NO_PIECES
; c
++)
101 if ((isw
= (**s
== pxx
[c
])) || (**s
== qxx
[c
]))
103 *piece
= isp
? promoted
[c
] : unpromoted
[c
];
115 /* piece is captured */
117 *square
= NO_SQUARES
+ *piece
;
121 /* determine column */
122 for (c
= 0; c
< NO_COLS
; c
++)
135 for (r
= 0; r
< NO_ROWS
; r
++)
147 /* determine square */
148 *square
= r
* NO_COLS
+ c
;
157 ScanPattern (char *s
, short *pindex
)
159 small_short side
, piece
, square
;
160 skipbb(&s
); /* skip blanks and comments */
164 pattern_data
[(*pindex
)++] = atoi(s
);
168 pattern_data
[(*pindex
)++] = END_OF_LINKS
;
173 if (ScanPiece(&s
, &side
, &piece
, &square
))
179 pattern_data
[(*pindex
)++] = piece
;
180 pattern_data
[(*pindex
)++] = (side
? -square
: square
);
185 pattern_data
[(*pindex
)++] = END_OF_FIELDS
;
191 ReadOpeningSequences (short *pindex
)
195 short max_pattern
= 0;
196 short max_opening_sequence
= 0;
198 if ((fd
= fopen (patternfile
, "r")) == NULL
)
199 fd
= fopen ("gnushogi.pat", "r");
205 while (fgets (s
, 256, fd
) != NULL
)
209 /* comment, skip line */
211 else if (is_alpha(*s
))
213 if (max_opening_sequence
++ > 0)
215 pattern_data
[(*pindex
)++] = END_OF_PATTERNS
;
218 pattern_data
[(*pindex
)++] = ValueOfOpeningName(s
);
222 if (ScanPattern(s
, pindex
))
224 ShowMessage("error in pattern sequence...");
234 pattern_data
[(*pindex
)++] = END_OF_PATTERNS
;
235 pattern_data
[(*pindex
)++] = END_OF_SEQUENCES
;
238 "Pattern: %d bytes for %d sequences with %d patterns.\n",
239 *pindex
, max_opening_sequence
, max_pattern
);
244 sprintf(s
, "no pattern file '%s'", patternfile
);
251 WriteOpeningSequences (short pindex
)
255 short max_pattern
= 0;
256 short max_opening_sequence
= 0;
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");
264 fprintf(fd
, " %d,\n", pattern_data
[n
++]);
271 while (pattern_data
[n
] != END_OF_LINKS
)
273 fprintf(fd
, "%d, ", pattern_data
[n
++]);
276 fprintf(fd
, "%d, ", pattern_data
[n
++]);
281 fprintf(fd
, "%d,", pattern_data
[n
++]);
283 while (pattern_data
[n
] != END_OF_FIELDS
);
285 fprintf(fd
, "%d,\n", pattern_data
[n
++]);
288 while (pattern_data
[n
] != END_OF_PATTERNS
);
290 fprintf(fd
, " %d,\n", pattern_data
[n
++]);
291 max_opening_sequence
++;
293 while (pattern_data
[n
] != END_OF_SEQUENCES
);
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
);