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 3x3 pattern centered on given move. */
35 static bool pattern3_move_here(struct pattern3s
*p
, struct board
*b
, struct move
*m
);
37 /* Generate all transpositions of given pattern, stored in an
39 void pattern3_transpose(int pat
, int (*transp
)[8]);
41 /* Reverse pattern to opposite color assignment. */
42 static int pattern3_reverse(int pat
);
46 pattern3_hash(struct board
*b
, coord_t c
)
49 int x
= coord_x(c
, b
), y
= coord_y(c
, b
);
50 pat
|= (board_atxy(b
, x
- 1, y
- 1) << 14)
51 | (board_atxy(b
, x
, y
- 1) << 12)
52 | (board_atxy(b
, x
+ 1, y
- 1) << 10);
53 pat
|= (board_atxy(b
, x
- 1, y
) << 8)
54 | (board_atxy(b
, x
+ 1, y
) << 6);
55 pat
|= (board_atxy(b
, x
- 1, y
+ 1) << 4)
56 | (board_atxy(b
, x
, y
+ 1) << 2)
57 | (board_atxy(b
, x
+ 1, y
+ 1));
62 pattern3_move_here(struct pattern3s
*p
, struct board
*b
, struct move
*m
)
65 int pat
= b
->pat3
[m
->coord
];
67 int pat
= pattern3_hash(b
, m
->coord
);
69 return (p
->hash
[pat
] & m
->color
);
73 pattern3_reverse(int pat
)
75 /* Reverse color assignment - achieved by swapping odd and even bits */
76 return ((pat
>> 1) & 0x5555) | ((pat
& 0x5555) << 1);