From 252c7398925b5267963c1a48251cd7ba2d4ec381 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 25 Jan 2018 15:20:54 +0000 Subject: Refactor game_state & cache to remove static hack --- src/main/game/game_state.cpp | 30 +--------- src/main/game/game_state.h | 23 ++++---- src/main/game/global_cache.cpp | 130 ++++++++++++++++++++++++----------------- src/main/game/global_cache.h | 41 ++++++++----- src/main/game/pile.h | 2 +- 5 files changed, 116 insertions(+), 110 deletions(-) (limited to 'src/main/game') 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::tableau_piles; -std::vector game_state::cells; -game_state::pile_ref game_state::stock; -game_state::pile_ref game_state::waste; -std::vector game_state::reserve; -std::vector 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 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 tableau_piles; - static std::vector cells; - static pile_ref stock; - static pile_ref waste; - static std::vector reserve; - static std::vector foundations; - static pile_ref hole; + const sol_rules rules; + std::vector tableau_piles; + std::vector cells; + pile_ref stock; + pile_ref waste; + std::vector reserve; + std::vector foundations; + pile_ref hole; // The core piles std::vector 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 +#include + #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& 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& piles) const { + return u_set.count(piles) > 0; } void global_cache::clear() { u_set.clear(); } -size_t hash_value(card const& c) { - boost::hash hasher; - -#ifdef SIMPLE_HASH - uint8_t suit_val = static_cast(c.get_suit()); -#else - uint8_t suit_val; - switch (game_state::rules.build_pol) { - case pol::SAME_SUIT: - suit_val = static_cast(c.get_suit()); - break; - case pol::RED_BLACK: - suit_val = static_cast(c.get_colour()); - break; - default: - suit_val = 0; - } -#endif - - auto raw_val = static_cast(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& 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_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 boost_hasher; + +#ifdef SIMPLE_HASH + uint8_t suit_val = static_cast(c.get_suit()); +#else + uint8_t suit_val; + switch (init_gs.rules.build_pol) { + case pol::SAME_SUIT: + suit_val = static_cast(c.get_suit()); + break; + case pol::RED_BLACK: + suit_val = static_cast(c.get_colour()); + break; + default: + suit_val = 0; + } +#endif + + auto raw_val = static_cast(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& x, + const std::vector& y) const { + if (x.size() != y.size()) return false; - vector x_piles = x.piles; - vector y_piles = y.piles; + vector x_piles = x; + vector 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 #include -#include - #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&, const std::vector&) 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&) 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&); + bool contains(const std::vector&) const; void clear(); -private: + std::unordered_set< - game_state, - boost::hash, - game_state_pred + std::vector, + 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 pile_vec; -- cgit v1.2.1 From 572daefb2dcf870c43751a990d703917ac2565aa Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Fri, 26 Jan 2018 22:20:51 +0000 Subject: Fix hash bug --- src/main/game/global_cache.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/main/game') diff --git a/src/main/game/global_cache.cpp b/src/main/game/global_cache.cpp index 1ce8d5d..e58ac04 100644 --- a/src/main/game/global_cache.cpp +++ b/src/main/game/global_cache.cpp @@ -59,10 +59,10 @@ size_t hasher::operator()(const vector& piles) const { 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.stock_deal_t == sdt::WASTE) { + combine(seed, hash_value(piles[init_gs.waste])); + } } if (init_gs.rules.hole) { -- cgit v1.2.1 From e4e124e9b055a5189cf885901cfdb984c49e84c7 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Fri, 26 Jan 2018 22:25:07 +0000 Subject: Fix simple hash bug --- src/main/game/global_cache.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/main/game') diff --git a/src/main/game/global_cache.cpp b/src/main/game/global_cache.cpp index e58ac04..bb0c433 100644 --- a/src/main/game/global_cache.cpp +++ b/src/main/game/global_cache.cpp @@ -39,7 +39,7 @@ size_t hasher::operator()(const vector& piles) const { #ifdef SIMPLE_HASH for (game_state::pile_ref pr = 0; pr < piles.size(); pr++) { - seed += combine(hash_value(piles[pr])); + seed += combine(seed, hash_value(piles[pr])); } #else // Using addition for commutative hash -- cgit v1.2.1 From 9ae4c039b998943dbe771772ee06dfbcad3d6b8a Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Sat, 27 Jan 2018 00:14:42 +0000 Subject: Alter printing to work when pile refs are changed --- src/main/game/game_state.cpp | 11 +++++++---- src/main/game/game_state.h | 6 ++++++ 2 files changed, 13 insertions(+), 4 deletions(-) (limited to 'src/main/game') diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp index 8f45a38..bcab777 100644 --- a/src/main/game/game_state.cpp +++ b/src/main/game/game_state.cpp @@ -48,6 +48,7 @@ game_state::game_state(const sol_rules& s_rules) : rules(s_rules) { for (uint8_t i = 0; i < rules.cells; i++) { piles.emplace_back(); cells.push_back(static_cast(piles.size() - 1)); + original_cells.push_back(static_cast(piles.size() - 1)); } } @@ -70,6 +71,7 @@ game_state::game_state(const sol_rules& s_rules) : rules(s_rules) { for (uint8_t i = 0; i < pile_count; i++) { piles.emplace_back(); reserve.push_back(static_cast(piles.size() - 1)); + original_reserve.push_back(static_cast(piles.size() - 1)); } } @@ -77,6 +79,7 @@ game_state::game_state(const sol_rules& s_rules) : rules(s_rules) { for (uint8_t i = 0; i < rules.tableau_pile_count; i++) { piles.emplace_back(); tableau_piles.push_back(static_cast(piles.size() - 1)); + original_tableau_piles.push_back(static_cast(piles.size() - 1)); } } @@ -210,8 +213,8 @@ 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()); } @@ -239,8 +242,8 @@ 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()); } diff --git a/src/main/game/game_state.h b/src/main/game/game_state.h index 8434bf7..e8e2f96 100644 --- a/src/main/game/game_state.h +++ b/src/main/game/game_state.h @@ -77,6 +77,12 @@ private: std::vector 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 original_tableau_piles; + std::vector original_cells; + std::vector original_reserve; + // The core piles std::vector piles; }; -- cgit v1.2.1 From 0193418c62d07c7f4134ea65c11fc606cbc0c3f7 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Tue, 30 Jan 2018 15:46:16 +0000 Subject: Update game_state so comm piles always ordered --- src/main/game/game_state.cpp | 136 ++++++++++++++++++++++++++++++++++++------- src/main/game/game_state.h | 21 ++++--- 2 files changed, 128 insertions(+), 29 deletions(-) (limited to 'src/main/game') diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp index bcab777..ad2ac3f 100644 --- a/src/main/game/game_state.cpp +++ b/src/main/game/game_state.cpp @@ -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 il) { - piles = il; +game_state::game_state(const sol_rules& s_rules, + std::initializer_list 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 @@ -216,22 +224,22 @@ void game_state::make_move(const move m) { 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); } } } @@ -245,24 +253,108 @@ void game_state::undo_move(const move m) { 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); + eval_pile_order(pr, true); +} + +// Same as above but for taking cards +card game_state::take_card(pile_ref pr) { + card c = piles[pr].take(); + eval_pile_order(pr, false); + return c; +} + +// 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_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 } diff --git a/src/main/game/game_state.h b/src/main/game/game_state.h index e8e2f96..fe43d99 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 +#include #include #include @@ -20,6 +21,7 @@ class game_state { friend class state_printer; friend class predicate; friend class hasher; + friend class global_cache; public: // Defines types typedef uint8_t pile_ref; @@ -31,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); + // Does the same but with an initialiser list (useful for testing) + game_state(const sol_rules&, std::initializer_list); // Alters state void make_move(move); void undo_move(move); + void place_card(pile_ref, card); + card take_card(pile_ref); std::vector get_legal_moves() const; @@ -55,6 +58,10 @@ private: // Private constructor explicit game_state(const sol_rules&); + // Used to maintain pile order invariant + void eval_pile_order(pile_ref, bool); + void eval_pile_order(std::list&, pile_ref, bool); + // Used in get_legal_moves() move get_stock_tableau_move() const; bool is_valid_tableau_move(pile_ref, pile_ref) const; @@ -69,11 +76,11 @@ private: // References to piles const sol_rules rules; - std::vector tableau_piles; - std::vector cells; + std::list tableau_piles; + std::list cells; pile_ref stock; pile_ref waste; - std::vector reserve; + std::list reserve; std::vector foundations; pile_ref hole; -- cgit v1.2.1 From 27d9dff6f3df31395f7d30aeeca121d0bfcfd30c Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Tue, 30 Jan 2018 15:48:53 +0000 Subject: Update global cache to insert ordered vec --- src/main/game/global_cache.cpp | 98 ++++++++++++++++++++++++++---------------- src/main/game/global_cache.h | 13 +++--- 2 files changed, 69 insertions(+), 42 deletions(-) (limited to 'src/main/game') diff --git a/src/main/game/global_cache.cpp b/src/main/game/global_cache.cpp index bb0c433..a595cbc 100644 --- a/src/main/game/global_cache.cpp +++ b/src/main/game/global_cache.cpp @@ -14,13 +14,12 @@ using namespace boost; typedef sol_rules::build_policy pol; typedef sol_rules::stock_deal_type sdt; - -bool global_cache::insert(const std::vector& piles) { - return u_set.insert(piles).second; +bool global_cache::insert(const game_state& gs) { + return u_set.emplace(get_ordered_vec(gs)).second; } -bool global_cache::contains(const std::vector& piles) const { - return u_set.count(piles) > 0; +bool global_cache::contains(const game_state& gs) const { + return u_set.count(get_ordered_vec(gs)) > 0; } void global_cache::clear() { @@ -31,6 +30,60 @@ global_cache::global_cache(const game_state& gs) : u_set(0, hasher(gs), predicate(gs)) { } +std::vector global_cache::get_ordered_vec(const game_state& gs) { + vector gs_vec = gs.get_data(); + vector ordered_vec; + + 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 + + return ordered_vec; +} + + + hasher::hasher(const game_state& gs) : init_gs(gs) { } @@ -118,37 +171,11 @@ size_t hasher::hash_value(card const& c) const { 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(); - } - // Larger card values first - for (pile::size_type pile_idx = 0; pile_idx < x.size(); pile_idx++) { - auto x_val = static_cast(x[pile_idx].get_suit()) * 13 - + x[pile_idx].get_rank(); - auto y_val = static_cast(y[pile_idx].get_suit()) * 13 - + y[pile_idx].get_rank(); - if (x_val != y_val) { - return x_val > y_val; - } - } - // It is a requirement of std::sort that equal values return false - return false; +predicate::predicate(const game_state& gs) : init_gs(gs) { } -bool predicate::operator()(const std::vector& x, - const std::vector& y) const { - if (x.size() != y.size()) return false; - - vector x_piles = x; - vector y_piles = y; - - // 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); - - // Compares the two for equality +bool predicate::operator()(const vector& x_piles, + const vector& y_piles) const { for (vector::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; @@ -177,6 +204,3 @@ bool predicate::operator()(const std::vector& 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 0e8fe0c..2d99275 100644 --- a/src/main/game/global_cache.h +++ b/src/main/game/global_cache.h @@ -13,8 +13,7 @@ class predicate { public: - explicit predicate(const game_state&); - static bool comp_pile(const pile&, const pile&); + predicate(const game_state&); bool operator() (const std::vector&, const std::vector&) const; private: const game_state& init_gs; @@ -32,12 +31,16 @@ struct hasher { const game_state& init_gs; }; -struct global_cache { +class global_cache { +public: explicit global_cache(const game_state&); - bool insert(const std::vector&); - bool contains(const std::vector&) const; + bool insert(const game_state&); + bool contains(const game_state&) const; void clear(); +private: + static std::vector get_ordered_vec(const game_state&); + std::unordered_set< std::vector, hasher, -- cgit v1.2.1 From 583b9a005ebd447d39c0515e2692178cc1dfe7b9 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Tue, 30 Jan 2018 15:49:28 +0000 Subject: Update card & pile with comp operators for sorting --- src/main/game/card.cpp | 28 ++++++++++++++++++++++++++++ src/main/game/card.h | 4 ++++ src/main/game/pile.cpp | 16 ++++++++++++++++ src/main/game/pile.h | 13 +++++++++---- 4 files changed, 57 insertions(+), 4 deletions(-) (limited to 'src/main/game') 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/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 2bf2d5b..4256b02 100644 --- a/src/main/game/pile.h +++ b/src/main/game/pile.h @@ -13,6 +13,7 @@ class pile { friend class hasher; + friend class game_state; public: typedef uint8_t size_type; @@ -25,16 +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 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 pile_vec; }; -- cgit v1.2.1 From 48f464b788ef0f2be539ec05b6e370ef46cfd015 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Tue, 30 Jan 2018 19:48:52 +0000 Subject: Add logic for ORDER_ON_CACHE to game_state --- src/main/game/game_state.cpp | 6 ++++++ src/main/game/game_state.h | 2 ++ 2 files changed, 8 insertions(+) (limited to 'src/main/game') diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp index ad2ac3f..faab429 100644 --- a/src/main/game/game_state.cpp +++ b/src/main/game/game_state.cpp @@ -277,16 +277,21 @@ 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) { @@ -356,6 +361,7 @@ void game_state::eval_pile_order(list& pile_lst, pile_ref changed_pr, } #endif } +#endif //////////////////////// diff --git a/src/main/game/game_state.h b/src/main/game/game_state.h index fe43d99..532eefc 100644 --- a/src/main/game/game_state.h +++ b/src/main/game/game_state.h @@ -58,9 +58,11 @@ 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, bool); +#endif // Used in get_legal_moves() move get_stock_tableau_move() const; -- cgit v1.2.1 From cc8313d56e31570659961fa71ef8cbe423c18e89 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Tue, 30 Jan 2018 19:49:15 +0000 Subject: Add logic for ORDER_ON_CACHE to global_cache --- src/main/game/global_cache.cpp | 19 ++++++++++++++++++- src/main/game/global_cache.h | 2 +- 2 files changed, 19 insertions(+), 2 deletions(-) (limited to 'src/main/game') diff --git a/src/main/game/global_cache.cpp b/src/main/game/global_cache.cpp index a595cbc..bf55f67 100644 --- a/src/main/game/global_cache.cpp +++ b/src/main/game/global_cache.cpp @@ -31,8 +31,24 @@ global_cache::global_cache(const game_state& gs) } std::vector global_cache::get_ordered_vec(const game_state& gs) { - vector gs_vec = gs.get_data(); +#ifdef ORDER_ON_CACHE + vector 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 + const vector& gs_vec = gs.get_data(); vector ordered_vec; + ordered_vec.reserve(gs_vec.size()); if (gs.rules.hole) ordered_vec.push_back(gs_vec[gs.hole]); @@ -77,6 +93,7 @@ std::vector global_cache::get_ordered_vec(const game_state& gs) { if (pr == gs.original_tableau_piles[gs.original_tableau_piles.size()-2]) break; } } +#endif #endif return ordered_vec; diff --git a/src/main/game/global_cache.h b/src/main/game/global_cache.h index 2d99275..17585ed 100644 --- a/src/main/game/global_cache.h +++ b/src/main/game/global_cache.h @@ -13,7 +13,7 @@ class predicate { public: - predicate(const game_state&); + explicit predicate(const game_state&); bool operator() (const std::vector&, const std::vector&) const; private: const game_state& init_gs; -- cgit v1.2.1