aboutsummaryrefslogtreecommitdiff
path: root/src/main/game/global_cache.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/game/global_cache.cpp')
-rw-r--r--src/main/game/global_cache.cpp50
1 files changed, 47 insertions, 3 deletions
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;
+}