Switch tactics.a linking order to avoid linker errors
[pachi/derm.git] / pattern3.c
bloba11c110732d869b91dbf06979d25c25ad1cdbd55
1 #include <assert.h>
2 #include <math.h>
3 #include <stdio.h>
4 #include <stdlib.h>
6 #include "board.h"
7 #include "debug.h"
8 #include "pattern3.h"
11 static void
12 pattern_record(char *table, char *str, hash3_t pat, int fixed_color)
14 /* Original color assignment */
15 table[pat] = fixed_color ? fixed_color : 3;
16 //fprintf(stderr, "[%s] %04x %d\n", str, pat, fixed_color);
18 /* Reverse color assignment */
19 table[pattern3_reverse(pat)] = fixed_color ? 2 - (fixed_color == 2) : 3;
20 //fprintf(stderr, "[%s] %04x %d\n", str, pat, fixed_color);
23 static int
24 pat_vmirror(hash3_t pat)
26 /* V mirror pattern; reverse order of 3-2-3 chunks */
27 return ((pat & 0xfc00) >> 10) | (pat & 0x03c0) | ((pat & 0x003f) << 10);
30 static int
31 pat_hmirror(hash3_t pat)
33 /* H mirror pattern; reverse order of 2-bit values within the chunks */
34 #define rev3(p) ((p >> 4) | (p & 0xc) | ((p & 0x3) << 4))
35 #define rev2(p) ((p >> 2) | ((p & 0x3) << 2))
36 return (rev3((pat & 0xfc00) >> 10) << 10)
37 | (rev2((pat & 0x03c0) >> 6) << 6)
38 | rev3((pat & 0x003f));
39 #undef rev3
40 #undef rev2
43 static int
44 pat_90rot(hash3_t pat)
46 /* Rotate by 90 degrees:
47 * 5 6 7 7 4 2
48 * 3 4 -> 6 1
49 * 0 1 2 5 3 0 */
50 /* I'm too lazy to optimize this :) */
51 int vals[8];
52 for (int i = 0; i < 8; i++)
53 vals[i] = (pat >> (i * 2)) & 0x3;
54 int vals2[8];
55 vals2[0] = vals[5]; vals2[1] = vals[3]; vals2[2] = vals[0];
56 vals2[3] = vals[6]; vals2[4] = vals[1];
57 vals2[5] = vals[7]; vals2[6] = vals[4]; vals2[7] = vals[2];
58 int p2 = 0;
59 for (int i = 0; i < 8; i++)
60 p2 |= vals2[i] << (i * 2);
61 return p2;
64 void
65 pattern3_transpose(hash3_t pat, hash3_t (*transp)[8])
67 int i = 0;
68 (*transp)[i++] = pat;
69 (*transp)[i++] = pat_vmirror(pat);
70 (*transp)[i++] = pat_hmirror(pat);
71 (*transp)[i++] = pat_vmirror(pat_hmirror(pat));
72 (*transp)[i++] = pat_90rot(pat);
73 (*transp)[i++] = pat_90rot(pat_vmirror(pat));
74 (*transp)[i++] = pat_90rot(pat_hmirror(pat));
75 (*transp)[i++] = pat_90rot(pat_vmirror(pat_hmirror(pat)));
78 static void
79 pattern_gen(struct pattern3s *p, hash3_t pat, char *src, int srclen, int fixed_color)
81 for (; srclen > 0; src++, srclen--) {
82 if (srclen == 5)
83 continue;
84 int patofs = (srclen > 5 ? srclen - 1 : srclen) - 1;
85 switch (*src) {
86 case '?':
87 *src = '.'; pattern_gen(p, pat, src, srclen, fixed_color);
88 *src = 'X'; pattern_gen(p, pat, src, srclen, fixed_color);
89 *src = 'O'; pattern_gen(p, pat, src, srclen, fixed_color);
90 *src = '#'; pattern_gen(p, pat, src, srclen, fixed_color);
91 *src = '?'; // for future recursions
92 return;
93 case 'x':
94 *src = '.'; pattern_gen(p, pat, src, srclen, fixed_color);
95 *src = 'O'; pattern_gen(p, pat, src, srclen, fixed_color);
96 *src = '#'; pattern_gen(p, pat, src, srclen, fixed_color);
97 *src = 'x'; // for future recursions
98 return;
99 case 'o':
100 *src = '.'; pattern_gen(p, pat, src, srclen, fixed_color);
101 *src = 'X'; pattern_gen(p, pat, src, srclen, fixed_color);
102 *src = '#'; pattern_gen(p, pat, src, srclen, fixed_color);
103 *src = 'o'; // for future recursions
104 return;
105 case '.': /* 0 */ break;
106 case 'X': pat |= S_BLACK << (patofs * 2); break;
107 case 'O': pat |= S_WHITE << (patofs * 2); break;
108 case '#': pat |= S_OFFBOARD << (patofs * 2); break;
112 /* Original pattern, all transpositions and rotations */
113 hash3_t transp[8];
114 pattern3_transpose(pat, &transp);
115 for (int i = 0; i < 8; i++)
116 pattern_record(p->hash, src - 9, transp[i], fixed_color);
119 static void
120 patterns_gen(struct pattern3s *p, char src[][11], int src_n)
122 for (int i = 0; i < src_n; i++) {
123 //printf("<%s>\n", src[i]);
124 int fixed_color = 0;
125 switch (src[i][9]) {
126 case 'X': fixed_color = S_BLACK; break;
127 case 'O': fixed_color = S_WHITE; break;
129 //fprintf(stderr, "** %s **\n", src[i]);
130 pattern_gen(p, 0, src[i], 9, fixed_color);
134 static bool
135 patterns_load(char src[][11], int src_n, char *filename)
137 FILE *f = fopen("moggy.patterns", "r");
138 if (!f) return false;
140 int i;
141 for (i = 0; i < src_n; i++) {
142 char line[32];
143 if (!fgets(line, sizeof(line), f))
144 goto error;
145 int l = strlen(line);
146 if (l != 10 + (line[l - 1] == '\n'))
147 goto error;
148 memcpy(src[i], line, 10);
150 fprintf(stderr, "moggy.patterns: %d patterns loaded\n", i);
151 fclose(f);
152 return true;
153 error:
154 fprintf(stderr, "Error loading moggy.patterns.\n");
155 fclose(f);
156 return false;
159 void
160 pattern3s_init(struct pattern3s *p, char src[][11], int src_n)
162 char nsrc[src_n][11];
164 if (!patterns_load(nsrc, src_n, "moggy.patterns")) {
165 /* Use default pattern set. */
166 for (int i = 0; i < src_n; i++)
167 strcpy(nsrc[i], src[i]);
170 patterns_gen(p, nsrc, src_n);