patternGroup.cc Source File

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
patternGroup.cc
Go to the documentation of this file.
1 /* patternGroup.cc
2 */
3 #include "osl/rating/group/patternGroup.h"
4 #include <sstream>
5
6 osl::rating::PatternGroup::PatternGroup(Direction d, Direction d2)
7 : Group(name(d, d2)), direction(d), direction2(d2)
8 {
9 for (int attack=0; attack<3; ++attack) {
10 for (int defense=0; defense<3; ++defense) {
11 for (int s=PTYPE_PIECE_MIN; s<= PTYPE_MAX; ++s) {
12 for (int t=PTYPE_PIECE_MIN; t<= PTYPE_MAX; ++t) {
13 push_back(new Pattern(d, d2, static_cast<Ptype>(s), static_cast<Ptype>(t), true, attack, defense));
14 push_back(new Pattern(d, d2, static_cast<Ptype>(s), static_cast<Ptype>(t), false, attack, defense));
15 }
16 push_back(new Pattern(d, d2, static_cast<Ptype>(s), PTYPE_EMPTY, true, attack, defense));
17 push_back(new Pattern(d, d2, static_cast<Ptype>(s), PTYPE_EDGE, true, attack, defense)); // redundant
18 }
19 }
20 }
21 target_table.fill(0);
22 for (int x=1; x<=9; ++x) {
23 for (int y=1; y<=9; ++y) {
24 const Square src(x,y);
25 const Square target_b = Pattern::nextSquare(BLACK, src, direction, direction2);
26 const Square target_w = Pattern::nextSquare(WHITE, src, direction, direction2);
27 target_table[playerToIndex(BLACK)][src.index()] = target_b.uintValue();
28 target_table[playerToIndex(WHITE)][src.index()] = target_w.uintValue();
29 }
30 }
31 }
32
33 int osl::rating::PatternGroup::findMatch(const NumEffectState& state, Move move, const RatingEnv& env) const
34 {
35 const Ptype self = move.ptype();
36 const Square position
37 = Square::makeDirect(target_table[playerToIndex(move.player())][move.to().index()]);
38 assert(position == Pattern::nextSquare(move, direction, direction2));
39 const Piece p = (position == move.from()) ? Piece::EMPTY() : state.pieceAt(position);
40 const Ptype target = p.ptype();
41 if (env.pattern_cache[position.index()] < 0)
42 env.pattern_cache[position.index()] = CountEffect2::index(state, position, env)
43 * (PTYPE_MAX+1-PTYPE_PIECE_MIN) * ((PTYPE_MAX+1 - PTYPE_PIECE_MIN)*2 +2);
44 const int base = env.pattern_cache[position.index()];
45
46 int index = base + (self - PTYPE_PIECE_MIN)*((PTYPE_MAX+1 - PTYPE_PIECE_MIN)*2 +2);
47 if (!isPiece(target)) {
48 index += (PTYPE_MAX+1-PTYPE_PIECE_MIN)*2 + (target == PTYPE_EMPTY ? 0 : 1);
49 }
50 else {
51 index += (target - PTYPE_PIECE_MIN)*2 + (p.owner() != move.player());
52 }
53 return index;
54 }
55
56 std::string osl::rating::PatternGroup::name(Direction direction, Direction direction2)
57 {
58 std::ostringstream ss;
59 ss << "Pattern" << direction;
60 if (direction2 != Pattern::INVALID)
61 ss << direction2;
62 return ss.str();
63 }
64
65 const osl::CArray<osl::Direction,4> osl::rating::PatternLongGroup::rook_direction4 = {{ U, L, D, R }};
66 const osl::CArray<osl::Direction,4> osl::rating::PatternLongGroup::bishop_direction4 = {{ UL, DL, DR, UR }};
67
68 std::string osl::rating::PatternLongGroup::name(int direction_id)
69 {
70 std::ostringstream ss;
71 ss << "PatLong" << direction_id;
72 return ss.str();
73 }
74
75 osl::rating::PatternLongGroup::PatternLongGroup(int d)
76 : Group(name(d)), direction_id(d)
77 {
78 const CArray<Ptype,5> self_list = {{ ROOK, PROOK, BISHOP, PBISHOP, LANCE }};
79 for (int s=0; s<((d == 0) ? 5 : 4); ++s) {
80 const Ptype self = self_list[s];
81 const Direction direction = makeDirection(self);
82 for (int attack=0; attack<3; ++attack) {
83 for (int defense=0; defense<3; ++defense) {
84 for (int t=PTYPE_PIECE_MIN; t<= PTYPE_MAX; ++t) {
85 push_back(new PatternLong(direction, self, LongTarget(static_cast<Ptype>(t), true, true, attack, defense)));
86 push_back(new PatternLong(direction, self, LongTarget(static_cast<Ptype>(t), true, false, attack, defense)));
87 push_back(new PatternLong(direction, self, LongTarget(static_cast<Ptype>(t), false, true, attack, defense)));
88 push_back(new PatternLong(direction, self, LongTarget(static_cast<Ptype>(t), false, false, attack, defense)));
89 }
90 push_back(new PatternLong(direction, self, LongTarget(PTYPE_EMPTY, true, true, attack, defense)));
91 push_back(new PatternLong(direction, self, LongTarget(PTYPE_EMPTY, false, true, attack, defense)));
92 }
93 }
94 push_back(new PatternLong(direction, self, LongTarget(PTYPE_EDGE, true, true, 0, 0)));
95 }
96 }
97
98 int osl::rating::PatternLongGroup::findMatch(const NumEffectState& state, Move move, const RatingEnv& env) const
99 {
100 const size_t unit = ((PTYPE_MAX+1-PTYPE_PIECE_MIN)*4+2)*9+1;
101 const Ptype self = move.ptype();
102 int base = 0;
103 switch (self) {
104 case ROOK:
105 break;
106 case PROOK:
107 base += unit; break;
108 case BISHOP:
109 base += unit*2; break;
110 case PBISHOP:
111 base += unit*3; break;
112 case LANCE:
113 if (direction_id != 0)
114 return -1;
115 base += unit*4; break;
116 default:
117 return -1;
118 }
119 const Direction direction = makeDirection(self);
120 const PieceSquare pp = PatternLong::find(state, move, direction);
121
122 int index = base;
123 if (pp.first.isEdge()) {
124 index += unit - 1;
125 } else {
126 index += ((PTYPE_MAX+1-PTYPE_PIECE_MIN)*4+2)*CountEffect2::index(state, pp.second, env);
127 if (pp.first.isEmpty()) {
128 index += (PTYPE_MAX+1-PTYPE_PIECE_MIN)*4;
129 index += ! LongTarget::isPromotable(move, pp.second);
130 }
131 else {
132 assert(pp.first.isPiece());
133 index += (pp.first.ptype()-PTYPE_PIECE_MIN)*4;
134 index += (! LongTarget::isPromotable(move, pp.second))*2;
135 index += (pp.first.owner() != move.player());
136 }
137 }
138 return index;
139 }
140
141 std::string osl::rating::PatternLongGroup2::name(int direction_id)
142 {
143 std::ostringstream ss;
144 ss << "PatLong2" << direction_id;
145 return ss.str();
146 }
147
148 osl::rating::PatternLongGroup2::PatternLongGroup2(int d)
149 : Group(name(d)), direction_id(d)
150 {
151 const CArray<Ptype,5> self_list = {{ ROOK, PROOK, BISHOP, PBISHOP, LANCE }};
152 for (int s=0; s<((d == 0) ? 5 : 4); ++s) {
153 const Ptype self = self_list[s];
154 const Direction direction = makeDirection(self);
155 for (int t=PTYPE_PIECE_MIN; t<= PTYPE_MAX; ++t) {
156 push_back(new PatternLong2(direction, self, LongTarget2(static_cast<Ptype>(t), true)));
157 push_back(new PatternLong2(direction, self, LongTarget2(static_cast<Ptype>(t), false)));
158 }
159 push_back(new PatternLong2(direction, self, LongTarget2(PTYPE_EDGE, true)));
160 }
161 }
162
163 int osl::rating::PatternLongGroup2::findMatch(const NumEffectState& state, Move move, const RatingEnv& ) const
164 {
165 const size_t unit = (PTYPE_MAX+1-PTYPE_PIECE_MIN)*2+1;
166 const Ptype self = move.ptype();
167 int base = 0;
168 switch (self) {
169 case ROOK:
170 break;
171 case PROOK:
172 base += unit; break;
173 case BISHOP:
174 base += unit*2; break;
175 case PBISHOP:
176 base += unit*3; break;
177 case LANCE:
178 if (direction_id != 0)
179 return -1;
180 base += unit*4; break;
181 default:
182 return -1;
183 }
184 const Direction direction = makeDirection(self);
185 const Piece p = PatternLong2::find(state, move, direction);
186
187 int index = base;
188 if (! p.isPiece()) {
189 index += unit - 1;
190 } else {
191 assert(p.isPiece());
192 index += (p.ptype()-PTYPE_PIECE_MIN)*2;
193 index += (p.owner() != move.player());
194 }
195 return index;
196 }
197
198 osl::rating::
199 PatternBlockGroup::PatternBlockGroup(Ptype a)
200 : Group(std::string("PatternBlock")+Ptype_Table.getCsaName(a)), attacker(a)
201 {
202 assert(a == LANCE || a == ROOK || a == BISHOP);
203 for (int s=PTYPE_PIECE_MIN; s<=PTYPE_MAX; ++s) {
204 const Ptype self = static_cast<Ptype>(s);
205 for (int attack=0; attack<3; ++attack) {
206 for (int defense=0; defense<3; ++defense) {
207 for (int t=PTYPE_PIECE_MIN; t<= PTYPE_MAX; ++t) {
208 push_back(new PatternBlock(self, a, LongTarget(static_cast<Ptype>(t), true, true, attack, defense)));
209 push_back(new PatternBlock(self, a, LongTarget(static_cast<Ptype>(t), true, false, attack, defense)));
210 push_back(new PatternBlock(self, a, LongTarget(static_cast<Ptype>(t), false, true, attack, defense)));
211 push_back(new PatternBlock(self, a, LongTarget(static_cast<Ptype>(t), false, false, attack, defense)));
212 }
213 push_back(new PatternBlock(self, a, LongTarget(PTYPE_EMPTY, true, true, attack, defense)));
214 push_back(new PatternBlock(self, a, LongTarget(PTYPE_EMPTY, false, true, attack, defense)));
215 }
216 }
217 }
218 }
219
220 int osl::rating::
221 PatternBlockGroup::findMatch(const NumEffectState& state, Move move, const RatingEnv& env) const
222 {
223 const size_t unit = ((PTYPE_MAX+1-PTYPE_PIECE_MIN)*4+2)*9;
224 const PieceSquare pp = PatternBlock::find(state, move, attacker);
225 if (pp.first.isEdge())
226 return -1;
227
228 int index = (move.ptype() - PTYPE_PIECE_MIN)*unit;
229 index += ((PTYPE_MAX+1-PTYPE_PIECE_MIN)*4+2)*CountEffect2::index(state, pp.second, env);
230 if (pp.first.isEmpty()) {
231 index += (PTYPE_MAX+1-PTYPE_PIECE_MIN)*4;
232 index += ! pp.second.canPromote(alt(state.turn()));
233 }
234 else {
235 assert(pp.first.isPiece());
236 index += (pp.first.ptype()-PTYPE_PIECE_MIN)*4;
237 index += (! pp.second.canPromote(pp.first.isPiece() ? alt(pp.first.owner()) : alt(move.player())))*2;
238 index += (pp.first.owner() != move.player());
239 }
240 return index;
241 }
242
243 /* ------------------------------------------------------------------------- */
244 // ;;; Local Variables:
245 // ;;; mode:c++
246 // ;;; c-basic-offset:2
247 // ;;; End: