diff options
Diffstat (limited to 'src/main/game')
| -rw-r--r-- | src/main/game/card.cpp | 9 | ||||
| -rw-r--r-- | src/main/game/card.h | 1 | ||||
| -rw-r--r-- | src/main/game/global_cache.cpp | 28 | ||||
| -rw-r--r-- | src/main/game/global_cache.h | 3 |
4 files changed, 29 insertions, 12 deletions
diff --git a/src/main/game/card.cpp b/src/main/game/card.cpp index 2f4cc08..0a05163 100644 --- a/src/main/game/card.cpp +++ b/src/main/game/card.cpp @@ -74,12 +74,3 @@ bool operator==(const card& l, const card& r) { bool operator!=(const card& l, const card& r) { return !(l == r); } - -size_t hash_value(card const& c) { - boost::hash<unsigned char> hasher; - - auto suit_val = static_cast<std::underlying_type_t<card::suit_t>>(c.get_suit()); - auto raw_val = static_cast<unsigned char>(suit_val * 13 + c.get_rank()); - - return hasher(raw_val); -}
\ No newline at end of file diff --git a/src/main/game/card.h b/src/main/game/card.h index 4cab774..0c09043 100644 --- a/src/main/game/card.h +++ b/src/main/game/card.h @@ -37,7 +37,6 @@ private: bool operator==(const card&, const card&); bool operator!=(const card&, const card&); -std::size_t hash_value(card const&); #endif //SOLITAIRE_PIP_CARD_H diff --git a/src/main/game/global_cache.cpp b/src/main/game/global_cache.cpp index cc95b3b..f55a72c 100644 --- a/src/main/game/global_cache.cpp +++ b/src/main/game/global_cache.cpp @@ -5,7 +5,11 @@ #include "global_cache.h" -global_cache::global_cache(const sol_rules&) {} +typedef sol_rules::build_policy pol; + +global_cache::global_cache(const sol_rules& rules) { + global_cache::rules = rules; +} bool global_cache::insert(const std::vector<pile>& pile_vec) { return u_set.insert(pile_vec).second; @@ -14,3 +18,25 @@ bool global_cache::insert(const std::vector<pile>& pile_vec) { bool global_cache::contains(const std::vector<pile>& pile_vec) { return u_set.count(pile_vec) > 0; } + +size_t hash_value(card const& c) { + boost::hash<unsigned char> hasher; + + auto suit_val; + switch (global_cache::rules.build_pol) { + case pol::SAME_SUIT: + suit_val = static_cast<std::underlying_type_t<card::suit_t>>( + c.get_suit()); + break; + case pol::RED_BLACK: + suit_val = static_cast<std::underlying_type_t<card::colour_t >>( + c.get_colour()); + break; + default: + suit_val = 0; + break; + } + + auto raw_val = static_cast<unsigned char>(suit_val * 13 + c.get_rank()); + return hasher(raw_val); +} diff --git a/src/main/game/global_cache.h b/src/main/game/global_cache.h index b35acc6..a5c6768 100644 --- a/src/main/game/global_cache.h +++ b/src/main/game/global_cache.h @@ -18,12 +18,13 @@ public: bool insert(const std::vector<pile>&); bool contains(const std::vector<pile>&); + friend std::size_t hash_value(card const&); private: + static sol_rules rules; std::unordered_set< std::vector<pile>, boost::hash<std::vector<pile>> > u_set; }; - #endif //SOLVITAIRE_GLOBAL_CACHE_H |
