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 static char *patternfile
= PATTERNFILE
;
40 small_short pattern_data
[MAX_PATTERN_DATA
];
42 /* minimal ShowMessage to avoid dependency on extraneous display code */
44 Dummy_ShowMessage(char *s
)
48 static struct display dummydsp
= {
49 .ShowMessage
= Dummy_ShowMessage
,
51 struct display
*dsp
= &dummydsp
;
53 #define is_digit(c) (((c) >= '0') && ((c) <= '9'))
54 #define is_alpha(c) ((((c) >= 'a') && ((c) <= 'z')) \
55 || (((c) >= 'A') && ((c) <= 'Z')))
56 #define eos(s) ((*s == '\0') || (*s == '\n'))
59 /* skip blanks and comments in brackets */
64 while ((**s
== ' ') || (**s
== '|') || (**s
== '['))
77 /* skip unsigned numbers */
90 ScanPiece(char **s
, small_short
*side
,
91 small_short
*piece
, small_short
*square
)
95 /* determine promotion status */
97 isp
= true, (*s
)++; /* FIXME: split into two lines. */
101 /* determine side and piece */
102 for (c
= 0; c
< NO_PIECES
; c
++)
104 if ((isw
= (**s
== pxx
[c
])) || (**s
== qxx
[c
]))
106 *piece
= isp
? promoted
[c
] : unpromoted
[c
];
118 /* piece is captured */
120 *square
= NO_SQUARES
+ *piece
;
124 /* determine column */
125 for (c
= 0; c
< NO_COLS
; c
++)
138 for (r
= 0; r
< NO_ROWS
; r
++)
150 /* determine square */
151 *square
= r
* NO_COLS
+ c
;
160 ScanPattern (char *s
, short *pindex
)
162 small_short side
, piece
, square
;
163 skipbb(&s
); /* skip blanks and comments */
167 pattern_data
[(*pindex
)++] = atoi(s
);
171 pattern_data
[(*pindex
)++] = END_OF_LINKS
;
176 if (ScanPiece(&s
, &side
, &piece
, &square
))
182 pattern_data
[(*pindex
)++] = piece
;
183 pattern_data
[(*pindex
)++] = (side
? -square
: square
);
188 pattern_data
[(*pindex
)++] = END_OF_FIELDS
;
194 ReadOpeningSequences (short *pindex
)
198 short max_pattern
= 0;
199 short max_opening_sequence
= 0;
201 if ((fd
= fopen (patternfile
, "r")) == NULL
)
202 fd
= fopen ("gnushogi.pat", "r");
208 while (fgets (s
, 256, fd
) != NULL
)
212 /* comment, skip line */
214 else if (is_alpha(*s
))
216 if (max_opening_sequence
++ > 0)
218 pattern_data
[(*pindex
)++] = END_OF_PATTERNS
;
221 pattern_data
[(*pindex
)++] = ValueOfOpeningName(s
);
225 if (ScanPattern(s
, pindex
))
227 dsp
->ShowMessage("error in pattern sequence...");
237 pattern_data
[(*pindex
)++] = END_OF_PATTERNS
;
238 pattern_data
[(*pindex
)++] = END_OF_SEQUENCES
;
241 "Pattern: %d bytes for %d sequences with %d patterns.\n",
242 *pindex
, max_opening_sequence
, max_pattern
);
247 sprintf(s
, "no pattern file '%s'", patternfile
);
254 WriteOpeningSequences (short pindex
)
258 short max_pattern
= 0;
259 short max_opening_sequence
= 0;
261 fd
= fopen ("pattern.inc", "w");
262 fprintf(fd
, "#define MAX_PATTERN_DATA %d\n\n", pindex
);
263 fprintf(fd
, "small_short pattern_data[MAX_PATTERN_DATA] =\n{\n");
267 fprintf(fd
, " %d,\n", pattern_data
[n
++]);
274 while (pattern_data
[n
] != END_OF_LINKS
)
276 fprintf(fd
, "%d, ", pattern_data
[n
++]);
279 fprintf(fd
, "%d, ", pattern_data
[n
++]);
284 fprintf(fd
, "%d,", pattern_data
[n
++]);
286 while (pattern_data
[n
] != END_OF_FIELDS
);
288 fprintf(fd
, "%d,\n", pattern_data
[n
++]);
291 while (pattern_data
[n
] != END_OF_PATTERNS
);
293 fprintf(fd
, " %d,\n", pattern_data
[n
++]);
294 max_opening_sequence
++;
296 while (pattern_data
[n
] != END_OF_SEQUENCES
);
298 fprintf(fd
, " %d\n}; \n", pattern_data
[n
++]);
299 fprintf(fd
, "\n#define MAX_OPENING_SEQUENCE %d\n", max_opening_sequence
);
300 fprintf(fd
, "\n#define MAX_PATTERN %d\n", max_pattern
);