aboutsummaryrefslogtreecommitdiff
path: root/src/main/game
diff options
context:
space:
mode:
authorCharlie Blake2018-06-10 00:28:24 +0100
committerCharlie Blake2018-06-10 00:28:24 +0100
commitf4bfb7e972fda893240899b44534c65e4a10e636 (patch)
treeb2bbe8c0aadaec16280f020e55eb55955be43d3f /src/main/game
parentRefactor moves logic (diff)
downloadmetasol-f4bfb7e972fda893240899b44534c65e4a10e636.tar.gz
metasol-f4bfb7e972fda893240899b44534c65e4a10e636.zip

Finish refactoring game state class

Diffstat (limited to 'src/main/game')
-rw-r--r--src/main/game/search-state/game_state.auto_foundation_moves.cpp93
-rw-r--r--src/main/game/search-state/game_state.cpp8
-rw-r--r--src/main/game/search-state/game_state.h30
-rw-r--r--src/main/game/search-state/game_state.legal_moves.cpp425
4 files changed, 260 insertions, 296 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 2faddd0..0b62af4 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
@@ -9,21 +9,6 @@ typedef sol_rules::build_policy pol;
using std::max;
-// 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
- || 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);
- }
-}
bool game_state::is_valid_auto_foundation_move(pile::ref target_pile) const {
#ifndef AUTO_FOUNDATIONS_STREAMLINER
@@ -71,3 +56,81 @@ bool game_state::is_valid_auto_foundation_move(pile::ref target_pile) const {
return other_within_2;
}
}
+
+// Returns a dominance move if one is available
+boost::optional<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 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 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(move::mtype::dominance, pr, target_foundation);
+ }
+ }
+#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
+ || 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);
+ }
+}
diff --git a/src/main/game/search-state/game_state.cpp b/src/main/game/search-state/game_state.cpp
index ac57142..0a20f60 100644
--- a/src/main/game/search-state/game_state.cpp
+++ b/src/main/game/search-state/game_state.cpp
@@ -233,7 +233,7 @@ void game_state::make_move(const move m) {
make_regular_move(m);
break;
case move::mtype::built_group:
- make_built_pile_move(m);
+ make_built_group_move(m);
break;
case move::mtype::stock_to_waste:
make_stock_to_waste_move(m);
@@ -257,7 +257,7 @@ void game_state::undo_move(const move m) {
undo_regular_move(m);
break;
case move::mtype::built_group:
- undo_built_pile_move(m);
+ undo_built_group_move(m);
break;
case move::mtype::stock_to_waste:
undo_stock_to_waste_move(m);
@@ -295,7 +295,7 @@ void game_state::undo_regular_move(const move m) {
#endif
}
-void game_state::make_built_pile_move(const move m) {
+void game_state::make_built_group_move(move m) {
assert(rules.move_built_group);
assert(m.from < piles.size() );
assert(m.to < piles.size() );
@@ -312,7 +312,7 @@ void game_state::make_built_pile_move(const move m) {
}
}
-void game_state::undo_built_pile_move(const move m) {
+void game_state::undo_built_group_move(move m) {
assert(rules.move_built_group);
assert(m.to < piles.size());
assert(m.count <= rules.max_rank);
diff --git a/src/main/game/search-state/game_state.h b/src/main/game/search-state/game_state.h
index be8b79c..7c0f9ca 100644
--- a/src/main/game/search-state/game_state.h
+++ b/src/main/game/search-state/game_state.h
@@ -70,8 +70,8 @@ private:
void make_regular_move(move move);
void undo_regular_move(move move);
- void make_built_pile_move(move move);
- void undo_built_pile_move(move move);
+ void make_built_group_move(move move);
+ void undo_built_group_move(move move);
void make_stock_to_waste_move(move move);
void undo_stock_to_waste_move(move move);
void make_stock_to_tableau_move(move move);
@@ -81,23 +81,37 @@ private:
/* Legal move generation */
+ bool stock_can_deal_tableau() const;
move get_stock_tableau_move() const;
+ bool stock_can_redeal() const;
+ bool undoes_prev_move(pile::ref, move) const;
+ bool is_foundation(pile::ref) const;
+
+ void add_stock_to_waste_move(std::vector<move>&) const;
+ void add_tableau_moves(std::vector<move>&, pile::ref) const;
+ void add_cell_moves(std::vector<move>&, pile::ref) const;
+ void add_foundation_moves(std::vector<move>&, pile::ref) const;
+ void add_built_group_moves(std::vector<move> &) const;
+ void add_foundation_complete_piles_moves(std::vector<move> &) const;
+
bool is_valid_tableau_move(pile::ref, pile::ref) const;
+ bool is_next_tableau_card(card, card) const;
bool is_valid_foundations_move(pile::ref, pile::ref) const;
bool is_valid_hole_move(pile::ref) const;
- void get_built_group_moves(std::vector<move>&) const;
+
pile::size_type get_built_group_height(pile::ref) const;
- bool valid_built_group_move(card, card, card) const;
void add_empty_built_group_moves(std::vector<move>&, pile::ref, pile::ref,
card) const;
- bool dominance_blocks_foundation_move(pile::ref);
- bool is_ordered_pile(pile::ref) const;
+ bool is_next_built_group_card(card, card) const;
+
+ bool is_next_legal_card(sol_rules::build_policy, card, card) const;
/* Auto-foundation moves */
- void update_auto_foundation_moves(pile::ref);
bool is_valid_auto_foundation_move(pile::ref) const;
-
+ bool is_ordered_pile(pile::ref) const;
+ bool dominance_blocks_foundation_move(pile::ref);
+ void update_auto_foundation_moves(pile::ref);
/* 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 3fb0156..2acfba6 100644
--- a/src/main/game/search-state/game_state.legal_moves.cpp
+++ b/src/main/game/search-state/game_state.legal_moves.cpp
@@ -15,123 +15,72 @@ typedef sol_rules::spaces_policy s_pol;
typedef sol_rules::stock_deal_type sdt;
+////////////////////////////////
+// MAIN LEGAL MOVE GEN CYCLE ///
+////////////////////////////////
+
+// Note that the moves added last here, are tried first
vector<move> game_state::get_legal_moves(move parent_move) {
-// The next legal moves
vector<move> moves;
-// If we have a stock that can deal to the tableau piles, adds this move
- if (rules.stock_size > 0
- && rules.stock_deal_t == sdt::TABLEAU_PILES
- && !piles[stock].empty()) {
-
- moves.emplace_back(get_stock_tableau_move());
- }
-
-// If the stock is empty and we are allowed to re-deal, move the waste
-// back onto the stock
- if (rules.stock_size > 0
- && rules.stock_redeal
- && rules.stock_deal_t == sdt::WASTE
- && piles[stock].empty()
- && !piles[waste].empty()) {
- moves.emplace_back(move::mtype::redeal);
+ // Attempts stock tableau deal and redeal
+ 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);
}
-// Cycles through each pile which we may be able to remove a card from
+ // Cycles through all of the piles from which we want to remove cards
for (pile::ref rem_ref = 0; rem_ref < piles.size(); rem_ref++) {
-// Never removes a card from the hole, an empty pile, or the pile we
-// just moved a card to
- if ((rules.hole && rem_ref == hole)
- || piles[rem_ref].empty()
- || (rem_ref == parent_move.to
- && parent_move.count == 1
- && parent_move.from != stock)
- ) 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) {
- if (rules.stock_deal_t == sdt::WASTE) {
- uint8_t cards_to_move =
- min(rules.stock_deal_count, piles[stock].size());
- moves.emplace_back(move::mtype::stock_to_waste, stock, waste, cards_to_move);
- }
+ if ((rules.hole && rem_ref == hole) || piles[rem_ref].empty())
+ continue;
+ if (undoes_prev_move(rem_ref, parent_move))
continue;
- }
-
-// For each tableau pile, check if moving from the rem pile is a valid
-// tableau move according to the rules
- for (auto add_ref : tableau_piles) {
- if (is_valid_tableau_move(rem_ref, add_ref)) {
- moves.emplace_back(move::mtype::regular, rem_ref, add_ref);
- }
- }
-// Any card can be moved to a cell, so long as the cell isn't full
- for (auto add_ref : cells) {
- if (add_ref != rem_ref && piles[add_ref].empty()) {
- moves.emplace_back(move::mtype::regular, rem_ref, add_ref);
- }
+ if (rules.foundations && is_foundation(rem_ref)) {
+ if (!rules.foundations_removable) continue;
+ else if (dominance_blocks_foundation_move(rem_ref)) continue;
}
-// For each foundation pile, check if moving from the rem pile is a valid
-// move according to the rules
- for (auto add_ref : foundations) {
- if (is_valid_foundations_move(rem_ref, add_ref)) {
- moves.emplace_back(move::mtype::regular, rem_ref, add_ref);
- }
+ if (rules.stock_size > 0 && rem_ref == stock) {
+ if (rules.stock_deal_t == sdt::WASTE)
+ add_stock_to_waste_move(moves);
+ continue;
}
-// Does the same for the hole
- if (rules.hole && is_valid_hole_move(rem_ref)) {
+ if (rules.tableau_pile_count > 0)
+ add_tableau_moves(moves, rem_ref);
+ if (rules.cells > 0)
+ add_cell_moves(moves, rem_ref);
+ if (rules.foundations)
+ add_foundation_moves(moves, rem_ref);
+ if (rules.hole && is_valid_hole_move(rem_ref))
moves.emplace_back(move::mtype::regular, rem_ref, hole);
- }
-
-// Note that the stock and the reserve aren't mentioned here, as
-// they cannot be added to
}
- if (rules.move_built_group) {
- get_built_group_moves(moves);
- }
+ if (rules.move_built_group)
+ add_built_group_moves(moves);
-// If only complete built piles can be moved to the foundations,
- if (rules.foundations_comp_piles) {
- for (pile::ref tab_pr : tableau_piles) {
- if (is_ordered_pile(tab_pr)) {
+ if (rules.foundations_comp_piles) // I.E. Spider-type winning condition
+ add_foundation_complete_piles_moves(moves);
- for (pile::ref found_pr : foundations) {
- if (piles[found_pr].empty()) {
- moves.emplace_back(move::mtype::built_group, tab_pr, found_pr, rules.max_rank);
- break;
- }
- }
- }
- }
- }
+ return moves;
+}
- return moves;
+////////////////////////////////
+// REGULAR MOVE GEN FUNCTIONS //
+////////////////////////////////
+
+bool game_state::stock_can_deal_tableau() const {
+ return rules.stock_deal_t == sdt::TABLEAU_PILES
+ && !piles[stock].empty();
}
-// This is a special kind of move which the solver handles differently. Hence
-// the arbitrary middle params. We must supply the number of stock cards
-// that we will deal, so that the move can be undone in backtracking
+// This is a special kind of move which the solver handles differently.
+// We must supply the number of stock cards
+// that we will deal, so that the move can be undone in backtracking.
move game_state::get_stock_tableau_move() const {
pile::size_type stock_moves =
piles[stock].size() >= tableau_piles.size()
@@ -142,70 +91,116 @@ move game_state::get_stock_tableau_move() const {
return move(move::mtype::stock_to_tableau, 0, 0, stock_moves);
}
-// If a tableau pile is empty and the space policy is
-// no-build, do not add. Otherwise any card with the right
-// suit/rank can be moved to a tableau
+bool game_state::stock_can_redeal() const {
+ return rules.stock_redeal
+ && rules.stock_deal_t == sdt::WASTE
+ && piles[stock].empty()
+ && !piles[waste].empty();
+}
+
+bool game_state::undoes_prev_move(pile::ref rem_ref, move parent_move) const {
+ return rem_ref == parent_move.to
+ && parent_move.count == 1
+ && parent_move.from != stock;
+}
+
+bool game_state::is_foundation(pile::ref ref) const {
+ return ref >= foundations.front() && ref <= foundations.back();
+}
+
+void game_state::add_stock_to_waste_move(std::vector<move>& moves) const {
+ uint8_t cards_to_move = min(rules.stock_deal_count, piles[stock].size());
+ moves.emplace_back(move::mtype::stock_to_waste, stock, waste, cards_to_move);
+}
+
+void game_state::add_tableau_moves(std::vector<move>& moves, pile::ref rem_ref) const {
+ for (auto add_ref : tableau_piles) {
+ if (is_valid_tableau_move(rem_ref, add_ref)) {
+ moves.emplace_back(move::mtype::regular, rem_ref, add_ref);
+ }
+ }
+}
+
+void game_state::add_cell_moves(std::vector<move>& moves, pile::ref rem_ref) const {
+ for (auto add_ref : cells) {
+ if (add_ref != rem_ref && piles[add_ref].empty()) {
+ moves.emplace_back(move::mtype::regular, rem_ref, add_ref);
+ }
+ }
+}
+
+void game_state::add_foundation_moves(std::vector<move>& moves, pile::ref rem_ref) const {
+ for (auto add_ref : foundations) {
+ if (is_valid_foundations_move(rem_ref, add_ref)) {
+ moves.emplace_back(move::mtype::regular, rem_ref, add_ref);
+ }
+ }
+}
+
+void game_state::add_foundation_complete_piles_moves(std::vector<move>& moves) const {
+ for (pile::ref tab_pr : tableau_piles) {
+ if (is_ordered_pile(tab_pr)) {
+
+ for (pile::ref found_pr : foundations) {
+ if (piles[found_pr].empty()) {
+ moves.emplace_back(move::mtype::built_group, tab_pr, found_pr, rules.max_rank);
+ break;
+ }
+ }
+ }
+ }
+}
+
bool game_state::is_valid_tableau_move(const pile::ref rem_ref,
const pile::ref add_ref) const {
- if (rem_ref == add_ref) {
- return false;
- } else if (rules.build_pol == pol::NO_BUILD) {
+ if (rem_ref == add_ref || rules.build_pol == pol::NO_BUILD)
return false;
- } else if (piles[add_ref].empty()) {
+
+ if (piles[add_ref].empty()) {
switch(rules.spaces_pol) {
case sol_rules::spaces_policy::NO_BUILD:
return false;
case sol_rules::spaces_policy::KINGS:
return piles[rem_ref].top_card().get_rank() == 13;
- case sol_rules::spaces_policy::ANY:
+ default:
return true;
}
- }
-
- card rem_c = piles[rem_ref].top_card();
- card add_c = piles[add_ref].top_card();
-
- // Checks violation of same suit policy
- if (rules.build_pol == pol::SAME_SUIT
- && rem_c.get_suit() != add_c.get_suit()) {
- return false;
- }
+ } else {
+ card rem_c = piles[rem_ref].top_card();
+ card add_c = piles[add_ref].top_card();
- // Checks violation of red-black suit policy
- else if (rules.build_pol == pol::RED_BLACK
- && rem_c.get_colour() == add_c.get_colour()) {
- return false;
+ return is_next_tableau_card(add_c, rem_c);
}
+}
- // Checks rank
- else return rem_c.get_rank() + 1 == add_c.get_rank();
+bool game_state::is_next_tableau_card(card a, card b) const {
+ return is_next_legal_card(rules.build_pol, a, b);
}
bool game_state::is_valid_foundations_move(const pile::ref rem_ref,
const pile::ref add_ref) const {
- if (rules.foundations_comp_piles || rem_ref == add_ref) return false;
+ if (rem_ref == add_ref || rules.foundations_comp_piles) return false;
card rem_c = piles[rem_ref].top_card();
- // Checks violation of same suit policy
+ // Checks same suit policy
uint8_t suit_idx = (add_ref - foundations.front()) % uint8_t(4);
- if (rem_c.get_suit() != suit_idx) {
+ if (rem_c.get_suit() != suit_idx)
return false;
- }
- // Checks rank
- else {
- card::rank_t add_rank = piles[add_ref].empty()
- ? card::rank_t(0)
- : piles[add_ref].top_card().get_rank();
- return rem_c.get_rank() - 1 == add_rank;
- }
+ // Checks rank
+ if (piles[add_ref].empty())
+ return rem_c.get_rank() == 1;
+ else
+ return rem_c.get_rank() == piles[add_ref].top_card().get_rank() + 1;
}
bool game_state::is_valid_hole_move(const pile::ref rem_ref) const {
if (rem_ref == hole) return false;
+
card::rank_t rank = piles[rem_ref].top_card().get_rank();
card::rank_t hole_rank = piles[hole].top_card().get_rank();
+
return rank + 1 == hole_rank
|| rank - 1 == hole_rank
|| (rank == rules.max_rank && hole_rank == 1)
@@ -213,45 +208,32 @@ bool game_state::is_valid_hole_move(const pile::ref rem_ref) const {
}
-///////////////////////
-// BUILT-GROUP MOVES //
-///////////////////////
+////////////////////////////////////
+// BUILT-GROUP MOVE GEN FUNCTIONS //
+////////////////////////////////////
-void game_state::get_built_group_moves(vector<move>& moves) const {
+void game_state::add_built_group_moves(vector<move> &moves) const {
assert(rules.built_group_pol != pol::NO_BUILD);
- // Cycles through each pile to see if it contains a built group of size > 1
+ // Cycles through each pile to see if it contains a built group
for (auto rem_ref : tableau_piles) {
if (piles[rem_ref].size() < 2) continue;
auto built_group_height = get_built_group_height(rem_ref);
if (built_group_height == 1) continue;
- // We have found a built group, so cycles through each pile to see if
- // this group can be added to it
+ // 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_low = piles[rem_ref].top_card();
card bg_high = piles[rem_ref][built_group_height - 1];
- // If the pile is empty, and we can move to empty piles, does so
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))
add_empty_built_group_moves(moves, rem_ref, add_ref, bg_high);
- }
- }
- // Otherwise, checks to see if it is a valid move
- else {
- card add_card = piles[add_ref].top_card();
-
- // Adds the move to the list of moves, as a special built group move
- if (valid_built_group_move(bg_low, bg_high, add_card)) {
-
+ } 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);
- }
}
}
}
@@ -259,131 +241,36 @@ void game_state::get_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 {
-
- // The index of the currently examined card, the current card, previous card
- // and whether we currently have a valid built group.
- pile::size_type group_card_idx = 0;
- bool built_group = true;
-
- do {
- card prev_c = piles[ref][group_card_idx++];
- card c = piles[ref][group_card_idx];
-
- // If the build policy is same suit, makes sure group has same suit
- if (rules.built_group_pol == pol::SAME_SUIT
- && c.get_suit() != prev_c.get_suit()) {
- built_group = false;
- }
- // If the build policy is red black, makes sure group alternates
- // in correct phase
- else if (rules.built_group_pol == pol::RED_BLACK
- && c.get_colour() == prev_c.get_colour()) {
- built_group = false;
- }
-
- // Makes sure the rank of the cards is sequential
- built_group = built_group
- && c.get_rank() == prev_c.get_rank()+1;
-
- }
- // Continues until group_card_idx points to a card outside the built
- // Group
- while (built_group && group_card_idx < piles[ref].size());
-
- return group_card_idx;
-}
-
-bool game_state::valid_built_group_move(card bg_low, card bg_high, card add_card)
-const {
- if (add_card.get_rank() != bg_high.get_rank() + 1) return false;
-
- // Checks to see if the suits are correct
- switch (rules.built_group_pol) {
- case pol::SAME_SUIT:
- return bg_low.get_suit() == add_card.get_suit();
- case pol::RED_BLACK: {
- return bg_high.get_colour() != add_card.get_colour();
- }
- default: return true;
- }
+ pile::size_type i = 1;
+ while (is_next_built_group_card(piles[ref][i], piles[ref][i-1]) && i < piles[ref].size())
+ i++;
+ return i;
}
-void game_state::add_empty_built_group_moves(vector<move>& moves,
- pile::ref rem_ref,
- pile::ref add_ref,
+// Loops through each possible built group move to an empty pile and adds it to the list
+void game_state::add_empty_built_group_moves(vector<move>& moves, pile::ref rem_ref, pile::ref add_ref,
card bg_high) const {
- // Loops through each possible built group move to an empty pile and adds it
- // to the list
pile::size_type card_idx = 1;
do {
- moves.emplace_back(
- move::mtype::built_group, rem_ref, add_ref, static_cast<pile::size_type>(card_idx + 1)
- );
+ 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);
}
-/////////////////////
-// Dominance Moves //
-/////////////////////
-
-// Returns a dominance move if one is available
-boost::optional<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 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 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(move::mtype::dominance, pr, target_foundation);
- }
- }
-#endif
-
- return boost::none;
+bool game_state::is_next_built_group_card(card a, card b) const {
+ return is_next_legal_card(rules.built_group_pol, a, b);
}
-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;
+bool game_state::is_next_legal_card(pol p, card a, card b) const {
+ // Checks build pol violations
+ switch(p) {
+ case sol_rules::build_policy::SAME_SUIT:
+ if (b.get_suit() != a.get_suit()) return false;
+ break;
+ case sol_rules::build_policy::RED_BLACK:
+ if (b.get_colour() == a.get_colour()) return false;
+ break;
+ default:;
}
- 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;
+ // Checks rank
+ return b.get_rank() + 1 == a.get_rank();
}