From dc291de04681fa78a949b9aa8eb5f8f29b0f9f0e Mon Sep 17 00:00:00 2001 From: Vegard Storheil Eriksen Date: Thu, 2 Dec 2010 05:05:29 +0100 Subject: Changed Hand::eat_-functions to return a bool, so a failure can be distinguished from a depleted list of tiles. --- server/hand.cpp | 86 +++++++++++++++++++++++++++++---------------------------- server/hand.h | 20 +++++++------- 2 files changed, 54 insertions(+), 52 deletions(-) diff --git a/server/hand.cpp b/server/hand.cpp index abdeb4b..07777fb 100644 --- a/server/hand.cpp +++ b/server/hand.cpp @@ -13,11 +13,12 @@ bool Hand::basic_format(const Tiles& tiles, bool pair_eaten) { return true; // All tiles eaten. } + Tiles rest; + if(!pair_eaten) { - if(Tiles rest = eat_pair(tiles)) { - if(basic_format(rest, true)) { - return true; - } + rest = tiles; + if(eat_pair(rest) && basic_format(rest, true)) { + return true; } } @@ -25,14 +26,14 @@ bool Hand::basic_format(const Tiles& tiles, bool pair_eaten) { return false; } - if(Tiles rest = eat_pon(tiles)) { - if(basic_format(rest, pair_eaten)) { - return true; - } + rest = tiles; + if(eat_pon(rest) && basic_format(rest, pair_eaten)) { + return true; } - if(Tiles rest = eat_chi(tiles)) { - return basic_format(rest, pair_eaten); + rest = tiles; + if(eat_chi(rest) && basic_format(rest, pair_eaten)) { + return true; } return false; @@ -43,8 +44,11 @@ bool Hand::basic_format_tenpai(const Tiles& tiles, bool pair_eaten, bool wait_ea return true; } + Tiles rest; + if(!pair_eaten && !wait_eaten) { - if(basic_format_tenpai(eat_tanki(tiles), true, true)) { + rest = tiles; + if(eat_tanki(rest) && basic_format_tenpai(rest, true, true)) { return true; } } @@ -54,18 +58,16 @@ bool Hand::basic_format_tenpai(const Tiles& tiles, bool pair_eaten, bool wait_ea } if(!pair_eaten) { - if(Tiles rest = eat_pair(tiles)) { - if(basic_format_tenpai(rest, true, wait_eaten)) { - return true; - } + rest = tiles; + if(eat_pair(rest) && basic_format_tenpai(rest, true, wait_eaten)) { + return true; } } if(!wait_eaten) { - if(Tiles rest = eat_chi_wait(tiles)) { - if(basic_format_tenpai(rest, pair_eaten, true)) { - return true; - } + rest = tiles; + if(eat_chi_wait(rest) && basic_format_tenpai(rest, pair_eaten, true)) { + return true; } } @@ -73,30 +75,30 @@ bool Hand::basic_format_tenpai(const Tiles& tiles, bool pair_eaten, bool wait_ea return false; } - if(Tiles rest = eat_pon(tiles)) { - if(basic_format_tenpai(rest, pair_eaten, wait_eaten)) { - return true; - } + rest = tiles; + if(eat_pon(rest) && basic_format_tenpai(rest, pair_eaten, wait_eaten)) { + return true; } - if(Tiles rest = eat_chi(tiles)) { - return basic_format_tenpai(rest, pair_eaten, wait_eaten); + rest = tiles; + if(eat_chi(rest) && basic_format_tenpai(rest, pair_eaten, wait_eaten)) { + return true; } return false; } -Tiles Hand::eat_tanki(Tiles tiles) { +bool Hand::eat_tanki(Tiles& tiles) { tiles.erase(tiles.begin()); - return tiles; + return true; } -Tiles Hand::eat_chi_wait(Tiles tiles) { +bool Hand::eat_chi_wait(Tiles& tiles) { Tile::Set set = tiles.front().get_set(); int num = tiles.front().get_num(); if(set == Tile::Honor || num > 8) { - return Tiles(); + return false; } // Look for T+1. @@ -104,7 +106,7 @@ Tiles Hand::eat_chi_wait(Tiles tiles) { if(it != tiles.end()) { tiles.erase(it); tiles.erase(tiles.begin()); - return tiles; + return true; } if(num > 7) { @@ -116,35 +118,35 @@ Tiles Hand::eat_chi_wait(Tiles tiles) { if(it != tiles.end()) { tiles.erase(it); tiles.erase(tiles.begin()); - return tiles; + return true; } - return Tiles(); + return false; } -Tiles Hand::eat_pair(Tiles tiles) { +bool Hand::eat_pair(Tiles& tiles) { if(tiles[0] == tiles[1]) { tiles.erase(tiles.begin(), tiles.begin() + 2); - return tiles; + return true; } - return Tiles(); + return false; } -Tiles Hand::eat_pon(Tiles tiles) { +bool Hand::eat_pon(Tiles& tiles) { if(tiles[0] == tiles[2]) { tiles.erase(tiles.begin(), tiles.begin() + 3); - return tiles; + return true; } - return Tiles(); + return false; } -Tiles Hand::eat_chi(Tiles tiles) { +bool Hand::eat_chi(Tiles& tiles) { 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 Tiles(); + return false; } Tiles::iterator it1 = std::find(tiles.begin(), tiles.end(), Tile(set, num + 1)); @@ -155,10 +157,10 @@ Tiles Hand::eat_chi(Tiles tiles) { tiles.erase(it2); tiles.erase(it1); tiles.erase(tiles.begin()); - return tiles; + return true; } } - return Tiles(); + return false; } diff --git a/server/hand.h b/server/hand.h index e38a387..879bc8d 100644 --- a/server/hand.h +++ b/server/hand.h @@ -16,20 +16,20 @@ namespace Hand { // Check if the tiles is matching the normal format but missing one. bool basic_format_tenpai(const Tiles& tiles, bool pair_eaten = false, bool wait_eaten = false); - // Eat a single tile (i.e. the tanki machi) from beginning of list and return rest. - Tiles eat_tanki(Tiles tiles); + // Eat a single tile (i.e. the tanki machi) from beginning of list and return success flag. + bool eat_tanki(Tiles& tiles); - // Eat two tiles waiting for a third to complete a chi (i.e. ryanmen, penchan or kanchan machi) if possible. - Tiles eat_chi_wait(Tiles tiles); + // Eat two tiles waiting for a third to complete a chi (i.e. ryanmen, penchan or kanchan machi) if possible and return success flag. + bool eat_chi_wait(Tiles& tiles); - // Eat a pair from beginning of list if possible and return rest, else return empty list. - Tiles eat_pair(Tiles tiles); + // Eat a pair from beginning of list if possible and return success flag. + bool eat_pair(Tiles& tiles); - // Eat a pon from beginning of list if possible and return rest, else return empty list. - Tiles eat_pon(Tiles tiles); + // Eat a pon from beginning of list if possible and return success flag. + bool eat_pon(Tiles& tiles); - // Eat a chi from beginning of list if possible and return rest, else return empty list. - Tiles eat_chi(Tiles tiles); + // Eat a chi from beginning of list if possible and return success flag. + bool eat_chi(Tiles& tiles); } #endif -- cgit v1.2.3