diff options
| author | Charlie Blake | 2018-06-26 10:37:54 +0100 |
|---|---|---|
| committer | Charlie Blake | 2018-06-26 10:37:54 +0100 |
| commit | 797400ea32ac1f6588e834fc1bcb42aa67fa6863 (patch) | |
| tree | 16352c5245f2f94dadf31c3e510eedfc9254fc3d /src/main/game/search-state | |
| parent | Add test to make sure cache encodes face up (diff) | |
| download | metasol-797400ea32ac1f6588e834fc1bcb42aa67fa6863.tar.gz metasol-797400ea32ac1f6588e834fc1bcb42aa67fa6863.zip | |
Finished klondike correct impl
Diffstat (limited to 'src/main/game/search-state')
| -rw-r--r-- | src/main/game/search-state/game_state.auto_foundation_moves.cpp | 8 | ||||
| -rw-r--r-- | src/main/game/search-state/game_state.cpp | 60 | ||||
| -rw-r--r-- | src/main/game/search-state/game_state.h | 8 | ||||
| -rw-r--r-- | src/main/game/search-state/game_state.legal_moves.cpp | 31 |
4 files changed, 96 insertions, 11 deletions
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<move> 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<pile> 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<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++; @@ -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<pile>); + game_state(const sol_rules&, std::initializer_list<std::initializer_list<std::string>>); /* 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<move>&) 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 <boost/optional/optional.hpp> @@ -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<move> 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<move> moves; @@ -185,6 +189,7 @@ vector<move> 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<move> &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<move> &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<move>& moves, pile::ref rem_ do { moves.emplace_back(move::mtype::built_group, rem_ref, add_ref, static_cast<pile::size_type>(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<move>& 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(); + } + } +} |
