From 390a3cb4f15d84cde2312411b16203b70dbb2df5 Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Mon, 23 Jul 2018 10:31:37 +0100 Subject: Add foundation wrap logic and fix top-of-waste bug --- .../game_state.auto_foundation_moves.cpp | 30 ++++++++++++++-------- src/main/game/search-state/game_state.cpp | 3 +-- src/main/game/search-state/game_state.h | 4 ++- .../game/search-state/game_state.legal_moves.cpp | 11 ++++---- src/main/game/sol_rules.cpp | 1 + 5 files changed, 31 insertions(+), 18 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 b74948a..12a21f5 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 @@ -3,10 +3,12 @@ // #include "game_state.h" +#include "../sol_rules.h" typedef sol_rules::build_policy pol; typedef game_state::streamliner_options sos; +typedef sol_rules::stock_deal_type sdt; using std::max; @@ -21,11 +23,12 @@ bool game_state::is_valid_auto_foundation_move(pile::ref target_pile) const { return true; card::suit_t target_suit(target_pile - foundations.front()); - card::rank_t target_rank = piles[target_pile].empty() - ? card::rank_t(0) - : piles[target_pile].top_card().get_rank(); + card::rank_t move_up_rank = piles[target_pile].empty() + ? card::rank_t(1) + : foundation_base_convert(piles[target_pile].top_card().get_rank() + card::rank_t(1)); + // The card to be moved up - card move_up_card(target_suit, card::rank_t(target_rank + 1)); + card move_up_card(target_suit, move_up_rank); // The max difference between the 'move up' card and the foundations of the // same colour and the other colour @@ -36,7 +39,7 @@ bool game_state::is_valid_auto_foundation_move(pile::ref target_pile) const { card::suit_t s(pr - foundations.front()); card::rank_t r = piles[pr].empty() ? card::rank_t(0) - : piles[pr].top_card().get_rank(); + : foundation_base_convert(piles[pr].top_card().get_rank()); card c(s, r); auto rank_diff = int(move_up_card.get_rank() - r); @@ -71,10 +74,11 @@ boost::optional game_state::get_dominance_move() const { // Cycles through the piles and sees if any cards can be automatically moved // to the foundations for (pile::ref pr = 0; pr < piles.size(); pr++) { - // Don't move foundation cards, hole or stock cards to the foundations + // Don't move foundation cards, hole, waste or stock cards to the foundations if ((pr >= foundations.front() && pr <= foundations.back()) || (rules.hole && pr == hole) || (rules.stock_size > 0 && pr == stock) + || (rules.stock_size > 0 && rules.stock_deal_t == sdt::WASTE && pr == waste) || (piles[pr].empty())) { continue; } @@ -85,10 +89,9 @@ boost::optional game_state::get_dominance_move() const { // If the card is the right rank and the auto-move boolean is true, then // returns the move card::rank_t target_rank = piles[target_foundation].empty() - ? card::rank_t(1) : - piles[target_foundation].top_card().get_rank() - + card::rank_t(1); - if (target_rank == c.get_rank() && + ? card::rank_t(1) + : foundation_base_convert(piles[target_foundation].top_card().get_rank() + card::rank_t(1)); + if (target_rank == foundation_base_convert(c.get_rank()) && auto_foundation_moves[c.get_suit()]) { move m(move::mtype::dominance, pr, target_foundation); @@ -144,3 +147,10 @@ void game_state::update_auto_foundation_moves(pile::ref target_pile) { auto_foundation_moves[pr] = is_valid_auto_foundation_move(pr); } } + +card::rank_t game_state::foundation_base_convert(card::rank_t r) const { + //card::rank_t s = r - (foundations_base - card::rank_t(1)); + //if (s < 1) s += rules.max_rank; + //return s; + return r; +} diff --git a/src/main/game/search-state/game_state.cpp b/src/main/game/search-state/game_state.cpp index 81fdd96..dbb3ea1 100644 --- a/src/main/game/search-state/game_state.cpp +++ b/src/main/game/search-state/game_state.cpp @@ -4,7 +4,6 @@ #include #include -#include #include #include @@ -631,7 +630,7 @@ bool game_state::is_solved() const { } else if (rules.sequence_count > 0) { for (pile::ref i = 0; i < sequences.size() && solved; i++) { for (pile::ref j = piles[sequences[i]].size(); j-- > 2;) { - if (!is_next_legal_card(rules.sequence_build_pol, piles[sequences[i]][j-1], piles[sequences[i]][j], false)) { // TODO + if (!is_next_legal_card(rules.sequence_build_pol, piles[sequences[i]][j-1], piles[sequences[i]][j], false)) { solved = false; break; } diff --git a/src/main/game/search-state/game_state.h b/src/main/game/search-state/game_state.h index 8b24346..f6fc666 100644 --- a/src/main/game/search-state/game_state.h +++ b/src/main/game/search-state/game_state.h @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -62,7 +63,7 @@ private: /* Constructors (& helper function) */ explicit game_state(const sol_rules&, streamliner_options); - static std::vector gen_shuffled_deck(card::rank_t, bool, mt19937); + static std::vector gen_shuffled_deck(card::rank_t, bool, std::mt19937); template static void shuffle(RandomIt, RandomIt, URBG&&); /* Pile order logic */ @@ -131,6 +132,7 @@ private: bool is_ordered_pile(pile::ref) const; bool dominance_blocks_foundation_move(pile::ref); void update_auto_foundation_moves(pile::ref); + card::rank_t foundation_base_convert(card::rank_t) const; /* Game rules */ 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 868087e..93dc669 100644 --- a/src/main/game/search-state/game_state.legal_moves.cpp +++ b/src/main/game/search-state/game_state.legal_moves.cpp @@ -65,7 +65,7 @@ vector game_state::get_legal_moves(move parent_move) { if (!piles[r].empty()) moves.emplace_back(move::mtype::regular, r, empty_cell); - if (rules.stock_size > 0 && rules.stock_deal_t == sdt::WASTE && !piles[stock].empty()) + if (rules.stock_size > 0 && rules.stock_deal_t == sdt::WASTE) add_stock_to_cell_move(moves, empty_cell); } @@ -95,7 +95,7 @@ vector game_state::get_legal_moves(move parent_move) { } // Stock to tableau moves - if (rules.stock_size > 0 && rules.stock_deal_t == sdt::WASTE && !piles[stock].empty()) + if (rules.stock_size > 0 && rules.stock_deal_t == sdt::WASTE) add_stock_to_tableau_moves(moves); switch (rules.move_built_group) { @@ -139,7 +139,7 @@ vector game_state::get_legal_moves(move parent_move) { // Tableau / cells / reserve / stock-waste to hole / foundation moves if (rules.hole || (rules.foundations_present && !rules.foundations_only_comp_piles)) { // Stock - if (rules.stock_size > 0 && rules.stock_deal_t == sdt::WASTE && !piles[stock].empty()) + if (rules.stock_size > 0 && rules.stock_deal_t == sdt::WASTE) add_stock_to_hole_foundation_moves(moves); list from_piles = tableau_piles; @@ -196,7 +196,8 @@ set> game_state::generate_k_plus_moves_to_check() const { set> stock_moves_to_check; // If the waste isn't empty, adds the move from the top of the current waste - if (piles[waste].size() != 0) stock_moves_to_check.insert(pair(0, false)); + if (!piles[waste].empty()) stock_moves_to_check.insert(pair(0, false)); + else if (piles[stock].empty()) return stock_moves_to_check; // Adds multiples of deal count (excluding last card) for (int8_t count = rules.stock_deal_count; @@ -398,7 +399,7 @@ void game_state::add_whole_pile_moves(vector& moves, pile::ref rem_ref, pi card bg_high = piles[rem_ref][built_group_height - 1]; if (piles[add_ref].empty()) { - if (rules.spaces_pol == s_pol::ANY || rules.spaces_pol == s_pol::KINGS && bg_high.get_rank() == 13) { + if (rules.spaces_pol == s_pol::ANY || (rules.spaces_pol == s_pol::KINGS && bg_high.get_rank() == 13)) { moves.emplace_back(move::mtype::built_group, rem_ref, add_ref, built_group_height); } } else if (is_next_built_group_card(piles[add_ref].top_card(), bg_high)) { diff --git a/src/main/game/sol_rules.cpp b/src/main/game/sol_rules.cpp index 55c6757..2778241 100644 --- a/src/main/game/sol_rules.cpp +++ b/src/main/game/sol_rules.cpp @@ -20,6 +20,7 @@ sol_rules::sol_rules() : spaces_pol(s_pol::ANY), move_built_group(bgt::NO), built_group_pol(pol::ANY_SUIT), + tableau_wraps(false), two_decks(false), max_rank(13), hole(false), -- cgit v1.2.1