From 28c5a79425fa12f9466e27385404f429f76f7524 Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Thu, 19 Jul 2018 11:06:38 +0100 Subject: Update rules so new schema works w/ existing games --- src/main/game/search-state/game_state.cpp | 3 ++- src/main/game/search-state/game_state.legal_moves.cpp | 3 ++- src/main/game/sol_rules.cpp | 6 ++++-- src/main/game/sol_rules.h | 14 ++++++++++++-- 4 files changed, 20 insertions(+), 6 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 0388d9c..10c8e20 100644 --- a/src/main/game/search-state/game_state.cpp +++ b/src/main/game/search-state/game_state.cpp @@ -38,6 +38,7 @@ typedef sol_rules::build_policy pol; typedef sol_rules::stock_deal_type sdt; typedef sol_rules::face_up_policy fu; typedef game_state::streamliner_options sos; +typedef sol_rules::foundations_init_type fit; ////////////////// // CONSTRUCTORS // @@ -139,7 +140,7 @@ game_state::game_state(const sol_rules& s_rules, int seed, streamliner_options s } // If the foundations begin filled, then fills them - if (rules.foundations_init_card) { + if (rules.foundations_init_cards == fit::ALL) { for (uint8_t f_idx = 0; f_idx < 4*(rules.two_decks ? 2:1); f_idx++) { card c = card(f_idx % uint8_t(4), 1); 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 e1cbc8e..99a37d6 100644 --- a/src/main/game/search-state/game_state.legal_moves.cpp +++ b/src/main/game/search-state/game_state.legal_moves.cpp @@ -22,6 +22,7 @@ typedef sol_rules::accordion_policy acc_pol; typedef sol_rules::stock_deal_type sdt; typedef sol_rules::face_up_policy fu; typedef sol_rules::direction dir; +typedef sol_rules::built_group_type bgt; //////////////////////////////// // MAIN LEGAL MOVE GEN CYCLE /// @@ -97,7 +98,7 @@ vector game_state::get_legal_moves(move parent_move) { if (rules.stock_size > 0 && rules.stock_deal_t == sdt::WASTE && !piles[stock].empty()) add_stock_to_tableau_moves(moves); - if (rules.move_built_group) + if (rules.move_built_group != bgt::NO) add_built_group_moves(moves); // Tableau to tableau moves diff --git a/src/main/game/sol_rules.cpp b/src/main/game/sol_rules.cpp index 0ea998e..d19d0b8 100644 --- a/src/main/game/sol_rules.cpp +++ b/src/main/game/sol_rules.cpp @@ -9,6 +9,8 @@ typedef sol_rules::spaces_policy s_pol; typedef sol_rules::stock_deal_type sdt; typedef sol_rules::direction dir; typedef sol_rules::face_up_policy fu; +typedef sol_rules::built_group_type bgt; +typedef sol_rules::foundations_init_type fit; // Note that the 'default' sol rules are set up by parsing the default json // rules description. This initialisation is really just for testing purposes. @@ -16,13 +18,13 @@ sol_rules::sol_rules() : tableau_pile_count(0), build_pol(pol::ANY_SUIT), spaces_pol(s_pol::ANY), - move_built_group(false), + move_built_group(bgt::NO), built_group_pol(pol::ANY_SUIT), two_decks(false), max_rank(13), hole(false), foundations_present(false), - foundations_init_card(false), + foundations_init_cards(fit::NONE), foundations_only_comp_piles(false), diagonal_deal(false), cells(0), diff --git a/src/main/game/sol_rules.h b/src/main/game/sol_rules.h index 0673771..25dcbe4 100644 --- a/src/main/game/sol_rules.h +++ b/src/main/game/sol_rules.h @@ -44,17 +44,27 @@ struct sol_rules { RIGHT, BOTH }; + enum class built_group_type { + YES, + NO, + WHOLE_PILE + }; + enum class foundations_init_type { + NONE, + ONE, + ALL + }; uint8_t tableau_pile_count; build_policy build_pol; spaces_policy spaces_pol; - bool move_built_group; + built_group_type move_built_group; build_policy built_group_pol; bool two_decks; uint8_t max_rank; bool hole; bool foundations_present; - bool foundations_init_card; + foundations_init_type foundations_init_cards; bool foundations_removable; bool foundations_only_comp_piles; bool diagonal_deal; -- cgit v1.2.1 From 2775a9ba0ef4e698acd0c0d32179d6a1e404297a Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Thu, 19 Jul 2018 16:16:39 +0100 Subject: Add parsing of new rules --- src/main/game/search-state/game_state.cpp | 1 + src/main/game/search-state/game_state.h | 1 + src/main/game/sol_rules.cpp | 1 + src/main/game/sol_rules.h | 7 ++++++- 4 files changed, 9 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 10c8e20..26910a1 100644 --- a/src/main/game/search-state/game_state.cpp +++ b/src/main/game/search-state/game_state.cpp @@ -49,6 +49,7 @@ typedef sol_rules::foundations_init_type fit; game_state::game_state(const sol_rules& s_rules, streamliner_options stream_opts_) : rules(s_rules) , stream_opts(stream_opts_) + , foundation_base(card::rank_t(0)) , stock(255) , waste(255) , hole (255) { diff --git a/src/main/game/search-state/game_state.h b/src/main/game/search-state/game_state.h index cbd72b4..b470a2c 100644 --- a/src/main/game/search-state/game_state.h +++ b/src/main/game/search-state/game_state.h @@ -134,6 +134,7 @@ private: const sol_rules rules; streamliner_options stream_opts; + card::rank_t foundation_base; /* Pile references */ diff --git a/src/main/game/sol_rules.cpp b/src/main/game/sol_rules.cpp index d19d0b8..55c6757 100644 --- a/src/main/game/sol_rules.cpp +++ b/src/main/game/sol_rules.cpp @@ -25,6 +25,7 @@ sol_rules::sol_rules() : hole(false), foundations_present(false), foundations_init_cards(fit::NONE), + foundations_base(1), foundations_only_comp_piles(false), diagonal_deal(false), cells(0), diff --git a/src/main/game/sol_rules.h b/src/main/game/sol_rules.h index 25dcbe4..2384b21 100644 --- a/src/main/game/sol_rules.h +++ b/src/main/game/sol_rules.h @@ -7,7 +7,9 @@ #include #include +#include +#include "card.h" #include "../../../lib/rapidjson/document.h" struct sol_rules { @@ -23,7 +25,8 @@ struct sol_rules { enum class spaces_policy { NO_BUILD, KINGS, - ANY + ANY, + AUTO_RESERVE_THEN_WASTE }; enum class accordion_policy { SAME_RANK, @@ -60,11 +63,13 @@ struct sol_rules { spaces_policy spaces_pol; built_group_type move_built_group; build_policy built_group_pol; + bool tableau_wraps; bool two_decks; uint8_t max_rank; bool hole; bool foundations_present; foundations_init_type foundations_init_cards; + boost::optional foundations_base; bool foundations_removable; bool foundations_only_comp_piles; bool diagonal_deal; -- cgit v1.2.1 From 5333895e0519c4425136bb299fedb5c19c5d4c0a Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Thu, 19 Jul 2018 16:58:31 +0100 Subject: Add random gen for Canfield --- src/main/game/search-state/game_state.cpp | 38 ++++++++++++++++++++++--------- src/main/game/search-state/game_state.h | 4 ++-- 2 files changed, 29 insertions(+), 13 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 26910a1..d032632 100644 --- a/src/main/game/search-state/game_state.cpp +++ b/src/main/game/search-state/game_state.cpp @@ -49,7 +49,7 @@ typedef sol_rules::foundations_init_type fit; game_state::game_state(const sol_rules& s_rules, streamliner_options stream_opts_) : rules(s_rules) , stream_opts(stream_opts_) - , foundation_base(card::rank_t(0)) + , foundations_base(card::rank_t(0)) , stock(255) , waste(255) , hole (255) { @@ -132,7 +132,8 @@ game_state::game_state(const sol_rules& s_rules, const Document& doc, streamline // Constructs an initial game state from a seed game_state::game_state(const sol_rules& s_rules, int seed, streamliner_options s_opts) : game_state(s_rules, s_opts) { - vector deck = gen_shuffled_deck(seed, rules.max_rank, rules.two_decks); + auto rng = mt19937(seed); + vector deck = gen_shuffled_deck(rules.max_rank, rules.two_decks, rng); // If there is a hole, moves the ace of spades to it if (rules.hole) { @@ -140,14 +141,30 @@ game_state::game_state(const sol_rules& s_rules, int seed, streamliner_options s place_card(hole, card("AS")); } + // If the foundation base is random, selects a random rank and suit + card::suit_t rand_suit = 0; + if (!rules.foundations_base) { + boost::random::uniform_int_distribution distr_rank(1, rules.max_rank); + foundations_base = distr_rank(rng); + boost::random::uniform_int_distribution distr_suit(0, 3); + rand_suit = distr_suit(rng); + } + // If the foundations begin filled, then fills them - if (rules.foundations_init_cards == fit::ALL) { - for (uint8_t f_idx = 0; f_idx < 4*(rules.two_decks ? 2:1); f_idx++) { - card c = card(f_idx % uint8_t(4), 1); + uint8_t founds_to_fill = 0; + switch (rules.foundations_init_cards) { + case sol_rules::foundations_init_type::NONE: + break; + case sol_rules::foundations_init_type::ONE: + founds_to_fill = 1; break; + case sol_rules::foundations_init_type::ALL: + founds_to_fill = static_cast(4 * (rules.two_decks ? 2 : 1)); break; + } + for (uint8_t f_idx = 0; f_idx < founds_to_fill; f_idx++) { + card c = card((f_idx + rand_suit) % uint8_t(4), foundations_base); - deck.erase(find(begin(deck), end(deck), c)); - place_card(foundations[f_idx], c); - } + deck.erase(find(begin(deck), end(deck), c)); + place_card(foundations[(f_idx + rand_suit)], c); } // If there is a stock, deals to it and set up a waste pile too @@ -250,8 +267,8 @@ 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 game_state::gen_shuffled_deck(card::rank_t max_rank, + bool two_decks, mt19937 rng) { vector deck; for (int deck_count = 1; deck_count <= (two_decks ? 2 : 1); deck_count++) { @@ -264,7 +281,6 @@ 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); game_state::shuffle(begin(deck), end(deck), rng); return deck; } diff --git a/src/main/game/search-state/game_state.h b/src/main/game/search-state/game_state.h index b470a2c..963f524 100644 --- a/src/main/game/search-state/game_state.h +++ b/src/main/game/search-state/game_state.h @@ -62,7 +62,7 @@ private: /* Constructors (& helper function) */ explicit game_state(const sol_rules&, streamliner_options); - static std::vector gen_shuffled_deck(int, card::rank_t, bool); + static std::vector gen_shuffled_deck(card::rank_t, bool, mt19937); template static void shuffle(RandomIt, RandomIt, URBG&&); /* Pile order logic */ @@ -134,7 +134,7 @@ private: const sol_rules rules; streamliner_options stream_opts; - card::rank_t foundation_base; + card::rank_t foundations_base; /* Pile references */ -- cgit v1.2.1 From 0261d129cfc015ca02b9d3db0d421cf1e1afbf77 Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Thu, 19 Jul 2018 17:28:49 +0100 Subject: Add whole pile legal moves gen --- src/main/game/search-state/game_state.h | 2 ++ .../game/search-state/game_state.legal_moves.cpp | 42 ++++++++++++++++++++-- 2 files changed, 42 insertions(+), 2 deletions(-) (limited to 'src/main/game') diff --git a/src/main/game/search-state/game_state.h b/src/main/game/search-state/game_state.h index 963f524..2b89d86 100644 --- a/src/main/game/search-state/game_state.h +++ b/src/main/game/search-state/game_state.h @@ -112,6 +112,8 @@ private: void add_built_group_moves(std::vector&) const; void add_built_group_moves(std::vector&, pile::ref, pile::size_type) const; + void add_whole_pile_moves(std::vector&) const; + void add_whole_pile_moves(std::vector&, pile::ref, pile::size_type) const; pile::size_type get_built_group_height(pile::ref) const; bool is_next_built_group_card(card, card) const; void add_empty_built_group_moves(std::vector&, pile::ref, pile::ref, pile::size_type, bool) 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 99a37d6..7c3ee88 100644 --- a/src/main/game/search-state/game_state.legal_moves.cpp +++ b/src/main/game/search-state/game_state.legal_moves.cpp @@ -98,8 +98,16 @@ vector game_state::get_legal_moves(move parent_move) { if (rules.stock_size > 0 && rules.stock_deal_t == sdt::WASTE && !piles[stock].empty()) add_stock_to_tableau_moves(moves); - if (rules.move_built_group != bgt::NO) - add_built_group_moves(moves); + switch (rules.move_built_group) { + case sol_rules::built_group_type::YES: + add_built_group_moves(moves); + break; + case sol_rules::built_group_type::WHOLE_PILE: + add_whole_pile_moves(moves); + break; + case sol_rules::built_group_type::NO: + break; + } // Tableau to tableau moves for (auto t_from : tableau_piles) { @@ -369,6 +377,36 @@ void game_state::add_built_group_moves(vector& moves, pile::ref rem_ref, p } } +void game_state::add_whole_pile_moves(vector& moves) const { + assert(rules.built_group_pol != pol::NO_BUILD); + + // Cycles through each pile to see if it contains a whole-pile built group + for (auto rem_ref : tableau_piles) { + auto built_group_height = get_built_group_height(rem_ref); + + if (built_group_height == piles[rem_ref].size()) { + add_whole_pile_moves(moves, rem_ref, built_group_height); + } + } +} + +void game_state::add_whole_pile_moves(vector& moves, pile::ref rem_ref, pile::size_type built_group_height) const { + // We have found a built group. Cycles through each pile to see if it can be added + for (auto add_ref : tableau_piles) { + if (add_ref == rem_ref) continue; + + 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) { + 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)) { + moves.emplace_back(move::mtype::built_group, rem_ref, add_ref, built_group_height); + } + } +} + // 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; -- cgit v1.2.1 From 40843e8fc66cb0bdeeee1b6df43673c8f245e677 Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Thu, 19 Jul 2018 17:54:45 +0100 Subject: Add tableau wraps logic --- src/main/game/search-state/game_state.cpp | 2 +- src/main/game/search-state/game_state.h | 2 +- src/main/game/search-state/game_state.legal_moves.cpp | 18 +++++++++++------- 3 files changed, 13 insertions(+), 9 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 d032632..81fdd96 100644 --- a/src/main/game/search-state/game_state.cpp +++ b/src/main/game/search-state/game_state.cpp @@ -631,7 +631,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])) { + if (!is_next_legal_card(rules.sequence_build_pol, piles[sequences[i]][j-1], piles[sequences[i]][j], false)) { // TODO solved = false; break; } diff --git a/src/main/game/search-state/game_state.h b/src/main/game/search-state/game_state.h index 2b89d86..8b24346 100644 --- a/src/main/game/search-state/game_state.h +++ b/src/main/game/search-state/game_state.h @@ -121,7 +121,7 @@ private: void add_non_empty_built_group_move(std::vector&, pile::ref, pile::ref, pile::size_type, bool) const; void add_sequence_moves(std::vector&) const; - bool is_next_legal_card(sol_rules::build_policy, card, card) const; + bool is_next_legal_card(sol_rules::build_policy, card, card, bool) const; bool is_next_legal_card(std::vector, card, card) const; void turn_face_down_cards(std::vector&) 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 7c3ee88..868087e 100644 --- a/src/main/game/search-state/game_state.legal_moves.cpp +++ b/src/main/game/search-state/game_state.legal_moves.cpp @@ -69,7 +69,7 @@ vector game_state::get_legal_moves(move parent_move) { add_stock_to_cell_move(moves, empty_cell); } - // Foundation to tableau moves + // Foundation to tableau / empty cell moves if (rules.foundations_removable) { for (auto f : foundations) { if (piles[f].empty() || parent_move.to == f || dominance_blocks_foundation_move(f)) continue; @@ -297,7 +297,7 @@ bool game_state::is_valid_tableau_move(const card rem_c, } bool game_state::is_next_tableau_card(card a, card b) const { - return is_next_legal_card(rules.build_pol, a, b); + return is_next_legal_card(rules.build_pol, a, b, rules.tableau_wraps); } bool game_state::is_valid_foundations_move(const pile::ref rem_ref, @@ -418,7 +418,7 @@ pile::size_type game_state::get_built_group_height(pile::ref ref) const { } bool game_state::is_next_built_group_card(card a, card b) const { - return is_next_legal_card(rules.built_group_pol, a, b); + return is_next_legal_card(rules.built_group_pol, a, b, rules.tableau_wraps); } // Loops through each possible built group move to an empty pile and adds it to the list @@ -487,7 +487,7 @@ void game_state::add_sequence_moves(std::vector& moves) const { // Otherwise must agree with left neighbour else { card neighbour_card = piles[sequences[space_loc.first]][space_loc.second + 1]; - if (neighbour_card != "AS" && is_next_legal_card(rules.sequence_build_pol, from_card, neighbour_card)) + if (neighbour_card != "AS" && is_next_legal_card(rules.sequence_build_pol, from_card, neighbour_card, false)) moves.emplace_back(move::mtype::sequence, from_idx, space_idx); } } @@ -501,7 +501,7 @@ void game_state::add_sequence_moves(std::vector& moves) const { // Otherwise must agree with right neighbour else { card neighbour_card = piles[sequences[space_loc.first]][space_loc.second - 1]; - if (neighbour_card != "AS" && is_next_legal_card(rules.sequence_build_pol, neighbour_card, from_card)) + if (neighbour_card != "AS" && is_next_legal_card(rules.sequence_build_pol, neighbour_card, from_card, false)) moves.emplace_back(move::mtype::sequence, from_idx, space_idx); } } @@ -533,7 +533,7 @@ void game_state::add_accordion_moves(vector& moves) const { /////////////////////// -bool game_state::is_next_legal_card(pol p, card a, card b) const { +bool game_state::is_next_legal_card(pol p, card a, card b, bool wraps) const { // Checks build pol violations switch(p) { case sol_rules::build_policy::SAME_SUIT: @@ -545,7 +545,11 @@ bool game_state::is_next_legal_card(pol p, card a, card b) const { default:; } // Checks rank - return b.get_rank() + 1 == a.get_rank(); + if (wraps) { + return (b.get_rank() % rules.max_rank) + 1 == a.get_rank(); + } else { + return b.get_rank() + 1 == a.get_rank(); + } } bool game_state::is_next_legal_card(vector vp, card a, card b) const { -- cgit v1.2.1 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 From 357fab143bcb41d55fb9060bcfbf257a8586e03c Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Mon, 23 Jul 2018 12:54:47 +0100 Subject: Small bug fixes inc found. base card & whole piles --- .../search-state/game_state.auto_foundation_moves.cpp | 7 +++---- src/main/game/search-state/game_state.cpp | 2 +- src/main/game/search-state/game_state.legal_moves.cpp | 19 +++++++++++-------- 3 files changed, 15 insertions(+), 13 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 12a21f5..b2b55b3 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 @@ -149,8 +149,7 @@ void game_state::update_auto_foundation_moves(pile::ref target_pile) { } 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; + card::rank_t s = r - (foundations_base - card::rank_t(1)); + if (s < 1) s += rules.max_rank; + return s; } diff --git a/src/main/game/search-state/game_state.cpp b/src/main/game/search-state/game_state.cpp index dbb3ea1..78247bf 100644 --- a/src/main/game/search-state/game_state.cpp +++ b/src/main/game/search-state/game_state.cpp @@ -48,7 +48,7 @@ typedef sol_rules::foundations_init_type fit; game_state::game_state(const sol_rules& s_rules, streamliner_options stream_opts_) : rules(s_rules) , stream_opts(stream_opts_) - , foundations_base(card::rank_t(0)) + , foundations_base(card::rank_t(1)) , stock(255) , waste(255) , hole (255) { 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 93dc669..09a32fa 100644 --- a/src/main/game/search-state/game_state.legal_moves.cpp +++ b/src/main/game/search-state/game_state.legal_moves.cpp @@ -110,12 +110,15 @@ vector game_state::get_legal_moves(move parent_move) { } // 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); + // If only whole pile moves are available, doesn't make regular ones + if (rules.move_built_group != bgt::WHOLE_PILE) { + 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); + } } } } @@ -317,9 +320,9 @@ bool game_state::is_valid_foundations_move(const card rem_c, // Checks rank if (piles[add_ref].empty()) - return rem_c.get_rank() == 1; + return rem_c.get_rank() == rules.foundations_base; else - return rem_c.get_rank() == piles[add_ref].top_card().get_rank() + 1; + return rem_c.get_rank() == (piles[add_ref].top_card().get_rank() + 1) % rules.max_rank; } bool game_state::is_valid_hole_move(const pile::ref rem_ref) const { -- cgit v1.2.1 From 2acab3e51cabd8ba80738b1faf791980d2abf724 Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Tue, 24 Jul 2018 12:00:04 +0100 Subject: Fix canfield to pass all unit tests --- .../game_state.auto_foundation_moves.cpp | 155 ------------------- src/main/game/search-state/game_state.cpp | 10 ++ .../search-state/game_state.dominance_moves.cpp | 172 +++++++++++++++++++++ src/main/game/search-state/game_state.h | 3 + .../game/search-state/game_state.legal_moves.cpp | 58 ++++--- 5 files changed, 220 insertions(+), 178 deletions(-) delete mode 100644 src/main/game/search-state/game_state.auto_foundation_moves.cpp create mode 100644 src/main/game/search-state/game_state.dominance_moves.cpp (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 deleted file mode 100644 index b2b55b3..0000000 --- a/src/main/game/search-state/game_state.auto_foundation_moves.cpp +++ /dev/null @@ -1,155 +0,0 @@ -// -// Created by thecharlesblake on 07/06/18. -// - -#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; - - -bool game_state::is_valid_auto_foundation_move(pile::ref target_pile) const { - if (rules.foundations_only_comp_piles) - return false; - else if ( stream_opts == sos::AUTO_FOUNDATIONS - || stream_opts == sos::BOTH - || rules.build_pol == pol::NO_BUILD - || rules.build_pol == pol::SAME_SUIT) - return true; - - card::suit_t target_suit(target_pile - foundations.front()); - 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, move_up_rank); - - // The max difference between the 'move up' card and the foundations of the - // same colour and the other colour - int same_rank_diff = 0; - int other_rank_diff = 0; - for (pile::ref pr : foundations) { - if (pr == target_pile) continue; - - card::suit_t s(pr - foundations.front()); - card::rank_t r = piles[pr].empty() ? card::rank_t(0) - : foundation_base_convert(piles[pr].top_card().get_rank()); - card c(s, r); - - auto rank_diff = int(move_up_card.get_rank() - r); - if (move_up_card.get_suit() == c.get_suit() - || (rules.build_pol == pol::RED_BLACK - && move_up_card.get_colour() == c.get_colour())) { - same_rank_diff = rank_diff; - } else { - other_rank_diff = max(other_rank_diff, rank_diff); - } - } - - bool other_within_1 = other_rank_diff <= 1; - bool other_within_2 = other_rank_diff <= 2; - bool same_within_3 = same_rank_diff <= 3; - - if (rules.build_pol == pol::RED_BLACK) { - return other_within_1 || (other_within_2 && same_within_3); - } else { - assert(rules.build_pol == pol::ANY_SUIT); - return other_within_2; - } -} - -// Returns a dominance move if one is available -boost::optional game_state::get_dominance_move() const { -#ifndef NO_AUTO_FOUNDATIONS - // If there are 2 decks or no foundations, return - if (!rules.foundations_present || rules.two_decks) - return boost::none; - - // 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, 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; - } - - card c = piles[pr].top_card(); - pile::ref target_foundation = foundations[c.get_suit()]; - - // 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) - : 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); - - // 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 - - return boost::none; -} - -bool game_state::is_ordered_pile(pile::ref pr) const { - if (piles[pr].size() != rules.max_rank) return false; - - card::suit_t s = piles[pr][0].get_suit(); - for (card::rank_t r = 0; r < rules.max_rank; r++) { - if (piles[pr][r] != card(s, card::rank_t(r + 1))) return false; - } - return true; -} - -// For games where the foundations can be removed from, this dominance blocks -// the foundations being removed from if the card that were to be removed would -// go 'automatically' up -bool game_state::dominance_blocks_foundation_move(pile::ref target_pile) { - assert(!piles[target_pile].empty()); - - card target_card = piles[target_pile].top_card(); - piles[target_pile].take(); - - bool blocked = is_valid_auto_foundation_move(target_pile); - piles[target_pile].place(target_card); - - return blocked; -} - -// Decides which 'auto foundation' booleans should be 'true' currently -void game_state::update_auto_foundation_moves(pile::ref target_pile) { - // If there are no foundations, or this is not a foundation move, do nothing - if (!rules.foundations_present - || target_pile < foundations.front() - || target_pile > foundations.back()) { - return; - } - - // Turns the 'auto' boolean for the target foundation false - - for (pile::ref pr : foundations) { - 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; -} diff --git a/src/main/game/search-state/game_state.cpp b/src/main/game/search-state/game_state.cpp index 78247bf..17fdc29 100644 --- a/src/main/game/search-state/game_state.cpp +++ b/src/main/game/search-state/game_state.cpp @@ -263,6 +263,16 @@ game_state::game_state(const sol_rules& s_rules, } pr++; } + + // Sets foundations base appropriately + if (rules.foundations_present && rules.foundations_base == boost::none) { + for (auto f : foundations) { + if (!piles[f].empty()) { + foundations_base = piles[f].top_card().get_rank(); + return; + } + } + } } // Generates a randomly ordered vector of cards diff --git a/src/main/game/search-state/game_state.dominance_moves.cpp b/src/main/game/search-state/game_state.dominance_moves.cpp new file mode 100644 index 0000000..b7e6e15 --- /dev/null +++ b/src/main/game/search-state/game_state.dominance_moves.cpp @@ -0,0 +1,172 @@ +// +// Created by thecharlesblake on 07/06/18. +// + +#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; +typedef sol_rules::spaces_policy s_pol; + +using std::max; +using boost::optional; + +bool game_state::is_valid_auto_foundation_move(pile::ref target_pile) const { + if (rules.foundations_only_comp_piles) + return false; + else if ( stream_opts == sos::AUTO_FOUNDATIONS + || stream_opts == sos::BOTH + || rules.build_pol == pol::NO_BUILD + || rules.build_pol == pol::SAME_SUIT) + return true; + + card::suit_t target_suit(target_pile - foundations.front()); + 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, move_up_rank); + + // The max difference between the 'move up' card and the foundations of the + // same colour and the other colour + int same_rank_diff = 0; + int other_rank_diff = 0; + for (pile::ref pr : foundations) { + if (pr == target_pile) continue; + + card::suit_t s(pr - foundations.front()); + card::rank_t r = piles[pr].empty() ? card::rank_t(0) + : foundation_base_convert(piles[pr].top_card().get_rank()); + card c(s, r); + + auto rank_diff = int(move_up_card.get_rank() - r); + if (move_up_card.get_suit() == c.get_suit() + || (rules.build_pol == pol::RED_BLACK + && move_up_card.get_colour() == c.get_colour())) { + same_rank_diff = rank_diff; + } else { + other_rank_diff = max(other_rank_diff, rank_diff); + } + } + + bool other_within_1 = other_rank_diff <= 1; + bool other_within_2 = other_rank_diff <= 2; + bool same_within_3 = same_rank_diff <= 3; + + if (rules.build_pol == pol::RED_BLACK) { + return other_within_1 || (other_within_2 && same_within_3); + } else { + assert(rules.build_pol == pol::ANY_SUIT); + return other_within_2; + } +} + +// Returns a dominance move if one is available +optional game_state::get_dominance_move() const { +#ifndef NO_AUTO_FOUNDATIONS + if (rules.spaces_pol == s_pol::AUTO_RESERVE_THEN_WASTE) { + optional arm = auto_reserve_move(); + if (arm) return arm; + } + + // If there are 2 decks or no foundations, return + if (!rules.foundations_present || rules.two_decks) + return boost::none; + + // 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, 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; + } + + card c = piles[pr].top_card(); + pile::ref target_foundation = foundations[c.get_suit()]; + + // 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) + : 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); + + // 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 + + return boost::none; +} + +optional game_state::auto_reserve_move() const { + if (rules.spaces_pol == s_pol::AUTO_RESERVE_THEN_WASTE && !piles[reserve.front()].empty()) { + for (auto to : tableau_piles) { + if (piles[to].empty()) { + return move(move::mtype::dominance, reserve.front(), to); + } + } + } + return boost::none; +} + +bool game_state::is_ordered_pile(pile::ref pr) const { + if (piles[pr].size() != rules.max_rank) return false; + + card::suit_t s = piles[pr][0].get_suit(); + for (card::rank_t r = 0; r < rules.max_rank; r++) { + if (piles[pr][r] != card(s, card::rank_t(r + 1))) return false; + } + return true; +} + +// For games where the foundations can be removed from, this dominance blocks +// the foundations being removed from if the card that were to be removed would +// go 'automatically' up +bool game_state::dominance_blocks_foundation_move(pile::ref target_pile) { + assert(!piles[target_pile].empty()); + + card target_card = piles[target_pile].top_card(); + piles[target_pile].take(); + + bool blocked = is_valid_auto_foundation_move(target_pile); + piles[target_pile].place(target_card); + + return blocked; +} + +// Decides which 'auto foundation' booleans should be 'true' currently +void game_state::update_auto_foundation_moves(pile::ref target_pile) { + // If there are no foundations, or this is not a foundation move, do nothing + if (!rules.foundations_present + || target_pile < foundations.front() + || target_pile > foundations.back()) { + return; + } + + // Turns the 'auto' boolean for the target foundation false + + for (pile::ref pr : foundations) { + 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; +} diff --git a/src/main/game/search-state/game_state.h b/src/main/game/search-state/game_state.h index f6fc666..d8d9f12 100644 --- a/src/main/game/search-state/game_state.h +++ b/src/main/game/search-state/game_state.h @@ -111,6 +111,7 @@ private: bool is_valid_hole_move(pile::ref) const; bool is_valid_hole_move(card) const; + void add_valid_tableau_moves(std::vector&, pile::ref) const; void add_built_group_moves(std::vector&) const; void add_built_group_moves(std::vector&, pile::ref, pile::size_type) const; void add_whole_pile_moves(std::vector&) const; @@ -121,6 +122,7 @@ private: void add_kings_only_built_group_move(std::vector&, pile::ref, pile::ref, pile::size_type, bool) const; void add_non_empty_built_group_move(std::vector&, pile::ref, pile::ref, pile::size_type, bool) const; void add_sequence_moves(std::vector&) const; + bool tableau_space_and_auto_reserve() const; bool is_next_legal_card(sol_rules::build_policy, card, card, bool) const; bool is_next_legal_card(std::vector, card, card) const; @@ -128,6 +130,7 @@ private: /* Auto-foundation moves */ + boost::optional auto_reserve_move() const; bool is_valid_auto_foundation_move(pile::ref) const; bool is_ordered_pile(pile::ref) const; bool dominance_blocks_foundation_move(pile::ref); 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 09a32fa..f97814a 100644 --- a/src/main/game/search-state/game_state.legal_moves.cpp +++ b/src/main/game/search-state/game_state.legal_moves.cpp @@ -74,9 +74,7 @@ vector game_state::get_legal_moves(move parent_move) { 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); + add_valid_tableau_moves(moves, f); if (empty_cell != 255) moves.emplace_back(move::mtype::regular, f, empty_cell); @@ -87,17 +85,14 @@ vector game_state::get_legal_moves(move parent_move) { 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); - } - } + add_valid_tableau_moves(moves, r); } - // Stock to tableau moves + // Stock-waste to tableau moves if (rules.stock_size > 0 && rules.stock_deal_t == sdt::WASTE) add_stock_to_tableau_moves(moves); + // Tableau built group moves switch (rules.move_built_group) { case sol_rules::built_group_type::YES: add_built_group_moves(moves); @@ -115,11 +110,7 @@ vector game_state::get_legal_moves(move parent_move) { 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); - } - } + add_valid_tableau_moves(moves, t_from); } } @@ -132,11 +123,7 @@ vector game_state::get_legal_moves(move parent_move) { 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); - } - } + add_valid_tableau_moves(moves, c); } // Tableau / cells / reserve / stock-waste to hole / foundation moves @@ -233,9 +220,14 @@ void game_state::add_stock_to_tableau_moves(std::vector& moves) const { for (auto k_plus_mv : generate_k_plus_moves_to_check()) { card from = stock_card_from_count(k_plus_mv.first); - for (auto t : tableau_piles) - if (is_valid_tableau_move(from, t)) + // Obeys the auto-reserve restriction unless the reserve is empty + if (!piles[reserve.front()].empty() && tableau_space_and_auto_reserve()) return; + + for (auto t : tableau_piles) { + if (is_valid_tableau_move(from, t)) { moves.emplace_back(move::mtype::stock_k_plus, stock, t, k_plus_mv.first, false, k_plus_mv.second); + } + } } } @@ -313,6 +305,8 @@ bool game_state::is_valid_foundations_move(const pile::ref rem_ref, bool game_state::is_valid_foundations_move(const card rem_c, const pile::ref add_ref) const { + if (piles[add_ref].size() == rules.max_rank) return false; + // Checks same suit policy uint8_t suit_idx = (add_ref - foundations.front()) % uint8_t(4); if (rem_c.get_suit() != suit_idx) @@ -320,9 +314,9 @@ bool game_state::is_valid_foundations_move(const card rem_c, // Checks rank if (piles[add_ref].empty()) - return rem_c.get_rank() == rules.foundations_base; + return rem_c.get_rank() == foundations_base; else - return rem_c.get_rank() == (piles[add_ref].top_card().get_rank() + 1) % rules.max_rank; + return rem_c.get_rank() == (piles[add_ref].top_card().get_rank() % rules.max_rank) + 1; } bool game_state::is_valid_hole_move(const pile::ref rem_ref) const { @@ -345,6 +339,16 @@ bool game_state::is_valid_hole_move(const card c) const { // BUILT-GROUP MOVE GEN FUNCTIONS // //////////////////////////////////// +void game_state::add_valid_tableau_moves(std::vector& moves, pile::ref from) const { + if (tableau_space_and_auto_reserve()) return; + + for (auto to : tableau_piles) { + if (is_valid_tableau_move(from, to)) { + moves.emplace_back(move::mtype::regular, from, to); + } + } +} + void game_state::add_built_group_moves(vector& moves) const { assert(rules.built_group_pol != pol::NO_BUILD); @@ -383,6 +387,7 @@ void game_state::add_built_group_moves(vector& moves, pile::ref rem_ref, p void game_state::add_whole_pile_moves(vector& moves) const { assert(rules.built_group_pol != pol::NO_BUILD); + if (tableau_space_and_auto_reserve()) return; // Cycles through each pile to see if it contains a whole-pile built group for (auto rem_ref : tableau_piles) { @@ -534,6 +539,13 @@ void game_state::add_accordion_moves(vector& moves) const { } } +// If auto-reserve is enabled and there is a space, returns +bool game_state::tableau_space_and_auto_reserve() const { + if (rules.spaces_pol == s_pol::AUTO_RESERVE_THEN_WASTE) + for (auto to : tableau_piles) + if (piles[to].empty()) return true; + return false; +} /////////////////////// -- cgit v1.2.1