aboutsummaryrefslogtreecommitdiff
path: root/src/main/game/search-state
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/game/search-state')
-rw-r--r--src/main/game/search-state/game_state.auto_foundation_moves.cpp6
-rw-r--r--src/main/game/search-state/game_state.cpp73
-rw-r--r--src/main/game/search-state/game_state.h4
-rw-r--r--src/main/game/search-state/game_state.legal_moves.cpp95
4 files changed, 81 insertions, 97 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 43f8911..b74948a 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
@@ -12,7 +12,7 @@ using std::max;
bool game_state::is_valid_auto_foundation_move(pile::ref target_pile) const {
- if (rules.foundations_comp_piles)
+ if (rules.foundations_only_comp_piles)
return false;
else if ( stream_opts == sos::AUTO_FOUNDATIONS
|| stream_opts == sos::BOTH
@@ -65,7 +65,7 @@ bool game_state::is_valid_auto_foundation_move(pile::ref target_pile) const {
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)
+ if (!rules.foundations_present || rules.two_decks)
return boost::none;
// Cycles through the piles and sees if any cards can be automatically moved
@@ -132,7 +132,7 @@ bool game_state::dominance_blocks_foundation_move(pile::ref target_pile) {
// 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
+ if (!rules.foundations_present
|| target_pile < foundations.front()
|| target_pile > foundations.back()) {
return;
diff --git a/src/main/game/search-state/game_state.cpp b/src/main/game/search-state/game_state.cpp
index a1d60bf..0720f6e 100644
--- a/src/main/game/search-state/game_state.cpp
+++ b/src/main/game/search-state/game_state.cpp
@@ -58,7 +58,7 @@ game_state::game_state(const sol_rules& s_rules, streamliner_options stream_opts
}
// If there are foundation piles, creates the relevant pile vectors
- if (rules.foundations) {
+ if (rules.foundations_present) {
for (uint8_t i = 0; i < 4*(rules.two_decks ? 2:1); i++) {
piles.emplace_back();
foundations.push_back(static_cast<pile::ref>(piles.size() - 1));
@@ -378,67 +378,78 @@ void game_state::undo_built_group_move(move m) {
}
void game_state::make_stock_k_plus_move(const move m) {
+#ifndef NDEBUG
assert(rules.stock_deal_t == sdt::WASTE);
- assert(m.count < piles[stock].size() + piles[waste].size());
+ assert(m.from == stock);
+ if (rules.stock_redeal) assert(m.count <= piles[stock].size() && m.count > -piles[waste].size());
assert(!rules.stock_redeal || m.to != waste);
-#ifndef NDEBUG
auto sz_before = piles[stock].size() + piles[waste].size();
#endif
- // if redeal -> stock tableau move implies s+w-=1
- // Index of the card to be moved
- auto count = m.count % (piles[stock].size() + piles[waste].size());
+ // Transfers count cards from the stock to the waste
+ if (m.count > 0) {
+ for (int i = 0; i < m.count; i++) {
+ place_card(waste, take_card(stock));
+ }
+ } else {
+ for (int i = 0; i > m.count; i--) {
+ place_card(stock, take_card(waste));
+ }
+ }
- for (int i = 0; i <= count; i++) {
- if (i < count) place_card(waste, take_card(stock));
- else place_card(m.to, take_card(stock));
+ // Moves the card on top of the waste to the target pile
+ place_card(m.to, take_card(waste));
- if (rules.stock_redeal) {
- // If the stock is empty, flips it
- // (invariant: no search state has an empty stock if rules.stock_size > 0 && rules.stock_redeal)
- if (piles[stock].empty())
- while (!piles[waste].empty())
- place_card(stock, take_card(waste));
+ // Flips the waste back on to the empty stock (if necessary)
+ if (m.flip_waste) {
+ assert(rules.stock_redeal && piles[stock].empty());
+ while (!piles[waste].empty()) {
+ place_card(stock, take_card(waste));
}
}
#ifndef NDEBUG
auto sz_after = piles[stock].size() + piles[waste].size();
assert(sz_before == sz_after + 1);
-#endif
assert(!(rules.stock_size > 0 && rules.stock_redeal && piles[stock].empty() && !piles[waste].empty()));
assert(piles[stock].size() <= rules.stock_size);
+#endif
}
void game_state::undo_stock_k_plus_move(move m) {
+#ifndef NDEBUG
assert(rules.stock_deal_t == sdt::WASTE);
- assert(m.count < piles[stock].size() + piles[waste].size() + 1);
+ assert(m.from == stock);
assert(!rules.stock_redeal || m.to != waste);
-#ifndef NDEBUG
auto sz_after = piles[stock].size() + piles[waste].size();
#endif
- auto count = m.count % (piles[stock].size() + piles[waste].size() + 1);
-
- for (int i = count; i >= 0; i--) {
- if (rules.stock_redeal) {
- // If the waste is empty, flips it
- // (invariant: no search state has an empty stock if rules.stock_size > 0 && rules.stock_redeal)
- if (piles[waste].empty())
- while (!piles[stock].empty())
- place_card(waste, take_card(stock));
+ if (m.flip_waste) {
+ assert(rules.stock_redeal && piles[waste].empty());
+ while (!piles[stock].empty()) {
+ place_card(waste, take_card(stock));
}
+ }
- if (i < count) place_card(stock, take_card(waste));
- else place_card(stock, take_card(m.to ));
+ place_card(waste, take_card(m.to));
+
+ if (m.count > 0) {
+ for (int i = 0; i < m.count; i++) {
+ place_card(stock, take_card(waste));
+ }
+ } else {
+ for (int i = 0; i > m.count; i--) {
+ place_card(waste, take_card(stock));
+ }
}
#ifndef NDEBUG
auto sz_before = piles[stock].size() + piles[waste].size();
assert(sz_before == sz_after + 1);
-#endif
+ if (rules.stock_redeal) assert(m.count <= piles[stock].size() && m.count > -piles[waste].size());
assert(!(rules.stock_size > 0 && rules.stock_redeal && piles[stock].empty() && !piles[waste].empty()));
assert(piles[stock].size() <= rules.stock_size);
+#endif
}
void game_state::make_stock_to_all_tableau_move(move m) {
@@ -514,7 +525,7 @@ bool game_state::is_solved() const {
solved = piles[hole].size()
== rules.max_rank * 4 * (rules.two_decks ? 2 : 1);
} else {
- assert(rules.foundations);
+ assert(rules.foundations_present);
for (auto f : foundations) {
if (piles[f].size() != rules.max_rank) {
solved = false;
diff --git a/src/main/game/search-state/game_state.h b/src/main/game/search-state/game_state.h
index ecb8ace..3f354d1 100644
--- a/src/main/game/search-state/game_state.h
+++ b/src/main/game/search-state/game_state.h
@@ -90,11 +90,11 @@ private:
bool stock_can_deal_all_tableau() const;
move get_stock_to_all_tableau_move() const;
- std::set<pile::size_type> generate_stock_moves_to_check() const;
+ std::set<std::pair<int8_t, bool>> generate_k_plus_moves_to_check() const;
void add_stock_to_cell_move(std::vector<move>&, pile::ref) const;
void add_stock_to_tableau_moves(std::vector<move>&) const;
void add_stock_to_hole_foundation_moves(std::vector<move>&) const;
- card stock_card_from_index(pile::size_type) const;
+ card stock_card_from_count(int8_t) const;
void add_foundation_complete_piles_moves(std::vector<move> &) const;
bool is_valid_tableau_move(pile::ref, 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 7dd75d8..f6fb68f 100644
--- a/src/main/game/search-state/game_state.legal_moves.cpp
+++ b/src/main/game/search-state/game_state.legal_moves.cpp
@@ -119,7 +119,7 @@ vector<move> game_state::get_legal_moves(move parent_move) {
}
// Tableau / cells / reserve / stock-waste to hole / foundation moves
- if (rules.hole || (rules.foundations && !rules.foundations_comp_piles)) {
+ 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())
add_stock_to_hole_foundation_moves(moves);
@@ -139,7 +139,7 @@ vector<move> game_state::get_legal_moves(move parent_move) {
}
}
- if (rules.foundations_comp_piles) // i.e. Spider-type winning condition
+ if (rules.foundations_only_comp_piles) // i.e. Spider-type winning condition
add_foundation_complete_piles_moves(moves);
if (rules.tableau_pile_count > 0 && rules.face_up != fu::ALL)
@@ -171,94 +171,67 @@ move game_state::get_stock_to_all_tableau_move() const {
return move(move::mtype::stock_to_all_tableau, 0, 0, stock_moves);
}
-set<pile::size_type> game_state::generate_stock_moves_to_check() const {
- set<pile::size_type> stock_moves_to_check;
+set<pair<int8_t, bool>> game_state::generate_k_plus_moves_to_check() const {
+ set<pair<int8_t, bool>> 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<int8_t, bool>(0, false));
// Adds multiples of deal count (excluding last card)
- for (auto i = static_cast<pile::size_type>(rules.stock_deal_count - 1);
- i < piles[stock].size() - 1;
- i += rules.stock_deal_count) {
- stock_moves_to_check.insert(i);
- }
+ for (int8_t count = rules.stock_deal_count;
+ count < piles[stock].size();
+ count += rules.stock_deal_count)
+ stock_moves_to_check.insert(pair<int8_t, bool>(count, false));
// Adds last card
- stock_moves_to_check.insert(static_cast<pile::size_type>(piles[stock].size() - 1));
+ bool flip_waste = rules.stock_redeal;
+ stock_moves_to_check.insert(pair<int8_t, bool>(static_cast<int8_t>(piles[stock].size()), flip_waste));
// If the stock can be redealt, searches through the waste then the stock again
- if (rules.stock_redeal) {
- auto stock_waste_sz = piles[waste].size() + piles[stock].size();
- for (auto i = piles[stock].size() - 1 + rules.stock_deal_count;
- i < piles[stock].size() + stock_waste_sz - 1;
- i += rules.stock_deal_count) {
- stock_moves_to_check.insert(static_cast<pile::size_type>(i % stock_waste_sz));
- }
- }
+ if (rules.stock_redeal)
+ for (auto count = static_cast<int8_t>(-piles[waste].size() + rules.stock_deal_count);
+ count < piles[stock].size();
+ count += rules.stock_deal_count)
+ stock_moves_to_check.insert(pair<int8_t, bool>(count, false));
return stock_moves_to_check;
}
void game_state::add_stock_to_cell_move(std::vector<move>& moves, pile::ref empty_cell) const {
- for (auto i : generate_stock_moves_to_check())
- moves.emplace_back(move::mtype::stock_k_plus, stock, empty_cell, i);
-
- // If there is no redeal option and the k-plus representation is not in effect,
- // adds a regular move from the waste
- if (!piles[waste].empty())
- moves.emplace_back(move::mtype::regular, waste, empty_cell);
+ for (auto k_plus_mv : generate_k_plus_moves_to_check())
+ moves.emplace_back(move::mtype::stock_k_plus, stock, empty_cell, k_plus_mv.first, false, k_plus_mv.second);
}
void game_state::add_stock_to_tableau_moves(std::vector<move>& moves) const {
// For each stock move to check, if it can be moved legally to one of the tableau
// piles, adds this as a move
- for (auto i : generate_stock_moves_to_check()) {
- card from = stock_card_from_index(i);
+ 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) {
+ for (auto t : tableau_piles)
if (is_valid_tableau_move(from, t))
- moves.emplace_back(move::mtype::stock_k_plus, stock, t, i);
- }
- }
-
- // If there is no redeal option and the k-plus representation is not in effect,
- // adds a regular move from the waste to valid tableau piles
- if (!piles[waste].empty()) {
- for (auto t : tableau_piles) {
- if (is_valid_tableau_move(waste, t))
- moves.emplace_back(move::mtype::regular, waste, t);
- }
+ moves.emplace_back(move::mtype::stock_k_plus, stock, t, k_plus_mv.first, false, k_plus_mv.second);
}
}
void game_state::add_stock_to_hole_foundation_moves(std::vector<move>& moves) const {
- for (auto i : generate_stock_moves_to_check()) {
- card from = stock_card_from_index(i);
+ for (auto k_plus_mv : generate_k_plus_moves_to_check()) {
+ card from = stock_card_from_count(k_plus_mv.first);
for (auto f : foundations)
if (is_valid_foundations_move(from, f))
- moves.emplace_back(move::mtype::stock_k_plus, stock, f, i);
- if (rules.hole && is_valid_hole_move(from))
- moves.emplace_back(move::mtype::stock_k_plus, stock, hole, i);
- }
-
- // If there is no redeal option and the k-plus representation is not in effect,
- // adds a regular move from the waste to valid foundation piles
- if (!piles[waste].empty()) {
- for (auto f : foundations)
- if (is_valid_foundations_move(waste, f))
- moves.emplace_back(move::mtype::regular, waste, f);
+ moves.emplace_back(move::mtype::stock_k_plus, stock, f, k_plus_mv.first, false, k_plus_mv.second);
- if (rules.hole && is_valid_hole_move(waste))
- moves.emplace_back(move::mtype::regular, waste, hole);
+ if (rules.hole && is_valid_hole_move(from))
+ moves.emplace_back(move::mtype::stock_k_plus, stock, hole, k_plus_mv.first, false, k_plus_mv.second);
}
}
-card game_state::stock_card_from_index(pile::size_type i) const {
- if (i < piles[stock].size()) {
- return piles[stock][i];
+card game_state::stock_card_from_count(int8_t count) const {
+ if (count > 0) {
+ return piles[stock][count - 1];
} else {
- // If we are moving from the waste in a readeal (by looping), finds the correct card
- auto _i = static_cast<pile::size_type>(piles[stock].size() + piles[waste].size() - 1 - i);
- return piles[waste][_i];
+ return piles[waste][-count];
}
}
@@ -308,7 +281,7 @@ bool game_state::is_next_tableau_card(card a, card b) const {
bool game_state::is_valid_foundations_move(const pile::ref rem_ref,
const pile::ref add_ref) const {
- if (rem_ref == add_ref || rules.foundations_comp_piles) return false;
+ if (rem_ref == add_ref || rules.foundations_only_comp_piles) return false;
return is_valid_foundations_move(piles[rem_ref].top_card(), add_ref);
}