aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main/game/card.cpp11
-rw-r--r--src/main/game/card.h1
-rw-r--r--src/main/game/game_state.cpp37
-rw-r--r--src/main/game/game_state.h2
-rw-r--r--src/main/game/pile.cpp7
-rw-r--r--src/main/game/pile.h1
-rw-r--r--src/main/solver/solver.cpp20
-rw-r--r--src/main/solver/solver.h17
8 files changed, 81 insertions, 15 deletions
diff --git a/src/main/game/card.cpp b/src/main/game/card.cpp
index 9f7dbdf..f5e9acb 100644
--- a/src/main/game/card.cpp
+++ b/src/main/game/card.cpp
@@ -4,9 +4,12 @@
#include <sstream>
+#include <boost/functional/hash.hpp>
+
#include "card.h"
using namespace std;
+using namespace boost;
// A card can either be created using two int values (rank & suit), or
// using a string (e.g. "AS")
@@ -89,4 +92,12 @@ std::ostream & operator<<(std::ostream & s, card const & c) {
s << c.get_suit();
return s;
+}
+
+size_t hash_value(card const& c) {
+ boost::hash<unsigned char> hasher;
+ unsigned char raw_val = static_cast<unsigned char>(
+ c.get_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 1cac344..b518485 100644
--- a/src/main/game/card.h
+++ b/src/main/game/card.h
@@ -26,6 +26,7 @@ private:
std::ostream & operator<<(std::ostream &, card const &);
bool operator==(const card&, const card&);
+std::size_t hash_value(card const&);
#endif //SOLITAIRE_PIP_CARD_H
diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp
index 1814a41..2b77fec 100644
--- a/src/main/game/game_state.cpp
+++ b/src/main/game/game_state.cpp
@@ -9,11 +9,13 @@
#include <functional>
#include <rapidjson/document.h>
+#include <boost/functional/hash.hpp>
#include "game_state.h"
-using namespace rapidjson;
using namespace std;
+using namespace rapidjson;
+using namespace boost;
typedef sol_rules::build_order ord;
typedef sol_rules::build_policy pol;
@@ -324,4 +326,35 @@ bool operator==(const game_state& a, const game_state& b) {
ostream& operator <<(ostream& stream, const game_state& gs) {
return gs.print(stream);
-} \ No newline at end of file
+}
+
+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.reserve_size > 0) {
+ hash_combine(seed, gs.reserve);
+ }
+ if (gs.rules.stock_size > 0) {
+ hash_combine(seed, gs.stock);
+ hash_combine(seed, gs.waste);
+ }
+ if (gs.rules.hole) {
+ hash_combine(seed, gs.hole);
+ }
+
+ return seed;
+}
+
+size_t hash_value(vector<pile> const& vp) {
+ return hash_range(begin(vp), end(vp));
+}
+
diff --git a/src/main/game/game_state.h b/src/main/game/game_state.h
index 12dcda9..873ca6f 100644
--- a/src/main/game/game_state.h
+++ b/src/main/game/game_state.h
@@ -29,6 +29,8 @@ public:
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(std::vector<pile> const&);
private:
static std::vector<card> shuffled_deck(int, int);
diff --git a/src/main/game/pile.cpp b/src/main/game/pile.cpp
index bebd1f6..1ce7c01 100644
--- a/src/main/game/pile.cpp
+++ b/src/main/game/pile.cpp
@@ -5,9 +5,12 @@
#include <vector>
#include <ostream>
+#include <boost/functional/hash.hpp>
+
#include "pile.h"
using namespace std;
+using namespace boost;
typedef sol_rules::build_order ord;
typedef sol_rules::build_policy pol;
@@ -143,3 +146,7 @@ void pile::clear() {
bool operator==(const pile& a, const pile& b) {
return a.pile_vec == b.pile_vec;
}
+
+size_t hash_value(pile const& p) {
+ return hash_range(begin(p.pile_vec), end(p.pile_vec));
+}
diff --git a/src/main/game/pile.h b/src/main/game/pile.h
index 6482e0c..1922c73 100644
--- a/src/main/game/pile.h
+++ b/src/main/game/pile.h
@@ -38,6 +38,7 @@ public:
card operator[] (std::vector<card>::size_type) const;
friend bool operator==(const pile&, const pile&);
+ friend std::size_t hash_value(pile const&);
private:
bool one_lt(int, int) const;
diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp
index f412d03..a4933b2 100644
--- a/src/main/solver/solver.cpp
+++ b/src/main/solver/solver.cpp
@@ -9,8 +9,6 @@
#include "solver.h"
#include "input-output/log_helper.h"
-#include "game/game_state.h"
-#include <boost/log/trivial.hpp>
using namespace std;
using namespace boost;
@@ -21,13 +19,13 @@ solver::solver(const game_state gs, const sol_rules& sr)
solver::node::node(const node* parent, const game_state& gs) :
history(solver::node::gen_history(parent)), state(gs) {}
-const vector<game_state> solver::node::gen_history(const node* parent) {
- vector<game_state> hist;
+const solver::node_history solver::node::gen_history(const node* parent) {
+ node_history hist;
if (parent != NULL) {
- hist = std::vector<game_state>(parent->history);
- hist.push_back(parent->state);
- } else {
- hist = vector<game_state>();
+ copy (begin(parent->history.get<0>()),
+ end(parent->history.get<0>()),
+ back_inserter(hist.get<0>()));
+ hist.get<0>().push_back(parent->state);
}
return hist;
}
@@ -51,8 +49,8 @@ const optional<solver::node> solver::run() {
for (auto it = new_children.rbegin(); it != new_children.rend(); it++) {
// If we have seen the state before, ignore it (loop detection))
- if (find(begin(current.history), end(current.history), *it)
- != end(current.history)) {
+ if (current.history.get<1>().find(*it)
+ != current.history.get<1>().end()) {
continue;
}
@@ -77,7 +75,7 @@ int solver::get_states_searched() const {
}
std::ostream& operator<< (std::ostream& stream, const solver::node& solution) {
- for (auto state : solution.history) {
+ for (auto state : solution.history.get<0>()) {
stream << state << "\n";
}
stream << solution.state << "\n";
diff --git a/src/main/solver/solver.h b/src/main/solver/solver.h
index 91c93d6..73e5e24 100644
--- a/src/main/solver/solver.h
+++ b/src/main/solver/solver.h
@@ -9,18 +9,30 @@
#include <vector>
#include <boost/optional.hpp>
+#include <boost/multi_index/hashed_index.hpp>
+#include <boost/multi_index_container.hpp>
+#include <boost/multi_index/identity.hpp>
+#include <boost/multi_index/sequenced_index.hpp>
#include "game/game_state.h"
#include "game/sol_rules.h"
class solver {
public:
+ typedef boost::multi_index::multi_index_container<
+ game_state,
+ boost::multi_index::indexed_by<
+ boost::multi_index::sequenced<>,
+ boost::multi_index::hashed_unique<boost::multi_index::identity<game_state>>
+ >
+ > node_history;
+
struct node {
node(const node*, const game_state&);
- std::vector<game_state> history;
+ node_history history;
game_state state;
- static const std::vector<game_state> gen_history(const node* parent);
+ static const node_history gen_history(const node* parent);
};
solver(const game_state, const sol_rules&);
@@ -33,6 +45,7 @@ public:
friend std::ostream& operator<< (std::ostream&, const solver::node&);
private:
+
const node root;
const sol_rules rules;
node solution;