File: makepattern.c

package info (click to toggle)
gnushogi 1.5~git20140725-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 1,900 kB
  • sloc: ansic: 12,731; sh: 4,104; makefile: 128
file content (308 lines) | stat: -rw-r--r-- 6,957 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
 * FILE: makepattern.c
 *
 * ----------------------------------------------------------------------
 * Copyright (c) 1993, 1994, 1995 Matthias Mutz
 * Copyright (c) 1999 Michael Vanier and the Free Software Foundation
 * Copyright (c) 2008, 2013, 2014 Yann Dirson and the Free Software Foundation
 *
 * GNU SHOGI is based on GNU CHESS
 *
 * Copyright (c) 1988, 1989, 1990 John Stanback
 * Copyright (c) 1992 Free Software Foundation
 *
 * This file is part of GNU SHOGI.
 *
 * GNU Shogi is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 3 of the License,
 * or (at your option) any later version.
 *
 * GNU Shogi is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with GNU Shogi; see the file COPYING. If not, see
 * <http://www.gnu.org/licenses/>.
 * ----------------------------------------------------------------------
 *
 */

#include "gnushogi.h"
#include "pattern.h"

#define MAX_PATTERN_DATA     5000
#define MAX_OPENING_SEQUENCE 20
#define MAX_PATTERN          200

short xboard = false;

small_short pattern_data[MAX_PATTERN_DATA];

/* minimal ShowMessage to avoid dependency on extraneous display code */
static void
Dummy_ShowMessage(char *format, ...)
{
    va_list ap;
    va_start(ap, format);
    vprintf(format, ap);
    va_end(ap);
}
static struct display dummydsp = {
  .ShowMessage = Dummy_ShowMessage,
};
struct display *dsp = &dummydsp;

#define is_digit(c) (((c) >= '0') && ((c) <= '9'))
#define is_alpha(c) ((((c) >= 'a') && ((c) <= 'z')) \
    || (((c) >= 'A') && ((c) <= 'Z')))
#define eos(s)      ((*s == '\0') || (*s == '\n'))


/* skip blanks and comments in brackets */

static void
skipbb(char **s)
{
    while ((**s == ' ') || (**s == '|') || (**s == '['))
    {
        if (**s == '[')
        {
            while (**s != ']')
                (*s)++;
        }

        (*s)++;
    }
}


/* skip unsigned numbers */

static void
skipi(char **s)
{
    while (is_digit(**s))
        (*s)++;

    skipbb(s);
}


static short
ScanPiece(char **s, small_short *side,
          small_short *piece, small_short *square)
{
    short isp, isw, c, r;

    /* determine promotion status */
    if (**s == '+')
        isp = true, (*s)++;  /* FIXME: split into two lines. */
    else
        isp = false;

    /* determine side and piece */
    for (c = 0; c < NO_PIECES; c++)
    {
        if ((isw = (**s == pxx[c])) || (**s == qxx[c]))
        {
            *piece = isp ? promoted[c] : unpromoted[c];
            *side  = isw;
            (*s)++;
            break;
        }
    }

    if (c == NO_PIECES)
        return 1;

    if (**s == '*')
    {
        /* piece is captured */
        (*s)++;
        *square = NO_SQUARES + *piece;
    }
    else
    {
        /* determine column */
        for (c = 0; c < NO_COLS; c++)
        {
            if (**s == COL_NAME(c))
            {
                (*s)++;
                break;
            }
        }

        if (c >= NO_COLS)
            return 1;

        /* determine row */
        for (r = 0; r < NO_ROWS; r++)
        {
            if (**s == ROW_NAME(r))
            {
                (*s)++;
                break;
            }
        }

        if (r >= NO_ROWS)
            return 1;

        /* determine square */
        *square = r * NO_COLS + c;
    }

    skipbb(s);
    return 0;
}


static short
ScanPattern (char *s, short *pindex)
{
    small_short side, piece, square;
    skipbb(&s); /* skip blanks and comments */

    while (is_digit(*s))
    {
        pattern_data[(*pindex)++] = atoi(s);
        skipi(&s);
    }

    pattern_data[(*pindex)++] = END_OF_LINKS;
    skipbb(&s);

    while (!eos(s))
    {
        if (ScanPiece(&s, &side, &piece, &square))
        {
            return 1;
        }
        else
        {
            pattern_data[(*pindex)++] = piece;
            pattern_data[(*pindex)++] = (side ? -square : square);
        }

    }

    pattern_data[(*pindex)++] = END_OF_FIELDS;
    return 0;
}


void
ReadOpeningSequences (short *pindex, const char* patternfile)
{
    FILE *fd;
    char s[256];
    short max_pattern = 0;
    short max_opening_sequence = 0;

    fd = fopen (patternfile, "r");

    if (fd == NULL) {
        sprintf(s, "no pattern file '%s'", patternfile);
        dsp->ShowMessage(s);
        return;
    }

    *pindex = 0;

    while (fgets (s, 256, fd) != NULL)
    {
        if (*s == '#')
        {
            /* comment, skip line */
        }
        else if (is_alpha(*s))
        {
            if (max_opening_sequence++ > 0)
            {
                pattern_data[(*pindex)++] = END_OF_PATTERNS;
            }

            pattern_data[(*pindex)++] = ValueOfOpeningName(s);
        }
        else
        {
            if (ScanPattern(s, pindex))
            {
                dsp->ShowMessage("error in pattern sequence...");
                exit(1);
            }
            else
            {
                max_pattern++;
            }
        }
    }

    pattern_data[(*pindex)++] = END_OF_PATTERNS;
    pattern_data[(*pindex)++] = END_OF_SEQUENCES;

    sprintf(s,
            "Pattern: %d bytes for %d sequences with %d patterns.\n",
            *pindex, max_opening_sequence, max_pattern);
    dsp->ShowMessage(s);

    fclose(fd);
}


void
WriteOpeningSequences (short pindex, const char* patternincfile)
{
    FILE *fd;
    short n = 0;
    short max_pattern = 0;
    short max_opening_sequence = 0;

    fd = fopen (patternincfile, "w");
    fprintf(fd, "#define MAX_PATTERN_DATA %d\n\n", pindex);
    fprintf(fd, "small_short pattern_data[MAX_PATTERN_DATA] =\n{\n");

    do
    {
        fprintf(fd, "  %d,\n", pattern_data[n++]);

        do
        {
            fprintf(fd, "    ");

            /* write links */
            while (pattern_data[n] != END_OF_LINKS)
            {
                fprintf(fd, "%d, ", pattern_data[n++]);
            };

            fprintf(fd, "%d, ", pattern_data[n++]);

            /* write pattern */
            do
            {
                fprintf(fd, "%d,", pattern_data[n++]);
            }
            while (pattern_data[n] != END_OF_FIELDS);

            fprintf(fd, "%d,\n", pattern_data[n++]);
            max_pattern++;
        }
        while (pattern_data[n] != END_OF_PATTERNS);

        fprintf(fd, "    %d,\n", pattern_data[n++]);
        max_opening_sequence++;
    }
    while (pattern_data[n] != END_OF_SEQUENCES);

    fprintf(fd, "  %d\n}; \n", pattern_data[n++]);
    fprintf(fd, "\n#define MAX_OPENING_SEQUENCE %d\n", max_opening_sequence);
    fprintf(fd, "\n#define MAX_PATTERN %d\n", max_pattern);
    fclose(fd);
}