From f12e2936fda51eb59016dd316ff9c73fba621fa2 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Fri, 12 Jan 2018 16:31:16 +0000 Subject: Add basic global_cache interface --- src/main/game/global_cache.cpp | 16 ++++++++++++++++ src/main/game/global_cache.h | 29 +++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 src/main/game/global_cache.cpp create mode 100644 src/main/game/global_cache.h (limited to 'src/main') diff --git a/src/main/game/global_cache.cpp b/src/main/game/global_cache.cpp new file mode 100644 index 0000000..cc95b3b --- /dev/null +++ b/src/main/game/global_cache.cpp @@ -0,0 +1,16 @@ +// +// Created by thecharlesblake on 1/10/18. +// + +#include "global_cache.h" + + +global_cache::global_cache(const sol_rules&) {} + +bool global_cache::insert(const std::vector& pile_vec) { + return u_set.insert(pile_vec).second; +} + +bool global_cache::contains(const std::vector& pile_vec) { + return u_set.count(pile_vec) > 0; +} diff --git a/src/main/game/global_cache.h b/src/main/game/global_cache.h new file mode 100644 index 0000000..b35acc6 --- /dev/null +++ b/src/main/game/global_cache.h @@ -0,0 +1,29 @@ +// +// Created by thecharlesblake on 1/10/18. +// + +#ifndef SOLVITAIRE_GLOBAL_CACHE_H +#define SOLVITAIRE_GLOBAL_CACHE_H + +#include +#include + +#include "../game/game_state.h" +#include "../game/sol_rules.h" + +class global_cache { +public: + explicit global_cache(const sol_rules&); + + bool insert(const std::vector&); + bool contains(const std::vector&); + +private: + std::unordered_set< + std::vector, + boost::hash> + > u_set; +}; + + +#endif //SOLVITAIRE_GLOBAL_CACHE_H -- cgit v1.2.1 From 9ea820ab371c7e1c033ab912317ba6096db87caa Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Fri, 12 Jan 2018 16:31:38 +0000 Subject: Add unit tests for global cache --- src/main/game/card.cpp | 6 +++--- src/main/game/pile.cpp | 2 ++ src/main/game/pile.h | 1 + src/main/game/sol_rules.cpp | 29 +++++++++++++++++++++++++++++ src/main/game/sol_rules.h | 2 ++ 5 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 src/main/game/sol_rules.cpp (limited to 'src/main') diff --git a/src/main/game/card.cpp b/src/main/game/card.cpp index 80ebef6..2f4cc08 100644 --- a/src/main/game/card.cpp +++ b/src/main/game/card.cpp @@ -77,9 +77,9 @@ bool operator!=(const card& l, const card& r) { size_t hash_value(card const& c) { boost::hash hasher; - auto raw_val = static_cast( - static_cast>(c.get_suit()) - * 13 + c.get_rank()); + + auto suit_val = static_cast>(c.get_suit()); + auto raw_val = static_cast(suit_val * 13 + c.get_rank()); return hasher(raw_val); } \ No newline at end of file diff --git a/src/main/game/pile.cpp b/src/main/game/pile.cpp index 82ae344..ad7b85e 100644 --- a/src/main/game/pile.cpp +++ b/src/main/game/pile.cpp @@ -12,6 +12,8 @@ using namespace std; using namespace boost; +pile::pile(std::vector pv) : pile_vec(pv) {} + card pile::top_card() const { assert(!empty()); return pile_vec.back(); diff --git a/src/main/game/pile.h b/src/main/game/pile.h index f2600fe..fb7bc38 100644 --- a/src/main/game/pile.h +++ b/src/main/game/pile.h @@ -13,6 +13,7 @@ class pile { public: + pile(std::vector); typedef uint8_t size_type; // Inspect diff --git a/src/main/game/sol_rules.cpp b/src/main/game/sol_rules.cpp new file mode 100644 index 0000000..6c16b4a --- /dev/null +++ b/src/main/game/sol_rules.cpp @@ -0,0 +1,29 @@ +// +// Created by thecharlesblake on 1/12/18. +// + +#include "sol_rules.h" + +typedef sol_rules::build_policy pol; +typedef sol_rules::spaces_policy s_pol; +typedef sol_rules::stock_deal_type sdt; + +// Note that the 'default' sol rules are set up by parsing the default json +// rules description. This initialisation is really just for testing purposes. +sol_rules::sol_rules() : + tableau_pile_count(0), + build_pol(pol::ANY_SUIT), + spaces_pol(s_pol::ANY), + move_built_group(false), + two_decks(false), + max_rank(13), + hole(false), + foundations(true), + foundations_init_card(false), + diagonal_deal(false), + cells(0), + stock_size(0), + stock_deal_t(sdt::WASTE), + reserve_size(0), + reserve_stacked(false) { +} diff --git a/src/main/game/sol_rules.h b/src/main/game/sol_rules.h index 812e82c..8aae42b 100644 --- a/src/main/game/sol_rules.h +++ b/src/main/game/sol_rules.h @@ -12,6 +12,8 @@ #include "card.h" struct sol_rules { + sol_rules(); + // Tableau policies enum class build_policy { NO_BUILD, -- cgit v1.2.1 From 01083ef011143eef36f8f83ecbdae8da6e948021 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Fri, 12 Jan 2018 17:55:56 +0000 Subject: Implement symmetrical hash for cards --- src/main/game/card.cpp | 9 --------- src/main/game/card.h | 1 - src/main/game/global_cache.cpp | 28 +++++++++++++++++++++++++++- src/main/game/global_cache.h | 3 ++- 4 files changed, 29 insertions(+), 12 deletions(-) (limited to 'src/main') diff --git a/src/main/game/card.cpp b/src/main/game/card.cpp index 2f4cc08..0a05163 100644 --- a/src/main/game/card.cpp +++ b/src/main/game/card.cpp @@ -74,12 +74,3 @@ bool operator==(const card& l, const card& r) { bool operator!=(const card& l, const card& r) { return !(l == r); } - -size_t hash_value(card const& c) { - boost::hash hasher; - - auto suit_val = static_cast>(c.get_suit()); - auto raw_val = static_cast(suit_val * 13 + c.get_rank()); - - return hasher(raw_val); -} \ No newline at end of file diff --git a/src/main/game/card.h b/src/main/game/card.h index 4cab774..0c09043 100644 --- a/src/main/game/card.h +++ b/src/main/game/card.h @@ -37,7 +37,6 @@ private: bool operator==(const card&, const card&); bool operator!=(const card&, const card&); -std::size_t hash_value(card const&); #endif //SOLITAIRE_PIP_CARD_H diff --git a/src/main/game/global_cache.cpp b/src/main/game/global_cache.cpp index cc95b3b..f55a72c 100644 --- a/src/main/game/global_cache.cpp +++ b/src/main/game/global_cache.cpp @@ -5,7 +5,11 @@ #include "global_cache.h" -global_cache::global_cache(const sol_rules&) {} +typedef sol_rules::build_policy pol; + +global_cache::global_cache(const sol_rules& rules) { + global_cache::rules = rules; +} bool global_cache::insert(const std::vector& pile_vec) { return u_set.insert(pile_vec).second; @@ -14,3 +18,25 @@ bool global_cache::insert(const std::vector& pile_vec) { bool global_cache::contains(const std::vector& pile_vec) { return u_set.count(pile_vec) > 0; } + +size_t hash_value(card const& c) { + boost::hash hasher; + + auto suit_val; + switch (global_cache::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; + break; + } + + auto raw_val = static_cast(suit_val * 13 + c.get_rank()); + return hasher(raw_val); +} diff --git a/src/main/game/global_cache.h b/src/main/game/global_cache.h index b35acc6..a5c6768 100644 --- a/src/main/game/global_cache.h +++ b/src/main/game/global_cache.h @@ -18,12 +18,13 @@ public: bool insert(const std::vector&); bool contains(const std::vector&); + friend std::size_t hash_value(card const&); private: + static sol_rules rules; std::unordered_set< std::vector, boost::hash> > u_set; }; - #endif //SOLVITAIRE_GLOBAL_CACHE_H -- cgit v1.2.1 From 895bbca6c0c7233fe11f331a10135443970ec4c5 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Fri, 12 Jan 2018 18:05:07 +0000 Subject: Implement hash for piles --- src/main/game/global_cache.cpp | 8 ++++++++ src/main/game/global_cache.h | 1 + src/main/game/pile.cpp | 7 ------- src/main/game/pile.h | 2 ++ 4 files changed, 11 insertions(+), 7 deletions(-) (limited to 'src/main') diff --git a/src/main/game/global_cache.cpp b/src/main/game/global_cache.cpp index f55a72c..e881408 100644 --- a/src/main/game/global_cache.cpp +++ b/src/main/game/global_cache.cpp @@ -2,8 +2,12 @@ // Created by thecharlesblake on 1/10/18. // +#include + #include "global_cache.h" +using namespace std; +using namespace boost; typedef sol_rules::build_policy pol; @@ -40,3 +44,7 @@ size_t hash_value(card const& c) { auto raw_val = static_cast(suit_val * 13 + c.get_rank()); return hasher(raw_val); } + +size_t hash_value(pile const& p) { + return hash_range(begin(p.pile_vec), end(p.pile_vec)); +} diff --git a/src/main/game/global_cache.h b/src/main/game/global_cache.h index a5c6768..26a73ef 100644 --- a/src/main/game/global_cache.h +++ b/src/main/game/global_cache.h @@ -21,6 +21,7 @@ public: friend std::size_t hash_value(card const&); private: static sol_rules rules; + std::unordered_set< std::vector, boost::hash> diff --git a/src/main/game/pile.cpp b/src/main/game/pile.cpp index ad7b85e..ba7c1b9 100644 --- a/src/main/game/pile.cpp +++ b/src/main/game/pile.cpp @@ -5,12 +5,9 @@ #include #include -#include - #include "pile.h" using namespace std; -using namespace boost; pile::pile(std::vector pv) : pile_vec(pv) {} @@ -49,7 +46,3 @@ card pile::take() { bool operator==(const pile& a, const pile& b) { return a.pile_vec == b.pile_vec; } - -size_t hash_value(pile const& p) { - return hash_range(begin(p.pile_vec), end(p.pile_vec)); -} diff --git a/src/main/game/pile.h b/src/main/game/pile.h index fb7bc38..ab661ca 100644 --- a/src/main/game/pile.h +++ b/src/main/game/pile.h @@ -29,6 +29,8 @@ public: card& operator[] (size_type); card operator[] (size_type) const; + + friend bool operator==(const pile&, const pile&); friend std::size_t hash_value(pile const&); private: -- cgit v1.2.1 From add27468ed6ee09a9df6f885becc6b0428f666bf Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Fri, 12 Jan 2018 19:03:23 +0000 Subject: Implement symmetrical hash for pile vector --- src/main/game/game_state.cpp | 33 ---------------------------- src/main/game/game_state.h | 5 ++--- src/main/game/global_cache.cpp | 50 +++++++++++++++++++++++++++++++++++++++--- src/main/game/global_cache.h | 5 +++-- src/main/game/pile.h | 2 -- 5 files changed, 52 insertions(+), 43 deletions(-) (limited to 'src/main') diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp index f8f9e1e..e2bfc24 100644 --- a/src/main/game/game_state.cpp +++ b/src/main/game/game_state.cpp @@ -317,39 +317,6 @@ bool operator==(const game_state& a, const game_state& b) { } -//////////////////// -// HASH FUNCTIONS // -//////////////////// - -size_t hash_value(game_state const& gs) { - size_t seed = 0; - - if (gs.rules.foundations) { - hash_combine(seed, gs.foundations); - } - if (gs.rules.cells) { - hash_combine(seed, gs.cells); - } - if (gs.rules.tableau_pile_count > 0) { - hash_combine(seed, gs.tableau_piles); - } - - if (gs.rules.stock_size > 0) { - hash_combine(seed, gs.piles[gs.stock]); - hash_combine(seed, gs.piles[gs.waste]); - } - if (gs.rules.hole) { - hash_combine(seed, gs.piles[gs.hole]); - } - - return seed; -} - -size_t hash_value(vector const& vp) { - return hash_range(begin(vp), end(vp)); -} - - /////////// // PRINT // /////////// diff --git a/src/main/game/game_state.h b/src/main/game/game_state.h index 323646f..8a41398 100644 --- a/src/main/game/game_state.h +++ b/src/main/game/game_state.h @@ -45,12 +45,11 @@ public: const std::vector& get_data() const; friend bool operator==(const game_state&, const game_state&); + friend std::ostream& operator<< (std::ostream&, const game_state&); - friend std::size_t hash_value(game_state const&); + friend std::size_t hash_value(card const&); friend std::size_t hash_value(std::vector const&); - friend std::ostream& operator<< (std::ostream&, const game_state&); - private: static std::vector gen_shuffled_deck(int, card::rank_t, bool); diff --git a/src/main/game/global_cache.cpp b/src/main/game/global_cache.cpp index e881408..a1d7585 100644 --- a/src/main/game/global_cache.cpp +++ b/src/main/game/global_cache.cpp @@ -10,9 +10,11 @@ using namespace std; using namespace boost; typedef sol_rules::build_policy pol; +typedef sol_rules::stock_deal_type sdt; +typedef global_cache cache; -global_cache::global_cache(const sol_rules& rules) { - global_cache::rules = rules; +global_cache::global_cache(const game_state init_game_state) { + global_cache::init_gs = init_game_state; } bool global_cache::insert(const std::vector& pile_vec) { @@ -27,7 +29,7 @@ size_t hash_value(card const& c) { boost::hash hasher; auto suit_val; - switch (global_cache::rules.build_pol) { + switch (cache::init_gs.rules.build_pol) { case pol::SAME_SUIT: suit_val = static_cast>( c.get_suit()); @@ -48,3 +50,45 @@ size_t hash_value(card const& c) { size_t hash_value(pile const& p) { return hash_range(begin(p.pile_vec), end(p.pile_vec)); } + +size_t hash_value(std::vector const& piles) { + size_t seed = 0; + + // Using addition for commutative hash + for (game_state::pile_ref pr : cache::init_gs.tableau_piles) { + seed += hash_value(piles[pr]); + } + + // Using hash_combine for order-dependent hash + for (game_state::pile_ref pr : cache::init_gs.foundations) { + hash_combine(seed, piles[pr]); + } + + // Commutative + for (game_state::pile_ref pr : cache::init_gs.reserve) { + seed += hash_value(piles[pr]); + } + + if (cache::init_gs.rules.stock_size > 0) { + hash_combine(seed, piles[cache::init_gs.stock]); + } + + if (cache::init_gs.rules.stock_deal_t == sdt::WASTE) { + hash_combine(seed, piles[cache::init_gs.waste]); + } + + if (cache::init_gs.rules.stock_deal_t == sdt::WASTE) { + hash_combine(seed, piles[cache::init_gs.waste]); + } + + if (cache::init_gs.rules.hole) { + hash_combine(seed, piles[cache::init_gs.hole]); + } + + // Commutative + for (game_state::pile_ref pr : cache::init_gs.cells) { + seed += hash_value(piles[pr]); + } + + return seed; +} diff --git a/src/main/game/global_cache.h b/src/main/game/global_cache.h index 26a73ef..73f0ce7 100644 --- a/src/main/game/global_cache.h +++ b/src/main/game/global_cache.h @@ -13,14 +13,15 @@ class global_cache { public: - explicit global_cache(const sol_rules&); + explicit global_cache(game_state); bool insert(const std::vector&); bool contains(const std::vector&); friend std::size_t hash_value(card const&); + friend std::size_t hash_value(std::vector const&); private: - static sol_rules rules; + static game_state init_gs; std::unordered_set< std::vector, diff --git a/src/main/game/pile.h b/src/main/game/pile.h index ab661ca..fb7bc38 100644 --- a/src/main/game/pile.h +++ b/src/main/game/pile.h @@ -29,8 +29,6 @@ public: card& operator[] (size_type); card operator[] (size_type) const; - - friend bool operator==(const pile&, const pile&); friend std::size_t hash_value(pile const&); private: -- cgit v1.2.1 From 81a0a3cd68fb9d1a0e5eeab44fa1c9eec9f38018 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 18 Jan 2018 13:43:19 +0000 Subject: Chance suit from enum to uint8_t for bit packing --- src/main/game/card.cpp | 38 +++++++++++++++++++++++--------------- src/main/game/card.h | 21 ++++++++++++++++----- 2 files changed, 39 insertions(+), 20 deletions(-) (limited to 'src/main') diff --git a/src/main/game/card.cpp b/src/main/game/card.cpp index 0a05163..5189171 100644 --- a/src/main/game/card.cpp +++ b/src/main/game/card.cpp @@ -20,12 +20,12 @@ card::card(const char* c) : suit(suit_from_str(c)), rank(rank_from_str(c)) {} card::suit_t card::suit_from_str(const char* c) { switch(tolower(c[strlen(c) - 1])) { - case 'c': return suit_t::Clubs; - case 'd': return suit_t::Diamonds; - case 'h': return suit_t::Hearts; - case 's': return suit_t::Spades; + case 'c': return suit::Clubs; + case 'h': return suit::Hearts; + case 's': return suit::Spades; + case 'd': return suit::Diamonds; default: assert(false); - return suit_t::Clubs; + return suit::Clubs; } } @@ -47,9 +47,9 @@ card::suit_t card::get_suit() const { card::colour_t card::get_colour() const { switch (suit) { - case card::suit_t::Spades : - case card::suit_t::Clubs : return card::colour_t::Black; - default : return card::colour_t::Red; + case card::suit::Spades : + case card::suit::Clubs : return card::colour::Black; + default : return card::colour::Red; } } @@ -57,14 +57,22 @@ card::rank_t card::get_rank() const { return rank; } -card::suit_t card::to_suit(uint8_t v) { - switch (v) { - case 0: return suit_t::Clubs; - case 1: return suit_t::Diamonds; - case 2: return suit_t::Hearts; - case 3: return suit_t::Spades; - default: assert(false); return suit_t::Clubs; +std::string card::to_string() const { + string s; + switch(rank) { + case 1 : s += "A"; break; + case 11: s += "J"; break; + case 12: s += "Q"; break; + case 13: s += "K"; break; + default: s += std::to_string(rank); } + switch(suit) { + case card::suit::Spades : s += "S"; break; + case card::suit::Hearts : s += "H"; break; + case card::suit::Clubs : s += "C"; break; + case card::suit::Diamonds: s += "D"; break; + } + return s; } bool operator==(const card& l, const card& r) { diff --git a/src/main/game/card.h b/src/main/game/card.h index 0c09043..aec6a88 100644 --- a/src/main/game/card.h +++ b/src/main/game/card.h @@ -11,12 +11,21 @@ class card { public: // Types & typedefs - enum class suit_t {Clubs, Diamonds, Hearts, Spades}; - enum class colour_t {Black, Red}; typedef uint8_t rank_t; + typedef uint8_t suit_t; + typedef uint8_t colour_t; - // Converts a number (0-3) to a suit (Clubs-Spades) - static suit_t to_suit(uint8_t); + struct suit { + static const suit_t Clubs = 0; + static const suit_t Hearts = 1; + static const suit_t Spades = 2; + static const suit_t Diamonds = 3; + }; + + struct colour { + static const colour_t Black = 0; + static const colour_t Red = 1; + }; // A card can either be created using two int values (rank & suit), or // using a string (e.g. "AS") @@ -27,11 +36,13 @@ public: colour_t get_colour() const; rank_t get_rank() const; + std::string to_string() const; + private: static suit_t suit_from_str(const char* c); static rank_t rank_from_str(const char* c); - suit_t suit; + suit_t suit : 4; rank_t rank : 4; }; -- cgit v1.2.1 From 11da5c5958aa074caa78f96472d710eb822f7acd Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 18 Jan 2018 13:45:06 +0000 Subject: Update card printing to use to string method --- src/main/input-output/output/state_printer.cpp | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) (limited to 'src/main') diff --git a/src/main/input-output/output/state_printer.cpp b/src/main/input-output/output/state_printer.cpp index bfb6c7d..1d1c182 100644 --- a/src/main/input-output/output/state_printer.cpp +++ b/src/main/input-output/output/state_printer.cpp @@ -112,17 +112,5 @@ void state_printer::print_top_of_pile(ostream& stream, } void state_printer::print_card(std::ostream& s, const card c) { - switch(c.get_rank()) { - case 1 : s << "A"; break; - case 11: s << "J"; break; - case 12: s << "Q"; break; - case 13: s << "K"; break; - default: s << int(c.get_rank()); - } - switch(c.get_suit()) { - case card::suit_t::Spades : s << "S"; break; - case card::suit_t::Hearts : s << "H"; break; - case card::suit_t::Clubs : s << "C"; break; - case card::suit_t::Diamonds: s << "D"; break; - } + s << c.to_string(); } -- cgit v1.2.1 From 0111172d25f73a23e9390025f71620d60f2f6c86 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 18 Jan 2018 13:47:38 +0000 Subject: Update parsing to check correct features --- .../input/json-parsing/deal_parser.cpp | 26 +++++++++++----------- .../input-output/input/json-parsing/deal_parser.h | 2 -- .../input/json-parsing/rules_parser.cpp | 1 + 3 files changed, 14 insertions(+), 15 deletions(-) (limited to 'src/main') 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 b2a4c9d..7cf5f45 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 (gs.rules.tableau_pile_count > 0) { + if (game_state::rules.tableau_pile_count > 0) { parse_tableau_piles(gs, doc); } // Construct hole card - if (gs.rules.hole) { + if (game_state::rules.hole) { parse_hole(gs, doc); } // Construct filled cells - if (gs.rules.cells) { + if (game_state::rules.cells) { parse_cells(gs, doc); } // Construct stock - if (gs.rules.stock_size > 0) { + if (game_state::rules.stock_size > 0) { parse_stock(gs, doc); } // Construct reserve - if (gs.rules.reserve_size > 0) { + if (game_state::rules.reserve_size > 0) { parse_reserve(gs, doc); } // If the foundations begin with cards in them, fill them - if (gs.rules.foundations_init_card) { + if (game_state::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() != gs.rules.tableau_pile_count ) { + if (json_tab_piles.Size() != game_state::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() != gs.rules.cells) { + else if (json_cell_arr.Size() != game_state::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() != gs.rules.stock_size) { + if (json_stock.Size() != game_state::rules.stock_size) { json_helper::json_parse_err("Incorrect stock size"); } @@ -121,25 +121,25 @@ 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() != gs.rules.reserve_size) { + if (json_reserve_piles.Size() != game_state::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 (!gs.rules.reserve_stacked) pr += i; + if (!game_state::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 * (gs.rules.two_decks ? 2:1)); + auto foundations_count = uint8_t(4 * (game_state::rules.two_decks ? 2:1)); assert(gs.foundations.size() == foundations_count); for (uint8_t f_idx = 0; f_idx < foundations_count; f_idx++) { gs.piles[gs.foundations[f_idx]].place( - card(card::to_suit(f_idx % uint8_t(4)), 1) + card(f_idx % uint8_t(4), 1) ); } } diff --git a/src/main/input-output/input/json-parsing/deal_parser.h b/src/main/input-output/input/json-parsing/deal_parser.h index ecb596c..98916cd 100644 --- a/src/main/input-output/input/json-parsing/deal_parser.h +++ b/src/main/input-output/input/json-parsing/deal_parser.h @@ -11,8 +11,6 @@ #include #include -#include "../../../game/sol_rules.h" - class game_state; class deal_parser { diff --git a/src/main/input-output/input/json-parsing/rules_parser.cpp b/src/main/input-output/input/json-parsing/rules_parser.cpp index 9154ffd..7b9de84 100644 --- a/src/main/input-output/input/json-parsing/rules_parser.cpp +++ b/src/main/input-output/input/json-parsing/rules_parser.cpp @@ -8,6 +8,7 @@ #include "rules_parser.h" #include "json_helper.h" #include "../sol_preset_types.h" +#include "../../../game/card.h" using namespace std; using namespace rapidjson; -- cgit v1.2.1 From 1f078952baf3cc8512b709e345a622add91a667b Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 18 Jan 2018 13:49:10 +0000 Subject: Add vector and initializer lst constructor to pile --- src/main/game/pile.cpp | 6 +++++- src/main/game/pile.h | 5 ++++- 2 files changed, 9 insertions(+), 2 deletions(-) (limited to 'src/main') diff --git a/src/main/game/pile.cpp b/src/main/game/pile.cpp index ba7c1b9..21eddc0 100644 --- a/src/main/game/pile.cpp +++ b/src/main/game/pile.cpp @@ -9,7 +9,11 @@ using namespace std; -pile::pile(std::vector pv) : pile_vec(pv) {} +pile::pile(std::vector pv) : pile_vec(pv) { +} + +pile::pile(std::initializer_list il) : pile_vec(il) { +} card pile::top_card() const { assert(!empty()); diff --git a/src/main/game/pile.h b/src/main/game/pile.h index fb7bc38..411f890 100644 --- a/src/main/game/pile.h +++ b/src/main/game/pile.h @@ -13,9 +13,12 @@ class pile { public: - pile(std::vector); typedef uint8_t size_type; + pile() = default; + pile(std::vector); + pile(std::initializer_list); + // Inspect card top_card() const; bool empty() const; -- cgit v1.2.1 From bc76e3cc4687b41ada8f3bcff76611461bf63ef4 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 18 Jan 2018 13:50:27 +0000 Subject: Update legal moves code to use new suit rep. --- src/main/game/game_state.legal_moves.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/main') diff --git a/src/main/game/game_state.legal_moves.cpp b/src/main/game/game_state.legal_moves.cpp index 7dfc87c..3043587 100644 --- a/src/main/game/game_state.legal_moves.cpp +++ b/src/main/game/game_state.legal_moves.cpp @@ -128,7 +128,7 @@ bool game_state::is_valid_foundations_move(const pile_ref rem_ref, // Checks violation of same suit policy uint8_t suit_idx = (add_ref - foundations.front()) % uint8_t(4); - if (rem_c.get_suit() != card::to_suit(suit_idx)) { + if (rem_c.get_suit() != suit_idx) { return false; } -- cgit v1.2.1 From 91442e15dd15f8a4db7455f52255bd46273b0a40 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 18 Jan 2018 13:51:09 +0000 Subject: Update game state with static pile refs & rules --- src/main/game/game_state.cpp | 57 +++++++++++++++++++++++++++++++------------- src/main/game/game_state.h | 35 ++++++++++++++------------- 2 files changed, 60 insertions(+), 32 deletions(-) (limited to 'src/main') diff --git a/src/main/game/game_state.cpp b/src/main/game/game_state.cpp index e2bfc24..8075f50 100644 --- a/src/main/game/game_state.cpp +++ b/src/main/game/game_state.cpp @@ -14,7 +14,6 @@ #include "game_state.h" #include "../input-output/input/json-parsing/deal_parser.h" #include "../input-output/output/state_printer.h" -#include "sol_rules.h" using namespace std; using namespace rapidjson; @@ -23,15 +22,15 @@ using namespace boost; typedef sol_rules::build_policy pol; typedef sol_rules::stock_deal_type sdt; - ////////////////// // CONSTRUCTORS // ////////////////// // 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) : - rules(s_rules) { +game_state::game_state(const sol_rules& s_rules) { + game_state::static_reset(); + game_state::rules = s_rules; // If there is a hole, creates pile if (rules.hole) { @@ -85,14 +84,14 @@ game_state::game_state(const sol_rules& s_rules) : } // Constructs an initial game state from a JSON doc -game_state::game_state(const sol_rules& s_rules, const Document& doc) : - game_state(s_rules) { +game_state::game_state(const sol_rules& s_rules, const Document& doc) + : game_state(s_rules) { deal_parser::parse(*this, doc); } // Constructs an initial game state from a seed -game_state::game_state(const sol_rules& s_rules, int seed) : - game_state(s_rules) { +game_state::game_state(const sol_rules& s_rules, int seed) + : game_state(s_rules) { vector deck = gen_shuffled_deck(seed, rules.max_rank, rules.two_decks); // If there is a hole, moves the ace of spades to it @@ -104,7 +103,7 @@ game_state::game_state(const sol_rules& s_rules, int seed) : // If the foundations begin filled, then fills them if (rules.foundations_init_card) { for (uint8_t f_idx = 0; f_idx < 4*(rules.two_decks ? 2:1); f_idx++) { - card c = card(card::to_suit(f_idx % uint8_t(4)), 1); + card c = card(f_idx % uint8_t(4), 1); deck.erase(find(begin(deck), end(deck), c)); piles[foundations[f_idx]].place(c); @@ -130,6 +129,9 @@ game_state::game_state(const sol_rules& s_rules, int seed) : } } + // This only occurs during testing + if (rules.tableau_pile_count == 0) return; + // Deals to the tableau piles (row-by-row) for (int t = 0; !deck.empty(); t++) { card c = deck.back(); @@ -163,6 +165,10 @@ game_state::game_state(const sol_rules& s_rules, int seed) : } } +game_state::game_state(std::initializer_list il) { + piles = il; +} + // Generates a randomly ordered vector of cards vector game_state::gen_shuffled_deck(int seed, card::rank_t max_rank, bool two_decks) { @@ -183,7 +189,7 @@ vector game_state::gen_shuffled_deck(int seed, card::rank_t max_rank, vector deck; for (uint8_t *i : v_ptrs) { auto r = static_cast(((*i) % max_rank) + 1); - card::suit_t s = card::to_suit((*i) / max_rank); + card::suit_t s = (*i) / max_rank; deck.emplace_back(card(s, r)); } @@ -197,6 +203,31 @@ 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 // ///////////////////////// @@ -310,12 +341,6 @@ const vector& game_state::get_data() const { return piles; } -bool operator==(const game_state& a, const game_state& b) { - return a.tableau_piles == b.tableau_piles - && a.foundations == b.foundations - && a.hole == b.hole; -} - /////////// // PRINT // diff --git a/src/main/game/game_state.h b/src/main/game/game_state.h index 8a41398..d8e35f3 100644 --- a/src/main/game/game_state.h +++ b/src/main/game/game_state.h @@ -15,12 +15,13 @@ #include "pile.h" #include "sol_rules.h" -class deal_parser; +//class deal_parser; -class game_state { - friend class deal_parser; +// TODO make class +struct game_state { + //friend class deal_parser; friend class state_printer; -public: +//public: // Defines types typedef uint8_t pile_ref; struct move { @@ -34,6 +35,9 @@ public: // Does the same from a 'rules' object game_state(const sol_rules&, int seed); + // For testing + game_state(std::initializer_list); + // Alters state void make_move(move); void undo_move(move); @@ -44,13 +48,13 @@ public: bool is_solved() const; const std::vector& get_data() const; - friend bool operator==(const game_state&, const game_state&); friend std::ostream& operator<< (std::ostream&, const game_state&); friend std::size_t hash_value(card const&); - friend std::size_t hash_value(std::vector const&); + friend std::size_t hash_value(game_state const&); -private: +//private: + void static_reset(); static std::vector gen_shuffled_deck(int, card::rank_t, bool); // Private constructor @@ -69,18 +73,17 @@ private: card) const; // References to piles - 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; + 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; // The core piles std::vector piles; }; - #endif //SOLVITAIRE_GAME_STATE_H -- cgit v1.2.1 From 4c328ff6bd2fadbebb48753c28edee32a6695848 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 18 Jan 2018 13:53:51 +0000 Subject: Revamp basic global cache class --- src/main/game/global_cache.cpp | 119 ++++++++++++++++++++++++++++------------- src/main/game/global_cache.h | 29 +++++----- 2 files changed, 99 insertions(+), 49 deletions(-) (limited to 'src/main') diff --git a/src/main/game/global_cache.cpp b/src/main/game/global_cache.cpp index a1d7585..83a9420 100644 --- a/src/main/game/global_cache.cpp +++ b/src/main/game/global_cache.cpp @@ -2,7 +2,7 @@ // Created by thecharlesblake on 1/10/18. // -#include +#include #include "global_cache.h" @@ -11,39 +11,35 @@ using namespace boost; typedef sol_rules::build_policy pol; typedef sol_rules::stock_deal_type sdt; -typedef global_cache cache; -global_cache::global_cache(const game_state init_game_state) { - global_cache::init_gs = init_game_state; +bool global_cache::insert(const game_state& gs) { + return u_set.insert(gs).second; } -bool global_cache::insert(const std::vector& pile_vec) { - return u_set.insert(pile_vec).second; +bool global_cache::contains(const game_state& gs) const { + return u_set.count(gs) > 0; } -bool global_cache::contains(const std::vector& pile_vec) { - return u_set.count(pile_vec) > 0; +void global_cache::clear() { + u_set.clear(); } size_t hash_value(card const& c) { - boost::hash hasher; + boost::hash hasher; - auto suit_val; - switch (cache::init_gs.rules.build_pol) { + uint8_t suit_val; + switch (game_state::rules.build_pol) { case pol::SAME_SUIT: - suit_val = static_cast>( - c.get_suit()); + suit_val = static_cast(c.get_suit()); break; case pol::RED_BLACK: - suit_val = static_cast>( - c.get_colour()); + suit_val = static_cast(c.get_colour()); break; default: suit_val = 0; - break; } - auto raw_val = static_cast(suit_val * 13 + c.get_rank()); + auto raw_val = static_cast(suit_val * 13 + c.get_rank()); return hasher(raw_val); } @@ -51,44 +47,95 @@ size_t hash_value(pile const& p) { return hash_range(begin(p.pile_vec), end(p.pile_vec)); } -size_t hash_value(std::vector const& piles) { +size_t hash_value(game_state const& gs) { + boost::hash pile_hasher; size_t seed = 0; // Using addition for commutative hash - for (game_state::pile_ref pr : cache::init_gs.tableau_piles) { - seed += hash_value(piles[pr]); + for (game_state::pile_ref pr : game_state::tableau_piles) { + seed += pile_hasher(gs.piles[pr]); } // Using hash_combine for order-dependent hash - for (game_state::pile_ref pr : cache::init_gs.foundations) { - hash_combine(seed, piles[pr]); + for (game_state::pile_ref pr : game_state::foundations) { + hash_combine(seed, gs.piles[pr]); } // Commutative - for (game_state::pile_ref pr : cache::init_gs.reserve) { - seed += hash_value(piles[pr]); - } - - if (cache::init_gs.rules.stock_size > 0) { - hash_combine(seed, piles[cache::init_gs.stock]); + for (game_state::pile_ref pr : game_state::reserve) { + seed += pile_hasher(gs.piles[pr]); } - if (cache::init_gs.rules.stock_deal_t == sdt::WASTE) { - hash_combine(seed, piles[cache::init_gs.waste]); + if (game_state::rules.stock_size > 0) { + hash_combine(seed, gs.piles[game_state::stock]); } - if (cache::init_gs.rules.stock_deal_t == sdt::WASTE) { - hash_combine(seed, piles[cache::init_gs.waste]); + if (game_state::rules.stock_deal_t == sdt::WASTE) { + hash_combine(seed, gs.piles[game_state::waste]); } - if (cache::init_gs.rules.hole) { - hash_combine(seed, piles[cache::init_gs.hole]); + if (game_state::rules.hole) { + hash_combine(seed, gs.piles[game_state::hole]); } // Commutative - for (game_state::pile_ref pr : cache::init_gs.cells) { - seed += hash_value(piles[pr]); + for (game_state::pile_ref pr : game_state::cells) { + seed += pile_hasher(gs.piles[pr]); } return seed; } + +bool global_cache::game_state_pred::comp_pile(const pile &x, const pile &y) { + // Largest piles first + if (x.size() != y.size()) { + return x.size() > y.size(); + } + // Larger card values first + for (pile::size_type pile_idx = 0; pile_idx < x.size(); pile_idx++) { + auto x_val = static_cast(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; +} + +bool global_cache::game_state_pred::operator()(const game_state& x, + const game_state& y) const { + if (x.piles.size() != y.piles.size()) return false; + + vector x_piles = x.piles; + vector y_piles = y.piles; + + sort(begin(x_piles), end(x_piles), comp_pile); + sort(begin(y_piles), end(y_piles), comp_pile); + + 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; + + for (auto card_idx = 0; card_idx < x_piles[pile_idx].size(); card_idx++) { + card cx = x_piles[pile_idx][card_idx]; + card cy = y_piles[pile_idx][card_idx]; + + if (cx.get_rank() != cy.get_rank()) return false; + + switch (game_state::rules.build_pol) { + case pol::SAME_SUIT: + if (cx.get_suit() != cy.get_suit()) return false; + break; + case pol::RED_BLACK: + if (cx.get_colour() != cy.get_colour()) return false; + break; + default: + break; + } + } + } + + return true; +} diff --git a/src/main/game/global_cache.h b/src/main/game/global_cache.h index 73f0ce7..a3d1e48 100644 --- a/src/main/game/global_cache.h +++ b/src/main/game/global_cache.h @@ -8,24 +8,27 @@ #include #include -#include "../game/game_state.h" -#include "../game/sol_rules.h" +#include + +#include "sol_rules.h" +#include "game_state.h" class global_cache { public: - explicit global_cache(game_state); - - bool insert(const std::vector&); - bool contains(const std::vector&); - - friend std::size_t hash_value(card const&); - friend std::size_t hash_value(std::vector const&); + bool insert(const game_state&); + bool contains(const game_state&) const; + void clear(); + + 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; + }; private: - static game_state init_gs; - std::unordered_set< - std::vector, - boost::hash> + game_state, + boost::hash, + game_state_pred > u_set; }; -- cgit v1.2.1 From f4963c12207c433fb37446c7d20ecf3f24612e36 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 18 Jan 2018 13:57:00 +0000 Subject: Update solver to use new cache --- src/main/solver/solver.cpp | 13 +++++++------ src/main/solver/solver.h | 11 +++-------- 2 files changed, 10 insertions(+), 14 deletions(-) (limited to 'src/main') diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp index bd19351..ffcbc09 100644 --- a/src/main/solver/solver.cpp +++ b/src/main/solver/solver.cpp @@ -13,8 +13,8 @@ using namespace std; using namespace boost; -solver::solver(const game_state& gs, const sol_rules& sr) - : initial_state(gs), rules(sr), states_searched(0) {} +solver::solver(const game_state& gs) + : initial_state(gs), states_searched(0) {} solver::node::node(const game_state::move m, vector uc) @@ -27,7 +27,7 @@ bool solver::run() { states_searched++; LOG_DEBUG (state); frontier.emplace_back(game_state::move(0,0), state.get_legal_moves()); - global_cache.insert(state.get_data()); + cache.insert(state); while (!frontier.empty()) { node& current = frontier.back(); @@ -41,7 +41,7 @@ bool solver::run() { // (unless it's the null first move) if (frontier.size() > 1) { state.undo_move(current.move); - assert(global_cache.count(state.get_data()) == 1); + assert(cache.contains(state)); LOG_DEBUG (state); } // Returns to the previous state @@ -53,8 +53,9 @@ bool solver::run() { // Applies the first possible move in this state state.make_move(next_move); + assert(state.piles.size() == 18); // Insert the state into the global cache - bool is_new_state = global_cache.insert(state.get_data()).second; + bool is_new_state = cache.insert(state); if (is_new_state) { states_searched++; LOG_DEBUG (state); @@ -64,7 +65,7 @@ bool solver::run() { } else { // If we've seen the state before, undoes the move state.undo_move(next_move); - assert(global_cache.count(state.get_data()) == 1); + assert(cache.contains(state)); } } } diff --git a/src/main/solver/solver.h b/src/main/solver/solver.h index 563a11e..526bd53 100644 --- a/src/main/solver/solver.h +++ b/src/main/solver/solver.h @@ -7,17 +7,13 @@ #include #include -#include -#include "../game/game_state.h" +#include "../game/global_cache.h" #include "../game/sol_rules.h" class solver { public: - std::unordered_set< - std::vector, - boost::hash> - > global_cache; + global_cache cache; struct node { explicit node(game_state::move, std::vector); @@ -25,14 +21,13 @@ public: std::vector unsearched_children; }; - solver(const game_state&, const sol_rules&); + explicit solver(const game_state&); bool run(); void print_solution() const; private: const game_state initial_state; - const sol_rules rules; std::vector frontier; int states_searched; }; -- cgit v1.2.1 From 4fb1b0c11109f9acdf663d01941a7c397ad43881 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 18 Jan 2018 13:57:26 +0000 Subject: Shift around way rules are passed in main --- src/main/main.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/main') diff --git a/src/main/main.cpp b/src/main/main.cpp index ac81a19..78a3551 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -18,7 +18,7 @@ namespace po = boost::program_options; const optional gen_rules(command_line_helper&); void solve_random_game(int, const sol_rules&); void solve_input_files(vector, const sol_rules&); -void solve_game(const game_state&, const sol_rules&); +void solve_game(const game_state&); int main(int argc, const char* argv[]) { @@ -66,7 +66,7 @@ const optional gen_rules(command_line_helper& clh) { void solve_random_game(int seed, const sol_rules& rules) { LOG_INFO ("Attempting to solve with seed: " << seed << "..."); game_state gs(rules, seed); - solve_game(gs, rules); + solve_game(gs); } void solve_input_files(const vector input_files, const sol_rules& rules) { @@ -79,7 +79,7 @@ void solve_input_files(const vector input_files, const sol_rules& rules) game_state gs(rules, in_doc); LOG_INFO ("Attempting to solve " << input_file << "..."); - solve_game(gs, rules); + solve_game(gs); } catch (const runtime_error& error) { string errmsg = "Error parsing deal file: "; @@ -89,8 +89,8 @@ void solve_input_files(const vector input_files, const sol_rules& rules) } } -void solve_game(const game_state& gs, const sol_rules& rules) { - solver solv(gs, rules); +void solve_game(const game_state& gs) { + solver solv(gs); bool solution = solv.run(); if (solution) { -- cgit v1.2.1 From 413008eafb6cab3f2e24ca23e7e5c052dd04ddd9 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 18 Jan 2018 13:57:53 +0000 Subject: Fix sol_rules setup issues --- src/main/game/sol_rules.cpp | 4 ++-- src/main/game/sol_rules.h | 4 +--- 2 files changed, 3 insertions(+), 5 deletions(-) (limited to 'src/main') diff --git a/src/main/game/sol_rules.cpp b/src/main/game/sol_rules.cpp index 6c16b4a..7311e06 100644 --- a/src/main/game/sol_rules.cpp +++ b/src/main/game/sol_rules.cpp @@ -18,12 +18,12 @@ sol_rules::sol_rules() : two_decks(false), max_rank(13), hole(false), - foundations(true), + foundations(false), foundations_init_card(false), diagonal_deal(false), cells(0), stock_size(0), - stock_deal_t(sdt::WASTE), + stock_deal_t(sdt::TABLEAU_PILES), reserve_size(0), reserve_stacked(false) { } diff --git a/src/main/game/sol_rules.h b/src/main/game/sol_rules.h index 8aae42b..067e107 100644 --- a/src/main/game/sol_rules.h +++ b/src/main/game/sol_rules.h @@ -9,8 +9,6 @@ #include -#include "card.h" - struct sol_rules { sol_rules(); @@ -35,7 +33,7 @@ struct sol_rules { spaces_policy spaces_pol; bool move_built_group; bool two_decks; - card::rank_t max_rank; + uint8_t max_rank; bool hole; bool foundations; bool foundations_init_card; -- cgit v1.2.1 From 60b86edcba4cd61a0b14aa5e1138a1dd1d7d8718 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 18 Jan 2018 18:57:58 +0000 Subject: Tidy up game_state private access --- src/main/game/game_state.h | 12 +++++------- src/main/game/global_cache.cpp | 4 ++-- src/main/game/global_cache.h | 12 ++++++------ 3 files changed, 13 insertions(+), 15 deletions(-) (limited to 'src/main') diff --git a/src/main/game/game_state.h b/src/main/game/game_state.h index d8e35f3..8dcffa4 100644 --- a/src/main/game/game_state.h +++ b/src/main/game/game_state.h @@ -15,13 +15,11 @@ #include "pile.h" #include "sol_rules.h" -//class deal_parser; - -// TODO make class -struct game_state { - //friend class deal_parser; +class game_state { + friend class deal_parser; friend class state_printer; -//public: + friend class game_state_pred; +public: // Defines types typedef uint8_t pile_ref; struct move { @@ -53,7 +51,7 @@ struct game_state { friend std::size_t hash_value(card const&); friend std::size_t hash_value(game_state const&); -//private: +private: void static_reset(); static std::vector gen_shuffled_deck(int, card::rank_t, bool); diff --git a/src/main/game/global_cache.cpp b/src/main/game/global_cache.cpp index 83a9420..7be491e 100644 --- a/src/main/game/global_cache.cpp +++ b/src/main/game/global_cache.cpp @@ -86,7 +86,7 @@ size_t hash_value(game_state const& gs) { return seed; } -bool global_cache::game_state_pred::comp_pile(const pile &x, const pile &y) { +bool game_state_pred::comp_pile(const pile &x, const pile &y) { // Largest piles first if (x.size() != y.size()) { return x.size() > y.size(); @@ -105,7 +105,7 @@ bool global_cache::game_state_pred::comp_pile(const pile &x, const pile &y) { return false; } -bool global_cache::game_state_pred::operator()(const game_state& x, +bool game_state_pred::operator()(const game_state& x, const game_state& y) const { if (x.piles.size() != y.piles.size()) return false; diff --git a/src/main/game/global_cache.h b/src/main/game/global_cache.h index a3d1e48..486006c 100644 --- a/src/main/game/global_cache.h +++ b/src/main/game/global_cache.h @@ -13,17 +13,17 @@ #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 global_cache { public: bool insert(const game_state&); bool contains(const game_state&) const; void clear(); - - 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; - }; private: std::unordered_set< game_state, -- cgit v1.2.1 From 4bc355a0c61d043a6231889e8d902b6cf951096c Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 18 Jan 2018 23:11:16 +0000 Subject: Add SIMPLE_HASH debug flag to cache logic --- src/main/game/global_cache.cpp | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'src/main') diff --git a/src/main/game/global_cache.cpp b/src/main/game/global_cache.cpp index 7be491e..8e3aa8b 100644 --- a/src/main/game/global_cache.cpp +++ b/src/main/game/global_cache.cpp @@ -27,6 +27,9 @@ void global_cache::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: @@ -38,6 +41,7 @@ size_t hash_value(card const& c) { default: suit_val = 0; } +#endif auto raw_val = static_cast(suit_val * 13 + c.get_rank()); return hasher(raw_val); @@ -48,9 +52,15 @@ size_t hash_value(pile const& p) { } size_t hash_value(game_state const& gs) { - boost::hash pile_hasher; 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]); + } +#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]); @@ -82,6 +92,7 @@ size_t hash_value(game_state const& gs) { for (game_state::pile_ref pr : game_state::cells) { seed += pile_hasher(gs.piles[pr]); } +#endif return seed; } @@ -112,9 +123,11 @@ bool game_state_pred::operator()(const game_state& x, vector x_piles = x.piles; vector y_piles = y.piles; + // 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 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; @@ -124,6 +137,9 @@ bool game_state_pred::operator()(const game_state& x, if (cx.get_rank() != cy.get_rank()) return false; +#ifdef SIMPLE_HASH + if (cx.get_suit() != cy.get_suit()) return false; +#else switch (game_state::rules.build_pol) { case pol::SAME_SUIT: if (cx.get_suit() != cy.get_suit()) return false; @@ -134,6 +150,7 @@ bool game_state_pred::operator()(const game_state& x, default: break; } +#endif } } -- cgit v1.2.1 From 3e27898606c71a937216187012229a26f1a3cd73 Mon Sep 17 00:00:00 2001 From: thecharlesblake Date: Thu, 18 Jan 2018 23:56:49 +0000 Subject: Removes unnecessary assert statement bug --- src/main/solver/solver.cpp | 1 - 1 file changed, 1 deletion(-) (limited to 'src/main') diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp index ffcbc09..9d25c35 100644 --- a/src/main/solver/solver.cpp +++ b/src/main/solver/solver.cpp @@ -53,7 +53,6 @@ bool solver::run() { // Applies the first possible move in this state state.make_move(next_move); - assert(state.piles.size() == 18); // Insert the state into the global cache bool is_new_state = cache.insert(state); if (is_new_state) { -- cgit v1.2.1