diff options
Diffstat (limited to 'src/main/game')
| -rw-r--r-- | src/main/game/card.cpp | 47 | ||||
| -rw-r--r-- | src/main/game/card.h | 22 | ||||
| -rw-r--r-- | src/main/game/game_state.cpp | 90 | ||||
| -rw-r--r-- | src/main/game/game_state.h | 30 | ||||
| -rw-r--r-- | src/main/game/game_state.legal_moves.cpp | 2 | ||||
| -rw-r--r-- | src/main/game/global_cache.cpp | 158 | ||||
| -rw-r--r-- | src/main/game/global_cache.h | 35 | ||||
| -rw-r--r-- | src/main/game/pile.cpp | 13 | ||||
| -rw-r--r-- | src/main/game/pile.h | 4 | ||||
| -rw-r--r-- | src/main/game/sol_rules.cpp | 29 | ||||
| -rw-r--r-- | src/main/game/sol_rules.h | 6 |
11 files changed, 331 insertions, 105 deletions
diff --git a/src/main/game/card.cpp b/src/main/game/card.cpp index 80ebef6..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) { @@ -74,12 +82,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<unsigned char> hasher; - auto raw_val = static_cast<unsigned char>( - static_cast<std::underlying_type_t<card::suit_t>>(c.get_suit()) - * 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..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,17 +36,18 @@ 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; }; 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/game_state.cpp b/src/main/game/game_state.cpp index f8f9e1e..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<card> 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<pile> il) { + piles = il; +} + // Generates a randomly ordered vector of cards vector<card> game_state::gen_shuffled_deck(int seed, card::rank_t max_rank, bool two_decks) { @@ -183,7 +189,7 @@ vector<card> game_state::gen_shuffled_deck(int seed, card::rank_t max_rank, vector<card> deck; for (uint8_t *i : v_ptrs) { auto r = static_cast<card::rank_t>(((*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::pile_ref> game_state::tableau_piles; +std::vector<game_state::pile_ref> game_state::cells; +game_state::pile_ref game_state::stock; +game_state::pile_ref game_state::waste; +std::vector<game_state::pile_ref> game_state::reserve; +std::vector<game_state::pile_ref> game_state::foundations; +game_state::pile_ref game_state::hole; + + +void game_state::static_reset() { + tableau_piles.clear(); + cells.clear(); + stock = 0; + waste = 0; + reserve.clear(); + foundations.clear(); + hole = 0; +} + + ///////////////////////// // MODIFYING FUNCTIONS // ///////////////////////// @@ -310,45 +341,6 @@ const vector<pile>& 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; -} - - -//////////////////// -// 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<pile> 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..8dcffa4 100644 --- a/src/main/game/game_state.h +++ b/src/main/game/game_state.h @@ -15,11 +15,10 @@ #include "pile.h" #include "sol_rules.h" -class deal_parser; - class game_state { friend class deal_parser; friend class state_printer; + friend class game_state_pred; public: // Defines types typedef uint8_t pile_ref; @@ -34,6 +33,9 @@ public: // Does the same from a 'rules' object game_state(const sol_rules&, int seed); + // For testing + game_state(std::initializer_list<pile>); + // Alters state void make_move(move); void undo_move(move); @@ -44,14 +46,13 @@ public: bool is_solved() const; const std::vector<pile>& 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(game_state const&); - friend std::size_t hash_value(std::vector<pile> const&); - - friend std::ostream& operator<< (std::ostream&, const game_state&); private: + void static_reset(); static std::vector<card> gen_shuffled_deck(int, card::rank_t, bool); // Private constructor @@ -70,18 +71,17 @@ private: card) const; // References to piles - sol_rules rules; - std::vector<pile_ref> tableau_piles; - std::vector<pile_ref> cells; - pile_ref stock; - pile_ref waste; - std::vector<pile_ref> reserve; - std::vector<pile_ref> foundations; - pile_ref hole; + static sol_rules rules; + static std::vector<pile_ref> tableau_piles; + static std::vector<pile_ref> cells; + static pile_ref stock; + static pile_ref waste; + static std::vector<pile_ref> reserve; + static std::vector<pile_ref> foundations; + static pile_ref hole; // The core piles std::vector<pile> piles; }; - #endif //SOLVITAIRE_GAME_STATE_H 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; } diff --git a/src/main/game/global_cache.cpp b/src/main/game/global_cache.cpp new file mode 100644 index 0000000..8e3aa8b --- /dev/null +++ b/src/main/game/global_cache.cpp @@ -0,0 +1,158 @@ +// +// Created by thecharlesblake on 1/10/18. +// + +#include <algorithm> + +#include "global_cache.h" + +using namespace std; +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::contains(const game_state& gs) const { + return u_set.count(gs) > 0; +} + +void global_cache::clear() { + u_set.clear(); +} + +size_t hash_value(card const& c) { + boost::hash<uint8_t> hasher; + +#ifdef SIMPLE_HASH + uint8_t suit_val = static_cast<uint8_t>(c.get_suit()); +#else + uint8_t suit_val; + switch (game_state::rules.build_pol) { + case pol::SAME_SUIT: + suit_val = static_cast<uint8_t>(c.get_suit()); + break; + case pol::RED_BLACK: + suit_val = static_cast<uint8_t>(c.get_colour()); + break; + default: + suit_val = 0; + } +#endif + + auto raw_val = static_cast<uint8_t>(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)); +} + +size_t hash_value(game_state const& gs) { + 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> pile_hasher; + + // Using addition for commutative hash + 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 : game_state::foundations) { + hash_combine(seed, gs.piles[pr]); + } + + // Commutative + for (game_state::pile_ref pr : game_state::reserve) { + seed += pile_hasher(gs.piles[pr]); + } + + if (game_state::rules.stock_size > 0) { + hash_combine(seed, gs.piles[game_state::stock]); + } + + if (game_state::rules.stock_deal_t == sdt::WASTE) { + hash_combine(seed, gs.piles[game_state::waste]); + } + + if (game_state::rules.hole) { + hash_combine(seed, gs.piles[game_state::hole]); + } + + // Commutative + for (game_state::pile_ref pr : game_state::cells) { + seed += pile_hasher(gs.piles[pr]); + } +#endif + + return seed; +} + +bool game_state_pred::comp_pile(const pile &x, const pile &y) { + // Largest piles first + if (x.size() != y.size()) { + return x.size() > y.size(); + } + // Larger card values first + for (pile::size_type pile_idx = 0; pile_idx < x.size(); pile_idx++) { + auto x_val = static_cast<uint8_t>(x[pile_idx].get_suit()) * 13 + + x[pile_idx].get_rank(); + auto y_val = static_cast<uint8_t>(y[pile_idx].get_suit()) * 13 + + y[pile_idx].get_rank(); + if (x_val != y_val) { + return x_val > y_val; + } + } + // It is a requirement of std::sort that equal values return false + 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; + + vector<pile> x_piles = x.piles; + vector<pile> 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<pile>::size_type pile_idx = 0; pile_idx < x_piles.size(); pile_idx++) { + if (x_piles[pile_idx].size() != y_piles[pile_idx].size()) return false; + + for (auto card_idx = 0; card_idx < x_piles[pile_idx].size(); card_idx++) { + card cx = x_piles[pile_idx][card_idx]; + card cy = y_piles[pile_idx][card_idx]; + + 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; + break; + case pol::RED_BLACK: + if (cx.get_colour() != cy.get_colour()) return false; + break; + default: + break; + } +#endif + } + } + + return true; +} diff --git a/src/main/game/global_cache.h b/src/main/game/global_cache.h new file mode 100644 index 0000000..486006c --- /dev/null +++ b/src/main/game/global_cache.h @@ -0,0 +1,35 @@ +// +// Created by thecharlesblake on 1/10/18. +// + +#ifndef SOLVITAIRE_GLOBAL_CACHE_H +#define SOLVITAIRE_GLOBAL_CACHE_H + +#include <vector> +#include <unordered_set> + +#include <boost/functional/hash.hpp> + +#include "sol_rules.h" +#include "game_state.h" + +struct game_state_pred { + static bool comp_pile(const pile& x, const pile& y); + bool operator() (const game_state& x, const game_state& y) + const; +}; + +class global_cache { +public: + bool insert(const game_state&); + bool contains(const game_state&) const; + void clear(); +private: + std::unordered_set< + game_state, + boost::hash<game_state>, + game_state_pred + > u_set; +}; + +#endif //SOLVITAIRE_GLOBAL_CACHE_H diff --git a/src/main/game/pile.cpp b/src/main/game/pile.cpp index 82ae344..21eddc0 100644 --- a/src/main/game/pile.cpp +++ b/src/main/game/pile.cpp @@ -5,12 +5,15 @@ #include <vector> #include <ostream> -#include <boost/functional/hash.hpp> - #include "pile.h" using namespace std; -using namespace boost; + +pile::pile(std::vector<card> pv) : pile_vec(pv) { +} + +pile::pile(std::initializer_list<card> il) : pile_vec(il) { +} card pile::top_card() const { assert(!empty()); @@ -47,7 +50,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 f2600fe..411f890 100644 --- a/src/main/game/pile.h +++ b/src/main/game/pile.h @@ -15,6 +15,10 @@ class pile { public: typedef uint8_t size_type; + pile() = default; + pile(std::vector<card>); + pile(std::initializer_list<card>); + // Inspect card top_card() const; bool empty() const; diff --git a/src/main/game/sol_rules.cpp b/src/main/game/sol_rules.cpp new file mode 100644 index 0000000..7311e06 --- /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(false), + foundations_init_card(false), + diagonal_deal(false), + cells(0), + stock_size(0), + 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 812e82c..067e107 100644 --- a/src/main/game/sol_rules.h +++ b/src/main/game/sol_rules.h @@ -9,9 +9,9 @@ #include <rapidjson/document.h> -#include "card.h" - struct sol_rules { + sol_rules(); + // Tableau policies enum class build_policy { NO_BUILD, @@ -33,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; |
