diff options
Diffstat (limited to 'src/main/game/game_state.legal_moves.cpp')
| -rw-r--r-- | src/main/game/game_state.legal_moves.cpp | 79 |
1 files changed, 77 insertions, 2 deletions
diff --git a/src/main/game/game_state.legal_moves.cpp b/src/main/game/game_state.legal_moves.cpp index 3043587..c1a87e4 100644 --- a/src/main/game/game_state.legal_moves.cpp +++ b/src/main/game/game_state.legal_moves.cpp @@ -2,15 +2,18 @@ // Created by thecharlesblake on 1/3/18. // +#include <boost/optional.hpp> + #include "game_state.h" using namespace std; +using namespace boost; typedef sol_rules::build_policy pol; typedef sol_rules::spaces_policy s_pol; typedef sol_rules::stock_deal_type sdt; -vector<game_state::move> game_state::get_legal_moves() const { +vector<game_state::move> game_state::get_legal_moves() { // The next legal moves vector<move> moves; @@ -22,7 +25,23 @@ vector<game_state::move> game_state::get_legal_moves() const { // Cycles through each pile which we may be able to remove a card from for (pile_ref rem_ref = 0; rem_ref < piles.size(); rem_ref++) { // Never removes a card from the hole, the waste, or an empty pile - if (rem_ref == hole || piles[rem_ref].empty()) continue; + if ((rules.hole && rem_ref == hole) || piles[rem_ref].empty()) continue; + + // If we have a foundations card as the rem_ref + if (rules.foundations + && rem_ref >= foundations.front() + && rem_ref <= foundations.back()) { + + // If the foundations are removable, checks to see if a dominance + // prevents us from removing from it. Otherwise, skips the pile ref + if (!rules.foundations_removable +#ifndef NO_AUTO_FOUNDATIONS + || dominance_blocks_foundation_move(rem_ref) +#endif + ) { + continue; + } + } // Stock cards can only be moved to the waste (assuming there is a waste) if (rules.stock_size > 0 && rem_ref == stock) { @@ -271,3 +290,59 @@ void game_state::add_empty_built_group_moves(vector<move>& moves, ); } while (piles[rem_ref][card_idx++] != bg_high); } + +///////////////////// +// Dominance Moves // +///////////////////// + +// Returns a dominance move if one is available +optional<game_state::move> game_state::get_dominance_move() const { +#ifndef NO_AUTO_FOUNDATIONS + // If there are 2 decks or no foundations, return + if (!rules.foundations || rules.two_decks) + return 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 or stock cards to the foundations + if ((pr >= foundations.front() && pr <= foundations.back()) + || (rules.hole && pr == hole) + || (rules.stock_size > 0 && pr == stock) + || (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) : + piles[target_foundation].top_card().get_rank() + + card::rank_t(1); + if (target_rank == c.get_rank() && + auto_foundation_moves[c.get_suit()]) { + return move(pr, target_foundation, move::dominance_flag); + } + } +#endif + + return none; +} + +// 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; +} |
