aboutsummaryrefslogtreecommitdiff
path: root/src/main/game
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/game')
-rw-r--r--src/main/game/game_state.cpp30
-rw-r--r--src/main/game/game_state.h23
-rw-r--r--src/main/game/global_cache.cpp130
-rw-r--r--src/main/game/global_cache.h41
-rw-r--r--src/main/game/pile.h2
5 files changed, 116 insertions, 110 deletions
diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp
index 8075f50..8f45a38 100644
--- a/src/main/game/game_state.cpp
+++ b/src/main/game/game_state.cpp
@@ -28,10 +28,7 @@ typedef sol_rules::stock_deal_type sdt;
// A private constructor used by both of the public ones. Initializes all of the
// piles and pile refs specified by the rules
-game_state::game_state(const sol_rules& s_rules) {
- game_state::static_reset();
- game_state::rules = s_rules;
-
+game_state::game_state(const sol_rules& s_rules) : rules(s_rules) {
// If there is a hole, creates pile
if (rules.hole) {
piles.emplace_back();
@@ -203,31 +200,6 @@ game_state::move::move(pile_ref f, pile_ref t, pile::size_type i)
}
-////////////////////////////
-// SETUP STATIC VARIABLES //
-////////////////////////////
-
-sol_rules game_state::rules;
-std::vector<game_state::pile_ref> game_state::tableau_piles;
-std::vector<game_state::pile_ref> game_state::cells;
-game_state::pile_ref game_state::stock;
-game_state::pile_ref game_state::waste;
-std::vector<game_state::pile_ref> game_state::reserve;
-std::vector<game_state::pile_ref> game_state::foundations;
-game_state::pile_ref game_state::hole;
-
-
-void game_state::static_reset() {
- tableau_piles.clear();
- cells.clear();
- stock = 0;
- waste = 0;
- reserve.clear();
- foundations.clear();
- hole = 0;
-}
-
-
/////////////////////////
// MODIFYING FUNCTIONS //
/////////////////////////
diff --git a/src/main/game/game_state.h b/src/main/game/game_state.h
index 8dcffa4..8434bf7 100644
--- a/src/main/game/game_state.h
+++ b/src/main/game/game_state.h
@@ -18,7 +18,8 @@
class game_state {
friend class deal_parser;
friend class state_printer;
- friend class game_state_pred;
+ friend class predicate;
+ friend class hasher;
public:
// Defines types
typedef uint8_t pile_ref;
@@ -48,11 +49,7 @@ public:
friend std::ostream& operator<< (std::ostream&, const game_state&);
- friend std::size_t hash_value(card const&);
- friend std::size_t hash_value(game_state const&);
-
private:
- void static_reset();
static std::vector<card> gen_shuffled_deck(int, card::rank_t, bool);
// Private constructor
@@ -71,14 +68,14 @@ private:
card) const;
// References to piles
- static sol_rules rules;
- static std::vector<pile_ref> tableau_piles;
- static std::vector<pile_ref> cells;
- static pile_ref stock;
- static pile_ref waste;
- static std::vector<pile_ref> reserve;
- static std::vector<pile_ref> foundations;
- static pile_ref hole;
+ const sol_rules rules;
+ std::vector<pile_ref> tableau_piles;
+ std::vector<pile_ref> cells;
+ pile_ref stock;
+ pile_ref waste;
+ std::vector<pile_ref> reserve;
+ std::vector<pile_ref> foundations;
+ pile_ref hole;
// The core piles
std::vector<pile> piles;
diff --git a/src/main/game/global_cache.cpp b/src/main/game/global_cache.cpp
index 8e3aa8b..1ce8d5d 100644
--- a/src/main/game/global_cache.cpp
+++ b/src/main/game/global_cache.cpp
@@ -4,6 +4,8 @@
#include <algorithm>
+#include <boost/functional/hash.hpp>
+
#include "global_cache.h"
using namespace std;
@@ -12,92 +14,111 @@ using namespace boost;
typedef sol_rules::build_policy pol;
typedef sol_rules::stock_deal_type sdt;
-bool global_cache::insert(const game_state& gs) {
- return u_set.insert(gs).second;
+
+bool global_cache::insert(const std::vector<pile>& piles) {
+ return u_set.insert(piles).second;
}
-bool global_cache::contains(const game_state& gs) const {
- return u_set.count(gs) > 0;
+bool global_cache::contains(const std::vector<pile>& piles) const {
+ return u_set.count(piles) > 0;
}
void global_cache::clear() {
u_set.clear();
}
-size_t hash_value(card const& c) {
- boost::hash<uint8_t> hasher;
-
-#ifdef SIMPLE_HASH
- uint8_t suit_val = static_cast<uint8_t>(c.get_suit());
-#else
- uint8_t suit_val;
- switch (game_state::rules.build_pol) {
- case pol::SAME_SUIT:
- suit_val = static_cast<uint8_t>(c.get_suit());
- break;
- case pol::RED_BLACK:
- suit_val = static_cast<uint8_t>(c.get_colour());
- break;
- default:
- suit_val = 0;
- }
-#endif
-
- auto raw_val = static_cast<uint8_t>(suit_val * 13 + c.get_rank());
- return hasher(raw_val);
+global_cache::global_cache(const game_state& gs)
+ : u_set(0, hasher(gs), predicate(gs)) {
}
-size_t hash_value(pile const& p) {
- return hash_range(begin(p.pile_vec), end(p.pile_vec));
+hasher::hasher(const game_state& gs) : init_gs(gs) {
}
-size_t hash_value(game_state const& gs) {
+size_t hasher::operator()(const vector<pile>& piles) const {
size_t seed = 0;
#ifdef SIMPLE_HASH
- for (game_state::pile_ref pr = 0; pr < gs.piles.size(); pr++) {
- hash_combine(seed, gs.piles[pr]);
+ for (game_state::pile_ref pr = 0; pr < piles.size(); pr++) {
+ seed += combine(hash_value(piles[pr]));
}
#else
- boost::hash<pile> pile_hasher;
-
// Using addition for commutative hash
- for (game_state::pile_ref pr : game_state::tableau_piles) {
- seed += pile_hasher(gs.piles[pr]);
+ for (game_state::pile_ref pr : init_gs.tableau_piles) {
+ combine_commutative(seed, hash_value(piles[pr]));
}
// Using hash_combine for order-dependent hash
- for (game_state::pile_ref pr : game_state::foundations) {
- hash_combine(seed, gs.piles[pr]);
+ for (game_state::pile_ref pr : init_gs.foundations) {
+ combine(seed, hash_value(piles[pr]));
}
// Commutative
- for (game_state::pile_ref pr : game_state::reserve) {
- seed += pile_hasher(gs.piles[pr]);
+ for (game_state::pile_ref pr : init_gs.reserve) {
+ combine_commutative(seed, hash_value(piles[pr]));
}
- if (game_state::rules.stock_size > 0) {
- hash_combine(seed, gs.piles[game_state::stock]);
+ if (init_gs.rules.stock_size > 0) {
+ combine(seed, hash_value(piles[init_gs.stock]));
}
- if (game_state::rules.stock_deal_t == sdt::WASTE) {
- hash_combine(seed, gs.piles[game_state::waste]);
+ if (init_gs.rules.stock_deal_t == sdt::WASTE) {
+ combine(seed, hash_value(piles[init_gs.waste]));
}
- if (game_state::rules.hole) {
- hash_combine(seed, gs.piles[game_state::hole]);
+ if (init_gs.rules.hole) {
+ combine(seed, hash_value(piles[init_gs.hole]));
}
// Commutative
- for (game_state::pile_ref pr : game_state::cells) {
- seed += pile_hasher(gs.piles[pr]);
+ for (game_state::pile_ref pr : init_gs.cells) {
+ combine_commutative(seed, hash_value(piles[pr]));
}
#endif
return seed;
}
-bool game_state_pred::comp_pile(const pile &x, const pile &y) {
+size_t hasher::hash_value(pile const& p) const {
+ std::size_t seed = 0;
+
+ for(auto first = begin(p.pile_vec); first != end(p.pile_vec); ++first) {
+ combine(seed, hash_value(*first));
+ }
+ return seed;
+}
+
+std::size_t hasher::combine(std::size_t& seed, std::size_t value) const {
+ return seed ^= value + 0x9e3779b9 + (seed<<6) + (seed>>2);
+}
+
+std::size_t hasher::combine_commutative(std::size_t& seed, std::size_t value) const {
+ return seed += value;
+}
+
+size_t hasher::hash_value(card const& c) const {
+ boost::hash<uint8_t> boost_hasher;
+
+#ifdef SIMPLE_HASH
+ uint8_t suit_val = static_cast<uint8_t>(c.get_suit());
+#else
+ uint8_t suit_val;
+ switch (init_gs.rules.build_pol) {
+ case pol::SAME_SUIT:
+ suit_val = static_cast<uint8_t>(c.get_suit());
+ break;
+ case pol::RED_BLACK:
+ suit_val = static_cast<uint8_t>(c.get_colour());
+ break;
+ default:
+ suit_val = 0;
+ }
+#endif
+
+ auto raw_val = static_cast<uint8_t>(suit_val * 13 + c.get_rank());
+ return boost_hasher(raw_val);
+}
+
+bool predicate::comp_pile(const pile &x, const pile &y) {
// Largest piles first
if (x.size() != y.size()) {
return x.size() > y.size();
@@ -116,12 +137,12 @@ bool game_state_pred::comp_pile(const pile &x, const pile &y) {
return false;
}
-bool game_state_pred::operator()(const game_state& x,
- const game_state& y) const {
- if (x.piles.size() != y.piles.size()) return false;
+bool predicate::operator()(const std::vector<pile>& x,
+ const std::vector<pile>& y) const {
+ if (x.size() != y.size()) return false;
- vector<pile> x_piles = x.piles;
- vector<pile> y_piles = y.piles;
+ vector<pile> x_piles = x;
+ vector<pile> y_piles = y;
// Orders the piles from largest to smallest
sort(begin(x_piles), end(x_piles), comp_pile);
@@ -140,7 +161,7 @@ bool game_state_pred::operator()(const game_state& x,
#ifdef SIMPLE_HASH
if (cx.get_suit() != cy.get_suit()) return false;
#else
- switch (game_state::rules.build_pol) {
+ switch (init_gs.rules.build_pol) {
case pol::SAME_SUIT:
if (cx.get_suit() != cy.get_suit()) return false;
break;
@@ -156,3 +177,6 @@ bool game_state_pred::operator()(const game_state& x,
return true;
}
+
+predicate::predicate(const game_state& gs) : init_gs(gs) {
+}
diff --git a/src/main/game/global_cache.h b/src/main/game/global_cache.h
index 486006c..0e8fe0c 100644
--- a/src/main/game/global_cache.h
+++ b/src/main/game/global_cache.h
@@ -8,27 +8,40 @@
#include <vector>
#include <unordered_set>
-#include <boost/functional/hash.hpp>
-
#include "sol_rules.h"
#include "game_state.h"
-struct game_state_pred {
- static bool comp_pile(const pile& x, const pile& y);
- bool operator() (const game_state& x, const game_state& y)
- const;
+class predicate {
+public:
+ explicit predicate(const game_state&);
+ static bool comp_pile(const pile&, const pile&);
+ bool operator() (const std::vector<pile>&, const std::vector<pile>&) const;
+private:
+ const game_state& init_gs;
};
-class global_cache {
-public:
- bool insert(const game_state&);
- bool contains(const game_state&) const;
+struct hasher {
+ explicit hasher(const game_state&);
+ std::size_t operator() (const std::vector<pile>&) const;
+
+ std::size_t hash_value(const card&) const;
+ std::size_t hash_value(const pile&) const;
+ std::size_t combine(std::size_t&, std::size_t) const;
+ std::size_t combine_commutative(std::size_t&, std::size_t) const;
+
+ const game_state& init_gs;
+};
+
+struct global_cache {
+ explicit global_cache(const game_state&);
+ bool insert(const std::vector<pile>&);
+ bool contains(const std::vector<pile>&) const;
void clear();
-private:
+
std::unordered_set<
- game_state,
- boost::hash<game_state>,
- game_state_pred
+ std::vector<pile>,
+ hasher,
+ predicate
> u_set;
};
diff --git a/src/main/game/pile.h b/src/main/game/pile.h
index 411f890..2bf2d5b 100644
--- a/src/main/game/pile.h
+++ b/src/main/game/pile.h
@@ -12,6 +12,7 @@
#include "card.h"
class pile {
+ friend class hasher;
public:
typedef uint8_t size_type;
@@ -33,7 +34,6 @@ public:
card operator[] (size_type) const;
friend bool operator==(const pile&, const pile&);
- friend std::size_t hash_value(pile const&);
private:
// Underlying vector
std::vector<card> pile_vec;