patterns.gamma*: Regenerate w/ current code from GoGoD 2008-10,11
[pachi/t.git] / pattern3.h
blobaf670863855746bca6d43b49801955b5f60ccbd1
1 #ifndef ZZGO_PATTERN3_H
2 #define ZZGO_PATTERN3_H
4 /* Fast matching of simple 3x3 patterns. */
6 #include "board.h"
7 #include "tactics.h"
9 /* (Note that this is completely independent from the general pattern
10 * matching infrastructure in pattern.[ch]. This is fast and simple.) */
12 struct board;
13 struct move;
15 struct pattern3s {
16 /* Hashtable: 2*8 bits (ignore middle point, 2 bits per intersection) */
17 /* Value: 0: no pattern, 1: black pattern,
18 * 2: white pattern, 3: both patterns */
19 char hash[65536];
22 /* XXX: See <board.h> for hash3_t typedef. */
24 /* Source pattern encoding:
25 * X: black; O: white; .: empty; #: edge
26 * x: !black; o: !white; ?: any
28 * extra X: pattern valid only for one side;
29 * middle point ignored. */
31 void pattern3s_init(struct pattern3s *p, char src[][11], int src_n);
33 /* Compute pattern3 hash at local position. */
34 static hash3_t pattern3_hash(struct board *b, coord_t c);
36 /* Check if we match any 3x3 pattern centered on given move. */
37 static bool pattern3_move_here(struct pattern3s *p, struct board *b, struct move *m);
39 /* Generate all transpositions of given pattern, stored in an
40 * hash3_t[8] array. */
41 void pattern3_transpose(hash3_t pat, hash3_t (*transp)[8]);
43 /* Reverse pattern to opposite color assignment. */
44 static hash3_t pattern3_reverse(hash3_t pat);
47 static inline hash3_t
48 pattern3_hash(struct board *b, coord_t c)
50 hash3_t pat = 0;
51 int x = coord_x(c, b), y = coord_y(c, b);
52 pat |= (board_atxy(b, x - 1, y - 1) << 14)
53 | (board_atxy(b, x, y - 1) << 12)
54 | (board_atxy(b, x + 1, y - 1) << 10);
55 pat |= (board_atxy(b, x - 1, y) << 8)
56 | (board_atxy(b, x + 1, y) << 6);
57 pat |= (board_atxy(b, x - 1, y + 1) << 4)
58 | (board_atxy(b, x, y + 1) << 2)
59 | (board_atxy(b, x + 1, y + 1));
60 return pat;
63 static inline bool
64 pattern3_move_here(struct pattern3s *p, struct board *b, struct move *m)
66 #ifdef BOARD_PAT3
67 hash3_t pat = b->pat3[m->coord];
68 #else
69 hash3_t pat = pattern3_hash(b, m->coord);
70 #endif
71 return (p->hash[pat] & m->color);
74 static inline hash3_t
75 pattern3_reverse(hash3_t pat)
77 /* Reverse color assignment - achieved by swapping odd and even bits */
78 return ((pat >> 1) & 0x5555) | ((pat & 0x5555) << 1);
81 #endif