1 #ifndef ZZGO_PATTERN3_H
2 #define ZZGO_PATTERN3_H
4 /* Fast matching of simple 3x3 patterns. */
9 /* (Note that this is completely independent from the general pattern
10 * matching infrastructure in pattern.[ch]. This is fast and simple.) */
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 */
22 /* Source pattern encoding:
23 * X: black; O: white; .: empty; #: edge
24 * x: !black; o: !white; ?: any
26 * extra X: pattern valid only for one side;
27 * middle point ignored. */
29 void pattern3s_init(struct pattern3s
*p
, char src
[][11], int src_n
);
31 /* Compute pattern3 hash at local position. */
32 static int pattern3_hash(struct board
*b
, coord_t c
);
34 /* Check if we match any pattern centered on given move; includes
36 static bool test_pattern3_here(struct pattern3s
*p
, struct board
*b
, struct move
*m
);
40 pattern3_hash(struct board
*b
, coord_t c
)
43 int x
= coord_x(c
, b
), y
= coord_y(c
, b
);
44 pat
|= (board_atxy(b
, x
- 1, y
- 1) << 14)
45 | (board_atxy(b
, x
, y
- 1) << 12)
46 | (board_atxy(b
, x
+ 1, y
- 1) << 10);
47 pat
|= (board_atxy(b
, x
- 1, y
) << 8)
48 | (board_atxy(b
, x
+ 1, y
) << 6);
49 pat
|= (board_atxy(b
, x
- 1, y
+ 1) << 4)
50 | (board_atxy(b
, x
, y
+ 1) << 2)
51 | (board_atxy(b
, x
+ 1, y
+ 1));
55 /* TODO: Make use of the incremental spatial matching infrastructure
58 test_pattern3_here(struct pattern3s
*p
, struct board
*b
, struct move
*m
)
61 int pat
= b
->pat3
[m
->coord
];
63 int pat
= pattern3_hash(b
, m
->coord
);
65 //fprintf(stderr, "(%d,%d) hashtable[%04x] = %d\n", x, y, pat, p->hash[pat]);
66 return (p
->hash
[pat
] & m
->color
) && !is_bad_selfatari(b
, m
->color
, m
->coord
);