aboutsummaryrefslogtreecommitdiff
path: root/src/main/game
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/game')
-rw-r--r--src/main/game/move.cpp2
-rw-r--r--src/main/game/search-state/game_state.cpp31
-rw-r--r--src/main/game/search-state/game_state.legal_moves.cpp56
3 files changed, 81 insertions, 8 deletions
diff --git a/src/main/game/move.cpp b/src/main/game/move.cpp
index 9e45c66..c51a120 100644
--- a/src/main/game/move.cpp
+++ b/src/main/game/move.cpp
@@ -8,7 +8,7 @@ move::move(mtype ty, pile::ref f, pile::ref t, pile::size_type i, bool reveal_mo
type(ty), from(f), to(t), count(i), reveal_move(reveal_move_) {
#ifndef NDEBUG
if (ty == mtype::regular) assert(i == 1);
- else assert(i >= 1);
+ else if (ty != mtype::stock_to_waste) assert(i >= 1);
#endif
}
diff --git a/src/main/game/search-state/game_state.cpp b/src/main/game/search-state/game_state.cpp
index 7870b1b..d974c26 100644
--- a/src/main/game/search-state/game_state.cpp
+++ b/src/main/game/search-state/game_state.cpp
@@ -385,16 +385,39 @@ void game_state::undo_built_group_move(move m) {
void game_state::make_stock_to_waste_move(const move m) {
assert(rules.stock_deal_t == sdt::WASTE);
- for (int i = 0; i < m.count; i++) {
- place_card(waste, take_card(stock));
+ // Index of the card to be moved
+ auto count = m.count % (piles[stock].size() + piles[waste].size());
+
+ for (int i = 0; i <= count; i++) {
+ if (i < count) place_card(waste, take_card(stock));
+ else place_card(m.to, 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));
+ }
}
}
void game_state::undo_stock_to_waste_move(const move m) {
assert(rules.stock_deal_t == sdt::WASTE);
- for (int i = 0; i < m.count; i++) {
- place_card(stock, take_card(waste));
+ 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 (i < count) place_card(stock, take_card(waste));
+ else place_card(stock, take_card(m.to ));
}
}
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 9ad8b1d..fd71e5c 100644
--- a/src/main/game/search-state/game_state.legal_moves.cpp
+++ b/src/main/game/search-state/game_state.legal_moves.cpp
@@ -4,12 +4,15 @@
#include "game_state.h"
#include "../sol_rules.h"
+#include "../move.h"
#include <boost/optional/optional.hpp>
using std::vector;
using std::min;
using std::list;
+using std::pair;
+using std::set;
using namespace rapidjson;
typedef sol_rules::build_policy pol;
@@ -88,7 +91,10 @@ vector<move> game_state::get_legal_moves(move parent_move) {
}
// Waste to tableau moves
- if (rules.stock_size > 0 && rules.stock_deal_t == sdt::WASTE && !piles[waste].empty()) {
+ if (rules.stock_size > 0
+ && rules.stock_deal_t == sdt::WASTE
+ && !rules.stock_redeal
+ && !piles[waste].empty()) {
for (auto t : tableau_piles) {
if (is_valid_tableau_move(waste, t)) {
moves.emplace_back(move::mtype::regular, waste, t);
@@ -234,8 +240,52 @@ bool game_state::is_foundation(pile::ref ref) const {
}
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);
+ set<pile::size_type> stock_moves_to_check;
+
+ // 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);
+ }
+
+ // Adds last card
+ stock_moves_to_check.insert(static_cast<pile::size_type>(piles[stock].size() - 1));
+
+ // 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));
+ }
+ }
+
+ // TODO: invariant -> no waste moves if redeal
+ // TODO: invariant -> count always < stock+waste
+
+ // 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 : stock_moves_to_check) {
+ card from;
+ // If we are moving from the waste in a readeal (by looping), finds the correct card
+ if (i < piles[stock].size()) {
+ from = piles[stock][i];
+ } else {
+ auto _i = static_cast<pile::size_type>(piles[stock].size() + piles[waste].size() - 1 - i);
+ from = piles[waste][_i];
+ }
+
+ for (auto t : tableau_piles) {
+ bool valid_move = piles[t].empty()
+ ? true
+ : is_next_tableau_card(piles[t].top_card(), from);
+
+ if (valid_move)
+ moves.emplace_back(move::mtype::stock_to_waste, stock, t, i);
+ }
+ }
}
void game_state::add_tableau_moves(std::vector<move>& moves, pile::ref rem_ref) const {