13 pattern_record(char *table
, char *str
, hash3_t pat
, int fixed_color
)
15 /* Original color assignment */
16 table
[pat
] = fixed_color
? fixed_color
: 3;
17 //fprintf(stderr, "[%s] %04x %d\n", str, pat, fixed_color);
19 /* Reverse color assignment */
20 table
[pattern3_reverse(pat
)] = fixed_color
? 2 - (fixed_color
== 2) : 3;
21 //fprintf(stderr, "[%s] %04x %d\n", str, pat, fixed_color);
25 pat_vmirror(hash3_t pat
)
27 /* V mirror pattern; reverse order of 3-2-3 chunks */
28 return ((pat
& 0xfc00) >> 10) | (pat
& 0x03c0) | ((pat
& 0x003f) << 10);
32 pat_hmirror(hash3_t pat
)
34 /* H mirror pattern; reverse order of 2-bit values within the chunks */
35 #define rev3(p) ((p >> 4) | (p & 0xc) | ((p & 0x3) << 4))
36 #define rev2(p) ((p >> 2) | ((p & 0x3) << 2))
37 return (rev3((pat
& 0xfc00) >> 10) << 10)
38 | (rev2((pat
& 0x03c0) >> 6) << 6)
39 | rev3((pat
& 0x003f));
45 pat_90rot(hash3_t pat
)
47 /* Rotate by 90 degrees:
51 /* I'm too lazy to optimize this :) */
53 for (int i
= 0; i
< 8; i
++)
54 vals
[i
] = (pat
>> (i
* 2)) & 0x3;
56 vals2
[0] = vals
[5]; vals2
[1] = vals
[3]; vals2
[2] = vals
[0];
57 vals2
[3] = vals
[6]; vals2
[4] = vals
[1];
58 vals2
[5] = vals
[7]; vals2
[6] = vals
[4]; vals2
[7] = vals
[2];
60 for (int i
= 0; i
< 8; i
++)
61 p2
|= vals2
[i
] << (i
* 2);
66 pattern3_transpose(hash3_t pat
, hash3_t (*transp
)[8])
70 (*transp
)[i
++] = pat_vmirror(pat
);
71 (*transp
)[i
++] = pat_hmirror(pat
);
72 (*transp
)[i
++] = pat_vmirror(pat_hmirror(pat
));
73 (*transp
)[i
++] = pat_90rot(pat
);
74 (*transp
)[i
++] = pat_90rot(pat_vmirror(pat
));
75 (*transp
)[i
++] = pat_90rot(pat_hmirror(pat
));
76 (*transp
)[i
++] = pat_90rot(pat_vmirror(pat_hmirror(pat
)));
80 pattern_gen(struct pattern3s
*p
, hash3_t pat
, char *src
, int srclen
, int fixed_color
)
82 for (; srclen
> 0; src
++, srclen
--) {
85 int patofs
= (srclen
> 5 ? srclen
- 1 : srclen
) - 1;
88 *src
= '.'; pattern_gen(p
, pat
, src
, srclen
, fixed_color
);
89 *src
= 'X'; pattern_gen(p
, pat
, src
, srclen
, fixed_color
);
90 *src
= 'O'; pattern_gen(p
, pat
, src
, srclen
, fixed_color
);
91 *src
= '#'; pattern_gen(p
, pat
, src
, srclen
, fixed_color
);
92 *src
= '?'; // for future recursions
95 *src
= '.'; pattern_gen(p
, pat
, src
, srclen
, fixed_color
);
96 *src
= 'O'; pattern_gen(p
, pat
, src
, srclen
, fixed_color
);
97 *src
= '#'; pattern_gen(p
, pat
, src
, srclen
, fixed_color
);
98 *src
= 'x'; // for future recursions
101 *src
= '.'; pattern_gen(p
, pat
, src
, srclen
, fixed_color
);
102 *src
= 'X'; pattern_gen(p
, pat
, src
, srclen
, fixed_color
);
103 *src
= '#'; pattern_gen(p
, pat
, src
, srclen
, fixed_color
);
104 *src
= 'o'; // for future recursions
106 case '.': /* 0 */ break;
107 case 'X': pat
|= S_BLACK
<< (patofs
* 2); break;
108 case 'O': pat
|= S_WHITE
<< (patofs
* 2); break;
109 case '#': pat
|= S_OFFBOARD
<< (patofs
* 2); break;
113 /* Original pattern, all transpositions and rotations */
115 pattern3_transpose(pat
, &transp
);
116 for (int i
= 0; i
< 8; i
++)
117 pattern_record(p
->hash
, src
- 9, transp
[i
], fixed_color
);
121 patterns_gen(struct pattern3s
*p
, char src
[][11], int src_n
)
123 for (int i
= 0; i
< src_n
; i
++) {
124 //printf("<%s>\n", src[i]);
127 case 'X': fixed_color
= S_BLACK
; break;
128 case 'O': fixed_color
= S_WHITE
; break;
130 //fprintf(stderr, "** %s **\n", src[i]);
131 pattern_gen(p
, 0, src
[i
], 9, fixed_color
);
136 patterns_load(char src
[][11], int src_n
, char *filename
)
138 FILE *f
= fopen("moggy.patterns", "r");
139 if (!f
) return false;
142 for (i
= 0; i
< src_n
; i
++) {
144 if (!fgets(line
, sizeof(line
), f
))
146 int l
= strlen(line
);
147 if (l
!= 10 + (line
[l
- 1] == '\n'))
149 memcpy(src
[i
], line
, 10);
151 fprintf(stderr
, "moggy.patterns: %d patterns loaded\n", i
);
155 fprintf(stderr
, "Error loading moggy.patterns.\n");
161 pattern3s_init(struct pattern3s
*p
, char src
[][11], int src_n
)
163 char nsrc
[src_n
][11];
165 if (!patterns_load(nsrc
, src_n
, "moggy.patterns")) {
166 /* Use default pattern set. */
167 for (int i
= 0; i
< src_n
; i
++)
168 strcpy(nsrc
[i
], src
[i
]);
171 patterns_gen(p
, nsrc
, src_n
);