test_pattern3_here: Public inline
[pachi/json.git] / pattern3.h
blobbba4b8460583031632a5860e82580c5d8add9356
1 #ifndef ZZGO_PATTERN3_H
2 #define ZZGO_PATTERN3_H
4 /* Fast matching of simple 3x3 patterns. */
6 #include "tactics.h"
8 /* (Note that this is completely independent from the general pattern
9 * matching infrastructure in pattern.[ch]. This is fast and simple.) */
11 struct board;
12 struct move;
14 struct pattern3s {
15 /* Hashtable: 2*8 bits (ignore middle point, 2 bits per intersection) */
16 /* Value: 0: no pattern, 1: black pattern,
17 * 2: white pattern, 3: both patterns */
18 char hash[65536];
21 /* Source pattern encoding:
22 * X: black; O: white; .: empty; #: edge
23 * x: !black; o: !white; ?: any
25 * extra X: pattern valid only for one side;
26 * middle point ignored. */
28 void pattern3s_init(struct pattern3s *p, char src[][11], int src_n);
30 /* Check if we match any pattern centered on given move. */
31 static bool test_pattern3_here(struct pattern3s *p, struct board *b, struct move *m);
34 /* TODO: Make use of the incremental spatial matching infrastructure
35 * in board.h? */
36 static inline bool
37 test_pattern3_here(struct pattern3s *p, struct board *b, struct move *m)
39 int pat = 0;
40 int x = coord_x(m->coord, b), y = coord_y(m->coord, b);
41 pat |= (board_atxy(b, x - 1, y - 1) << 14)
42 | (board_atxy(b, x, y - 1) << 12)
43 | (board_atxy(b, x + 1, y - 1) << 10);
44 pat |= (board_atxy(b, x - 1, y) << 8)
45 | (board_atxy(b, x + 1, y) << 6);
46 pat |= (board_atxy(b, x - 1, y + 1) << 4)
47 | (board_atxy(b, x, y + 1) << 2)
48 | (board_atxy(b, x + 1, y + 1));
49 //fprintf(stderr, "(%d,%d) hashtable[%04x] = %d\n", x, y, pat, p->hash[pat]);
50 return (p->hash[pat] & m->color) && !is_bad_selfatari(b, m->color, m->coord);
53 #endif