forked from hoshir/zebra
-
Notifications
You must be signed in to change notification settings - Fork 1
/
patterns.c
400 lines (253 loc) · 7.34 KB
/
patterns.c
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/*
File: patterns.c
Created: July 4, 1997
Modified: December 25, 1999
Author: Gunnar Andersson ([email protected])
Contents: The patterns.
*/
#include <stdio.h>
#include <stdlib.h>
#include "constant.h"
#include "display.h"
#include "globals.h"
#include "macros.h"
#include "patterns.h"
/* Global variables */
int pow3[10] = { 1, 3, 9, 27, 81, 243, 729, 2187, 6561, 19683 };
/* Connections between the squares and the bit masks */
int row_no[100];
int row_index[100];
int col_no[100];
int col_index[100];
int color_pattern[3];
/* The patterns describing the current state of the board. */
int row_pattern[8];
int col_pattern[8];
/* Symmetry maps */
int flip8[6561];
/* Bit masks which represent dependencies between discs and patterns */
unsigned int depend_lo[100];
unsigned int depend_hi[100];
/* Bit masks that show what patterns have been modified */
unsigned int modified_lo;
unsigned int modified_hi;
/*
TRANSFORMATION_SET_UP
Calculate the various symmetry and color transformations.
*/
static void
transformation_setup( void ) {
int i, j;
int row[10];
/* Build the pattern tables for 8*1-patterns */
for ( i = 0; i < 8; i++ )
row[i] = 0;
for ( i = 0; i < 6561; i++ ) {
/* Create the symmetry map */
flip8[i] = 0;
for ( j = 0; j < 8; j++ )
flip8[i] += row[j] * pow3[7 - j];
/* Next configuration */
j = 0;
do { /* The odometer principle */
row[j]++;
if ( row[j] == 3 )
row[j] = 0;
j++;
} while ( (row[j - 1] == 0) && (j < 8) );
}
}
/*
ADD_SINGLE
Mark board position POS as depending on pattern # MASK.
*/
static void
add_single( int mask, int pos ) {
if ( mask < 32 )
depend_lo[pos] |= ((unsigned int )1) << ((unsigned int)mask);
else
depend_hi[pos] |= 1 << (mask - 32);
}
/*
ADD_MULTIPLE
Mark board positions POS, POS+STEP, ..., POS+(COUNT-1)STEP as
depending on pattern # MASK.
*/
static void
add_multiple( int mask, int pos, int count, int step ) {
int i;
for ( i = 0; i < count; i++ )
add_single( mask, pos + i * step );
}
/*
PATTERN_DEPENDENCY
Fill the dependency masks for each square with the bit masks
for the patterns which it depends.
Note: The definitions of the patterns and their corresponding name
must match the order given in endmacro.c.
*/
static void
pattern_dependency( void ) {
/* A-file+2X: a1-a8 + b2,b7 */
add_multiple( AFILEX1, 11, 8, 10 );
add_single( AFILEX1, 22 );
add_single( AFILEX1, 72 );
/* A-file+2X: h1-h8 + g2,g7 */
add_multiple( AFILEX2, 18, 8, 10 );
add_single( AFILEX2, 27 );
add_single( AFILEX2, 77 );
/* A-file+2X: a1-h1 + b2,g2 */
add_multiple( AFILEX3, 11, 8, 1 );
add_single( AFILEX3, 22 );
add_single( AFILEX3, 27 );
/* A-file+2X: a8-h8 + b7,g7 */
add_multiple( AFILEX4, 81, 8, 1 );
add_single( AFILEX4, 72 );
add_single( AFILEX4, 77 );
/* B-file: b1-b8 */
add_multiple( BFILE1, 12, 8, 10 );
/* B-file: g1-g8 */
add_multiple( BFILE2, 17, 8, 10 );
/* B-file: a2-h2 */
add_multiple( BFILE3, 21, 8, 1 );
/* B-file: a7-h7 */
add_multiple( BFILE4, 71, 8, 1 );
/* C-file: c1-c8 */
add_multiple( CFILE1, 13, 8, 10 );
/* C-file: f1-f8 */
add_multiple( CFILE2, 16, 8, 10 );
/* C-file: a3-h3 */
add_multiple( CFILE3, 31, 8, 1 );
/* C-file: a6-h6 */
add_multiple( CFILE4, 61, 8, 1);
/* D-file: d1-d8 */
add_multiple( DFILE1, 14, 8, 10 );
/* D-file: e1-e8 */
add_multiple( DFILE2, 15, 8, 10 );
/* D-file: a4-h4 */
add_multiple( DFILE3, 41, 8, 1 );
/* D-file: a5-h5 */
add_multiple( DFILE4, 51, 8, 1 );
/* Diag8: a1-h8 */
add_multiple( DIAG8_1, 11, 8, 11 );
/* Diag8: h1-a8 */
add_multiple( DIAG8_2, 18, 8, 9 );
/* Diag7: b1-h7 */
add_multiple( DIAG7_1, 12, 7, 11 );
/* Diag7: a2-g8 */
add_multiple( DIAG7_2, 21, 7, 11 );
/* Diag7: a7-g1 */
add_multiple( DIAG7_3, 17, 7, 9 );
/* Diag7: b8-h2 */
add_multiple( DIAG7_4, 28, 7, 9 );
/* Diag6: c1-h6 */
add_multiple( DIAG6_1, 13, 6, 11 );
/* Diag6: a3-f8 */
add_multiple( DIAG6_2, 31, 6, 11 );
/* Diag6: a6-f1 */
add_multiple( DIAG6_3, 16, 6, 9 );
/* Diag6: c8-h3 */
add_multiple( DIAG6_4, 38, 6, 9 );
/* Diag5: d1-h5 */
add_multiple( DIAG5_1, 14, 5, 11 );
/* Diag5: a4-e8 */
add_multiple( DIAG5_2, 41, 5, 11 );
/* Diag5: a5-e1 */
add_multiple( DIAG5_3, 15, 5, 9 );
/* Diag5: d8-h4 */
add_multiple( DIAG5_4, 48, 5, 9 );
/* Diag4: e1-h4 */
add_multiple( DIAG4_1, 15, 4, 11 );
/* Diag4: a5-d8 */
add_multiple( DIAG4_2, 51, 4, 11 );
/* Diag4: a4-d1 */
add_multiple( DIAG4_3, 14, 4, 9 );
/* Diag4: e8-h5 */
add_multiple( DIAG4_4, 58, 4, 9 );
/* Corner3x3: a1-c1 + a2-c2 + a3-c3 */
add_multiple( CORNER33_1, 11, 3, 1 );
add_multiple( CORNER33_1, 21, 3, 1 );
add_multiple( CORNER33_1, 31, 3, 1 );
/* Corner3x3: a8-c8 + a7-c7 + a6-c6 */
add_multiple( CORNER33_2, 81, 3, 1 );
add_multiple( CORNER33_2, 71, 3, 1 );
add_multiple( CORNER33_2, 61, 3, 1 );
/* Corner3x3: f1-h1 + f2-h2 + f3-h3 */
add_multiple( CORNER33_3, 18, 3, -1 );
add_multiple( CORNER33_3, 28, 3, -1 );
add_multiple( CORNER33_3, 38, 3, -1 );
/* Corner3x3: f8-h8 + f7-h7 + f6-h6 */
add_multiple( CORNER33_4, 88, 3, -1 );
add_multiple( CORNER33_4, 78, 3, -1 );
add_multiple( CORNER33_4, 68, 3, -1 );
/* Corner4x2: a1-d1 + a2-d2 */
add_multiple( CORNER42_1, 11, 4, 1 );
add_multiple( CORNER42_1, 21, 4, 1 );
/* Corner4x2: a8-d8 + a7-d7 */
add_multiple( CORNER42_2, 81, 4, 1 );
add_multiple( CORNER42_2, 71, 4, 1 );
/* Corner4x2: e1-h1 + e2-h2 */
add_multiple( CORNER42_3, 18, 4, -1 );
add_multiple( CORNER42_3, 28, 4, -1 );
/* Corner4x2: e8-h8 + e7-h7 */
add_multiple( CORNER42_4, 88, 4, -1 );
add_multiple( CORNER42_4, 78, 4, -1 );
/* Corner4x2: a1-a4 + b1-b4 */
add_multiple( CORNER42_5, 11, 4, 10 );
add_multiple( CORNER42_5, 12, 4, 10 );
/* Corner4x2: h1-h4 + g1-g4 */
add_multiple( CORNER42_6, 18, 4, 10 );
add_multiple( CORNER42_6, 17, 4, 10 );
/* Corner4x2: a8-a5 + b8-b5 */
add_multiple( CORNER42_7, 81, 4, -10 );
add_multiple( CORNER42_7, 82, 4, -10 );
/* Corner4x2: h8-h5 + g8-g5 */
add_multiple( CORNER42_8, 88, 4, -10 );
add_multiple( CORNER42_8, 87, 4, -10 );
}
/*
INIT_PATTERNS
Pre-computes some tables needed for fast pattern access.
*/
void
init_patterns( void ) {
int i, j;
int pos;
transformation_setup();
for ( i = 1; i <= 8; i++ )
for ( j = 1; j <= 8; j++ ) {
pos = 10 * i + j;
row_no[pos] = i - 1;
row_index[pos] = j - 1;
col_no[pos] = j - 1;
col_index[pos] = i - 1;
}
pattern_dependency();
/* These values needed for compatibility with the old book format */
color_pattern[BLACKSQ] = BLACK_PATTERN;
color_pattern[WHITESQ] = WHITE_PATTERN;
}
/*
COMPUTE_LINE_PATTERNS
Translate the current board configuration into patterns.
*/
void
compute_line_patterns( int *in_board ) {
int i, j;
int pos;
int mask;
for ( i = 0; i < 8; i++ ) {
row_pattern[i] = 0;
col_pattern[i] = 0;
}
for ( i = 1; i <= 8; i++ )
for ( j = 1; j <= 8; j++ ) {
pos = 10 * i + j;
if ( in_board[pos] == EMPTY )
mask = EMPTY_PATTERN;
else
mask = color_pattern[in_board[pos]];
row_pattern[row_no[pos]] += mask * pow3[row_index[pos]];
col_pattern[col_no[pos]] += mask * pow3[col_index[pos]];
}
}