/nix/store/rr0r40cw3mzzbz0sxrnkvnrjpzb82wpc-j27cjzgh5nmq3y8k6hwa6zpi3qgcjia8-source/src/analysis/handtree.cc
Line | Count | Source |
1 | | #include "analysis/handtree.h" |
2 | | |
3 | | #include <algorithm> |
4 | | #include <fstream> |
5 | | #include <iostream> |
6 | | #include <map> |
7 | | #include <memory> |
8 | | #include <vector> |
9 | | |
10 | | #include "analysis/handnode.h" |
11 | | #include "types/pieces.h" |
12 | | #include "types/piecetype.h" |
13 | | #include "types/sets.h" |
14 | | |
15 | | namespace mahjong { |
16 | | namespace { |
17 | | |
18 | | const int kMaxPossible = 14; |
19 | 21.5k | bool possibleChiForward(const std::map<Piece, int>& counts, Piece p) { |
20 | 21.5k | if (p.isHonor() || !counts.contains(p) || !counts.contains(p + 1) || |
21 | 21.5k | !counts.contains(p + 2)) { |
22 | 18.5k | return false; |
23 | 18.5k | } |
24 | 3.05k | return counts.at(p) > 0 && counts.at(p + 1) > 0 && counts.at(p + 2) > 0; |
25 | 21.5k | } |
26 | | |
27 | 6.68k | int possibleChis(const std::map<Piece, int>& counts, Piece p) { |
28 | 6.68k | int chi_count = possibleChiForward(counts, p) ? 1 : 0; |
29 | 6.68k | chi_count += possibleChiForward(counts, p - 1) ? 1 : 0; |
30 | 6.68k | chi_count += possibleChiForward(counts, p - 2) ? 1 : 0; |
31 | 6.68k | return chi_count; |
32 | 6.68k | } |
33 | | |
34 | 385 | bool anyPossibleChi(const std::map<Piece, int>& counts, Piece p) { |
35 | 385 | return possibleChis(counts, p) > 0; |
36 | 385 | } |
37 | | |
38 | 6.93k | bool possiblePair(const std::map<Piece, int>& counts, Piece p) { |
39 | 6.93k | return counts.contains(p) && counts.at(p) >= 2; |
40 | 6.93k | } |
41 | | |
42 | 6.93k | bool possiblePon(const std::map<Piece, int>& counts, Piece p) { |
43 | 6.93k | return counts.contains(p) && counts.at(p) >= 3; |
44 | 6.93k | } |
45 | | |
46 | 2.63k | Piece updatePossibilities(Breakdown* b) { |
47 | 2.63k | b->possibilities.clear(); |
48 | 2.63k | int min_possible = kMaxPossible; |
49 | 2.63k | Piece min_possible_piece = kError; |
50 | 6.30k | for (const auto& piece : b->pieces) { |
51 | 6.30k | b->possibilities[piece] += possibleChis(b->counts, piece); |
52 | 6.30k | if (possiblePair(b->counts, piece)) { |
53 | 2.64k | b->possibilities[piece]++; |
54 | 2.64k | } |
55 | 6.30k | if (possiblePon(b->counts, piece)) { |
56 | 1.28k | b->possibilities[piece]++; |
57 | 1.28k | } |
58 | 6.30k | if (b->possibilities[piece] < min_possible) { |
59 | 2.03k | min_possible = b->possibilities[piece]; |
60 | 2.03k | min_possible_piece = piece; |
61 | 2.03k | } |
62 | 6.30k | } |
63 | 2.63k | return min_possible_piece; |
64 | 2.63k | } |
65 | | |
66 | 187 | void breakdownForwardChi(Breakdown* b, Piece piece) { |
67 | 748 | for (int i = 0; i < 3; i++) { |
68 | 561 | b->counts[piece + i]--; |
69 | 561 | if (b->counts[piece + i] == 0) { |
70 | 470 | std::erase(b->pieces, piece + i); |
71 | 470 | } |
72 | 561 | } |
73 | 187 | b->currentNode = b->currentNode->addLeaf(piece, SetType::kChi, b->id++); |
74 | 187 | } |
75 | | |
76 | 402 | void breakdownPon(Breakdown* b, Piece piece) { |
77 | 402 | b->counts[piece] -= 3; |
78 | 402 | if (b->counts[piece] == 0) { |
79 | 392 | std::erase(b->pieces, piece); |
80 | 392 | } |
81 | 402 | b->currentNode = b->currentNode->addLeaf(piece, SetType::kPon, b->id++); |
82 | 402 | } |
83 | | |
84 | 634 | void breakdownPair(Breakdown* b, Piece piece) { |
85 | 634 | b->counts[piece] -= 2; |
86 | 634 | if (b->counts[piece] == 0) { |
87 | 232 | std::erase(b->pieces, piece); |
88 | 232 | } |
89 | 634 | b->currentNode = b->currentNode->addLeaf(piece, SetType::kPair, b->id++); |
90 | 634 | } |
91 | | |
92 | 850 | void breakdownSingle(Breakdown* b, Piece piece) { |
93 | 850 | b->currentNode = b->currentNode->addLeaf(piece, SetType::kSingle, b->id++); |
94 | | |
95 | 850 | b->counts[piece]--; |
96 | 850 | if (b->counts[piece] == 0) { |
97 | 850 | std::erase(b->pieces, piece); |
98 | 850 | } |
99 | 850 | } |
100 | | |
101 | 419 | void resetCounts(Breakdown* b, const Node* target) { |
102 | 419 | if (b->currentNode == nullptr) { |
103 | 0 | std::cerr << "reset Failure: current node nullptr." << '\n'; |
104 | 0 | std::ofstream os("error.gv"); |
105 | 0 | b->rootNode->DumpAsDot(os); |
106 | 0 | os.close(); |
107 | 0 | throw -1; |
108 | 0 | } |
109 | 1.34k | while (b->currentNode != target) { |
110 | 921 | if (b->currentNode->parent() == nullptr) { |
111 | 0 | std::cerr << "reset Failure: parent node nullptr." << '\n'; |
112 | 0 | std::ofstream os("error.gv"); |
113 | 0 | b->rootNode->DumpAsDot(os); |
114 | 0 | os.close(); |
115 | 0 | throw -2; |
116 | 0 | } |
117 | 921 | if (b->currentNode->type() == SetType::kChi) { |
118 | 136 | for (int i = 0; i < 3; i++) { |
119 | 102 | if (b->counts[b->currentNode->start() + i] == 0) { |
120 | 51 | b->pieces.push_back(Piece{b->currentNode->start() + i}); |
121 | 51 | std::ranges::sort(b->pieces); |
122 | 51 | } |
123 | 102 | b->counts[b->currentNode->start() + i]++; |
124 | 102 | } |
125 | 887 | } else { |
126 | 887 | if (b->counts[b->currentNode->start()] == 0) { |
127 | 653 | b->pieces.emplace_back(b->currentNode->start()); |
128 | 653 | std::ranges::sort(b->pieces); |
129 | 653 | } |
130 | 887 | if (b->currentNode->type() == SetType::kSingle) { |
131 | 234 | b->counts[b->currentNode->start()]++; |
132 | 234 | } |
133 | 887 | if (b->currentNode->type() == SetType::kPair) { |
134 | 251 | b->counts[b->currentNode->start()] += 2; |
135 | 251 | } |
136 | 887 | if (b->currentNode->type() == SetType::kPon) { |
137 | 402 | b->counts[b->currentNode->start()] += 3; |
138 | 402 | } |
139 | 887 | } |
140 | 921 | b->currentNode = b->currentNode->parent(); |
141 | 921 | } |
142 | 419 | } |
143 | | } // namespace |
144 | | |
145 | | // NOLINTNEXTLINE(misc-no-recursion) |
146 | 980 | void driver(Breakdown* b) { |
147 | | // - Always process the tile into the fewest groupings first |
148 | | // - If a tile has only one way to be grouped, use it |
149 | | // - If a tile has multiple ways, try each possibility |
150 | | // - Continue until all tiles are in groups |
151 | | |
152 | | // While there are ungrouped pieces, get the piece with minimum possibilities |
153 | 2.63k | for (Piece current_piece = updatePossibilities(b); !b->pieces.empty(); |
154 | 1.65k | current_piece = updatePossibilities(b)) { |
155 | 1.65k | if (b->possibilities[current_piece] == 0) { |
156 | | // No valid grouping possible, single piece |
157 | 850 | breakdownSingle(b, current_piece); |
158 | 850 | continue; |
159 | 850 | } |
160 | | |
161 | 804 | if (b->possibilities[current_piece] == 1) { |
162 | | // One way to be grouped, use it |
163 | 385 | if (anyPossibleChi(b->counts, current_piece)) { |
164 | | // A chi is possible, determine how it can be a part of one |
165 | 281 | for (int i = 0; i < 3; i++) { |
166 | 281 | const Piece chi_start = current_piece - i; |
167 | | // Check if we can start a chi with this piece |
168 | 281 | if (possibleChiForward(b->counts, chi_start)) { |
169 | | // Find the position of chi_start in the pieces vector |
170 | 169 | auto piece_itr = std::ranges::find(b->pieces, chi_start); |
171 | 169 | if (piece_itr != b->pieces.end()) { |
172 | 169 | breakdownForwardChi(b, *piece_itr); |
173 | 169 | } |
174 | 169 | break; |
175 | 169 | } |
176 | 281 | } |
177 | 169 | continue; |
178 | 169 | } |
179 | 216 | if (possiblePon(b->counts, current_piece)) { |
180 | | // A pon is possible |
181 | 0 | breakdownPon(b, current_piece); |
182 | 0 | continue; |
183 | 0 | } |
184 | 216 | if (possiblePair(b->counts, current_piece)) { |
185 | | // A pair is possible |
186 | 216 | breakdownPair(b, current_piece); |
187 | 216 | continue; |
188 | 216 | } |
189 | 216 | } |
190 | 419 | if (b->possibilities[current_piece] >= 2) { |
191 | | // Tile has multiple groups, we need to check branches |
192 | | |
193 | | // Save current position in tree so we can backtrack |
194 | 419 | auto* current = b->currentNode; |
195 | | |
196 | | // Check grouping possibilities |
197 | 419 | const bool can_chi_start = possibleChiForward(b->counts, current_piece); |
198 | | |
199 | 419 | bool can_chi_middle = false; |
200 | 419 | Piece chi_middle_piece; |
201 | 419 | if (possibleChiForward(b->counts, current_piece - 1)) { |
202 | 2 | const Piece chi_start = current_piece - 1; |
203 | 2 | auto it = std::ranges::find(b->pieces, chi_start); |
204 | 2 | if (it != b->pieces.end()) { |
205 | 2 | can_chi_middle = true; |
206 | 2 | chi_middle_piece = *it; |
207 | 2 | } |
208 | 2 | } |
209 | | |
210 | 419 | bool can_chi_end = false; |
211 | 419 | Piece chi_end_piece; |
212 | 419 | if (possibleChiForward(b->counts, current_piece - 2)) { |
213 | 1 | const Piece chi_start = current_piece - 2; |
214 | 1 | auto it = std::ranges::find(b->pieces, chi_start); |
215 | 1 | if (it != b->pieces.end()) { |
216 | 1 | can_chi_end = true; |
217 | 1 | chi_end_piece = *it; |
218 | 1 | } |
219 | 1 | } |
220 | | |
221 | 419 | const bool can_pon = possiblePon(b->counts, current_piece); |
222 | 419 | const bool can_pair = possiblePair(b->counts, current_piece); |
223 | | |
224 | | // Count total branches that will actually execute |
225 | 419 | int total_branches = 0; |
226 | 419 | if (can_chi_start) { |
227 | 15 | total_branches++; |
228 | 15 | } |
229 | 419 | if (can_chi_middle) { |
230 | 2 | total_branches++; |
231 | 2 | } |
232 | 419 | if (can_chi_end) { |
233 | 1 | total_branches++; |
234 | 1 | } |
235 | 419 | if (can_pon) { |
236 | 402 | total_branches++; |
237 | 402 | } |
238 | 419 | if (can_pair) { |
239 | 418 | total_branches++; |
240 | 418 | } |
241 | | |
242 | | // Execute branches |
243 | 419 | int current_branch = 0; |
244 | | |
245 | 419 | if (can_chi_start) { |
246 | | // Can be the beginning of a chi |
247 | 15 | current_branch++; |
248 | 15 | breakdownForwardChi(b, current_piece); |
249 | 15 | driver(b); |
250 | 15 | if (current_branch < total_branches) { |
251 | 15 | resetCounts(b, current); |
252 | 15 | } |
253 | 15 | } |
254 | | |
255 | 419 | if (can_chi_middle) { |
256 | | // Can be the middle of a chi |
257 | 2 | current_branch++; |
258 | 2 | breakdownForwardChi(b, chi_middle_piece); |
259 | 2 | driver(b); |
260 | 2 | if (current_branch < total_branches) { |
261 | 2 | resetCounts(b, current); |
262 | 2 | } |
263 | 2 | } |
264 | | |
265 | 419 | if (can_chi_end) { |
266 | | // Can be the end of a chi |
267 | 1 | current_branch++; |
268 | 1 | breakdownForwardChi(b, chi_end_piece); |
269 | 1 | driver(b); |
270 | 1 | if (current_branch < total_branches) { |
271 | 0 | resetCounts(b, current); |
272 | 0 | } |
273 | 1 | } |
274 | | |
275 | 419 | if (can_pon) { |
276 | | // Can be a pon |
277 | 402 | current_branch++; |
278 | 402 | breakdownPon(b, current_piece); |
279 | 402 | driver(b); |
280 | 402 | if (current_branch < total_branches) { |
281 | 402 | resetCounts(b, current); |
282 | 402 | } |
283 | 402 | } |
284 | | |
285 | 419 | if (can_pair) { |
286 | | // Can be a pair |
287 | 418 | current_branch++; |
288 | 418 | breakdownPair(b, current_piece); |
289 | 418 | driver(b); |
290 | 418 | if (current_branch < total_branches) { |
291 | 0 | resetCounts(b, current); |
292 | 0 | } |
293 | 418 | } |
294 | 419 | } |
295 | 419 | } |
296 | 980 | } |
297 | | } // namespace mahjong |