Do not resign if we're so short of time that evaluation of best move
[pachi/json.git] / pattern3.c
blob39a945117d757ef22939dccfe57f45239e005b5e
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"
9 #include "tactics.h"
12 static void
13 pattern_record(char *table, char *str, int 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);
24 static int
25 pat_vmirror(int pat)
27 /* V mirror pattern; reverse order of 3-2-3 chunks */
28 return ((pat & 0xfc00) >> 10) | (pat & 0x03c0) | ((pat & 0x003f) << 10);
31 static int
32 pat_hmirror(int 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));
40 #undef rev3
41 #undef rev2
44 static int
45 pat_90rot(int pat)
47 /* Rotate by 90 degrees:
48 * 5 6 7 7 4 2
49 * 3 4 -> 6 1
50 * 0 1 2 5 3 0 */
51 /* I'm too lazy to optimize this :) */
52 int vals[8];
53 for (int i = 0; i < 8; i++)
54 vals[i] = (pat >> (i * 2)) & 0x3;
55 int vals2[8];
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];
59 int p2 = 0;
60 for (int i = 0; i < 8; i++)
61 p2 |= vals2[i] << (i * 2);
62 return p2;
65 void
66 pattern3_transpose(int pat, int (*transp)[8])
68 int i = 0;
69 (*transp)[i++] = pat;
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)));
79 static void
80 pattern_gen(struct pattern3s *p, int pat, char *src, int srclen, int fixed_color)
82 for (; srclen > 0; src++, srclen--) {
83 if (srclen == 5)
84 continue;
85 int patofs = (srclen > 5 ? srclen - 1 : srclen) - 1;
86 switch (*src) {
87 case '?':
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
93 return;
94 case 'x':
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
99 return;
100 case 'o':
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
105 return;
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 */
114 int transp[8];
115 pattern3_transpose(pat, &transp);
116 for (int i = 0; i < 8; i++)
117 pattern_record(p->hash, src - 9, transp[i], fixed_color);
120 static void
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]);
125 int fixed_color = 0;
126 switch (src[i][9]) {
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);
135 static bool
136 patterns_load(char src[][11], int src_n, char *filename)
138 FILE *f = fopen("moggy.patterns", "r");
139 if (!f) return false;
141 int i;
142 for (i = 0; i < src_n; i++) {
143 char line[32];
144 if (!fgets(line, sizeof(line), f))
145 goto error;
146 int l = strlen(line);
147 if (l != 10 + (line[l - 1] == '\n'))
148 goto error;
149 memcpy(src[i], line, 10);
151 fprintf(stderr, "moggy.patterns: %d patterns loaded\n", i);
152 fclose(f);
153 return true;
154 error:
155 fprintf(stderr, "Error loading moggy.patterns.\n");
156 fclose(f);
157 return false;
160 void
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);