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 1, or (at your option) any
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, write to the Free
27 * Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
28 * ----------------------------------------------------------------------
35 #define MAX_PATTERN_DATA 5000
36 #define MAX_OPENING_SEQUENCE 20
37 #define MAX_PATTERN 200
39 char *patternfile
= PATTERNFILE
;
41 #define is_digit(c) (((c) >= '0') && ((c) <= '9'))
42 #define is_alpha(c) ((((c) >= 'a') && ((c) <= 'z')) \
43 || (((c) >= 'A') && ((c) <= 'Z')))
44 #define eos(s) ((*s == '\0') || (*s == '\n'))
47 /* skip blanks and comments in brackets */
52 while ((**s
== ' ') || (**s
== '|') || (**s
== '['))
65 /* skip unsigned numbers */
78 ScanPiece(char **s
, small_short
*side
,
79 small_short
*piece
, small_short
*square
)
83 /* determine promotion status */
85 isp
= true, (*s
)++; /* FIXME: split into two lines. */
89 /* determine side and piece */
90 for (c
= 0; c
< NO_PIECES
; c
++)
92 if ((isw
= (**s
== pxx
[c
])) || (**s
== qxx
[c
]))
94 *piece
= isp
? promoted
[c
] : unpromoted
[c
];
106 /* piece is captured */
108 *square
= NO_SQUARES
+ *piece
;
112 /* determine column */
113 for (c
= 0; c
< NO_COLS
; c
++)
126 for (r
= 0; r
< NO_ROWS
; r
++)
138 /* determine square */
139 *square
= r
* NO_COLS
+ c
;
148 ScanPattern (char *s
, short *pindex
)
150 small_short side
, piece
, square
;
151 skipbb(&s
); /* skip blanks and comments */
155 pattern_data
[(*pindex
)++] = atoi(s
);
159 pattern_data
[(*pindex
)++] = END_OF_LINKS
;
164 if (ScanPiece(&s
, &side
, &piece
, &square
))
170 pattern_data
[(*pindex
)++] = piece
;
171 pattern_data
[(*pindex
)++] = (side
? -square
: square
);
176 pattern_data
[(*pindex
)++] = END_OF_FIELDS
;
182 ReadOpeningSequences (short *pindex
)
187 short max_pattern
= 0;
188 short max_opening_sequence
= 0;
190 if ((fd
= fopen (patternfile
, "r")) == NULL
)
191 fd
= fopen ("gnushogi.pat", "r");
197 while (fgets (s
, 256, fd
) != NULL
)
201 /* comment, skip line */
203 else if (is_alpha(*s
))
205 if (max_opening_sequence
++ > 0)
207 pattern_data
[(*pindex
)++] = END_OF_PATTERNS
;
210 pattern_data
[(*pindex
)++] = ValueOfOpeningName(s
);
214 if (ScanPattern(s
, pindex
))
216 ShowMessage("error in pattern sequence...");
226 pattern_data
[(*pindex
)++] = END_OF_PATTERNS
;
227 pattern_data
[(*pindex
)++] = END_OF_SEQUENCES
;
232 "Pattern: %d bytes for %d sequences with %d patterns.\n",
233 *pindex
, max_opening_sequence
, max_pattern
);
240 sprintf(s
, "no pattern file '%s'", patternfile
);
247 WriteOpeningSequences (short pindex
)
251 short max_pattern
= 0;
252 short max_opening_sequence
= 0;
254 fd
= fopen ("pattern.inc", "w");
255 fprintf(fd
, "#define MAX_PATTERN_DATA %d\n\n", pindex
);
256 fprintf(fd
, "small_short pattern_data[MAX_PATTERN_DATA] =\n{\n");
260 fprintf(fd
, " %d,\n", pattern_data
[n
++]);
267 while (pattern_data
[n
] != END_OF_LINKS
)
269 fprintf(fd
, "%d, ", pattern_data
[n
++]);
272 fprintf(fd
, "%d, ", pattern_data
[n
++]);
277 fprintf(fd
, "%d,", pattern_data
[n
++]);
279 while (pattern_data
[n
] != END_OF_FIELDS
);
281 fprintf(fd
, "%d,\n", pattern_data
[n
++]);
284 while (pattern_data
[n
] != END_OF_PATTERNS
);
286 fprintf(fd
, " %d,\n", pattern_data
[n
++]);
287 max_opening_sequence
++;
289 while (pattern_data
[n
] != END_OF_SEQUENCES
);
291 fprintf(fd
, " %d\n}; \n", pattern_data
[n
++]);
292 fprintf(fd
, "\n#define MAX_OPENING_SEQUENCE %d\n", max_opening_sequence
);
293 fprintf(fd
, "\n#define MAX_PATTERN %d\n", max_pattern
);