summaryrefslogtreecommitdiff
path: root/server/hand.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'server/hand.cpp')
-rw-r--r--server/hand.cpp88
1 files changed, 87 insertions, 1 deletions
diff --git a/server/hand.cpp b/server/hand.cpp
index 07777fb..bc4fb7a 100644
--- a/server/hand.cpp
+++ b/server/hand.cpp
@@ -8,6 +8,14 @@ bool Hand::tenpai(const Tiles& tiles) {
return basic_format_tenpai(tiles);
}
+List<Sets> Hand::get_breakdowns(const Tiles& tiles) {
+ List<Sets> hands;
+
+ basic_format(tiles, hands);
+
+ return hands;
+}
+
bool Hand::basic_format(const Tiles& tiles, bool pair_eaten) {
if(!tiles) {
return true; // All tiles eaten.
@@ -39,6 +47,40 @@ bool Hand::basic_format(const Tiles& tiles, bool pair_eaten) {
return false;
}
+void Hand::basic_format(const Tiles& tiles, List<Sets>& hands, const Sets& hand, bool pair_eaten) {
+ if(!tiles) {
+ hands.push_back(hand);
+ return; // All tiles eaten.
+ }
+
+ Tiles rest;
+ Sets new_hand;
+
+ if(!pair_eaten) {
+ rest = tiles;
+ new_hand = hand;
+ if(eat_pair(rest, new_hand)) {
+ basic_format(rest, hands, new_hand, true);
+ }
+ }
+
+ if(tiles.size() < 3) {
+ return;
+ }
+
+ rest = tiles;
+ new_hand = hand;
+ if(eat_pon(rest, new_hand)) {
+ basic_format(rest, hands, new_hand, pair_eaten);
+ }
+
+ rest = tiles;
+ new_hand = hand;
+ if(eat_chi(rest, new_hand)) {
+ basic_format(rest, hands, new_hand, pair_eaten);
+ }
+}
+
bool Hand::basic_format_tenpai(const Tiles& tiles, bool pair_eaten, bool wait_eaten) {
if(!tiles) {
return true;
@@ -132,6 +174,15 @@ bool Hand::eat_pair(Tiles& tiles) {
return false;
}
+bool Hand::eat_pair(Tiles& tiles, Sets& hand) {
+ if(tiles[0] == tiles[1]) {
+ hand.push_back(Set(Set::Pair, Tiles(tiles.begin(), tiles.begin() + 2), false));
+ tiles.erase(tiles.begin(), tiles.begin() + 2);
+ return true;
+ }
+ return false;
+}
+
bool Hand::eat_pon(Tiles& tiles) {
if(tiles[0] == tiles[2]) {
tiles.erase(tiles.begin(), tiles.begin() + 3);
@@ -140,6 +191,15 @@ bool Hand::eat_pon(Tiles& tiles) {
return false;
}
+bool Hand::eat_pon(Tiles& tiles, Sets& hand) {
+ if(tiles[0] == tiles[2]) {
+ hand.push_back(Set(Set::Pon, Tiles(tiles.begin(), tiles.begin() + 3), false));
+ tiles.erase(tiles.begin(), tiles.begin() + 3);
+ return true;
+ }
+ return false;
+}
+
bool Hand::eat_chi(Tiles& tiles) {
Tile::Set set = tiles.front().get_set();
int num = tiles.front().get_num();
@@ -163,4 +223,30 @@ bool Hand::eat_chi(Tiles& tiles) {
return false;
}
-
+bool Hand::eat_chi(Tiles& tiles, Sets& hand) {
+ Tile::Set set = tiles.front().get_set();
+ int num = tiles.front().get_num();
+
+ if(set == Tile::Honor || num > 7) {
+ // Can't be chi-ed.
+ return false;
+ }
+
+ Tiles::iterator it1 = std::find(tiles.begin(), tiles.end(), Tile(set, num + 1));
+ if(it1 != tiles.end()) {
+ Tiles::iterator it2 = std::find(tiles.begin(), tiles.end(), Tile(set, num + 2));
+ if(it2 != tiles.end()) {
+ // We can eat a chi of tiles.
+ Tiles chi;
+ chi.push_back(tiles.front());
+ chi.push_back(*it1);
+ chi.push_back(*it2);
+ hand.push_back(Set(Set::Chi, chi, false));
+ tiles.erase(it2);
+ tiles.erase(it1);
+ tiles.erase(tiles.begin());
+ return true;
+ }
+ }
+ return false;
+}