diff options
| author | Ian Gent | 2018-06-26 16:43:45 +0100 |
|---|---|---|
| committer | Ian Gent | 2018-06-26 16:43:45 +0100 |
| commit | 0798cfe1ecb92996ed7959383dcc4b8b0d51a1d9 (patch) | |
| tree | f6f6dbd927012de317feea6b2bb25d466cff1ac3 /src/main/game/search-state/game_state.cpp | |
| parent | first R file (diff) | |
| parent | Merge pull request #71 from thecharlesblake/correct-klondike (diff) | |
| download | metasol-0798cfe1ecb92996ed7959383dcc4b8b0d51a1d9.tar.gz metasol-0798cfe1ecb92996ed7959383dcc4b8b0d51a1d9.zip | |
Merge branch ‘master’ of github.com:thecharlesblake/Solvitaire into ipg
Diffstat (limited to 'src/main/game/search-state/game_state.cpp')
| -rw-r--r-- | src/main/game/search-state/game_state.cpp | 137 |
1 files changed, 104 insertions, 33 deletions
diff --git a/src/main/game/search-state/game_state.cpp b/src/main/game/search-state/game_state.cpp index 0a20f60..7870b1b 100644 --- a/src/main/game/search-state/game_state.cpp +++ b/src/main/game/search-state/game_state.cpp @@ -10,6 +10,7 @@ #include <boost/functional/hash.hpp> #include <boost/optional.hpp> +#include <boost/random.hpp> #include "game_state.h" #include "document.h" @@ -17,6 +18,7 @@ #include "../../input-output/output/state_printer.h" #include "../../input-output/output/log_helper.h" #include "../move.h" +#include "../sol_rules.h" using namespace rapidjson; using std::vector; @@ -27,12 +29,14 @@ using std::end; using std::rbegin; using std::rend; using std::runtime_error; -using std::default_random_engine; using std::max; using std::ostream; +using std::mt19937; +using std::string; typedef sol_rules::build_policy pol; typedef sol_rules::stock_deal_type sdt; +typedef sol_rules::face_up_policy fu; ////////////////// // CONSTRUCTORS // @@ -40,10 +44,12 @@ 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) : rules(s_rules), - stock(255), - waste(255), - hole(255) { +game_state::game_state(const sol_rules& s_rules, bool streamliners_) + : rules(s_rules) + , streamliners(streamliners_) + , stock(255) + , waste(255) + , hole (255) { // If there is a hole, creates pile if (rules.hole) { piles.emplace_back(); @@ -100,14 +106,14 @@ game_state::game_state(const sol_rules& s_rules) : 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, bool streamliners_) + : game_state(s_rules, streamliners_) { 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, bool streamliners_) + : game_state(s_rules, streamliners_) { 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 @@ -152,7 +158,10 @@ game_state::game_state(const sol_rules& s_rules, int seed) for (int t = 0; !deck.empty(); t++) { card c = deck.back(); - // Add the randomly generated card to the tableau piles + // If only the top cards are face up, initially deals all face down + if (rules.face_up == fu::TOP_CARDS) c.turn_face_down(); + + // Adds the randomly generated card to the tableau piles auto p = t % original_tableau_piles.size(); // If we are doing a diagonal deal, each row should have one fewer card. @@ -173,6 +182,12 @@ game_state::game_state(const sol_rules& s_rules, int seed) } } + // Now if necessary, turns the top cards face up + if (rules.face_up == fu::TOP_CARDS) + for (auto& p : piles) + if (!p.empty()) + p[0].turn_face_up(); + // The size of all piles must equal the deck size int piles_sz = 0; for (auto& p : piles) piles_sz += p.size(); @@ -182,11 +197,11 @@ game_state::game_state(const sol_rules& s_rules, int seed) } game_state::game_state(const sol_rules& s_rules, - std::initializer_list<pile> il) - : game_state(s_rules) { - pile::ref pr = 0; - for (const pile& p : il) { - for (const card c : p.pile_vec) { + std::initializer_list<std::initializer_list<string>> il) + : game_state(s_rules, false) {pile::ref pr = 0; + for (auto& p_il : il) { + for (const string& card_str : p_il) { + card c(card_str.c_str(), s_rules.face_up == fu::TOP_CARDS); place_card(pr, c); } pr++; @@ -196,31 +211,38 @@ game_state::game_state(const sol_rules& s_rules, // Generates a randomly ordered vector of cards vector<card> game_state::gen_shuffled_deck(int seed, card::rank_t max_rank, bool two_decks) { - vector<uint8_t> values; - vector<uint8_t*> v_ptrs; - for (int i = 0; i < max_rank * 4; i++) { - values.emplace_back(i); - if (two_decks) values.emplace_back(i); - } - for (auto& v : values) { - v_ptrs.emplace_back(&v); - } - - // Randomly shuffle the pointers - auto rng = default_random_engine(seed+1); - shuffle(begin(v_ptrs), end(v_ptrs), rng); - vector<card> deck; - for (uint8_t *i : v_ptrs) { - auto r = static_cast<card::rank_t>(((*i) % max_rank) + 1); - card::suit_t s = (*i) / max_rank; - deck.emplace_back(card(s, r)); + + for (int deck_count = 1; deck_count <= (two_decks ? 2 : 1); deck_count++) { + for (card::rank_t rank = 1; rank <= max_rank; rank++) { + for (card::suit_t suit = 0 ; suit < 4; suit++) { + deck.emplace_back(suit, rank); + } + } } assert(deck.size() == pile::size_type(max_rank * (two_decks ? 8 : 4))); + + auto rng = mt19937(seed); + game_state::shuffle(begin(deck), end(deck), rng); return deck; } +template<class RandomIt, class URBG> +void game_state::shuffle(RandomIt first, RandomIt last, URBG&& g) { + typedef typename std::iterator_traits<RandomIt>::difference_type diff_t; + typedef boost::random::uniform_int_distribution<diff_t> distr_t; + typedef typename distr_t::param_type param_t; + + distr_t D; + diff_t n = last - first; + for (diff_t i = n-1; i > 0; --i) { + using std::swap; + swap(first[i], first[D(g, param_t(0, i))]); + } +} + + //////////////////// // ALTERING STATE // @@ -248,6 +270,10 @@ void game_state::make_move(const move m) { assert(false); break; } + +#ifndef NDEBUG + check_face_down_consistent(); +#endif } void game_state::undo_move(const move m) { @@ -272,6 +298,10 @@ void game_state::undo_move(const move m) { assert(false); break; } + +#ifndef NDEBUG + check_face_down_consistent(); +#endif } void game_state::make_regular_move(const move m) { @@ -280,6 +310,12 @@ void game_state::make_regular_move(const move m) { place_card(m.to, take_card(m.from)); + if (m.reveal_move) { + assert(!piles[m.from].empty()); + assert(piles[m.from][0].is_face_down()); + piles[m.from][0].turn_face_up(); + } + #ifndef NO_AUTO_FOUNDATIONS update_auto_foundation_moves(m.to); #endif @@ -288,6 +324,12 @@ void game_state::make_regular_move(const move m) { void game_state::undo_regular_move(const move m) { assert(m.to < piles.size()); + if (m.reveal_move) { + assert(!piles[m.from].empty()); + assert(!piles[m.from][0].is_face_down()); + piles[m.from][0].turn_face_down(); + } + place_card(m.from, take_card(m.to)); #ifndef NO_AUTO_FOUNDATIONS @@ -310,6 +352,12 @@ void game_state::make_built_group_move(move m) { for (uint8_t rem_count = 0; rem_count < m.count; rem_count++) { take_card(m.from); } + + if (m.reveal_move) { + assert(!piles[m.from].empty()); + assert(piles[m.from][0].is_face_down()); + piles[m.from][0].turn_face_up(); + } } void game_state::undo_built_group_move(move m) { @@ -317,6 +365,12 @@ void game_state::undo_built_group_move(move m) { assert(m.to < piles.size()); assert(m.count <= rules.max_rank); + if (m.reveal_move) { + assert(!piles[m.from].empty()); + assert(!piles[m.from][0].is_face_down()); + piles[m.from][0].turn_face_down(); + } + // Adds the cards to the 'from' pile for (auto pile_idx = m.count; pile_idx-- > 0;) { place_card(m.from, piles[m.to][pile_idx]); @@ -406,6 +460,23 @@ card game_state::take_card(pile::ref pr) { return c; } +#ifndef NDEBUG +void game_state::check_face_down_consistent() const { + for (auto& p : original_tableau_piles) { + if (piles[p].empty()) continue; + // Makes sure the top cards of each tableau pile are face up + assert(!piles[p].top_card().is_face_down()); + + // Makes sure face down cards are never above face down ones + bool seen_face_up = false; + for (auto& c : piles[p].pile_vec) { + seen_face_up = seen_face_up || !c.is_face_down(); + assert(!(c.is_face_down() && seen_face_up)); + } + } +} +#endif + //////////////////////// // INSPECT GAME STATE // |
