diff options
| author | Charlie Blake | 2018-01-30 19:51:38 +0000 |
|---|---|---|
| committer | GitHub | 2018-01-30 19:51:38 +0000 |
| commit | 2fc005bdbb4707810ad0ae36a6863e70f0ff91b5 (patch) | |
| tree | 59ca3063ffe6a876d27c96ea95d6a3683273675a /src/main | |
| parent | Merge pull request #31 from thecharlesblake/profiling-fix (diff) | |
| parent | Add ORDER_ON_CACHE to benchmark script (diff) | |
| download | metasol-2fc005bdbb4707810ad0ae36a6863e70f0ff91b5.tar.gz metasol-2fc005bdbb4707810ad0ae36a6863e70f0ff91b5.zip | |
Merge pull request #32 from thecharlesblake/piles-always-sorted
Piles always sorted
Diffstat (limited to 'src/main')
| -rw-r--r-- | src/main/game/card.cpp | 28 | ||||
| -rw-r--r-- | src/main/game/card.h | 4 | ||||
| -rw-r--r-- | src/main/game/game_state.cpp | 183 | ||||
| -rw-r--r-- | src/main/game/game_state.h | 46 | ||||
| -rw-r--r-- | src/main/game/global_cache.cpp | 197 | ||||
| -rw-r--r-- | src/main/game/global_cache.h | 34 | ||||
| -rw-r--r-- | src/main/game/pile.cpp | 16 | ||||
| -rw-r--r-- | src/main/game/pile.h | 15 | ||||
| -rw-r--r-- | src/main/input-output/input/json-parsing/deal_parser.cpp | 49 | ||||
| -rw-r--r-- | src/main/input-output/output/state_printer.cpp | 6 | ||||
| -rw-r--r-- | src/main/solver/solver.cpp | 4 |
11 files changed, 399 insertions, 183 deletions
diff --git a/src/main/game/card.cpp b/src/main/game/card.cpp index 5189171..25a981f 100644 --- a/src/main/game/card.cpp +++ b/src/main/game/card.cpp @@ -82,3 +82,31 @@ bool operator==(const card& l, const card& r) { bool operator!=(const card& l, const card& r) { return !(l == r); } + +bool operator<(const card& l, const card& r) { + if (l.get_rank() == r.get_rank()) + return l.get_suit() < r.get_suit(); + else + return l.get_rank() < r.get_rank(); +} + +bool operator>(const card& l, const card& r) { + if (l.get_rank() == r.get_rank()) + return l.get_suit() > r.get_suit(); + else + return l.get_rank() > r.get_rank(); +} + +bool operator<=(const card& l, const card& r) { + if (l.get_rank() == r.get_rank()) + return l.get_suit() <= r.get_suit(); + else + return l.get_rank() <= r.get_rank(); +} + +bool operator>=(const card& l, const card& r) { + if (l.get_rank() == r.get_rank()) + return l.get_suit() >= r.get_suit(); + else + return l.get_rank() >= r.get_rank(); +} diff --git a/src/main/game/card.h b/src/main/game/card.h index aec6a88..318cd12 100644 --- a/src/main/game/card.h +++ b/src/main/game/card.h @@ -48,6 +48,10 @@ private: bool operator==(const card&, const card&); bool operator!=(const card&, const card&); +bool operator<(const card&, const card&); +bool operator>(const card&, const card&); +bool operator<=(const card&, const card&); +bool operator>=(const card&, const card&); #endif //SOLITAIRE_PIP_CARD_H diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp index 8075f50..faab429 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(); @@ -51,6 +48,7 @@ game_state::game_state(const sol_rules& s_rules) { for (uint8_t i = 0; i < rules.cells; i++) { piles.emplace_back(); cells.push_back(static_cast<pile_ref>(piles.size() - 1)); + original_cells.push_back(static_cast<pile_ref>(piles.size() - 1)); } } @@ -73,6 +71,7 @@ game_state::game_state(const sol_rules& s_rules) { for (uint8_t i = 0; i < pile_count; i++) { piles.emplace_back(); reserve.push_back(static_cast<pile_ref>(piles.size() - 1)); + original_reserve.push_back(static_cast<pile_ref>(piles.size() - 1)); } } @@ -80,6 +79,7 @@ game_state::game_state(const sol_rules& s_rules) { for (uint8_t i = 0; i < rules.tableau_pile_count; i++) { piles.emplace_back(); tableau_piles.push_back(static_cast<pile_ref>(piles.size() - 1)); + original_tableau_piles.push_back(static_cast<pile_ref>(piles.size() - 1)); } } @@ -97,7 +97,7 @@ game_state::game_state(const sol_rules& s_rules, int seed) // If there is a hole, moves the ace of spades to it if (rules.hole) { deck.erase(find(begin(deck), end(deck), card("AS"))); - piles[hole].place(card("AS")); + place_card(hole, card("AS")); } // If the foundations begin filled, then fills them @@ -106,14 +106,14 @@ game_state::game_state(const sol_rules& s_rules, int seed) card c = card(f_idx % uint8_t(4), 1); deck.erase(find(begin(deck), end(deck), c)); - piles[foundations[f_idx]].place(c); + place_card(foundations[f_idx], c); } } // If there is a stock, deals to it and set up a waste pile too if (rules.stock_size > 0) { for (unsigned int i = 0; i < rules.stock_size; i++) { - piles[stock].place(deck.back()); + place_card(stock, deck.back()); deck.pop_back(); } } @@ -124,7 +124,7 @@ game_state::game_state(const sol_rules& s_rules, int seed) if (rules.reserve_size > 0) { for (unsigned int i = 0; i < rules.reserve_size; i++) { int idx = rules.reserve_stacked ? 0 : i; - piles[reserve[idx]].place(deck.back()); + place_card(original_reserve[idx], deck.back()); deck.pop_back(); } } @@ -137,22 +137,22 @@ game_state::game_state(const sol_rules& s_rules, int seed) card c = deck.back(); // Add the randomly generated card to the tableau piles - auto p = t % tableau_piles.size(); + auto p = t % original_tableau_piles.size(); // If we are doing a diagonal deal, each row should have one fewer card. // Leftover cards are dealt normally in full rows. - auto row_idx = t / tableau_piles.size(); - if (rules.diagonal_deal && row_idx < tableau_piles.size()) { - p = tableau_piles.size()-p-1; - pile_ref tableau_pile = tableau_piles[p]; + auto row_idx = t / original_tableau_piles.size(); + if (rules.diagonal_deal && row_idx < original_tableau_piles.size()) { + p = original_tableau_piles.size()-p-1; + pile_ref tableau_pile = original_tableau_piles[p]; if (p >= row_idx) { - piles[tableau_pile].place(c); + place_card(tableau_pile, c); deck.pop_back(); } } else { - pile_ref tableau_pile = tableau_piles[p]; - piles[tableau_pile].place(c); + pile_ref tableau_pile = original_tableau_piles[p]; + place_card(tableau_pile, c); deck.pop_back(); } } @@ -165,8 +165,16 @@ game_state::game_state(const sol_rules& s_rules, int seed) } } -game_state::game_state(std::initializer_list<pile> il) { - piles = il; +game_state::game_state(const sol_rules& s_rules, + std::initializer_list<pile> il) + : game_state(s_rules) { + pile_ref pr = 0; + for (const pile& p : il) { + for (const card c : p.pile_vec) { + place_card(pr, c); + } + pr++; + } } // Generates a randomly ordered vector of cards @@ -203,31 +211,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 // ///////////////////////// @@ -238,25 +221,25 @@ void game_state::make_move(const move m) { // Handles special stock-to-tableau-piles move if (rules.stock_deal_t == sdt::TABLEAU_PILES && m.from == stock) { - for (pile_ref tab_pr = tableau_piles.front(); - tab_pr < tableau_piles.front() + m.count; + for (pile_ref tab_pr = original_tableau_piles.front(); + tab_pr < original_tableau_piles.front() + m.count; tab_pr++) { - piles[tab_pr].place(piles[stock].take()); + place_card(tab_pr, take_card(stock)); } } // If this is not a built-pile move else if (m.count == 1) { - piles[m.to].place(piles[m.from].take()); + place_card(m.to, take_card(m.from)); } else { assert(m.count <= rules.max_rank); // Adds the cards to the 'to' pile for (auto pile_idx = m.count; pile_idx-- > 0;) { - piles[m.to].place(piles[m.from][pile_idx]); + place_card(m.to, piles[m.from][pile_idx]); } // Removes the cards from the 'from' pile for (uint8_t rem_count = 0; rem_count < m.count; rem_count++) { - piles[m.from].take(); + take_card(m.from); } } } @@ -267,28 +250,118 @@ void game_state::undo_move(const move m) { // Handles special stock-to-tableau-piles move if (rules.stock_deal_t == sdt::TABLEAU_PILES && m.from == stock) { - for (pile_ref tab_pr = tableau_piles.front() + m.count; - tab_pr-- > tableau_piles.front(); + for (pile_ref tab_pr = original_tableau_piles.front() + m.count; + tab_pr-- > original_tableau_piles.front(); ) { - piles[stock].place(piles[tab_pr].take()); + place_card(stock, take_card(tab_pr)); } } // If this is not a built-pile move else if (m.count == 1) { - piles[m.from].place(piles[m.to].take()); + place_card(m.from, take_card(m.to)); } else { assert(m.count <= rules.max_rank); // Adds the cards to the 'from' pile for (auto pile_idx = m.count; pile_idx-- > 0;) { - piles[m.from].place(piles[m.to][pile_idx]); + place_card(m.from, piles[m.to][pile_idx]); } // Removes the cards from the 'to' pile for (uint8_t rem_count = 0; rem_count < m.count; rem_count++) { - piles[m.to].take(); + take_card(m.to); + } + } +} + +// Places a card on a pile and if it is on a tableau, cell or reserve pile, +// 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) { + if (!original_tableau_piles.empty() + && pr >= original_tableau_piles[0] + && pr <= original_tableau_piles[0] + original_tableau_piles.size()) { + + eval_pile_order(tableau_piles, pr, is_place); + } else if (!original_cells.empty() + && pr >= original_cells[0] + && pr <= original_cells[0] + original_cells.size()) { + + eval_pile_order(cells, pr, is_place); + } else if (!original_reserve.empty() + && pr >= original_reserve[0] + && pr <= original_reserve[0] + original_reserve.size()) { + + eval_pile_order(reserve, pr, is_place); + } +} + +// Finds the pile ref in the list and evaluates whether it should be moved to +// maintain the "pile order" +void game_state::eval_pile_order(list<pile_ref>& pile_lst, pile_ref changed_pr, + bool is_place) { + // Finds the first pile that is larger/smaller than the ref pile + // (the comp pile). + // If the card has been placed, search for the ref from the right hand side + // and if not from the lhs + if (is_place) { + auto changed_ref_iter = find(rbegin(pile_lst), rend(pile_lst), changed_pr); + auto comp_iter = changed_ref_iter; + + while (++comp_iter != rend(pile_lst) + && piles[*comp_iter] < piles[*changed_ref_iter]); + + if (comp_iter != ++changed_ref_iter) { + // Places the ref pile before the comp pile + pile_lst.insert(comp_iter.base(), changed_pr); + pile_lst.erase(changed_ref_iter.base()); + } + } else { + auto changed_ref_iter = find(begin(pile_lst), end(pile_lst), changed_pr); + auto comp_iter = changed_ref_iter; + + while (++comp_iter != end(pile_lst) + && piles[*comp_iter] > piles[*changed_ref_iter]); + + if (comp_iter != ++changed_ref_iter) { + // Places the ref pile before the comp pile + pile_lst.insert(comp_iter, changed_pr); + pile_lst.erase(--changed_ref_iter); } } + +#ifndef NDEBUG + // Makes sure the piles are in order + if (pile_lst.size() > 1) { + auto prev = begin(pile_lst); + auto cur = begin(pile_lst); + cur++; + + while (cur != end(pile_lst)) { + assert(piles[*prev] >= piles[*cur]); + prev++; cur++; + } + } +#endif } +#endif //////////////////////// diff --git a/src/main/game/game_state.h b/src/main/game/game_state.h index 8dcffa4..532eefc 100644 --- a/src/main/game/game_state.h +++ b/src/main/game/game_state.h @@ -6,6 +6,7 @@ #define SOLVITAIRE_GAME_STATE_H #include <vector> +#include <list> #include <string> #include <rapidjson/document.h> @@ -18,7 +19,9 @@ class game_state { friend class deal_parser; friend class state_printer; - friend class game_state_pred; + friend class predicate; + friend class hasher; + friend class global_cache; public: // Defines types typedef uint8_t pile_ref; @@ -30,15 +33,16 @@ public: // Constructors // Creates a game state representation from a JSON doc game_state(const sol_rules&, const rapidjson::Document&); - // Does the same from a 'rules' object + // Does the same from a seed game_state(const sol_rules&, int seed); - - // For testing - game_state(std::initializer_list<pile>); + // Does the same but with an initialiser list (useful for testing) + game_state(const sol_rules&, std::initializer_list<pile>); // Alters state void make_move(move); void undo_move(move); + void place_card(pile_ref, card); + card take_card(pile_ref); std::vector<move> get_legal_moves() const; @@ -48,16 +52,18 @@ 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 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; bool is_valid_tableau_move(pile_ref, pile_ref) const; @@ -71,14 +77,20 @@ 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::list<pile_ref> tableau_piles; + std::list<pile_ref> cells; + pile_ref stock; + pile_ref waste; + std::list<pile_ref> reserve; + std::vector<pile_ref> foundations; + pile_ref hole; + + // Stores the original pile refs of the commutative piles so that they can + // be printed in the order the user expects + std::vector<pile_ref> original_tableau_piles; + std::vector<pile_ref> original_cells; + std::vector<pile_ref> original_reserve; // 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..bf55f67 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; @@ -13,121 +15,184 @@ 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; + return u_set.emplace(get_ordered_vec(gs)).second; } bool global_cache::contains(const game_state& gs) const { - return u_set.count(gs) > 0; + return u_set.count(get_ordered_vec(gs)) > 0; } void global_cache::clear() { u_set.clear(); } -size_t hash_value(card const& c) { - boost::hash<uint8_t> hasher; +global_cache::global_cache(const game_state& gs) + : u_set(0, hasher(gs), predicate(gs)) { +} -#ifdef SIMPLE_HASH - uint8_t suit_val = static_cast<uint8_t>(c.get_suit()); +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()); + } #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; + 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]); + + for (game_state::pile_ref pr : gs.foundations) + ordered_vec.push_back(gs_vec[pr]); + + for (game_state::pile_ref pr : gs.cells) + ordered_vec.push_back(gs_vec[pr]); + + if (gs.rules.stock_size > 0) { + ordered_vec.push_back(gs_vec[gs.stock]); + + if (gs.rules.stock_deal_t == sdt::WASTE) { + ordered_vec.push_back(gs_vec[gs.waste]); + } + } + + 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; + } + } + 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; + } } #endif +#endif - auto raw_val = static_cast<uint8_t>(suit_val * 13 + c.get_rank()); - return hasher(raw_val); + return ordered_vec; } -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(seed, 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) { - // Largest piles first - if (x.size() != y.size()) { - return x.size() > y.size(); - } - // Larger card values first - for (pile::size_type pile_idx = 0; pile_idx < x.size(); pile_idx++) { - auto x_val = static_cast<uint8_t>(x[pile_idx].get_suit()) * 13 - + x[pile_idx].get_rank(); - auto y_val = static_cast<uint8_t>(y[pile_idx].get_suit()) * 13 - + y[pile_idx].get_rank(); - if (x_val != y_val) { - return x_val > y_val; - } +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)); } - // It is a requirement of std::sort that equal values return false - return false; + return seed; +} + +std::size_t hasher::combine(std::size_t& seed, std::size_t value) const { + return seed ^= value + 0x9e3779b9 + (seed<<6) + (seed>>2); } -bool game_state_pred::operator()(const game_state& x, - const game_state& y) const { - if (x.piles.size() != y.piles.size()) return false; +std::size_t hasher::combine_commutative(std::size_t& seed, std::size_t value) const { + return seed += value; +} - vector<pile> x_piles = x.piles; - vector<pile> y_piles = y.piles; +size_t hasher::hash_value(card const& c) const { + boost::hash<uint8_t> boost_hasher; - // Orders the piles from largest to smallest - sort(begin(x_piles), end(x_piles), comp_pile); - sort(begin(y_piles), end(y_piles), comp_pile); +#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); +} + +predicate::predicate(const game_state& gs) : init_gs(gs) { +} - // Compares the two for equality +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; @@ -140,7 +205,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; diff --git a/src/main/game/global_cache.h b/src/main/game/global_cache.h index 486006c..17585ed 100644 --- a/src/main/game/global_cache.h +++ b/src/main/game/global_cache.h @@ -8,27 +8,43 @@ #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&); + bool operator() (const std::vector<pile>&, const std::vector<pile>&) const; +private: + const game_state& init_gs; +}; + +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; }; class global_cache { public: + explicit global_cache(const game_state&); bool insert(const game_state&); bool contains(const game_state&) const; void clear(); + private: + static std::vector<pile> get_ordered_vec(const game_state&); + 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.cpp b/src/main/game/pile.cpp index 21eddc0..6d52335 100644 --- a/src/main/game/pile.cpp +++ b/src/main/game/pile.cpp @@ -50,3 +50,19 @@ card pile::take() { bool operator==(const pile& a, const pile& b) { return a.pile_vec == b.pile_vec; } + +bool operator<(const pile& a, const pile& b) { + return a.pile_vec < b.pile_vec; +} + +bool operator>(const pile& a, const pile& b) { + return a.pile_vec > b.pile_vec; +} + +bool operator<=(const pile& a, const pile& b) { + return a.pile_vec <= b.pile_vec; +} + +bool operator>=(const pile& a, const pile& b) { + return a.pile_vec >= b.pile_vec; +} diff --git a/src/main/game/pile.h b/src/main/game/pile.h index 411f890..4256b02 100644 --- a/src/main/game/pile.h +++ b/src/main/game/pile.h @@ -12,6 +12,8 @@ #include "card.h" class pile { + friend class hasher; + friend class game_state; public: typedef uint8_t size_type; @@ -24,17 +26,20 @@ public: bool empty() const; size_type size() const; - // Modify - void place(card); - card take(); - // Indexes the vector, with the top card as index 0 card& operator[] (size_type); card operator[] (size_type) const; friend bool operator==(const pile&, const pile&); - friend std::size_t hash_value(pile const&); + friend bool operator<(const pile&, const pile&); + friend bool operator>(const pile&, const pile&); + friend bool operator<=(const pile&, const pile&); + friend bool operator>=(const pile&, const pile&); private: + // Modify + void place(card); + card take(); + // Underlying vector std::vector<card> pile_vec; }; diff --git a/src/main/input-output/input/json-parsing/deal_parser.cpp b/src/main/input-output/input/json-parsing/deal_parser.cpp index 7cf5f45..0112cda 100644 --- a/src/main/input-output/input/json-parsing/deal_parser.cpp +++ b/src/main/input-output/input/json-parsing/deal_parser.cpp @@ -13,32 +13,32 @@ typedef sol_rules::build_policy pol; void deal_parser::parse(game_state &gs, const rapidjson::Document& doc) { // Construct tableau piles - if (game_state::rules.tableau_pile_count > 0) { + if (gs.rules.tableau_pile_count > 0) { parse_tableau_piles(gs, doc); } // Construct hole card - if (game_state::rules.hole) { + if (gs.rules.hole) { parse_hole(gs, doc); } // Construct filled cells - if (game_state::rules.cells) { + if (gs.rules.cells) { parse_cells(gs, doc); } // Construct stock - if (game_state::rules.stock_size > 0) { + if (gs.rules.stock_size > 0) { parse_stock(gs, doc); } // Construct reserve - if (game_state::rules.reserve_size > 0) { + if (gs.rules.reserve_size > 0) { parse_reserve(gs, doc); } // If the foundations begin with cards in them, fill them - if (game_state::rules.foundations_init_card) { + if (gs.rules.foundations_init_card) { fill_foundations(gs); } } @@ -48,18 +48,17 @@ void deal_parser::parse_tableau_piles(game_state &gs, const rapidjson::Document& const Value& json_tab_piles = doc["tableau piles"]; assert(json_tab_piles.IsArray()); - if (json_tab_piles.Size() != game_state::rules.tableau_pile_count ) { + if (json_tab_piles.Size() != gs.rules.tableau_pile_count ) { json_helper::json_parse_err("Incorrect number of tableau piles"); } - for (auto p = std::make_pair(begin(json_tab_piles.GetArray()), begin(gs.tableau_piles)); - p.second != end(gs.tableau_piles); + for (auto p = std::make_pair(begin(json_tab_piles.GetArray()), begin(gs.original_tableau_piles)); + p.second != end(gs.original_tableau_piles); ++p.first, ++p.second) { - assert(p.first->IsArray()); for (auto& json_card : p.first->GetArray()) { assert(json_card.IsString()); - gs.piles[*p.second].place(card(json_card.GetString())); + gs.place_card(*p.second, card(json_card.GetString())); } } } @@ -69,7 +68,7 @@ void deal_parser::parse_hole(game_state &gs, const Document& doc) { const Value &json_hole = doc["hole"]; assert(json_hole.IsString()); - gs.piles[gs.hole].place(card(json_hole.GetString())); + gs.place_card(gs.hole, card(json_hole.GetString())); } } @@ -81,17 +80,17 @@ void deal_parser::parse_cells(game_state &gs, const Document& doc) { const auto json_cell_arr = json_cells.GetArray(); if (json_cell_arr.Empty()) return; - else if (json_cell_arr.Size() != game_state::rules.cells) { + else if (json_cell_arr.Size() != gs.rules.cells) { json_helper::json_parse_err("Incorrect number of cells"); } - for (auto p = std::make_pair(begin(json_cell_arr), begin(gs.cells)); - p.second != end(gs.cells); + for (auto p = std::make_pair(begin(json_cell_arr), begin(gs.original_cells)); + p.second != end(gs.original_cells); ++p.first, ++p.second) { auto json_card = p.first; assert(json_card->IsString()); - gs.piles[*p.second].place(card(json_card->GetString())); + gs.place_card(*p.second, card(json_card->GetString())); } } } @@ -101,13 +100,13 @@ void deal_parser::parse_stock(game_state &gs, const Document& doc) { const Value &json_stock = doc["stock"]; assert(json_stock.IsArray()); - if (json_stock.Size() != game_state::rules.stock_size) { + if (json_stock.Size() != gs.rules.stock_size) { json_helper::json_parse_err("Incorrect stock size"); } for (const Value& json_card : json_stock.GetArray()) { assert(json_card.IsString()); - gs.piles[gs.stock].place(card(json_card.GetString())); + gs.place_card(gs.stock, card(json_card.GetString())); } } } @@ -121,25 +120,23 @@ void deal_parser::parse_reserve(game_state &gs, const Document& doc) { // We treat a regular reserve like multiple single-card piles, // but a stacked reserve as a single multiple-card pile - if (json_reserve_piles.Size() != game_state::rules.reserve_size) { + if (json_reserve_piles.Size() != gs.rules.reserve_size) { json_helper::json_parse_err("Incorrect reserve size"); } for (game_state::pile_ref i = 0; i < json_card_arr.Size(); i++) { assert(json_card_arr[i].IsString()); - game_state::pile_ref pr = gs.reserve[0]; - if (!game_state::rules.reserve_stacked) pr += i; - gs.piles[pr].place(card(json_card_arr[i].GetString())); + game_state::pile_ref pr = gs.original_reserve[0]; + if (!gs.rules.reserve_stacked) pr += i; + gs.place_card(pr, card(json_card_arr[i].GetString())); } } void deal_parser::fill_foundations(game_state &gs) { - auto foundations_count = uint8_t(4 * (game_state::rules.two_decks ? 2:1)); + auto foundations_count = uint8_t(4 * (gs.rules.two_decks ? 2:1)); assert(gs.foundations.size() == foundations_count); for (uint8_t f_idx = 0; f_idx < foundations_count; f_idx++) { - gs.piles[gs.foundations[f_idx]].place( - card(f_idx % uint8_t(4), 1) - ); + gs.place_card(gs.foundations[f_idx], card(f_idx % uint8_t(4), 1)); } } diff --git a/src/main/input-output/output/state_printer.cpp b/src/main/input-output/output/state_printer.cpp index 1d1c182..0fdacae 100644 --- a/src/main/input-output/output/state_printer.cpp +++ b/src/main/input-output/output/state_printer.cpp @@ -15,11 +15,11 @@ ostream& state_printer::print(ostream& stream, const game_state& gs) { } if (gs.rules.cells) { state_printer::print_header(stream, "Cells"); - state_printer::print_piles(stream, gs.cells, gs); + state_printer::print_piles(stream, gs.original_cells, gs); } if (gs.rules.tableau_pile_count > 0) { state_printer::print_header(stream, "Tableau Piles"); - state_printer::print_piles(stream, gs.tableau_piles, gs); + state_printer::print_piles(stream, gs.original_tableau_piles, gs); } if (gs.rules.reserve_size > 0) { if (gs.rules.reserve_stacked) { @@ -27,7 +27,7 @@ ostream& state_printer::print(ostream& stream, const game_state& gs) { } else { state_printer::print_header(stream, "Reserve"); } - state_printer::print_piles(stream, gs.reserve, gs); + state_printer::print_piles(stream, gs.original_reserve, gs); } if (gs.rules.stock_size > 0) { if (gs.rules.stock_deal_t == sdt::WASTE) { diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp index 19fa79b..6dddfb4 100644 --- a/src/main/solver/solver.cpp +++ b/src/main/solver/solver.cpp @@ -16,7 +16,7 @@ using namespace std; using namespace boost; solver::solver(const game_state& gs) - : initial_state(gs), states_searched(0) {} + : cache(gs), initial_state(gs), states_searched(0) {} solver::node::node(const game_state::move m, vector<game_state::move> uc) @@ -24,7 +24,7 @@ solver::node::node(const game_state::move m, } bool solver::run() { - ProfilerStart("solvitaire"); + //ProfilerStart("solvitaire"); game_state state = initial_state; states_searched++; |
