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 +- .../input/json-parsing/deal_parser.cpp | 24 ++-- src/main/solver/solver.cpp | 10 +- src/test/gloabl_cache_test.cpp | 102 ---------------- src/test/global_cache_test.cpp | 93 +++++++++++++++ src/test/test_helper.cpp | 29 +++-- 10 files changed, 239 insertions(+), 245 deletions(-) delete mode 100644 src/test/gloabl_cache_test.cpp create mode 100644 src/test/global_cache_test.cpp (limited to 'src') 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; 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..09b2065 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,7 +48,7 @@ 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"); } @@ -81,7 +81,7 @@ 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"); } @@ -101,7 +101,7 @@ 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"); } @@ -121,20 +121,20 @@ 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; + if (!gs.rules.reserve_stacked) pr += i; gs.piles[pr].place(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++) { diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp index 19fa79b..072db18 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 uc) @@ -30,7 +30,7 @@ bool solver::run() { states_searched++; LOG_DEBUG (state); frontier.emplace_back(game_state::move(0,0), state.get_legal_moves()); - cache.insert(state); + cache.insert(state.get_data()); while (!frontier.empty()) { node& current = frontier.back(); @@ -47,7 +47,7 @@ bool solver::run() { // (unless it's the null first move) if (frontier.size() > 1) { state.undo_move(current.move); - assert(cache.contains(state)); + assert(cache.contains(state.get_data())); LOG_DEBUG (state); } // Returns to the previous state @@ -60,7 +60,7 @@ bool solver::run() { state.make_move(next_move); // Insert the state into the global cache - bool is_new_state = cache.insert(state); + bool is_new_state = cache.insert(state.get_data()); if (is_new_state) { states_searched++; LOG_DEBUG (state); @@ -70,7 +70,7 @@ bool solver::run() { } else { // If we've seen the state before, undoes the move state.undo_move(next_move); - assert(cache.contains(state)); + assert(cache.contains(state.get_data())); } } } diff --git a/src/test/gloabl_cache_test.cpp b/src/test/gloabl_cache_test.cpp deleted file mode 100644 index beb1e0f..0000000 --- a/src/test/gloabl_cache_test.cpp +++ /dev/null @@ -1,102 +0,0 @@ -// -// Created by thecharlesblake on 1/11/18. -// - -#include - -#include "test_helper.h" -#include "../main/game/game_state.h" -#include "../main/game/global_cache.h" - -typedef sol_rules::build_policy pol; - -TEST(GlobalCache, BuildSameSuit) { - test_helper::run_card_cache_test(pol::SAME_SUIT, false, false); -} - -TEST(GlobalCache, BuildRedBlack) { - test_helper::run_card_cache_test(pol::RED_BLACK, false, true); -} - -TEST(GlobalCache, BuildAny) { - test_helper::run_card_cache_test(pol::ANY_SUIT, true, true); -} - -TEST(GlobalCache, BuildNone) { - test_helper::run_card_cache_test(pol::NO_BUILD, true, true); -} - -TEST(GlobalCache, CommutativeTableauPiles) { - sol_rules rules; - rules.tableau_pile_count = 3; - rules.build_pol = sol_rules::build_policy::SAME_SUIT; - game_state gs(rules, 0); - global_cache cache; - - cache.insert ({{card("AC")},{card("2D")},{card("3H")}}); - ASSERT_TRUE (cache.contains({{card("2D")},{card("3H")},{card("AC")}})); - ASSERT_FALSE(cache.contains({{card("3H")},{card("2D")},{card("3H")}})); - - // Test with empty piles - cache.clear(); - cache.insert ({{card("4C")},{card("5D")},{}}); - ASSERT_TRUE (cache.contains({{},{card("4C")},{card("5D")}})); - ASSERT_FALSE(cache.contains({{},{card("4C")},{}})); - - // Test with piles of size > 1 - cache.clear(); - cache.insert ({{card("6C"),card("7D")},{card("8C")},{card("9D")}}); - ASSERT_TRUE (cache.contains({{card("8C")},{card("6C"),card("7D")},{card("9D")}})); - ASSERT_FALSE(cache.contains({{card("8C")},{card("6C"),card("KD")},{card("9D")}})); - - rules.tableau_pile_count = 0; - game_state _gs(rules, 0); -} - -TEST(GlobalCache, CommutativeReserve) { - sol_rules rules; - rules.reserve_size = 3; - rules.build_pol = sol_rules::build_policy::SAME_SUIT; - game_state gs(rules, 0); - global_cache cache; - - cache.insert ({{card("AC")},{card("2D")},{card("3H")}}); - ASSERT_TRUE (cache.contains({{card("2D")},{card("3H")},{card("AC")}})); - ASSERT_FALSE(cache.contains({{card("3H")},{card("2D")},{card("3H")}})); - - // Test with empty piles - cache.clear(); - cache.insert ({{card("4C")},{card("5D")},{}}); - ASSERT_TRUE (cache.contains({{},{card("4C")},{card("5D")}})); - ASSERT_FALSE(cache.contains({{},{card("4C")},{}})); - - // Test with piles of size > 1 - cache.clear(); - cache.insert ({{card("6C"),card("7D")},{card("8C")},{card("9D")}}); - ASSERT_TRUE (cache.contains({{card("8C")},{card("6C"),card("7D")},{card("9D")}})); - ASSERT_FALSE(cache.contains({{card("8C")},{card("6C"),card("KD")},{card("9D")}})); - - rules.reserve_size = 0; - game_state _gs(rules, 0); -} - -TEST(GlobalCache, CommutativeCells) { - sol_rules rules; - rules.cells = 3; - rules.build_pol = sol_rules::build_policy::SAME_SUIT; - game_state gs(rules, 0); - global_cache cache; - - cache.insert ({{card("AC")},{card("2D")},{card("3H")}}); - ASSERT_TRUE (cache.contains({{card("2D")},{card("3H")},{card("AC")}})); - ASSERT_FALSE(cache.contains({{card("3H")},{card("2D")},{card("3H")}})); - - // Test with empty piles - cache.clear(); - cache.insert ({{card("4C")},{card("5D")},{}}); - ASSERT_TRUE (cache.contains({{},{card("4C")},{card("5D")}})); - ASSERT_FALSE(cache.contains({{},{card("4C")},{}})); - - rules.cells = 0; - game_state _gs(rules, 0); -} diff --git a/src/test/global_cache_test.cpp b/src/test/global_cache_test.cpp new file mode 100644 index 0000000..69bfc91 --- /dev/null +++ b/src/test/global_cache_test.cpp @@ -0,0 +1,93 @@ +// +// Created by thecharlesblake on 1/11/18. +// + +#include + +#include "test_helper.h" +#include "../main/game/game_state.h" +#include "../main/game/global_cache.h" + +typedef sol_rules::build_policy pol; + +TEST(GlobalCache, BuildSameSuit) { + test_helper::run_card_cache_test(pol::SAME_SUIT, false, false); +} + +TEST(GlobalCache, BuildRedBlack) { + test_helper::run_card_cache_test(pol::RED_BLACK, false, true); +} + +TEST(GlobalCache, BuildAny) { + test_helper::run_card_cache_test(pol::ANY_SUIT, true, true); +} + +TEST(GlobalCache, BuildNone) { + test_helper::run_card_cache_test(pol::NO_BUILD, true, true); +} + +TEST(GlobalCache, CommutativeTableauPiles) { + sol_rules rules; + rules.tableau_pile_count = 3; + rules.build_pol = sol_rules::build_policy::SAME_SUIT; + game_state gs(rules, 0); + global_cache cache(gs); + + cache.insert ({{card("AC")},{card("2D")},{card("3H")}}); + ASSERT_TRUE (cache.contains({{card("2D")},{card("3H")},{card("AC")}})); + ASSERT_FALSE(cache.contains({{card("3H")},{card("2D")},{card("3H")}})); + + // Test with empty piles + cache.clear(); + cache.insert ({{card("4C")},{card("5D")},{}}); + ASSERT_TRUE (cache.contains({{},{card("4C")},{card("5D")}})); + ASSERT_FALSE(cache.contains({{},{card("4C")},{}})); + + // Test with piles of size > 1 + cache.clear(); + cache.insert ({{card("6C"),card("7D")},{card("8C")},{card("9D")}}); + ASSERT_TRUE (cache.contains({{card("8C")},{card("6C"),card("7D")},{card("9D")}})); + ASSERT_FALSE(cache.contains({{card("8C")},{card("6C"),card("KD")},{card("9D")}})); +} + +TEST(GlobalCache, CommutativeReserve) { + sol_rules rules; + rules.reserve_size = 3; + rules.build_pol = sol_rules::build_policy::SAME_SUIT; + game_state gs(rules, 0); + global_cache cache(gs); + + cache.insert ({{card("AC")},{card("2D")},{card("3H")}}); + ASSERT_TRUE (cache.contains({{card("2D")},{card("3H")},{card("AC")}})); + ASSERT_FALSE(cache.contains({{card("3H")},{card("2D")},{card("3H")}})); + + // Test with empty piles + cache.clear(); + cache.insert ({{card("4C")},{card("5D")},{}}); + ASSERT_TRUE (cache.contains({{},{card("4C")},{card("5D")}})); + ASSERT_FALSE(cache.contains({{},{card("4C")},{}})); + + // Test with piles of size > 1 + cache.clear(); + cache.insert ({{card("6C"),card("7D")},{card("8C")},{card("9D")}}); + ASSERT_TRUE (cache.contains({{card("8C")},{card("6C"),card("7D")},{card("9D")}})); + ASSERT_FALSE(cache.contains({{card("8C")},{card("6C"),card("KD")},{card("9D")}})); +} + +TEST(GlobalCache, CommutativeCells) { + sol_rules rules; + rules.cells = 3; + rules.build_pol = sol_rules::build_policy::SAME_SUIT; + game_state gs(rules, 0); + global_cache cache(gs); + + cache.insert ({{card("AC")},{card("2D")},{card("3H")}}); + ASSERT_TRUE (cache.contains({{card("2D")},{card("3H")},{card("AC")}})); + ASSERT_FALSE(cache.contains({{card("3H")},{card("2D")},{card("3H")}})); + + // Test with empty piles + cache.clear(); + cache.insert ({{card("4C")},{card("5D")},{}}); + ASSERT_TRUE (cache.contains({{},{card("4C")},{card("5D")}})); + ASSERT_FALSE(cache.contains({{},{card("4C")},{}})); +} diff --git a/src/test/test_helper.cpp b/src/test/test_helper.cpp index 5758165..81f11e7 100644 --- a/src/test/test_helper.cpp +++ b/src/test/test_helper.cpp @@ -11,7 +11,7 @@ #include "../main/input-output/input/json-parsing/json_helper.h" #include "../main/input-output/input/json-parsing/rules_parser.h" -#define ASSERT_(t_f, statement) ASSERT_TRUE((statement) == t_f) +#define ASSERT_(t_f, statement) ASSERT_TRUE((statement) == (t_f)) using namespace rapidjson; using namespace std; @@ -36,7 +36,7 @@ void test_helper::run_card_cache_test(sol_rules::build_policy bp, for (uint8_t suit_idx = 0; suit_idx < 4; suit_idx++) { for (card::rank_t rank = 1; rank <= 13; rank++) { - global_cache cache; + global_cache cache(gs); // c2 is the same as c_ but with a different suit const card c = card(suit_idx, rank); @@ -49,42 +49,39 @@ void test_helper::run_card_cache_test(sol_rules::build_policy bp, const game_state s_same_col = {{c_same_col}}; const game_state s_dif_rank = {{c_dif_rank}}; - cache.insert(s); + cache.insert(s.get_data()); - ASSERT_TRUE(cache.contains(s)) << "Contains self"; + ASSERT_TRUE(cache.contains(s.get_data())) << "Contains self"; - ASSERT_(assert_dif_col, hash_value(s) == hash_value(s_dif_suit)) + ASSERT_(assert_dif_col, hasher(gs)(s.get_data()) == hasher(gs)(s_dif_suit.get_data())) << "Insert: " << c.to_string() << ", Comp: " << c_dif_col.to_string(); - ASSERT_(assert_dif_col, global_cache::game_state_pred()(s, s_dif_suit)) + ASSERT_(assert_dif_col, predicate(gs)(s.get_data(), s_dif_suit.get_data())) << "Insert: " << c.to_string() << ", Comp: " << c_dif_col.to_string(); - ASSERT_(assert_dif_col, cache.contains(s_dif_suit)) + ASSERT_(assert_dif_col, cache.contains(s_dif_suit.get_data())) << "Insert: " << c.to_string() << ", Comp: " << c_dif_col.to_string(); - ASSERT_(assert_same_col, hash_value(s) == hash_value(s_same_col)) + ASSERT_(assert_same_col, hasher(gs)(s.get_data()) == hasher(gs)(s_same_col.get_data())) << "Insert: " << c.to_string() << ", Comp: " << c_same_col.to_string(); - ASSERT_(assert_same_col, global_cache::game_state_pred()(s, s_same_col)) + ASSERT_(assert_same_col, predicate(gs)(s.get_data(), s_same_col.get_data())) << "Insert: " << c.to_string() << ", Comp: " << c_same_col.to_string(); - ASSERT_(assert_same_col, cache.contains(s_same_col)) + ASSERT_(assert_same_col, cache.contains(s_same_col.get_data())) << "Insert: " << c.to_string() << ", Comp: " << c_same_col.to_string(); - ASSERT_FALSE(hash_value(s) == hash_value(s_dif_rank)) + ASSERT_FALSE(hasher(gs)(s.get_data()) == hasher(gs)(s_dif_rank.get_data())) << "Insert: " << c.to_string() << ", Comp: " << c_dif_rank.to_string(); - ASSERT_FALSE(global_cache::game_state_pred()(s, s_dif_rank)) + ASSERT_FALSE(predicate(gs)(s.get_data(), s_dif_rank.get_data())) << "Insert: " << c.to_string() << ", Comp: " << c_dif_rank.to_string(); - ASSERT_FALSE(cache.contains(s_dif_rank)) + ASSERT_FALSE(cache.contains(s_dif_rank.get_data())) << "Insert: " << c.to_string() << ", Comp: " << c_dif_rank.to_string(); } } - - rules.tableau_pile_count = 0; - game_state _gs(rules, 0); } -- 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') 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') 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 ++++++ src/main/input-output/output/state_printer.cpp | 6 +++--- 3 files changed, 16 insertions(+), 7 deletions(-) (limited to 'src') 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; }; 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) { -- 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') 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') 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') 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 ba4c52fe6528ffa989c20580e057f93a744a364e Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Tue, 30 Jan 2018 15:50:05 +0000 Subject: Make deal parser use new gs.place_card() method --- .../input/json-parsing/deal_parser.cpp | 25 ++++++++++------------ 1 file changed, 11 insertions(+), 14 deletions(-) (limited to 'src') 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 09b2065..0112cda 100644 --- a/src/main/input-output/input/json-parsing/deal_parser.cpp +++ b/src/main/input-output/input/json-parsing/deal_parser.cpp @@ -52,14 +52,13 @@ void deal_parser::parse_tableau_piles(game_state &gs, const rapidjson::Document& 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())); } } @@ -85,13 +84,13 @@ void deal_parser::parse_cells(game_state &gs, const Document& doc) { 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())); } } } @@ -107,7 +106,7 @@ void deal_parser::parse_stock(game_state &gs, const Document& doc) { 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())); } } } @@ -127,9 +126,9 @@ void deal_parser::parse_reserve(game_state &gs, const Document& doc) { 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]; + game_state::pile_ref pr = gs.original_reserve[0]; if (!gs.rules.reserve_stacked) pr += i; - gs.piles[pr].place(card(json_card_arr[i].GetString())); + gs.place_card(pr, card(json_card_arr[i].GetString())); } } @@ -138,8 +137,6 @@ void deal_parser::fill_foundations(game_state &gs) { 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)); } } -- cgit v1.2.1 From 375a45f4a2ecc17b1e77816b1e2192f15816d362 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Tue, 30 Jan 2018 15:50:35 +0000 Subject: Update solver: cache uses game state not pile vec --- src/main/solver/solver.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp index 072db18..6dddfb4 100644 --- a/src/main/solver/solver.cpp +++ b/src/main/solver/solver.cpp @@ -24,13 +24,13 @@ solver::node::node(const game_state::move m, } bool solver::run() { - ProfilerStart("solvitaire"); + //ProfilerStart("solvitaire"); game_state state = initial_state; states_searched++; LOG_DEBUG (state); frontier.emplace_back(game_state::move(0,0), state.get_legal_moves()); - cache.insert(state.get_data()); + cache.insert(state); while (!frontier.empty()) { node& current = frontier.back(); @@ -47,7 +47,7 @@ bool solver::run() { // (unless it's the null first move) if (frontier.size() > 1) { state.undo_move(current.move); - assert(cache.contains(state.get_data())); + assert(cache.contains(state)); LOG_DEBUG (state); } // Returns to the previous state @@ -60,7 +60,7 @@ bool solver::run() { state.make_move(next_move); // Insert the state into the global cache - bool is_new_state = cache.insert(state.get_data()); + bool is_new_state = cache.insert(state); if (is_new_state) { states_searched++; LOG_DEBUG (state); @@ -70,7 +70,7 @@ bool solver::run() { } else { // If we've seen the state before, undoes the move state.undo_move(next_move); - assert(cache.contains(state.get_data())); + assert(cache.contains(state)); } } } -- cgit v1.2.1 From 9b2429c349d4cae96a244fbbedb598f59c2718c8 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Tue, 30 Jan 2018 15:51:37 +0000 Subject: Update tests to use new game_state constructor --- src/test/global_cache_test.cpp | 54 +++++++++++++++++++++--------------------- src/test/test_helper.cpp | 38 +++++++++++------------------ 2 files changed, 41 insertions(+), 51 deletions(-) (limited to 'src') diff --git a/src/test/global_cache_test.cpp b/src/test/global_cache_test.cpp index 69bfc91..70a6b33 100644 --- a/src/test/global_cache_test.cpp +++ b/src/test/global_cache_test.cpp @@ -30,64 +30,64 @@ TEST(GlobalCache, CommutativeTableauPiles) { sol_rules rules; rules.tableau_pile_count = 3; rules.build_pol = sol_rules::build_policy::SAME_SUIT; - game_state gs(rules, 0); + game_state gs(rules, std::initializer_list{{},{},{}}); global_cache cache(gs); - cache.insert ({{card("AC")},{card("2D")},{card("3H")}}); - ASSERT_TRUE (cache.contains({{card("2D")},{card("3H")},{card("AC")}})); - ASSERT_FALSE(cache.contains({{card("3H")},{card("2D")},{card("3H")}})); + cache.insert (game_state(rules, {{card("AC")},{card("2D")},{card("3H")}})); + ASSERT_TRUE (cache.contains(game_state(rules, {{card("2D")},{card("3H")},{card("AC")}}))); + ASSERT_FALSE(cache.contains(game_state(rules, {{card("3H")},{card("2D")},{card("3H")}}))); // Test with empty piles cache.clear(); - cache.insert ({{card("4C")},{card("5D")},{}}); - ASSERT_TRUE (cache.contains({{},{card("4C")},{card("5D")}})); - ASSERT_FALSE(cache.contains({{},{card("4C")},{}})); + cache.insert (game_state(rules, {{card("4C")},{card("5D")},{}})); + ASSERT_TRUE (cache.contains(game_state(rules, {{},{card("4C")},{card("5D")}}))); + ASSERT_FALSE(cache.contains(game_state(rules, {{},{card("4C")},{}}))); // Test with piles of size > 1 cache.clear(); - cache.insert ({{card("6C"),card("7D")},{card("8C")},{card("9D")}}); - ASSERT_TRUE (cache.contains({{card("8C")},{card("6C"),card("7D")},{card("9D")}})); - ASSERT_FALSE(cache.contains({{card("8C")},{card("6C"),card("KD")},{card("9D")}})); + cache.insert (game_state(rules, {{card("6C"),card("7D")},{card("8C")},{card("9D")}})); + ASSERT_TRUE (cache.contains(game_state(rules, {{card("8C")},{card("6C"),card("7D")},{card("9D")}}))); + ASSERT_FALSE(cache.contains(game_state(rules, {{card("8C")},{card("6C"),card("KD")},{card("9D")}}))); } TEST(GlobalCache, CommutativeReserve) { sol_rules rules; rules.reserve_size = 3; rules.build_pol = sol_rules::build_policy::SAME_SUIT; - game_state gs(rules, 0); + game_state gs(rules, std::initializer_list{{},{},{}}); global_cache cache(gs); - cache.insert ({{card("AC")},{card("2D")},{card("3H")}}); - ASSERT_TRUE (cache.contains({{card("2D")},{card("3H")},{card("AC")}})); - ASSERT_FALSE(cache.contains({{card("3H")},{card("2D")},{card("3H")}})); + cache.insert (game_state(rules, {{card("AC")},{card("2D")},{card("3H")}})); + ASSERT_TRUE (cache.contains(game_state(rules, {{card("2D")},{card("3H")},{card("AC")}}))); + ASSERT_FALSE(cache.contains(game_state(rules, {{card("3H")},{card("2D")},{card("3H")}}))); // Test with empty piles cache.clear(); - cache.insert ({{card("4C")},{card("5D")},{}}); - ASSERT_TRUE (cache.contains({{},{card("4C")},{card("5D")}})); - ASSERT_FALSE(cache.contains({{},{card("4C")},{}})); + cache.insert (game_state(rules, {{card("4C")},{card("5D")},{}})); + ASSERT_TRUE (cache.contains(game_state(rules, {{},{card("4C")},{card("5D")}}))); + ASSERT_FALSE(cache.contains(game_state(rules, {{},{card("4C")},{}}))); // Test with piles of size > 1 cache.clear(); - cache.insert ({{card("6C"),card("7D")},{card("8C")},{card("9D")}}); - ASSERT_TRUE (cache.contains({{card("8C")},{card("6C"),card("7D")},{card("9D")}})); - ASSERT_FALSE(cache.contains({{card("8C")},{card("6C"),card("KD")},{card("9D")}})); + cache.insert (game_state(rules, {{card("6C"),card("7D")},{card("8C")},{card("9D")}})); + ASSERT_TRUE (cache.contains(game_state(rules, {{card("8C")},{card("6C"),card("7D")},{card("9D")}}))); + ASSERT_FALSE(cache.contains(game_state(rules, {{card("8C")},{card("6C"),card("KD")},{card("9D")}}))); } TEST(GlobalCache, CommutativeCells) { sol_rules rules; rules.cells = 3; rules.build_pol = sol_rules::build_policy::SAME_SUIT; - game_state gs(rules, 0); + game_state gs(rules, std::initializer_list{{},{},{}}); global_cache cache(gs); - cache.insert ({{card("AC")},{card("2D")},{card("3H")}}); - ASSERT_TRUE (cache.contains({{card("2D")},{card("3H")},{card("AC")}})); - ASSERT_FALSE(cache.contains({{card("3H")},{card("2D")},{card("3H")}})); + cache.insert (game_state(rules, {{card("AC")},{card("2D")},{card("3H")}})); + ASSERT_TRUE (cache.contains(game_state(rules, {{card("2D")},{card("3H")},{card("AC")}}))); + ASSERT_FALSE(cache.contains(game_state(rules, {{card("3H")},{card("2D")},{card("3H")}}))); // Test with empty piles cache.clear(); - cache.insert ({{card("4C")},{card("5D")},{}}); - ASSERT_TRUE (cache.contains({{},{card("4C")},{card("5D")}})); - ASSERT_FALSE(cache.contains({{},{card("4C")},{}})); + cache.insert (game_state(rules, {{card("4C")},{card("5D")},{}})); + ASSERT_TRUE (cache.contains(game_state(rules, {{},{card("4C")},{card("5D")}}))); + ASSERT_FALSE(cache.contains(game_state(rules, {{},{card("4C")},{}}))); } diff --git a/src/test/test_helper.cpp b/src/test/test_helper.cpp index 81f11e7..22499de 100644 --- a/src/test/test_helper.cpp +++ b/src/test/test_helper.cpp @@ -32,54 +32,44 @@ void test_helper::run_card_cache_test(sol_rules::build_policy bp, sol_rules rules; rules.tableau_pile_count = 1; rules.build_pol = bp; - game_state gs(rules, 0); for (uint8_t suit_idx = 0; suit_idx < 4; suit_idx++) { for (card::rank_t rank = 1; rank <= 13; rank++) { - global_cache cache(gs); - // c2 is the same as c_ but with a different suit const card c = card(suit_idx, rank); const card c_dif_col = card(uint8_t((suit_idx + 1) % 4), rank); const card c_same_col = card(uint8_t((suit_idx + 2) % 4), rank); const card c_dif_rank = card(suit_idx, card::rank_t((rank % 13) + 1)); - const game_state s = {{c}}; - const game_state s_dif_suit = {{c_dif_col}}; - const game_state s_same_col = {{c_same_col}}; - const game_state s_dif_rank = {{c_dif_rank}}; + const game_state s = game_state(rules, initializer_list{{c}}); + const game_state s_dif_suit(rules, initializer_list{{c_dif_col}}); + const game_state s_same_col(rules, initializer_list{{c_same_col}}); + const game_state s_dif_rank(rules, initializer_list{{c_dif_rank}}); - cache.insert(s.get_data()); + global_cache cache(s); - ASSERT_TRUE(cache.contains(s.get_data())) << "Contains self"; + cache.insert(s); - ASSERT_(assert_dif_col, hasher(gs)(s.get_data()) == hasher(gs)(s_dif_suit.get_data())) - << "Insert: " << c.to_string() - << ", Comp: " << c_dif_col.to_string(); - ASSERT_(assert_dif_col, predicate(gs)(s.get_data(), s_dif_suit.get_data())) + ASSERT_TRUE(cache.contains(s)) << "Contains self"; + + ASSERT_(assert_dif_col, hasher(s)(s.get_data()) == hasher(s)(s_dif_suit.get_data())) << "Insert: " << c.to_string() << ", Comp: " << c_dif_col.to_string(); - ASSERT_(assert_dif_col, cache.contains(s_dif_suit.get_data())) + ASSERT_(assert_dif_col, cache.contains(s_dif_suit)) << "Insert: " << c.to_string() << ", Comp: " << c_dif_col.to_string(); - ASSERT_(assert_same_col, hasher(gs)(s.get_data()) == hasher(gs)(s_same_col.get_data())) + ASSERT_(assert_same_col, hasher(s)(s.get_data()) == hasher(s)(s_same_col.get_data())) << "Insert: " << c.to_string() << ", Comp: " << c_same_col.to_string(); - ASSERT_(assert_same_col, predicate(gs)(s.get_data(), s_same_col.get_data())) - << "Insert: " << c.to_string() - << ", Comp: " << c_same_col.to_string(); - ASSERT_(assert_same_col, cache.contains(s_same_col.get_data())) + ASSERT_(assert_same_col, cache.contains(s_same_col)) << "Insert: " << c.to_string() << ", Comp: " << c_same_col.to_string(); - ASSERT_FALSE(hasher(gs)(s.get_data()) == hasher(gs)(s_dif_rank.get_data())) - << "Insert: " << c.to_string() - << ", Comp: " << c_dif_rank.to_string(); - ASSERT_FALSE(predicate(gs)(s.get_data(), s_dif_rank.get_data())) + ASSERT_FALSE(hasher(s)(s.get_data()) == hasher(s)(s_dif_rank.get_data())) << "Insert: " << c.to_string() << ", Comp: " << c_dif_rank.to_string(); - ASSERT_FALSE(cache.contains(s_dif_rank.get_data())) + ASSERT_FALSE(cache.contains(s_dif_rank)) << "Insert: " << c.to_string() << ", Comp: " << c_dif_rank.to_string(); } -- 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') 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') 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