aboutsummaryrefslogtreecommitdiff
path: root/src/main/game/search-state/game_state.cpp
diff options
context:
space:
mode:
authorCharlie Blake2018-07-06 17:25:53 +0100
committerCharlie Blake2018-07-06 17:25:53 +0100
commit4c209e2f5ab9ed7574a9b73eb96687dc5ae77391 (patch)
tree634eede8d39d3e313c1206cb2b9a6f6205570f8c /src/main/game/search-state/game_state.cpp
parentRefactored game rules JSON (diff)
downloadmetasol-4c209e2f5ab9ed7574a9b73eb96687dc5ae77391.tar.gz
metasol-4c209e2f5ab9ed7574a9b73eb96687dc5ae77391.zip

Refactor k-plus impl to simplify and fix deal1 bug

Diffstat (limited to 'src/main/game/search-state/game_state.cpp')
-rw-r--r--src/main/game/search-state/game_state.cpp75
1 files changed, 39 insertions, 36 deletions
diff --git a/src/main/game/search-state/game_state.cpp b/src/main/game/search-state/game_state.cpp
index 169c96e..0720f6e 100644
--- a/src/main/game/search-state/game_state.cpp
+++ b/src/main/game/search-state/game_state.cpp
@@ -378,75 +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());
-
- for (int i = 0; i < count; i++) {
- place_card(waste, take_card(stock));
- 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));
+ // 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));
}
}
- place_card(m.to, take_card(stock));
- if (rules.stock_redeal && piles[stock].empty() && count != 0)
- while (!piles[waste].empty())
+ // Moves the card on top of the waste to the target pile
+ place_card(m.to, 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);
-
- if (rules.stock_redeal && piles[waste].empty() && count != 0)
- while (!piles[stock].empty())
+ if (m.flip_waste) {
+ assert(rules.stock_redeal && piles[waste].empty());
+ while (!piles[stock].empty()) {
place_card(waste, take_card(stock));
- place_card(stock, take_card(m.to));
-
- for (int i = 0; i < count; 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));
}
+ }
+
+ place_card(waste, take_card(m.to));
- place_card(stock, take_card(waste));
+ 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) {