From abae8e0529cf2e6048e9b2eb8248434e1f9551de Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Wed, 20 Jun 2018 17:37:22 +0100 Subject: Add streamliner command line option (inc for spgs) --- src/main/game/global_cache.cpp | 16 ++-------------- .../game_state.auto_foundation_moves.cpp | 4 +--- src/main/game/search-state/game_state.cpp | 20 +++++++++++--------- src/main/game/search-state/game_state.h | 7 ++++--- 4 files changed, 18 insertions(+), 29 deletions(-) (limited to 'src/main/game') diff --git a/src/main/game/global_cache.cpp b/src/main/game/global_cache.cpp index db0c6de..f688c33 100644 --- a/src/main/game/global_cache.cpp +++ b/src/main/game/global_cache.cpp @@ -81,13 +81,7 @@ void cached_game_state::add_card(card c, const game_state& gs) { // If the game is a 'hole-based' game, or suit-reduction is on, reduces // the cached suit of the card where possible - bool is_suit_symmetry = gs.rules.hole; -#ifdef SUIT_REDUCTION_STREAMLINER - is_suit_symmetry = true; -#endif -#ifdef NO_SUIT_SYMMETRY - is_suit_symmetry = false; -#endif + bool is_suit_symmetry = gs.streamliners || gs.rules.hole; if (is_suit_symmetry) { switch (gs.rules.build_pol) { @@ -141,13 +135,7 @@ size_t hasher::hash_value(card const& c) const { // If the game is a 'hole-based' game, or suit-reduction is enabled, hash // the reduced suit - bool is_suit_symmetry = init_gs.rules.hole; -#ifdef SUIT_REDUCTION_STREAMLINER - is_suit_symmetry = true; -#endif -#ifdef NO_SUIT_SYMMETRY - is_suit_symmetry = false; -#endif + bool is_suit_symmetry = init_gs.streamliners || init_gs.rules.hole; uint8_t suit_val; if (is_suit_symmetry) { diff --git a/src/main/game/search-state/game_state.auto_foundation_moves.cpp b/src/main/game/search-state/game_state.auto_foundation_moves.cpp index 0b62af4..1bec3fe 100644 --- a/src/main/game/search-state/game_state.auto_foundation_moves.cpp +++ b/src/main/game/search-state/game_state.auto_foundation_moves.cpp @@ -11,9 +11,7 @@ using std::max; bool game_state::is_valid_auto_foundation_move(pile::ref target_pile) const { -#ifndef AUTO_FOUNDATIONS_STREAMLINER - if (rules.build_pol == pol::NO_BUILD || rules.build_pol == pol::SAME_SUIT) -#endif + if (streamliners || rules.build_pol == pol::NO_BUILD || rules.build_pol == pol::SAME_SUIT) return true; card::suit_t target_suit(target_pile - foundations.front()); diff --git a/src/main/game/search-state/game_state.cpp b/src/main/game/search-state/game_state.cpp index 0a20f60..5d80d3e 100644 --- a/src/main/game/search-state/game_state.cpp +++ b/src/main/game/search-state/game_state.cpp @@ -40,10 +40,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 +102,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 deck = gen_shuffled_deck(seed, rules.max_rank, rules.two_decks); // If there is a hole, moves the ace of spades to it @@ -183,7 +185,7 @@ game_state::game_state(const sol_rules& s_rules, int seed) game_state::game_state(const sol_rules& s_rules, std::initializer_list il) - : game_state(s_rules) { + : game_state(s_rules, false) { pile::ref pr = 0; for (const pile& p : il) { for (const card c : p.pile_vec) { diff --git a/src/main/game/search-state/game_state.h b/src/main/game/search-state/game_state.h index 7c0f9ca..a0a74ea 100644 --- a/src/main/game/search-state/game_state.h +++ b/src/main/game/search-state/game_state.h @@ -28,9 +28,9 @@ public: /* Constructors */ // Creates a game state representation from a JSON doc - game_state(const sol_rules&, const rapidjson::Document&); + game_state(const sol_rules&, const rapidjson::Document&, bool); // Does the same from a seed - game_state(const sol_rules&, int seed); + game_state(const sol_rules&, int seed, bool); // Does the same but with an initialiser list (useful for testing) game_state(const sol_rules&, std::initializer_list); @@ -58,7 +58,7 @@ public: private: /* Constructors (& helper function) */ - explicit game_state(const sol_rules&); + explicit game_state(const sol_rules&, bool); static std::vector gen_shuffled_deck(int, card::rank_t, bool); /* Pile order logic */ @@ -116,6 +116,7 @@ private: /* Game rules */ const sol_rules rules; + bool streamliners; /* Pile references */ -- cgit v1.2.1 From 495ccca135cc139221b4205c7443499fee5ab63e Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Thu, 21 Jun 2018 11:53:11 +0100 Subject: Make randomness consistent --- src/main/game/search-state/game_state.cpp | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) (limited to 'src/main/game') diff --git a/src/main/game/search-state/game_state.cpp b/src/main/game/search-state/game_state.cpp index 5d80d3e..8303516 100644 --- a/src/main/game/search-state/game_state.cpp +++ b/src/main/game/search-state/game_state.cpp @@ -27,9 +27,9 @@ 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; typedef sol_rules::build_policy pol; typedef sol_rules::stock_deal_type sdt; @@ -198,28 +198,20 @@ game_state::game_state(const sol_rules& s_rules, // Generates a randomly ordered vector of cards vector game_state::gen_shuffled_deck(int seed, card::rank_t max_rank, bool two_decks) { - vector values; - vector 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 deck; - for (uint8_t *i : v_ptrs) { - auto r = static_cast(((*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); + shuffle(begin(deck), end(deck), rng); return deck; } -- cgit v1.2.1 From 982c29136530be549f84af290e20523688d7fbdd Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Thu, 21 Jun 2018 15:07:07 +0100 Subject: Fixup randomness --- src/main/game/search-state/game_state.cpp | 18 +++++++++++++++++- src/main/game/search-state/game_state.h | 1 + 2 files changed, 18 insertions(+), 1 deletion(-) (limited to 'src/main/game') diff --git a/src/main/game/search-state/game_state.cpp b/src/main/game/search-state/game_state.cpp index 8303516..82e413b 100644 --- a/src/main/game/search-state/game_state.cpp +++ b/src/main/game/search-state/game_state.cpp @@ -10,6 +10,7 @@ #include #include +#include #include "game_state.h" #include "document.h" @@ -211,10 +212,25 @@ vector game_state::gen_shuffled_deck(int seed, card::rank_t max_rank, assert(deck.size() == pile::size_type(max_rank * (two_decks ? 8 : 4))); auto rng = mt19937(seed); - shuffle(begin(deck), end(deck), rng); + game_state::shuffle(begin(deck), end(deck), rng); return deck; } +template +void game_state::shuffle(RandomIt first, RandomIt last, URBG&& g) { + typedef typename std::iterator_traits::difference_type diff_t; + typedef boost::random::uniform_int_distribution 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 // diff --git a/src/main/game/search-state/game_state.h b/src/main/game/search-state/game_state.h index a0a74ea..f335de1 100644 --- a/src/main/game/search-state/game_state.h +++ b/src/main/game/search-state/game_state.h @@ -60,6 +60,7 @@ private: explicit game_state(const sol_rules&, bool); static std::vector gen_shuffled_deck(int, card::rank_t, bool); + template static void shuffle(RandomIt, RandomIt, URBG&&); /* Pile order logic */ -- cgit v1.2.1 From 933554c4c2b1f1925fdbf3ce9ccd9b632c8f172a Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Fri, 22 Jun 2018 16:02:51 +0100 Subject: Change move order generation --- .../game/search-state/game_state.legal_moves.cpp | 126 ++++++++++++++++++++- 1 file changed, 123 insertions(+), 3 deletions(-) (limited to 'src/main/game') diff --git a/src/main/game/search-state/game_state.legal_moves.cpp b/src/main/game/search-state/game_state.legal_moves.cpp index f9fc941..5fb48df 100644 --- a/src/main/game/search-state/game_state.legal_moves.cpp +++ b/src/main/game/search-state/game_state.legal_moves.cpp @@ -8,6 +8,7 @@ using std::vector; using std::min; +using std::list; using namespace rapidjson; typedef sol_rules::build_policy pol; @@ -21,6 +22,125 @@ typedef sol_rules::stock_deal_type sdt; // Note that the moves added last here, are tried first vector game_state::get_legal_moves(move parent_move) { + // Order: + // Foundations complete piles moves + // Tableau built group moves + // Tableau / cells / reserve to hole / foundation moves + // Cell to tableau moves + // Tableau to tableau moves + // Waste to tableau moves + // Reserve to tableau moves + // Foundation to tableau moves + // Tableau to cell moves + // Stock to waste moves + // Stock to tableau / redeal moves + + vector moves; + + // Stock to tableau / redeal moves + if (rules.stock_size > 0) { + if (stock_can_deal_tableau()) + moves.emplace_back(get_stock_tableau_move()); + else if (stock_can_redeal()) + moves.emplace_back(move::mtype::redeal); + } + + // Stock to waste moves + if (rules.stock_size > 0 && rules.stock_deal_t == sdt::WASTE && !piles[stock].empty()) + add_stock_to_waste_move(moves); + + // Tableau to cell moves + pile::ref empty_cell = 255; + for (auto c : cells) { + if (piles[c].empty()) empty_cell = c; + } + if (empty_cell != 255) { + for (auto t : tableau_piles) { + if (piles[t].empty() || parent_move.to == t || !is_valid_tableau_move(t, empty_cell)) continue; + else moves.emplace_back(move::mtype::regular, t, empty_cell); + } + } + + // Foundation to tableau moves + if (rules.foundations_removable) { + for (auto f : foundations) { + if (piles[f].empty() || parent_move.to == f || dominance_blocks_foundation_move(f)) continue; + + for (auto t : tableau_piles) { + if (is_valid_tableau_move(f, t)) { + moves.emplace_back(move::mtype::regular, f, t); + } + } + } + } + + // Reserve to tableau moves + for (auto r : reserve) { + if (piles[r].empty()) continue; + + for (auto t : tableau_piles) { + if (is_valid_tableau_move(r, t)) { + moves.emplace_back(move::mtype::regular, r, t); + } + } + } + + // Waste to tableau moves + if (rules.stock_size > 0 && rules.stock_deal_t == sdt::WASTE && !piles[waste].empty()) { + for (auto t : tableau_piles) { + if (is_valid_tableau_move(waste, t)) { + moves.emplace_back(move::mtype::regular, waste, t); + } + } + } + + // Tableau to tableau moves + for (auto t_from : tableau_piles) { + if (piles[t_from].empty() || parent_move.to == t_from) continue; + + for (auto t_to : tableau_piles) { + if (is_valid_tableau_move(t_from, t_to)) { + moves.emplace_back(move::mtype::regular, t_from, t_to); + } + } + } + + // Cell to tableau moves + for (auto c : cells) { + if (piles[c].empty() || parent_move.to == c) continue; + + for (auto t : tableau_piles) { + if (is_valid_tableau_move(c, t)) { + moves.emplace_back(move::mtype::regular, c, t); + } + } + } + + // Tableau / cells to hole / foundation moves + if (rules.hole || rules.foundations) { + list from_piles = tableau_piles; + if (rules.cells > 0) from_piles.insert(from_piles.end(), cells.begin(), cells.end()); + if (rules.reserve_size > 0) from_piles.insert(from_piles.end(), reserve.begin(), reserve.end()); + + for (auto fp : from_piles) { + if (piles[fp].empty() || parent_move.to == fp) continue; + + for (auto f : foundations) + if (is_valid_foundations_move(fp, f)) + moves.emplace_back(move::mtype::regular, fp, f); + if (rules.hole && is_valid_hole_move(fp)) + moves.emplace_back(move::mtype::regular, fp, hole); + } + } + + if (rules.move_built_group) + add_built_group_moves(moves); + + if (rules.foundations_comp_piles) // I.E. Spider-type winning condition + add_foundation_complete_piles_moves(moves); + + /* + vector moves; // Attempts stock tableau deal and redeal @@ -49,10 +169,10 @@ vector game_state::get_legal_moves(move parent_move) { continue; } - if (rules.tableau_pile_count > 0) - add_tableau_moves(moves, rem_ref); if (rules.cells > 0) add_cell_moves(moves, rem_ref); + if (rules.tableau_pile_count > 0) + add_tableau_moves(moves, rem_ref); if (rules.foundations) add_foundation_moves(moves, rem_ref); if (rules.hole && is_valid_hole_move(rem_ref)) @@ -64,7 +184,7 @@ vector game_state::get_legal_moves(move parent_move) { if (rules.foundations_comp_piles) // I.E. Spider-type winning condition add_foundation_complete_piles_moves(moves); - +*/ return moves; } -- cgit v1.2.1 From 3a781e332c2a2d0d03b1eb35f1538929851aedaa Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Fri, 22 Jun 2018 16:21:37 +0100 Subject: Add simple simon --- src/main/game/search-state/game_state.auto_foundation_moves.cpp | 4 +++- src/main/game/search-state/game_state.legal_moves.cpp | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) (limited to 'src/main/game') diff --git a/src/main/game/search-state/game_state.auto_foundation_moves.cpp b/src/main/game/search-state/game_state.auto_foundation_moves.cpp index 1bec3fe..794c040 100644 --- a/src/main/game/search-state/game_state.auto_foundation_moves.cpp +++ b/src/main/game/search-state/game_state.auto_foundation_moves.cpp @@ -11,7 +11,9 @@ using std::max; bool game_state::is_valid_auto_foundation_move(pile::ref target_pile) const { - if (streamliners || rules.build_pol == pol::NO_BUILD || rules.build_pol == pol::SAME_SUIT) + if (rules.foundations_comp_piles) + return false; + else if (streamliners || rules.build_pol == pol::NO_BUILD || rules.build_pol == pol::SAME_SUIT) return true; card::suit_t target_suit(target_pile - foundations.front()); diff --git a/src/main/game/search-state/game_state.legal_moves.cpp b/src/main/game/search-state/game_state.legal_moves.cpp index 5fb48df..b7b910e 100644 --- a/src/main/game/search-state/game_state.legal_moves.cpp +++ b/src/main/game/search-state/game_state.legal_moves.cpp @@ -117,7 +117,7 @@ vector game_state::get_legal_moves(move parent_move) { } // Tableau / cells to hole / foundation moves - if (rules.hole || rules.foundations) { + if (rules.hole || (rules.foundations && !rules.foundations_comp_piles)) { list from_piles = tableau_piles; if (rules.cells > 0) from_piles.insert(from_piles.end(), cells.begin(), cells.end()); if (rules.reserve_size > 0) from_piles.insert(from_piles.end(), reserve.begin(), reserve.end()); -- cgit v1.2.1 From 2b92783762c2824e519b881afbf5d66a606c394f Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Mon, 25 Jun 2018 11:50:34 +0100 Subject: Add card face down unit tests --- src/main/game/card.cpp | 10 ++++++++++ src/main/game/card.h | 4 ++++ src/main/game/sol_rules.h | 6 ++++++ 3 files changed, 20 insertions(+) (limited to 'src/main/game') diff --git a/src/main/game/card.cpp b/src/main/game/card.cpp index cfb94a9..710b394 100644 --- a/src/main/game/card.cpp +++ b/src/main/game/card.cpp @@ -85,6 +85,16 @@ std::string card::to_string() const { return s; } +bool card::is_face_up() const { + return true; +} + +void card::turn_face_up() { +} + +void card::turn_face_down() { +} + const card card::divider = card(0, 0); bool operator==(const card& l, const card& r) { diff --git a/src/main/game/card.h b/src/main/game/card.h index a4c6b1e..ad14d2b 100644 --- a/src/main/game/card.h +++ b/src/main/game/card.h @@ -39,6 +39,10 @@ public: colour_t get_colour() const; rank_t get_rank() const; + bool is_face_up() const; + void turn_face_up(); + void turn_face_down(); + std::string to_string() const; private: diff --git a/src/main/game/sol_rules.h b/src/main/game/sol_rules.h index adab00d..a7b9ae6 100644 --- a/src/main/game/sol_rules.h +++ b/src/main/game/sol_rules.h @@ -28,6 +28,10 @@ struct sol_rules { WASTE, TABLEAU_PILES }; + enum class face_up_policy { + ALL, + TOP_CARDS + }; uint8_t tableau_pile_count; build_policy build_pol; @@ -49,6 +53,8 @@ struct sol_rules { bool stock_redeal; uint8_t reserve_size; bool reserve_stacked; + face_up_policy face_up; + }; #endif //SOLVITAIRE_SOL_RULES_H -- cgit v1.2.1 From 8167f678fcbfc34982458b0311346bbeb04da9af Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Mon, 25 Jun 2018 12:25:02 +0100 Subject: Add face down deal parsing --- src/main/game/card.cpp | 9 +++++---- src/main/game/card.h | 7 ++++--- 2 files changed, 9 insertions(+), 7 deletions(-) (limited to 'src/main/game') diff --git a/src/main/game/card.cpp b/src/main/game/card.cpp index 710b394..21bbd2f 100644 --- a/src/main/game/card.cpp +++ b/src/main/game/card.cpp @@ -22,11 +22,12 @@ const card::colour_t card::colour::Red = 1; // A card can either be created using two int values (rank & suit), or // using a string (e.g. "AS") -card::card(suit_t s, rank_t r) : card_rank(r), card_suit(s) {} +card::card(suit_t s, rank_t r, bool fd) : card_rank(r), card_suit(s), face_down(fd) {} -card::card(const char* c) : card_rank(rank_from_str(c)), card_suit(suit_from_str(c)) {} +card::card(const char* c, bool face_down_possible) : + card_rank(rank_from_str(c)), card_suit(suit_from_str(c)), face_down(face_down_possible) {} // TODO: CHANGE! -card::card() : card(0, 0) {} +card::card() : card(0, 0, false) {} card::suit_t card::suit_from_str(const char* c) { switch(tolower(c[strlen(c) - 1])) { @@ -95,7 +96,7 @@ void card::turn_face_up() { void card::turn_face_down() { } -const card card::divider = card(0, 0); +const card card::divider = card(); bool operator==(const card& l, const card& r) { return l.get_rank() == r.get_rank() && l.get_suit() == r.get_suit(); diff --git a/src/main/game/card.h b/src/main/game/card.h index ad14d2b..ea808b4 100644 --- a/src/main/game/card.h +++ b/src/main/game/card.h @@ -31,8 +31,8 @@ public: // A card can either be created using two int values (rank & suit), or // using a string (e.g. "AS") - card(suit_t, rank_t); - card(const char*); + card(suit_t, rank_t, bool face_down=false); + card(const char*, bool face_down_possible=false); card(); suit_t get_suit() const; @@ -50,7 +50,8 @@ private: static rank_t rank_from_str(const char* c); rank_t card_rank : 4; - suit_t card_suit : 4; + suit_t card_suit : 2; + bool face_down : 1; }; bool operator==(const card&, const card&); -- cgit v1.2.1 From a99b32bf9f62c4efa6c959bdd6ee4d4e52054bd0 Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Mon, 25 Jun 2018 12:30:23 +0100 Subject: Add game state face down random gen --- src/main/game/search-state/game_state.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'src/main/game') diff --git a/src/main/game/search-state/game_state.cpp b/src/main/game/search-state/game_state.cpp index 82e413b..8a1298b 100644 --- a/src/main/game/search-state/game_state.cpp +++ b/src/main/game/search-state/game_state.cpp @@ -18,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; @@ -34,6 +35,7 @@ using std::mt19937; typedef sol_rules::build_policy pol; typedef sol_rules::stock_deal_type sdt; +typedef sol_rules::face_up_policy fu; ////////////////// // CONSTRUCTORS // @@ -155,7 +157,10 @@ game_state::game_state(const sol_rules& s_rules, int seed, bool streamliners_) 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. @@ -176,6 +181,12 @@ game_state::game_state(const sol_rules& s_rules, int seed, bool streamliners_) } } + // 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(); -- cgit v1.2.1 From b3cd52e5b844414f66a2e9fde996ddeaf0b68fec Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Mon, 25 Jun 2018 12:31:41 +0100 Subject: Add state printing for face down cards --- src/main/game/card.cpp | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/main/game') diff --git a/src/main/game/card.cpp b/src/main/game/card.cpp index 21bbd2f..12e577f 100644 --- a/src/main/game/card.cpp +++ b/src/main/game/card.cpp @@ -69,6 +69,8 @@ card::rank_t card::get_rank() const { } std::string card::to_string() const { + if (face_down) return "##"; + string s; switch(card_rank) { case 1 : s += "A"; break; -- cgit v1.2.1 From 797400ea32ac1f6588e834fc1bcb42aa67fa6863 Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Tue, 26 Jun 2018 10:37:54 +0100 Subject: Finished klondike correct impl --- src/main/game/card.cpp | 21 ++++++-- src/main/game/card.h | 3 +- src/main/game/move.cpp | 10 +++- src/main/game/move.h | 5 +- .../game_state.auto_foundation_moves.cpp | 8 ++- src/main/game/search-state/game_state.cpp | 60 ++++++++++++++++++++-- src/main/game/search-state/game_state.h | 8 ++- .../game/search-state/game_state.legal_moves.cpp | 31 +++++++++-- 8 files changed, 128 insertions(+), 18 deletions(-) (limited to 'src/main/game') diff --git a/src/main/game/card.cpp b/src/main/game/card.cpp index 12e577f..60aaa74 100644 --- a/src/main/game/card.cpp +++ b/src/main/game/card.cpp @@ -25,7 +25,8 @@ const card::colour_t card::colour::Red = 1; card::card(suit_t s, rank_t r, bool fd) : card_rank(r), card_suit(s), face_down(fd) {} card::card(const char* c, bool face_down_possible) : - card_rank(rank_from_str(c)), card_suit(suit_from_str(c)), face_down(face_down_possible) {} // TODO: CHANGE! + card_rank(rank_from_str(c)), card_suit(suit_from_str(c)), + face_down(face_down_from_str(c, face_down_possible)) {} card::card() : card(0, 0, false) {} @@ -52,6 +53,18 @@ card::rank_t card::rank_from_str(const char* c) { } } +bool card::face_down_from_str(const char *c, bool possible) { + if (!possible) return false; + + switch(c[strlen(c) - 1]) { + case 'c': return true; + case 'h': return true; + case 's': return true; + case 'd': return true; + default: return false; + } +} + card::suit_t card::get_suit() const { return card_suit; } @@ -88,14 +101,16 @@ std::string card::to_string() const { return s; } -bool card::is_face_up() const { - return true; +bool card::is_face_down() const { + return face_down; } void card::turn_face_up() { + face_down = false; } void card::turn_face_down() { + face_down = true; } const card card::divider = card(); diff --git a/src/main/game/card.h b/src/main/game/card.h index ea808b4..7885d66 100644 --- a/src/main/game/card.h +++ b/src/main/game/card.h @@ -39,7 +39,7 @@ public: colour_t get_colour() const; rank_t get_rank() const; - bool is_face_up() const; + bool is_face_down() const; void turn_face_up(); void turn_face_down(); @@ -48,6 +48,7 @@ public: private: static suit_t suit_from_str(const char* c); static rank_t rank_from_str(const char* c); + bool face_down_from_str(const char *c, bool possible); rank_t card_rank : 4; suit_t card_suit : 2; diff --git a/src/main/game/move.cpp b/src/main/game/move.cpp index ff8269c..9e45c66 100644 --- a/src/main/game/move.cpp +++ b/src/main/game/move.cpp @@ -4,16 +4,22 @@ #include "move.h" -move::move(mtype ty, pile::ref f, pile::ref t, pile::size_type i) : type(ty), from(f), to(t), count(i) { +move::move(mtype ty, pile::ref f, pile::ref t, pile::size_type i, bool reveal_move_) : + type(ty), from(f), to(t), count(i), reveal_move(reveal_move_) { #ifndef NDEBUG if (ty == mtype::regular) assert(i == 1); else assert(i >= 1); #endif } +void move::make_reveal_move() { + reveal_move = true; +} + bool operator==(const move& a, const move& b) { return (a.from == b.from ) && (a.to == b.to ) && (a.count == b.count) - && (a.type == b.type ); + && (a.type == b.type ) + && (a.reveal_move == b.reveal_move); } diff --git a/src/main/game/move.h b/src/main/game/move.h index 1e3a9d1..8651d1e 100644 --- a/src/main/game/move.h +++ b/src/main/game/move.h @@ -10,13 +10,16 @@ struct move { enum class mtype : uint8_t {regular, dominance, built_group, stock_to_waste, stock_to_tableau, redeal, null}; - explicit move(mtype, pile::ref = 255 , pile::ref = 255, pile::size_type = 1); + explicit move(mtype, pile::ref = 255 , pile::ref = 255, pile::size_type = 1, bool reveal_move_ = false); friend bool operator==(const move&, const move&); + void make_reveal_move(); + mtype type; pile::ref from; pile::ref to; pile::size_type count; + bool reveal_move; }; #endif //SOLVITAIRE_MOVE_H diff --git a/src/main/game/search-state/game_state.auto_foundation_moves.cpp b/src/main/game/search-state/game_state.auto_foundation_moves.cpp index 794c040..99b43a2 100644 --- a/src/main/game/search-state/game_state.auto_foundation_moves.cpp +++ b/src/main/game/search-state/game_state.auto_foundation_moves.cpp @@ -86,7 +86,13 @@ boost::optional game_state::get_dominance_move() const { + card::rank_t(1); if (target_rank == c.get_rank() && auto_foundation_moves[c.get_suit()]) { - return move(move::mtype::dominance, pr, target_foundation); + move m(move::mtype::dominance, pr, target_foundation); + + // If this dominance move reveals a card, adds this to the move + if (piles[pr].size() > 1 && piles[pr][1].is_face_down()) + m.make_reveal_move(); + + return m; } } #endif diff --git a/src/main/game/search-state/game_state.cpp b/src/main/game/search-state/game_state.cpp index 8a1298b..7870b1b 100644 --- a/src/main/game/search-state/game_state.cpp +++ b/src/main/game/search-state/game_state.cpp @@ -32,6 +32,7 @@ using std::runtime_error; 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; @@ -196,11 +197,11 @@ game_state::game_state(const sol_rules& s_rules, int seed, bool streamliners_) } game_state::game_state(const sol_rules& s_rules, - std::initializer_list il) - : game_state(s_rules, false) { - pile::ref pr = 0; - for (const pile& p : il) { - for (const card c : p.pile_vec) { + std::initializer_list> 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++; @@ -269,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) { @@ -293,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) { @@ -301,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 @@ -309,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 @@ -331,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) { @@ -338,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]); @@ -427,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 // diff --git a/src/main/game/search-state/game_state.h b/src/main/game/search-state/game_state.h index f335de1..b8b0d03 100644 --- a/src/main/game/search-state/game_state.h +++ b/src/main/game/search-state/game_state.h @@ -32,7 +32,7 @@ public: // Does the same from a seed game_state(const sol_rules&, int seed, bool); // Does the same but with an initialiser list (useful for testing) - game_state(const sol_rules&, std::initializer_list); + game_state(const sol_rules&, std::initializer_list>); /* Altering state */ @@ -80,6 +80,10 @@ private: void make_redeal_move(); void undo_redeal_move(); +#ifndef NDEBUG + void check_face_down_consistent() const; +#endif + /* Legal move generation */ bool stock_can_deal_tableau() const; @@ -107,6 +111,8 @@ private: bool is_next_legal_card(sol_rules::build_policy, card, card) const; + void turn_face_down_cards(std::vector&) const; + /* Auto-foundation moves */ bool is_valid_auto_foundation_move(pile::ref) const; diff --git a/src/main/game/search-state/game_state.legal_moves.cpp b/src/main/game/search-state/game_state.legal_moves.cpp index b7b910e..9ad8b1d 100644 --- a/src/main/game/search-state/game_state.legal_moves.cpp +++ b/src/main/game/search-state/game_state.legal_moves.cpp @@ -3,6 +3,7 @@ // #include "game_state.h" +#include "../sol_rules.h" #include @@ -14,6 +15,7 @@ using namespace rapidjson; typedef sol_rules::build_policy pol; typedef sol_rules::spaces_policy s_pol; typedef sol_rules::stock_deal_type sdt; +typedef sol_rules::face_up_policy fu; //////////////////////////////// @@ -136,9 +138,11 @@ vector game_state::get_legal_moves(move parent_move) { if (rules.move_built_group) add_built_group_moves(moves); - if (rules.foundations_comp_piles) // I.E. Spider-type winning condition + if (rules.foundations_comp_piles) // i.e. Spider-type winning condition add_foundation_complete_piles_moves(moves); + if (rules.face_up != fu::ALL) + turn_face_down_cards(moves); /* vector moves; @@ -185,6 +189,7 @@ vector game_state::get_legal_moves(move parent_move) { if (rules.foundations_comp_piles) // I.E. Spider-type winning condition add_foundation_complete_piles_moves(moves); */ + return moves; } @@ -352,8 +357,12 @@ void game_state::add_built_group_moves(vector &moves) const { if (rules.spaces_pol == s_pol::ANY || (rules.spaces_pol == s_pol::KINGS && bg_high.get_rank() == 13)) add_empty_built_group_moves(moves, rem_ref, add_ref, bg_high); } else { - if (is_next_built_group_card(piles[add_ref].top_card(), bg_high)) - moves.emplace_back(move::mtype::built_group, rem_ref, add_ref, built_group_height); + if (is_next_built_group_card(piles[add_ref].top_card(), bg_high)) { + bool is_reveal_move = + piles[rem_ref].size() > built_group_height + && piles[rem_ref][built_group_height].is_face_down(); + moves.emplace_back(move::mtype::built_group, rem_ref, add_ref, built_group_height, is_reveal_move); + } } } } @@ -362,7 +371,9 @@ void game_state::add_built_group_moves(vector &moves) const { // Finds the size of the built group at the top of a pile pile::size_type game_state::get_built_group_height(pile::ref ref) const { pile::size_type i = 1; - while (i < piles[ref].size() && is_next_built_group_card(piles[ref][i], piles[ref][i-1])) + while (i < piles[ref].size() + && is_next_built_group_card(piles[ref][i], piles[ref][i-1]) + && !piles[ref][i].is_face_down()) i++; return i; } @@ -374,6 +385,9 @@ void game_state::add_empty_built_group_moves(vector& moves, pile::ref rem_ do { moves.emplace_back(move::mtype::built_group, rem_ref, add_ref, static_cast(card_idx + 1)); } while (piles[rem_ref][card_idx++] != bg_high); + + if (card_idx < piles[rem_ref].size() && piles[rem_ref][card_idx].is_face_down()) + moves.back().make_reveal_move(); } bool game_state::is_next_built_group_card(card a, card b) const { @@ -394,3 +408,12 @@ bool game_state::is_next_legal_card(pol p, card a, card b) const { // Checks rank return b.get_rank() + 1 == a.get_rank(); } + +void game_state::turn_face_down_cards(vector& moves) const { + for (auto& m : moves) { + bool is_tableau_move = m.from >= original_tableau_piles.front() && m.from <= original_tableau_piles.back(); + if (is_tableau_move && piles[m.from].size() > 1 && piles[m.from][1].is_face_down()) { + m.make_reveal_move(); + } + } +} -- cgit v1.2.1