diff options
Diffstat (limited to 'src/main')
| -rw-r--r-- | src/main/game/game_state.cpp | 33 | ||||
| -rw-r--r-- | src/main/game/game_state.h | 5 | ||||
| -rw-r--r-- | src/main/game/global_cache.cpp | 50 | ||||
| -rw-r--r-- | src/main/game/global_cache.h | 5 | ||||
| -rw-r--r-- | src/main/game/pile.h | 2 |
5 files changed, 52 insertions, 43 deletions
diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp index f8f9e1e..e2bfc24 100644 --- a/src/main/game/game_state.cpp +++ b/src/main/game/game_state.cpp @@ -317,39 +317,6 @@ bool operator==(const game_state& a, const game_state& b) { } -//////////////////// -// HASH FUNCTIONS // -//////////////////// - -size_t hash_value(game_state const& gs) { - size_t seed = 0; - - if (gs.rules.foundations) { - hash_combine(seed, gs.foundations); - } - if (gs.rules.cells) { - hash_combine(seed, gs.cells); - } - if (gs.rules.tableau_pile_count > 0) { - hash_combine(seed, gs.tableau_piles); - } - - if (gs.rules.stock_size > 0) { - hash_combine(seed, gs.piles[gs.stock]); - hash_combine(seed, gs.piles[gs.waste]); - } - if (gs.rules.hole) { - hash_combine(seed, gs.piles[gs.hole]); - } - - return seed; -} - -size_t hash_value(vector<pile> const& vp) { - return hash_range(begin(vp), end(vp)); -} - - /////////// // PRINT // /////////// diff --git a/src/main/game/game_state.h b/src/main/game/game_state.h index 323646f..8a41398 100644 --- a/src/main/game/game_state.h +++ b/src/main/game/game_state.h @@ -45,12 +45,11 @@ public: const std::vector<pile>& get_data() const; friend bool operator==(const game_state&, const game_state&); + friend std::ostream& operator<< (std::ostream&, const game_state&); - friend std::size_t hash_value(game_state const&); + friend std::size_t hash_value(card const&); friend std::size_t hash_value(std::vector<pile> const&); - friend std::ostream& operator<< (std::ostream&, const game_state&); - private: static std::vector<card> gen_shuffled_deck(int, card::rank_t, bool); diff --git a/src/main/game/global_cache.cpp b/src/main/game/global_cache.cpp index e881408..a1d7585 100644 --- a/src/main/game/global_cache.cpp +++ b/src/main/game/global_cache.cpp @@ -10,9 +10,11 @@ using namespace std; using namespace boost; typedef sol_rules::build_policy pol; +typedef sol_rules::stock_deal_type sdt; +typedef global_cache cache; -global_cache::global_cache(const sol_rules& rules) { - global_cache::rules = rules; +global_cache::global_cache(const game_state init_game_state) { + global_cache::init_gs = init_game_state; } bool global_cache::insert(const std::vector<pile>& pile_vec) { @@ -27,7 +29,7 @@ size_t hash_value(card const& c) { boost::hash<unsigned char> hasher; auto suit_val; - switch (global_cache::rules.build_pol) { + switch (cache::init_gs.rules.build_pol) { case pol::SAME_SUIT: suit_val = static_cast<std::underlying_type_t<card::suit_t>>( c.get_suit()); @@ -48,3 +50,45 @@ size_t hash_value(card const& c) { size_t hash_value(pile const& p) { return hash_range(begin(p.pile_vec), end(p.pile_vec)); } + +size_t hash_value(std::vector<pile> const& piles) { + size_t seed = 0; + + // Using addition for commutative hash + for (game_state::pile_ref pr : cache::init_gs.tableau_piles) { + seed += hash_value(piles[pr]); + } + + // Using hash_combine for order-dependent hash + for (game_state::pile_ref pr : cache::init_gs.foundations) { + hash_combine(seed, piles[pr]); + } + + // Commutative + for (game_state::pile_ref pr : cache::init_gs.reserve) { + seed += hash_value(piles[pr]); + } + + if (cache::init_gs.rules.stock_size > 0) { + hash_combine(seed, piles[cache::init_gs.stock]); + } + + if (cache::init_gs.rules.stock_deal_t == sdt::WASTE) { + hash_combine(seed, piles[cache::init_gs.waste]); + } + + if (cache::init_gs.rules.stock_deal_t == sdt::WASTE) { + hash_combine(seed, piles[cache::init_gs.waste]); + } + + if (cache::init_gs.rules.hole) { + hash_combine(seed, piles[cache::init_gs.hole]); + } + + // Commutative + for (game_state::pile_ref pr : cache::init_gs.cells) { + seed += hash_value(piles[pr]); + } + + return seed; +} diff --git a/src/main/game/global_cache.h b/src/main/game/global_cache.h index 26a73ef..73f0ce7 100644 --- a/src/main/game/global_cache.h +++ b/src/main/game/global_cache.h @@ -13,14 +13,15 @@ class global_cache { public: - explicit global_cache(const sol_rules&); + explicit global_cache(game_state); bool insert(const std::vector<pile>&); bool contains(const std::vector<pile>&); friend std::size_t hash_value(card const&); + friend std::size_t hash_value(std::vector<pile> const&); private: - static sol_rules rules; + static game_state init_gs; std::unordered_set< std::vector<pile>, diff --git a/src/main/game/pile.h b/src/main/game/pile.h index ab661ca..fb7bc38 100644 --- a/src/main/game/pile.h +++ b/src/main/game/pile.h @@ -29,8 +29,6 @@ public: card& operator[] (size_type); card operator[] (size_type) const; - - friend bool operator==(const pile&, const pile&); friend std::size_t hash_value(pile const&); private: |
