aboutsummaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'src/main')
-rw-r--r--src/main/game/move.cpp21
-rw-r--r--src/main/game/move.h11
-rw-r--r--src/main/game/search-state/game_state.cpp223
-rw-r--r--src/main/game/search-state/game_state.h15
-rw-r--r--src/main/game/search-state/game_state.legal_moves.cpp26
-rw-r--r--src/main/solver/solver.cpp8
6 files changed, 180 insertions, 124 deletions
diff --git a/src/main/game/move.cpp b/src/main/game/move.cpp
index 053b3ba..ff8269c 100644
--- a/src/main/game/move.cpp
+++ b/src/main/game/move.cpp
@@ -4,19 +4,16 @@
#include "move.h"
-const pile::size_type move::dominance_flag = pile::max_size_type;
-
-move::move(pile::ref f, pile::ref t, pile::size_type i) : from(f), to(t), count(i) {
- assert(i >= 1);
-}
-
-// Returns true is the move has been flagged as a special 'dominance' move
-bool move::is_dominance() const {
- return count == dominance_flag;
+move::move(mtype ty, pile::ref f, pile::ref t, pile::size_type i) : type(ty), from(f), to(t), count(i) {
+#ifndef NDEBUG
+ if (ty == mtype::regular) assert(i == 1);
+ else assert(i >= 1);
+#endif
}
bool operator==(const move& a, const move& b) {
- return (a.from == b.from )
+ return (a.from == b.from )
&& (a.to == b.to )
- && (a.count == b.count);
-} \ No newline at end of file
+ && (a.count == b.count)
+ && (a.type == b.type );
+}
diff --git a/src/main/game/move.h b/src/main/game/move.h
index 81a6116..b852811 100644
--- a/src/main/game/move.h
+++ b/src/main/game/move.h
@@ -8,12 +8,15 @@
#include "pile.h"
struct move {
- move(pile::ref , pile::ref, pile::size_type = 1);
- bool is_dominance() const;
+ enum class mtype {regular, dominance, built_group, stock_to_waste, stock_to_tableau, redeal, null};
+
+ explicit move(mtype, pile::ref = 255 , pile::ref = 255, pile::size_type = 1);
friend bool operator==(const move&, const move&);
- const static pile::size_type dominance_flag;
- pile::ref from; pile::ref to; pile::size_type count;
+ mtype type;
+ pile::ref from;
+ pile::ref to;
+ pile::size_type count;
};
diff --git a/src/main/game/search-state/game_state.cpp b/src/main/game/search-state/game_state.cpp
index 42f9b40..ac57142 100644
--- a/src/main/game/search-state/game_state.cpp
+++ b/src/main/game/search-state/game_state.cpp
@@ -227,114 +227,157 @@ vector<card> game_state::gen_shuffled_deck(int seed, card::rank_t max_rank,
////////////////////
void game_state::make_move(const move m) {
- // Handles stock moves
- if (m.from == stock) {
- // Handles special stock-to-tableau-piles move
- if (rules.stock_deal_t == sdt::TABLEAU_PILES) {
- assert(m.from == stock);
- assert(m.to == 255);
-
- for (pile::ref tab_pr = original_tableau_piles.front();
- tab_pr < pile::ref(original_tableau_piles.front() + m.count);
- tab_pr++) {
- place_card(tab_pr, take_card(stock));
- }
- }
- // Handles stock-to-waste moves
- else {
- if (piles[stock].empty()) {
- // Re-deal waste back to stock
- while (!piles[waste].empty()) {
- place_card(stock, take_card(waste));
- }
- } else {
- assert(m.to == waste);
- for (int i = 0; i < m.count; i++) {
- place_card(waste, take_card(stock));
- }
- }
- }
+ switch (m.type) {
+ case move::mtype::regular:
+ case move::mtype::dominance:
+ make_regular_move(m);
+ break;
+ case move::mtype::built_group:
+ make_built_pile_move(m);
+ break;
+ case move::mtype::stock_to_waste:
+ make_stock_to_waste_move(m);
+ break;
+ case move::mtype::stock_to_tableau:
+ make_stock_to_tableau_move(m);
+ break;
+ case move::mtype::redeal:
+ make_redeal_move();
+ break;
+ case move::mtype::null:
+ assert(false);
+ break;
+ }
+}
+
+void game_state::undo_move(const move m) {
+ switch (m.type) {
+ case move::mtype::regular:
+ case move::mtype::dominance:
+ undo_regular_move(m);
+ break;
+ case move::mtype::built_group:
+ undo_built_pile_move(m);
+ break;
+ case move::mtype::stock_to_waste:
+ undo_stock_to_waste_move(m);
+ break;
+ case move::mtype::stock_to_tableau:
+ undo_stock_to_tableau_move(m);
+ break;
+ case move::mtype::redeal:
+ undo_redeal_move();
+ break;
+ case move::mtype::null:
+ assert(false);
+ break;
}
+}
- // If this is not a built-pile move
- else if (m.count == 1 || m.is_dominance()) {
- assert(m.from < piles.size());
- assert(m.to < piles.size());
+void game_state::make_regular_move(const move m) {
+ assert(m.from < piles.size());
+ assert(m.to < piles.size());
+
+ place_card(m.to, take_card(m.from));
- place_card(m.to, take_card(m.from));
#ifndef NO_AUTO_FOUNDATIONS
- update_auto_foundation_moves(m.to);
+ update_auto_foundation_moves(m.to);
#endif
+}
+
+void game_state::undo_regular_move(const move m) {
+ assert(m.to < piles.size());
+
+ place_card(m.from, take_card(m.to));
+
+#ifndef NO_AUTO_FOUNDATIONS
+ update_auto_foundation_moves(m.from);
+#endif
+}
+
+void game_state::make_built_pile_move(const move m) {
+ assert(rules.move_built_group);
+ assert(m.from < piles.size() );
+ assert(m.to < piles.size() );
+ assert(m.count <= rules.max_rank);
+
+ // Adds the cards to the 'to' pile
+ for (auto pile_idx = m.count; pile_idx-- > 0;) {
+ place_card(m.to, piles[m.from][pile_idx]);
}
- // If this is a built-pile move
- else {
- assert(m.from < piles.size() );
- assert(m.to < piles.size() );
- assert(m.count <= rules.max_rank);
+ // Removes the cards from the 'from' pile
+ for (uint8_t rem_count = 0; rem_count < m.count; rem_count++) {
+ take_card(m.from);
+ }
+}
- // Adds the cards to the 'to' pile
- for (auto pile_idx = m.count; pile_idx-- > 0;) {
- place_card(m.to, piles[m.from][pile_idx]);
- }
+void game_state::undo_built_pile_move(const move m) {
+ assert(rules.move_built_group);
+ assert(m.to < piles.size());
+ assert(m.count <= rules.max_rank);
- // Removes the cards from the 'from' pile
- for (uint8_t rem_count = 0; rem_count < m.count; rem_count++) {
- take_card(m.from);
- }
+ // Adds the cards to the 'from' pile
+ for (auto pile_idx = m.count; pile_idx-- > 0;) {
+ place_card(m.from, piles[m.to][pile_idx]);
+ }
+
+ // Removes the cards from the 'to' pile
+ for (uint8_t rem_count = 0; rem_count < m.count; rem_count++) {
+ take_card(m.to);
}
}
-void game_state::undo_move(const move m) {
- assert(m.from < piles.size());
+void game_state::make_stock_to_waste_move(const move m) {
+ assert(rules.stock_deal_t == sdt::WASTE);
- // Handles stock moves
- if (m.from == stock) {
- // Handles special stock-to-tableau-piles move
- if (rules.stock_deal_t == sdt::TABLEAU_PILES) {
- for (pile::ref tab_pr = original_tableau_piles.front() + m.count;
- tab_pr-- > original_tableau_piles.front();
- ) {
- place_card(stock, take_card(tab_pr));
- }
- }
- // Handles stock-to-waste moves
- else {
- if (piles[waste].empty()) {
- // Re-deal waste back to stock
- while (!piles[stock].empty()) {
- 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 < m.count; i++) {
+ place_card(waste, take_card(stock));
}
+}
- // If this is not a built-pile move
- else if (m.count == 1 || m.is_dominance()) {
- assert(m.to < piles.size());
- place_card(m.from, take_card(m.to));
-#ifndef NO_AUTO_FOUNDATIONS
- update_auto_foundation_moves(m.from);
-#endif
+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));
}
+}
- // If this is a built-pile move
- else {
- assert(m.to < piles.size());
- assert(m.count <= rules.max_rank);
- // Adds the cards to the 'from' pile
- for (auto pile_idx = m.count; pile_idx-- > 0;) {
- place_card(m.from, piles[m.to][pile_idx]);
- }
+void game_state::make_stock_to_tableau_move(const move m) {
+ assert(rules.stock_deal_t == sdt::TABLEAU_PILES);
- // Removes the cards from the 'to' pile
- for (uint8_t rem_count = 0; rem_count < m.count; rem_count++) {
- take_card(m.to);
- }
+ for (pile::ref tab_pr = original_tableau_piles.front();
+ tab_pr < pile::ref(original_tableau_piles.front() + m.count);
+ tab_pr++) {
+ place_card(tab_pr, take_card(stock));
+ }
+}
+
+void game_state::undo_stock_to_tableau_move(const move m) {
+ assert(rules.stock_deal_t == sdt::TABLEAU_PILES);
+
+ for (pile::ref tab_pr = original_tableau_piles.front() + m.count;
+ tab_pr-- > original_tableau_piles.front();
+ ) {
+ place_card(stock, take_card(tab_pr));
+ }
+}
+
+void game_state::make_redeal_move() {
+ assert(piles[stock].empty());
+
+ // Re-deal waste back to stock
+ while (!piles[waste].empty()) {
+ place_card(stock, take_card(waste));
+ }
+}
+
+void game_state::undo_redeal_move() {
+ assert(piles[waste].empty());
+
+ while (!piles[stock].empty()) {
+ place_card(waste, take_card(stock));
}
}
diff --git a/src/main/game/search-state/game_state.h b/src/main/game/search-state/game_state.h
index 3c6b192..be8b79c 100644
--- a/src/main/game/search-state/game_state.h
+++ b/src/main/game/search-state/game_state.h
@@ -43,7 +43,7 @@ public:
/* Legal move generation */
- std::vector<move> get_legal_moves(move = move(255, 255, 255));
+ std::vector<move> get_legal_moves(move = move(move::mtype::regular));
boost::optional<move> get_dominance_move() const;
/* State inspection */
@@ -66,6 +66,19 @@ private:
void eval_pile_order(pile::ref, bool);
void eval_pile_order(std::list<pile::ref>&, pile::ref, bool);
+ /* Altering state */
+
+ 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_stock_to_waste_move(move move);
+ void undo_stock_to_waste_move(move move);
+ void make_stock_to_tableau_move(move move);
+ void undo_stock_to_tableau_move(move move);
+ void make_redeal_move();
+ void undo_redeal_move();
+
/* Legal move generation */
move get_stock_tableau_move() 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 b6e110d..3fb0156 100644
--- a/src/main/game/search-state/game_state.legal_moves.cpp
+++ b/src/main/game/search-state/game_state.legal_moves.cpp
@@ -34,7 +34,7 @@ vector<move> game_state::get_legal_moves(move parent_move) {
&& rules.stock_deal_t == sdt::WASTE
&& piles[stock].empty()
&& !piles[waste].empty()) {
- moves.emplace_back(stock, waste);
+ moves.emplace_back(move::mtype::redeal);
}
// Cycles through each pile which we may be able to remove a card from
@@ -44,7 +44,7 @@ vector<move> game_state::get_legal_moves(move parent_move) {
if ((rules.hole && rem_ref == hole)
|| piles[rem_ref].empty()
|| (rem_ref == parent_move.to
- && parent_move.count <= 1
+ && parent_move.count == 1
&& parent_move.from != stock)
) continue;
@@ -69,7 +69,7 @@ vector<move> game_state::get_legal_moves(move parent_move) {
if (rules.stock_deal_t == sdt::WASTE) {
uint8_t cards_to_move =
min(rules.stock_deal_count, piles[stock].size());
- moves.emplace_back(stock, waste, cards_to_move);
+ moves.emplace_back(move::mtype::stock_to_waste, stock, waste, cards_to_move);
}
continue;
}
@@ -78,14 +78,14 @@ vector<move> game_state::get_legal_moves(move parent_move) {
// tableau move according to the rules
for (auto add_ref : tableau_piles) {
if (is_valid_tableau_move(rem_ref, add_ref)) {
- moves.emplace_back(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(rem_ref, add_ref);
+ moves.emplace_back(move::mtype::regular, rem_ref, add_ref);
}
}
@@ -93,13 +93,13 @@ vector<move> game_state::get_legal_moves(move parent_move) {
// move according to the rules
for (auto add_ref : foundations) {
if (is_valid_foundations_move(rem_ref, add_ref)) {
- moves.emplace_back(rem_ref, add_ref);
+ moves.emplace_back(move::mtype::regular, rem_ref, add_ref);
}
}
// Does the same for the hole
if (rules.hole && is_valid_hole_move(rem_ref)) {
- moves.emplace_back(rem_ref, hole);
+ moves.emplace_back(move::mtype::regular, rem_ref, hole);
}
// Note that the stock and the reserve aren't mentioned here, as
@@ -117,7 +117,7 @@ vector<move> game_state::get_legal_moves(move parent_move) {
for (pile::ref found_pr : foundations) {
if (piles[found_pr].empty()) {
- moves.emplace_back(tab_pr, found_pr, rules.max_rank);
+ moves.emplace_back(move::mtype::built_group, tab_pr, found_pr, rules.max_rank);
break;
}
}
@@ -130,7 +130,7 @@ vector<move> game_state::get_legal_moves(move parent_move) {
}
// This is a special kind of move which the solver handles differently. Hence
-// the arbitrary second param. We must supply the number of stock cards
+// 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
move game_state::get_stock_tableau_move() const {
pile::size_type stock_moves =
@@ -139,7 +139,7 @@ move game_state::get_stock_tableau_move() const {
: piles[stock].size();
assert(stock_moves > 0 && stock_moves <= piles[stock].size());
- return {stock, 255, stock_moves};
+ return move(move::mtype::stock_to_tableau, 0, 0, stock_moves);
}
// If a tableau pile is empty and the space policy is
@@ -250,7 +250,7 @@ void game_state::get_built_group_moves(vector<move>& moves) const {
// 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)) {
- moves.emplace_back(rem_ref, add_ref, built_group_height);
+ moves.emplace_back(move::mtype::built_group, rem_ref, add_ref, built_group_height);
}
}
}
@@ -317,7 +317,7 @@ void game_state::add_empty_built_group_moves(vector<move>& moves,
pile::size_type card_idx = 1;
do {
moves.emplace_back(
- rem_ref, add_ref, static_cast<pile::size_type>(card_idx + 1)
+ move::mtype::built_group, rem_ref, add_ref, static_cast<pile::size_type>(card_idx + 1)
);
} while (piles[rem_ref][card_idx++] != bg_high);
}
@@ -355,7 +355,7 @@ boost::optional<move> game_state::get_dominance_move() const {
+ card::rank_t(1);
if (target_rank == c.get_rank() &&
auto_foundation_moves[c.get_suit()]) {
- return move(pr, target_foundation, move::dominance_flag);
+ return move(move::mtype::dominance, pr, target_foundation);
}
}
#endif
diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp
index b104da3..7d490be 100644
--- a/src/main/solver/solver.cpp
+++ b/src/main/solver/solver.cpp
@@ -26,7 +26,7 @@ solver::solver(const game_state& gs)
, backtracks(0)
, dominance_moves(0)
, depth(0)
- , root(nullptr, move(255, 255, 255))
+ , root(nullptr, move(move::mtype::null))
, current_node(&root) {
}
@@ -53,7 +53,7 @@ solver::sol_state solver::run(boost::optional<atomic<bool> &> terminate_solver)
}
#ifndef NDEBUG
- if (current_node->mv.is_dominance()) {
+ if (current_node->mv.type == move::mtype::dominance) {
LOG_DEBUG("(dominance move)");
}
LOG_DEBUG(state);
@@ -92,7 +92,7 @@ solver::sol_state solver::run(boost::optional<atomic<bool> &> terminate_solver)
current_node = &current_node->children.back();
state.make_move(current_node->mv);
depth++;
- if (current_node->mv.is_dominance()) dominance_moves++;
+ if (current_node->mv.type == move::mtype::dominance) dominance_moves++;
}
states_searched++;
@@ -137,7 +137,7 @@ bool solver::revert_to_last_node_with_children() {
// Checks that the state after the undo is in the cache
// (as long as the move wasn't a dominance move)
- if (!current_node->mv.is_dominance()) {
+ if (current_node->mv.type != move::mtype::dominance) {
assert(cache.contains(state));
LOG_DEBUG("(undo move)");
} else {