/nix/store/rr0r40cw3mzzbz0sxrnkvnrjpzb82wpc-j27cjzgh5nmq3y8k6hwa6zpi3qgcjia8-source/src/analysis/util.cc
Line | Count | Source |
1 | | #include "analysis/util.h" |
2 | | |
3 | | #include <algorithm> |
4 | | #include <cstdlib> |
5 | | #include <map> |
6 | | #include <vector> |
7 | | |
8 | | #include "analysis/analysis.h" |
9 | | #include "types/hand.h" |
10 | | #include "types/pieces.h" |
11 | | #include "types/piecetype.h" |
12 | | #include "types/sets.h" |
13 | | |
14 | | namespace mahjong { |
15 | | namespace { |
16 | | const std::vector<Piece> kPieceSet{ |
17 | | kOneBamboo, kTwoBamboo, kThreeBamboo, kFourBamboo, |
18 | | kFiveBamboo, kSixBamboo, kSevenBamboo, kEightBamboo, |
19 | | kNineBamboo, kOnePin, kTwoPin, kThreePin, |
20 | | kFourPin, kFivePin, kSixPin, kSevenPin, |
21 | | kEightPin, kNinePin, kOneCharacter, kNineCharacter, |
22 | | kTwoCharacter, kThreeCharacter, kFourCharacter, kFiveCharacter, |
23 | | kSixCharacter, kSevenCharacter, kEightCharacter, kWhiteDragon, |
24 | | kGreenDragon, kRedDragon, kEastWind, kSouthWind, |
25 | | kNorthWind, kWestWind, |
26 | | }; |
27 | | } // namespace |
28 | | |
29 | 2 | std::vector<Piece> completeSet(const Piece a, const Piece b) { |
30 | 2 | if (a.getSuit() != b.getSuit()) { |
31 | 0 | return {}; |
32 | 0 | } |
33 | 2 | const int piece_diff = static_cast<int>(a.getPieceNum()) - b.getPieceNum(); |
34 | 2 | if (std::abs(piece_diff) > 2) { |
35 | 0 | return {}; |
36 | 0 | } |
37 | 2 | if (std::abs(piece_diff) != 1) { |
38 | | // piece_diff: -2 / 2 |
39 | | // piece_diff >> 1: -1 / 1 |
40 | | // b + piece_diff >> 1: c |
41 | | // a - b == -2: b (c) a |
42 | | // a - b == 2: a (c) b |
43 | 1 | return {b + (piece_diff >> 1)}; |
44 | 1 | } |
45 | 1 | std::vector<Piece> waits; |
46 | 1 | if (a + piece_diff != kError) { |
47 | 1 | waits.push_back(a + piece_diff); |
48 | 1 | } |
49 | 1 | if (b - piece_diff != kError) { |
50 | 1 | waits.push_back(b - piece_diff); |
51 | 1 | } |
52 | 1 | return waits; |
53 | 2 | } |
54 | | |
55 | 3 | std::vector<Piece> getWaits(const Hand& hand) { |
56 | | // 13, 10, 7, 4, 1 are possible piece counts for non-melded tiles |
57 | 3 | if (hand.live_count == 0 || hand.live_count % 3 != 1) { |
58 | 0 | return {}; |
59 | 0 | } |
60 | 3 | constexpr int kMaxSingles = 2; |
61 | 3 | std::vector<Piece> waits; |
62 | 3 | for (const Branch& branch : AnalyzeHand(hand)) { |
63 | 3 | if (branch.singles.size() > kMaxSingles) { |
64 | 0 | continue; |
65 | 0 | } |
66 | 3 | if (branch.singles.size() == 1 && |
67 | 3 | (branch.pairs.empty() || branch.pairs.size() == 6)) { |
68 | 1 | waits.emplace_back(branch.singles.front()); |
69 | 2 | } else if (branch.singles.size() == 2) { |
70 | 2 | waits.append_range(completeSet(branch.singles[0], branch.singles[1])); |
71 | 2 | } |
72 | 3 | } |
73 | 3 | return waits; |
74 | 3 | } |
75 | | |
76 | 3 | std::vector<Piece> getWaits(const Hand& hand, const Piece& piece) { |
77 | 3 | Hand new_hand = hand; |
78 | | // This will move all pieces matching piece to the end of the array, but |
79 | | // we will only decrease the count by 1 so only a single piece will be |
80 | | // removed. |
81 | 3 | std::ranges::remove(new_hand.live, piece); |
82 | 3 | new_hand.live_count--; |
83 | 3 | return getWaits(new_hand); |
84 | 3 | } |
85 | | |
86 | 10 | std::map<Piece, std::vector<Piece>> getPossibleWaits(const Hand& hand) { |
87 | | // 14, 11, 8, 5, 2 are the possible piece counts for non-melded tiles. |
88 | 10 | if (hand.live_count == 0 || hand.live_count % 3 != 2) { |
89 | 0 | return {}; |
90 | 0 | } |
91 | 10 | constexpr int kMaxSingles = 3; |
92 | 10 | std::map<Piece, std::vector<Piece>> waits; |
93 | 12 | for (const Branch& branch : AnalyzeHand(hand)) { |
94 | 12 | if (branch.singles.size() > kMaxSingles) { |
95 | 12 | continue; |
96 | 12 | } |
97 | 0 | if (branch.singles.size() == 2 && |
98 | 0 | (branch.pairs.empty() || branch.pairs.size() == 6)) { |
99 | 0 | waits[branch.singles[0]].emplace_back(branch.singles[1]); |
100 | 0 | waits[branch.singles[1]].emplace_back(branch.singles[0]); |
101 | 0 | } else if (branch.singles.size() == 3) { |
102 | 0 | waits[branch.singles[0]].append_range( |
103 | 0 | completeSet(branch.singles[1], branch.singles[2])); |
104 | 0 | waits[branch.singles[1]].append_range( |
105 | 0 | completeSet(branch.singles[2], branch.singles[0])); |
106 | 0 | waits[branch.singles[2]].append_range( |
107 | 0 | completeSet(branch.singles[0], branch.singles[1])); |
108 | 0 | } |
109 | 0 | } |
110 | 10 | return waits; |
111 | 10 | } |
112 | | |
113 | 0 | bool CheckBranch(const Branch& branch, const SetCheckFunc& func) { |
114 | 0 | return std::ranges::all_of( |
115 | 0 | branch.chis, |
116 | 0 | [&func](Piece p) { return func(SetType::kChi, p); }) && |
117 | 0 | std::ranges::all_of( |
118 | 0 | branch.pons, |
119 | 0 | [&func](Piece p) { return func(SetType::kPon, p); }) && |
120 | 0 | std::ranges::all_of( |
121 | 0 | branch.kans, |
122 | 0 | [&func](Piece p) { return func(SetType::kKan, p); }) && |
123 | 0 | std::ranges::all_of( |
124 | 0 | branch.pairs, |
125 | 0 | [&func](Piece p) { return func(SetType::kSingle, p); }) && |
126 | 0 | std::ranges::all_of(branch.singles, [&func](Piece p) { |
127 | 0 | return func(SetType::kPair, p); |
128 | 0 | }); |
129 | 0 | } |
130 | | |
131 | | } // namespace mahjong |