diff options
Diffstat (limited to 'src/main/game')
| -rw-r--r-- | src/main/game/card.cpp | 4 | ||||
| -rw-r--r-- | src/main/game/card.h | 3 | ||||
| -rw-r--r-- | src/main/game/game_state.cpp | 10 | ||||
| -rw-r--r-- | src/main/game/game_state.h | 4 | ||||
| -rw-r--r-- | src/main/game/global_cache.cpp | 238 | ||||
| -rw-r--r-- | src/main/game/global_cache.h | 37 | ||||
| -rw-r--r-- | src/main/game/pile.cpp | 24 | ||||
| -rw-r--r-- | src/main/game/pile.h | 1 |
8 files changed, 158 insertions, 163 deletions
diff --git a/src/main/game/card.cpp b/src/main/game/card.cpp index 25a981f..271bf73 100644 --- a/src/main/game/card.cpp +++ b/src/main/game/card.cpp @@ -18,6 +18,8 @@ card::card(suit_t s, rank_t r) : suit(s), rank(r) {} card::card(const char* c) : suit(suit_from_str(c)), rank(rank_from_str(c)) {} +card::card() : card(0, 0) {} + card::suit_t card::suit_from_str(const char* c) { switch(tolower(c[strlen(c) - 1])) { case 'c': return suit::Clubs; @@ -75,6 +77,8 @@ std::string card::to_string() const { return s; } +const card card::divider = card(0, 0); + bool operator==(const card& l, const card& r) { return l.get_rank() == r.get_rank() && l.get_suit() == r.get_suit(); } diff --git a/src/main/game/card.h b/src/main/game/card.h index 318cd12..74f513d 100644 --- a/src/main/game/card.h +++ b/src/main/game/card.h @@ -27,10 +27,13 @@ public: static const colour_t Red = 1; }; + const static card divider; + // A card can either be created using two int values (rank & suit), or // using a string (e.g. "AS") card(suit_t, rank_t); explicit card(const char*); + card(); suit_t get_suit() const; colour_t get_colour() const; diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp index faab429..097c744 100644 --- a/src/main/game/game_state.cpp +++ b/src/main/game/game_state.cpp @@ -277,21 +277,16 @@ void game_state::undo_move(const move m) { // reorders the pile refs so that the largest pile is first void game_state::place_card(pile_ref pr, card c) { piles[pr].place(c); -#ifndef ORDER_ON_CACHE eval_pile_order(pr, true); -#endif } // Same as above but for taking cards card game_state::take_card(pile_ref pr) { card c = piles[pr].take(); -#ifndef ORDER_ON_CACHE eval_pile_order(pr, false); -#endif return c; } -#ifndef ORDER_ON_CACHE // Assesses whether the pile ref that was modified was a tableau, cell or // reserve pile, and if so makes the relevant function call void game_state::eval_pile_order(pile_ref pr, bool is_place) { @@ -361,7 +356,6 @@ void game_state::eval_pile_order(list<pile_ref>& pile_lst, pile_ref changed_pr, } #endif } -#endif //////////////////////// @@ -410,10 +404,6 @@ bool game_state::is_solved() const { return solved; } -const vector<pile>& game_state::get_data() const { - return piles; -} - /////////// // PRINT // diff --git a/src/main/game/game_state.h b/src/main/game/game_state.h index 532eefc..9b0d7b5 100644 --- a/src/main/game/game_state.h +++ b/src/main/game/game_state.h @@ -22,6 +22,7 @@ class game_state { friend class predicate; friend class hasher; friend class global_cache; + friend class cached_game_state; public: // Defines types typedef uint8_t pile_ref; @@ -48,7 +49,6 @@ public: // Inspects state bool is_solved() const; - const std::vector<pile>& get_data() const; friend std::ostream& operator<< (std::ostream&, const game_state&); @@ -58,11 +58,9 @@ private: // Private constructor explicit game_state(const sol_rules&); -#ifndef ORDER_ON_CACHE // Used to maintain pile order invariant void eval_pile_order(pile_ref, bool); void eval_pile_order(std::list<pile_ref>&, pile_ref, bool); -#endif // Used in get_legal_moves() move get_stock_tableau_move() const; diff --git a/src/main/game/global_cache.cpp b/src/main/game/global_cache.cpp index bf55f67..37738f2 100644 --- a/src/main/game/global_cache.cpp +++ b/src/main/game/global_cache.cpp @@ -14,140 +14,127 @@ 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.emplace(get_ordered_vec(gs)).second; -} - -bool global_cache::contains(const game_state& gs) const { - return u_set.count(get_ordered_vec(gs)) > 0; -} -void global_cache::clear() { - u_set.clear(); -} - -global_cache::global_cache(const game_state& gs) - : u_set(0, hasher(gs), predicate(gs)) { -} +/////////////////////// +// CACHED GAME STATE // +/////////////////////// -std::vector<pile> global_cache::get_ordered_vec(const game_state& gs) { -#ifdef ORDER_ON_CACHE - vector<pile> ordered_vec = gs.get_data(); - if (gs.rules.cells > 0) { - sort(begin(ordered_vec) + gs.original_cells[0], - begin(ordered_vec) + gs.original_cells[0] + gs.original_cells.size()); - } - if (gs.rules.reserve_size > 0) { - sort(begin(ordered_vec) + gs.original_reserve[0], - begin(ordered_vec) + gs.original_reserve[0] + gs.original_reserve.size()); - } - if (gs.rules.tableau_pile_count > 0) { - sort(begin(ordered_vec) + gs.original_tableau_piles[0], - begin(ordered_vec) + gs.original_tableau_piles[0] + gs.original_tableau_piles.size()); - } +cached_game_state::cached_game_state(const game_state& gs) { + if (gs.rules.hole) { +#ifdef NO_REDUCED_STATE + add_pile(gs.hole, gs); #else - const vector<pile>& gs_vec = gs.get_data(); - vector<pile> ordered_vec; - ordered_vec.reserve(gs_vec.size()); - - if (gs.rules.hole) - ordered_vec.push_back(gs_vec[gs.hole]); + add_card(gs.piles[gs.hole].top_card(), gs); +#endif + } + // No need to add foundations for reduced state, as they are implicit in the + // other piles +#ifdef NO_REDUCED_STATE for (game_state::pile_ref pr : gs.foundations) - ordered_vec.push_back(gs_vec[pr]); + add_pile(pr, gs); +#endif - for (game_state::pile_ref pr : gs.cells) - ordered_vec.push_back(gs_vec[pr]); + for (game_state::pile_ref pr : gs.cells) { + add_pile(pr, gs); + } + if (gs.rules.cells > 0) { + add_card_divider(); + } if (gs.rules.stock_size > 0) { - ordered_vec.push_back(gs_vec[gs.stock]); + add_pile(gs.stock, gs); + add_card_divider(); if (gs.rules.stock_deal_t == sdt::WASTE) { - ordered_vec.push_back(gs_vec[gs.waste]); + add_pile(gs.waste, gs); + add_card_divider(); } } - for (game_state::pile_ref pr : gs.reserve) - ordered_vec.push_back(gs_vec[pr]); - - for (game_state::pile_ref pr : gs.tableau_piles) - ordered_vec.push_back(gs_vec[pr]); - -#ifndef NDEBUG - // Makes sure the piles are in order - if (gs.original_cells.size() > 1) { - for (game_state::pile_ref pr : gs.original_cells) { - assert(ordered_vec[pr] >= ordered_vec[pr+1]); - if (pr == gs.original_cells[gs.original_cells.size()-2]) break; - } - } - if (gs.original_reserve.size() > 1) { - for (game_state::pile_ref pr : gs.original_reserve) { - assert(ordered_vec[pr] >= ordered_vec[pr+1]); - if (pr == gs.original_reserve[gs.original_reserve.size()-2]) break; - } + for (game_state::pile_ref pr : gs.reserve) { + add_pile(pr, gs); } - if (gs.original_tableau_piles.size() > 1) { - for (game_state::pile_ref pr : gs.original_tableau_piles) { - assert(ordered_vec[pr] >= ordered_vec[pr+1]); - if (pr == gs.original_tableau_piles[gs.original_tableau_piles.size()-2]) break; - } + if (gs.rules.reserve_size > 0) { + add_card_divider(); } -#endif -#endif - return ordered_vec; + for (game_state::pile_ref pr : gs.tableau_piles) { + add_pile(pr, gs); + add_card_divider(); + } } - -hasher::hasher(const game_state& gs) : init_gs(gs) { +void cached_game_state::add_pile(game_state::pile_ref pr, const game_state& gs) { +#ifdef NO_REDUCED_STATE + data.emplace_back(pile()); +#endif + for (card c : gs.piles[pr].pile_vec) { + add_card(c, gs); + } } -size_t hasher::operator()(const vector<pile>& piles) const { - size_t seed = 0; +void cached_game_state::add_card(card c, const game_state& gs) { +#ifdef NO_REDUCED_STATE + auto& target = data.back().pile_vec; +#else + auto& target = data; +#endif + + switch (gs.rules.build_pol) { #ifdef SIMPLE_HASH - for (game_state::pile_ref pr = 0; pr < piles.size(); pr++) { - seed += combine(seed, hash_value(piles[pr])); - } + default: + target.emplace_back(c); + break; #else - // Using addition for commutative hash - for (game_state::pile_ref pr : init_gs.tableau_piles) { - combine_commutative(seed, hash_value(piles[pr])); + case pol::SAME_SUIT: + target.emplace_back(c); + break; + case pol::RED_BLACK: + target.emplace_back(c.get_colour(), c.get_rank()); + break; + default: + target.emplace_back(0, c.get_rank()); + break; +#endif } +} - // Using hash_combine for order-dependent hash - for (game_state::pile_ref pr : init_gs.foundations) { - combine(seed, hash_value(piles[pr])); - } +void cached_game_state::add_card_divider() { +#ifndef NO_REDUCED_STATE + data.emplace_back(0, 0); +#endif +} - // Commutative - for (game_state::pile_ref pr : init_gs.reserve) { - combine_commutative(seed, hash_value(piles[pr])); - } +bool operator==(const cached_game_state& a, const cached_game_state& b) { + return a.data == b.data; +} - if (init_gs.rules.stock_size > 0) { - combine(seed, hash_value(piles[init_gs.stock])); - if (init_gs.rules.stock_deal_t == sdt::WASTE) { - combine(seed, hash_value(piles[init_gs.waste])); - } - } - if (init_gs.rules.hole) { - combine(seed, hash_value(piles[init_gs.hole])); - } +////////////////// +// STATE HASHER // +////////////////// - // Commutative - for (game_state::pile_ref pr : init_gs.cells) { - combine_commutative(seed, hash_value(piles[pr])); - } -#endif +hasher::hasher(const game_state& gs) : init_gs(gs) { +} + +size_t hasher::operator()(const cached_game_state& cgs) const { + size_t seed = 0; +#ifdef NO_REDUCED_STATE + for (const pile& d : cgs.data) { +#else + for (card d : cgs.data) { +#endif + combine(seed, hash_value(d)); + } return seed; } +#ifdef NO_REDUCED_STATE size_t hasher::hash_value(pile const& p) const { std::size_t seed = 0; @@ -156,28 +143,25 @@ size_t hasher::hash_value(pile const& p) const { } return seed; } +#endif 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()); + uint8_t suit_val = 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()); + suit_val = c.get_suit(); break; case pol::RED_BLACK: - suit_val = static_cast<uint8_t>(c.get_colour()); + suit_val = c.get_colour(); break; default: suit_val = 0; @@ -188,36 +172,24 @@ size_t hasher::hash_value(card const& c) const { return boost_hasher(raw_val); } -predicate::predicate(const game_state& gs) : init_gs(gs) { -} -bool predicate::operator()(const vector<pile>& x_piles, - const vector<pile>& y_piles) const { - for (vector<pile>::size_type pile_idx = 0; pile_idx < x_piles.size(); pile_idx++) { - if (x_piles[pile_idx].size() != y_piles[pile_idx].size()) return false; - for (auto card_idx = 0; card_idx < x_piles[pile_idx].size(); card_idx++) { - card cx = x_piles[pile_idx][card_idx]; - card cy = y_piles[pile_idx][card_idx]; +////////////////// +// GLOBAL CACHE // +////////////////// - if (cx.get_rank() != cy.get_rank()) return false; +bool global_cache::insert(const game_state& gs) { + return u_set.emplace(gs).second; +} -#ifdef SIMPLE_HASH - if (cx.get_suit() != cy.get_suit()) return false; -#else - switch (init_gs.rules.build_pol) { - case pol::SAME_SUIT: - if (cx.get_suit() != cy.get_suit()) return false; - break; - case pol::RED_BLACK: - if (cx.get_colour() != cy.get_colour()) return false; - break; - default: - break; - } -#endif - } - } +bool global_cache::contains(const game_state& gs) const { + return u_set.count(cached_game_state(gs)) > 0; +} - return true; +void global_cache::clear() { + u_set.clear(); +} + +global_cache::global_cache(const game_state& gs) + : u_set(0, hasher(gs)) { } diff --git a/src/main/game/global_cache.h b/src/main/game/global_cache.h index 17585ed..4658e52 100644 --- a/src/main/game/global_cache.h +++ b/src/main/game/global_cache.h @@ -7,26 +7,40 @@ #include <vector> #include <unordered_set> +#include <boost/pool/pool.hpp> +#include <boost/pool/pool_alloc.hpp> #include "sol_rules.h" #include "game_state.h" -class predicate { -public: - explicit predicate(const game_state&); - bool operator() (const std::vector<pile>&, const std::vector<pile>&) const; -private: - const game_state& init_gs; +struct cached_game_state { + typedef std::vector< +#ifdef NO_REDUCED_STATE + pile +#else + card +#endif + > state_data; + typedef state_data::size_type size_type; + + explicit cached_game_state(const game_state&); + void add_pile(game_state::pile_ref, const game_state&); + void add_card(card, const game_state&); + void add_card_divider(); + + state_data data; }; +bool operator==(const cached_game_state&, const cached_game_state&); struct hasher { explicit hasher(const game_state&); - std::size_t operator() (const std::vector<pile>&) const; + std::size_t operator() (const cached_game_state&) const; std::size_t hash_value(const card&) const; +#ifdef NO_REDUCED_STATE std::size_t hash_value(const pile&) const; +#endif 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; }; @@ -39,12 +53,9 @@ public: void clear(); private: - static std::vector<pile> get_ordered_vec(const game_state&); - std::unordered_set< - std::vector<pile>, - hasher, - predicate + cached_game_state, + hasher > u_set; }; diff --git a/src/main/game/pile.cpp b/src/main/game/pile.cpp index 6d52335..367f7a0 100644 --- a/src/main/game/pile.cpp +++ b/src/main/game/pile.cpp @@ -52,17 +52,33 @@ bool operator==(const pile& a, const pile& b) { } bool operator<(const pile& a, const pile& b) { - return a.pile_vec < b.pile_vec; + if (a.size() == b.size()) { + return a.pile_vec < b.pile_vec; + } else { + return a.size() < b.size(); + } } bool operator>(const pile& a, const pile& b) { - return a.pile_vec > b.pile_vec; + if (a.size() == b.size()) { + return a.pile_vec > b.pile_vec; + } else { + return a.size() > b.size(); + } } bool operator<=(const pile& a, const pile& b) { - return a.pile_vec <= b.pile_vec; + if (a.size() == b.size()) { + return a.pile_vec <= b.pile_vec; + } else { + return a.size() <= b.size(); + } } bool operator>=(const pile& a, const pile& b) { - return a.pile_vec >= b.pile_vec; + if (a.size() == b.size()) { + return a.pile_vec >= b.pile_vec; + } else { + return a.size() >= b.size(); + } } diff --git a/src/main/game/pile.h b/src/main/game/pile.h index 4256b02..7257738 100644 --- a/src/main/game/pile.h +++ b/src/main/game/pile.h @@ -14,6 +14,7 @@ class pile { friend class hasher; friend class game_state; + friend class cached_game_state; public: typedef uint8_t size_type; |
