-
Notifications
You must be signed in to change notification settings - Fork 0
/
pattern.cpp
90 lines (83 loc) · 2.52 KB
/
pattern.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include "pattern.h"
#include "common.h"
Pattern::Pattern() {}
Pattern::~Pattern() {}
VecCoords Pattern::find_candidates(Board &b, VecCoords &candy) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (b.get_stone(i, j) == EMPTY)
continue;
find_pattern_inline(b, candy, i, j, 1, 0);
find_pattern_inline(b, candy, i, j, -1, 0);
find_pattern_inline(b, candy, i, j, 0, 1);
find_pattern_inline(b, candy, i, j, 0, -1);
find_pattern_inline(b, candy, i, j, 1, 1);
find_pattern_inline(b, candy, i, j, 1, -1);
find_pattern_inline(b, candy, i, j, -1, 1);
find_pattern_inline(b, candy, i, j, -1, -1);
}
}
refine_candy(candy, b);
return candy;
}
void Pattern::find_pattern_inline(Board &b, VecCoords &candy, int i, int j,
int di, int dj) {
find_pattern_each(b, candy, i, j, di, dj, "*?____");
find_pattern_each(b, candy, i, j, di, dj, "==?");
find_pattern_each(b, candy, i, j, di, dj, "||?");
find_pattern_each(b, candy, i, j, di, dj, "=?=");
find_pattern_each(b, candy, i, j, di, dj, "|?|");
find_pattern_each(b, candy, i, j, di, dj, "=_=?");
find_pattern_each(b, candy, i, j, di, dj, "|_|?");
find_pattern_each(b, candy, i, j, di, dj, "|||_?");
find_pattern_each(b, candy, i, j, di, dj, "===_?");
}
bool Pattern::find_pattern_each(Board &b, VecCoords &candy, int i, int j,
int di, int dj, string pt) {
int size = pt.length();
for (int s = 0; s < size; s++) {
int x = i + s * di;
int y = j + s * dj;
if (!b.in_range(x, y))
return false;
if (!match_stones(b, pt[s], x, y))
return false;
}
// collecting candidates
for (int s = 0; s < size; s++) {
int x = i + s * di;
int y = j + s * dj;
if (pt[s] == '?')
candy.push_back(make_pair(x, y));
}
return true;
}
bool Pattern::match_stones(Board &b, char ch, int i, int j) {
switch (ch) {
case '*':
return (b.get_stone(i, j) != EMPTY) ? true : false;
break;
case '_':
case '?':
return (b.get_stone(i, j) == EMPTY) ? true : false;
break;
case '=':
return (b.get_stone(i, j) == b.whose_turn()) ? true : false;
case '|':
return (b.get_stone(i, j) == b.last_turn()) ? true : false;
break;
default:
return false;
}
}
void Pattern::refine_candy(VecCoords &candy, Board &b) {
uniq_vec(candy);
for (auto it = candy.begin(); it != candy.end();) {
int i, j;
tie(i, j) = (*it);
if (b.check_3_3(i, j))
it = candy.erase(it);
else
++it;
}
}