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 /* 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
);
48 pattern3_hash(struct board
*b
, coord_t c
)
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));
64 pattern3_move_here(struct pattern3s
*p
, struct board
*b
, struct move
*m
)
67 hash3_t pat
= b
->pat3
[m
->coord
];
69 hash3_t pat
= pattern3_hash(b
, m
->coord
);
71 return (p
->hash
[pat
] & m
->color
);
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);