aboutsummaryrefslogtreecommitdiff
path: root/src/main/game
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/game')
-rw-r--r--src/main/game/global_cache.cpp4
-rw-r--r--src/main/game/move.cpp2
-rw-r--r--src/main/game/move.h2
-rw-r--r--src/main/game/search-state/game_state.cpp103
-rw-r--r--src/main/game/search-state/game_state.h41
-rw-r--r--src/main/game/search-state/game_state.legal_moves.cpp317
6 files changed, 264 insertions, 205 deletions
diff --git a/src/main/game/global_cache.cpp b/src/main/game/global_cache.cpp
index f688c33..2cec338 100644
--- a/src/main/game/global_cache.cpp
+++ b/src/main/game/global_cache.cpp
@@ -89,10 +89,10 @@ void cached_game_state::add_card(card c, const game_state& gs) {
target.emplace_back(c);
break;
case pol::RED_BLACK:
- target.emplace_back(c.get_colour(), c.get_rank());
+ target.emplace_back(c.get_colour(), c.get_rank(), c.is_face_down());
break;
default:
- target.emplace_back(0, c.get_rank());
+ target.emplace_back(0, c.get_rank(), c.is_face_down());
break;
}
} else {
diff --git a/src/main/game/move.cpp b/src/main/game/move.cpp
index 9e45c66..915bac6 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_k_plus) assert(i >= 1);
#endif
}
diff --git a/src/main/game/move.h b/src/main/game/move.h
index 8651d1e..5e6892f 100644
--- a/src/main/game/move.h
+++ b/src/main/game/move.h
@@ -8,7 +8,7 @@
#include "pile.h"
struct move {
- enum class mtype : uint8_t {regular, dominance, built_group, stock_to_waste, stock_to_tableau, redeal, null};
+ enum class mtype : uint8_t {regular, dominance, built_group, stock_k_plus, stock_to_all_tableau, null};
explicit move(mtype, pile::ref = 255 , pile::ref = 255, pile::size_type = 1, bool reveal_move_ = false);
friend bool operator==(const move&, const move&);
diff --git a/src/main/game/search-state/game_state.cpp b/src/main/game/search-state/game_state.cpp
index 7870b1b..276d582 100644
--- a/src/main/game/search-state/game_state.cpp
+++ b/src/main/game/search-state/game_state.cpp
@@ -257,14 +257,11 @@ void game_state::make_move(const move m) {
case move::mtype::built_group:
make_built_group_move(m);
break;
- case move::mtype::stock_to_waste:
- make_stock_to_waste_move(m);
+ case move::mtype::stock_k_plus:
+ make_stock_k_plus_move(m);
break;
- case move::mtype::stock_to_tableau:
- make_stock_to_tableau_move(m);
- break;
- case move::mtype::redeal:
- make_redeal_move();
+ case move::mtype::stock_to_all_tableau:
+ make_stock_to_all_tableau_move(m);
break;
case move::mtype::null:
assert(false);
@@ -285,14 +282,11 @@ void game_state::undo_move(const move m) {
case move::mtype::built_group:
undo_built_group_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);
+ case move::mtype::stock_k_plus:
+ undo_stock_k_plus_move(m);
break;
- case move::mtype::redeal:
- undo_redeal_move();
+ case move::mtype::stock_to_all_tableau:
+ undo_stock_to_all_tableau_move(m);
break;
case move::mtype::null:
assert(false);
@@ -382,23 +376,71 @@ void game_state::undo_built_group_move(move m) {
}
}
-void game_state::make_stock_to_waste_move(const move m) {
+void game_state::make_stock_k_plus_move(const move m) {
assert(rules.stock_deal_t == sdt::WASTE);
+ assert(m.count < piles[stock].size() + 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
- 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));
+ }
}
+
+#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);
}
-void game_state::undo_stock_to_waste_move(const move m) {
+void game_state::undo_stock_k_plus_move(move m) {
assert(rules.stock_deal_t == sdt::WASTE);
+ assert(m.count < piles[stock].size() + piles[waste].size() + 1);
+ 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 = 0; i < m.count; i++) {
- place_card(stock, take_card(waste));
+ 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 ));
}
+
+#ifndef NDEBUG
+ auto sz_before = 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);
}
-void game_state::make_stock_to_tableau_move(const move m) {
+void game_state::make_stock_to_all_tableau_move(move m) {
assert(rules.stock_deal_t == sdt::TABLEAU_PILES);
for (pile::ref tab_pr = original_tableau_piles.front();
@@ -408,7 +450,7 @@ void game_state::make_stock_to_tableau_move(const move m) {
}
}
-void game_state::undo_stock_to_tableau_move(const move m) {
+void game_state::undo_stock_to_all_tableau_move(move m) {
assert(rules.stock_deal_t == sdt::TABLEAU_PILES);
for (pile::ref tab_pr = original_tableau_piles.front() + m.count;
@@ -418,23 +460,6 @@ void game_state::undo_stock_to_tableau_move(const move m) {
}
}
-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));
- }
-}
-
// Places a card on a pile and if it is on a tableau, cell or reserve pile,
// reorders the pile refs so that the largest pile is first
void game_state::place_card(pile::ref pr, card c) {
diff --git a/src/main/game/search-state/game_state.h b/src/main/game/search-state/game_state.h
index b8b0d03..78f1fb9 100644
--- a/src/main/game/search-state/game_state.h
+++ b/src/main/game/search-state/game_state.h
@@ -24,6 +24,7 @@ class game_state {
friend class cached_game_state;
friend class deal_parser;
friend class state_printer;
+ friend class test_helper;
public:
/* Constructors */
@@ -73,12 +74,10 @@ private:
void undo_regular_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);
- void undo_stock_to_tableau_move(move move);
- void make_redeal_move();
- void undo_redeal_move();
+ void make_stock_k_plus_move(move move);
+ void undo_stock_k_plus_move(move move);
+ void make_stock_to_all_tableau_move(move move);
+ void undo_stock_to_all_tableau_move(move move);
#ifndef NDEBUG
void check_face_down_consistent() const;
@@ -86,31 +85,33 @@ 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;
+ 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;
+ 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;
void add_foundation_complete_piles_moves(std::vector<move> &) const;
bool is_valid_tableau_move(pile::ref, pile::ref) const;
+ bool is_valid_tableau_move(card, pile::ref) const;
bool is_next_tableau_card(card, card) const;
bool is_valid_foundations_move(pile::ref, pile::ref) const;
+ bool is_valid_foundations_move(card, pile::ref) const;
bool is_valid_hole_move(pile::ref) const;
+ bool is_valid_hole_move(card) const;
+ void add_built_group_moves(std::vector<move>&) const;
+ void add_built_group_moves(std::vector<move>&, pile::ref, pile::size_type) const;
pile::size_type get_built_group_height(pile::ref) const;
- void add_empty_built_group_moves(std::vector<move>&, pile::ref, pile::ref,
- card) const;
bool is_next_built_group_card(card, card) const;
+ void add_empty_built_group_moves(std::vector<move>&, pile::ref, pile::ref, pile::size_type, bool) const;
+ void add_kings_only_built_group_move(std::vector<move>&, pile::ref, pile::ref, pile::size_type, bool) const;
+ void add_non_empty_built_group_move(std::vector<move>&, pile::ref, pile::ref, pile::size_type, bool) const;
bool is_next_legal_card(sol_rules::build_policy, card, card) const;
-
void turn_face_down_cards(std::vector<move>&) const;
/* Auto-foundation moves */
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..5dfa00a 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,16 @@
#include "game_state.h"
#include "../sol_rules.h"
+#include "../move.h"
#include <boost/optional/optional.hpp>
+#include <iostream>
using std::vector;
using std::min;
using std::list;
+using std::pair;
+using std::set;
using namespace rapidjson;
typedef sol_rules::build_policy pol;
@@ -17,7 +21,6 @@ typedef sol_rules::spaces_policy s_pol;
typedef sol_rules::stock_deal_type sdt;
typedef sol_rules::face_up_policy fu;
-
////////////////////////////////
// MAIN LEGAL MOVE GEN CYCLE ///
////////////////////////////////
@@ -26,41 +29,35 @@ typedef sol_rules::face_up_policy fu;
vector<move> game_state::get_legal_moves(move parent_move) {
// Order:
// Foundations complete piles moves
- // Tableau built group moves
- // Tableau / cells / reserve to hole / foundation moves
+ // Tableau / cells / reserve / stock-waste to hole / foundation moves ---
// Cell to tableau moves
// Tableau to tableau moves
- // Waste to tableau moves
+ // Tableau built group moves
+ // Stock-waste to tableau moves
// Reserve to tableau moves
// Foundation to tableau moves
- // Tableau to cell moves
- // Stock to waste moves
- // Stock to tableau / redeal moves
+ // Tableau / stock-waste to cell moves
+ // Stock to all tableau moves
vector<move> moves;
// Stock to tableau / redeal moves
- 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);
- }
-
- // Stock to waste moves
- if (rules.stock_size > 0 && rules.stock_deal_t == sdt::WASTE && !piles[stock].empty())
- add_stock_to_waste_move(moves);
+ if (rules.stock_size > 0 && stock_can_deal_all_tableau())
+ moves.emplace_back(get_stock_to_all_tableau_move());
- // Tableau to cell moves
+ // Tableau / waste to cell moves
pile::ref empty_cell = 255;
for (auto c : cells) {
if (piles[c].empty()) empty_cell = c;
}
if (empty_cell != 255) {
for (auto t : tableau_piles) {
- if (piles[t].empty() || parent_move.to == t || !is_valid_tableau_move(t, empty_cell)) continue;
+ if (piles[t].empty() || parent_move.to == t) continue;
else moves.emplace_back(move::mtype::regular, t, empty_cell);
}
+
+ if (rules.stock_size > 0 && rules.stock_deal_t == sdt::WASTE && !piles[stock].empty())
+ add_stock_to_cell_move(moves, empty_cell);
}
// Foundation to tableau moves
@@ -87,14 +84,12 @@ 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()) {
- for (auto t : tableau_piles) {
- if (is_valid_tableau_move(waste, t)) {
- moves.emplace_back(move::mtype::regular, waste, t);
- }
- }
- }
+ // Stock to tableau moves
+ if (rules.stock_size > 0 && rules.stock_deal_t == sdt::WASTE && !piles[stock].empty())
+ add_stock_to_tableau_moves(moves);
+
+ if (rules.move_built_group)
+ add_built_group_moves(moves);
// Tableau to tableau moves
for (auto t_from : tableau_piles) {
@@ -118,8 +113,12 @@ vector<move> game_state::get_legal_moves(move parent_move) {
}
}
- // Tableau / cells to hole / foundation moves
+ // Tableau / cells / reserve / stock-waste to hole / foundation moves
if (rules.hole || (rules.foundations && !rules.foundations_comp_piles)) {
+ // Stock
+ if (rules.stock_size > 0 && rules.stock_deal_t == sdt::WASTE && !piles[stock].empty())
+ add_stock_to_hole_foundation_moves(moves);
+
list<pile::ref> from_piles = tableau_piles;
if (rules.cells > 0) from_piles.insert(from_piles.end(), cells.begin(), cells.end());
if (rules.reserve_size > 0) from_piles.insert(from_piles.end(), reserve.begin(), reserve.end());
@@ -135,60 +134,11 @@ vector<move> game_state::get_legal_moves(move parent_move) {
}
}
- if (rules.move_built_group)
- add_built_group_moves(moves);
-
if (rules.foundations_comp_piles) // i.e. Spider-type winning condition
add_foundation_complete_piles_moves(moves);
- if (rules.face_up != fu::ALL)
+ if (rules.tableau_pile_count > 0 && rules.face_up != fu::ALL)
turn_face_down_cards(moves);
- /*
-
- vector<move> moves;
-
- // 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 all of the piles from which we want to remove cards
- for (pile::ref rem_ref = 0; rem_ref < piles.size(); rem_ref++) {
- if ((rules.hole && rem_ref == hole) || piles[rem_ref].empty())
- continue;
- if (undoes_prev_move(rem_ref, parent_move))
- continue;
-
- if (rules.foundations && is_foundation(rem_ref)) {
- if (!rules.foundations_removable) continue;
- else if (dominance_blocks_foundation_move(rem_ref)) continue;
- }
-
- if (rules.stock_size > 0 && rem_ref == stock) {
- if (rules.stock_deal_t == sdt::WASTE)
- add_stock_to_waste_move(moves);
- continue;
- }
-
- if (rules.cells > 0)
- add_cell_moves(moves, rem_ref);
- if (rules.tableau_pile_count > 0)
- add_tableau_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);
- }
-
- if (rules.move_built_group)
- add_built_group_moves(moves);
-
- if (rules.foundations_comp_piles) // I.E. Spider-type winning condition
- add_foundation_complete_piles_moves(moves);
-*/
return moves;
}
@@ -198,7 +148,7 @@ vector<move> game_state::get_legal_moves(move parent_move) {
// REGULAR MOVE GEN FUNCTIONS //
////////////////////////////////
-bool game_state::stock_can_deal_tableau() const {
+bool game_state::stock_can_deal_all_tableau() const {
return rules.stock_deal_t == sdt::TABLEAU_PILES
&& !piles[stock].empty();
}
@@ -206,59 +156,104 @@ bool game_state::stock_can_deal_tableau() const {
// 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 {
+move game_state::get_stock_to_all_tableau_move() const {
pile::size_type stock_moves =
piles[stock].size() >= tableau_piles.size()
? pile::size_type(tableau_piles.size())
: piles[stock].size();
assert(stock_moves > 0 && stock_moves <= piles[stock].size());
- return move(move::mtype::stock_to_tableau, 0, 0, stock_moves);
+ return move(move::mtype::stock_to_all_tableau, 0, 0, stock_moves);
}
-bool game_state::stock_can_redeal() const {
- return rules.stock_redeal
- && rules.stock_deal_t == sdt::WASTE
- && piles[stock].empty()
- && !piles[waste].empty();
-}
+set<pile::size_type> game_state::generate_stock_moves_to_check() const {
+ set<pile::size_type> stock_moves_to_check;
-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;
-}
+ // 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);
+ }
-bool game_state::is_foundation(pile::ref ref) const {
- return ref >= foundations.front() && ref <= foundations.back();
+ // 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));
+ }
+ }
+
+ return stock_moves_to_check;
}
-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_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 (!rules.stock_redeal && !piles[waste].empty())
+ moves.emplace_back(move::mtype::regular, waste, empty_cell);
}
-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_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 t : tableau_piles) {
+ if (is_valid_tableau_move(from, t))
+ moves.emplace_back(move::mtype::stock_k_plus, stock, t, i);
}
}
-}
-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);
+ // 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 (/*!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);
}
}
}
-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_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 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 (!rules.stock_redeal && !piles[waste].empty()) {
+ for (auto f : foundations)
+ if (is_valid_foundations_move(waste, f))
+ moves.emplace_back(move::mtype::regular, waste, f);
+
+ if (rules.hole && is_valid_hole_move(waste))
+ moves.emplace_back(move::mtype::regular, waste, hole);
+ }
+}
+
+card game_state::stock_card_from_index(pile::size_type i) const {
+ if (i < piles[stock].size()) {
+ return piles[stock][i];
+ } 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];
}
}
@@ -281,17 +276,21 @@ bool game_state::is_valid_tableau_move(const pile::ref rem_ref,
if (rem_ref == add_ref || rules.build_pol == pol::NO_BUILD)
return false;
+ return is_valid_tableau_move(piles[rem_ref].top_card(), add_ref);
+}
+
+bool game_state::is_valid_tableau_move(const card rem_c,
+ const pile::ref add_ref) const {
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;
+ return rem_c.get_rank() == 13;
default:
return true;
}
} else {
- card rem_c = piles[rem_ref].top_card();
card add_c = piles[add_ref].top_card();
return is_next_tableau_card(add_c, rem_c);
@@ -306,8 +305,11 @@ 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;
- card rem_c = piles[rem_ref].top_card();
+ return is_valid_foundations_move(piles[rem_ref].top_card(), add_ref);
+}
+bool game_state::is_valid_foundations_move(const card rem_c,
+ const pile::ref add_ref) const {
// Checks same suit policy
uint8_t suit_idx = (add_ref - foundations.front()) % uint8_t(4);
if (rem_c.get_suit() != suit_idx)
@@ -322,8 +324,11 @@ bool game_state::is_valid_foundations_move(const pile::ref rem_ref,
bool game_state::is_valid_hole_move(const pile::ref rem_ref) const {
if (rem_ref == hole) return false;
+ return is_valid_hole_move(piles[rem_ref].top_card());
+}
- card::rank_t rank = piles[rem_ref].top_card().get_rank();
+bool game_state::is_valid_hole_move(const card c) const {
+ card::rank_t rank = c.get_rank();
card::rank_t hole_rank = piles[hole].top_card().get_rank();
return rank + 1 == hole_rank
@@ -337,7 +342,7 @@ bool game_state::is_valid_hole_move(const pile::ref rem_ref) const {
// BUILT-GROUP MOVE GEN FUNCTIONS //
////////////////////////////////////
-void game_state::add_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
@@ -347,23 +352,28 @@ void game_state::add_built_group_moves(vector<move> &moves) const {
auto built_group_height = get_built_group_height(rem_ref);
if (built_group_height == 1) continue;
- // 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_high = piles[rem_ref][built_group_height - 1];
-
- if (piles[add_ref].empty()) {
- 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);
- } else {
- if (is_next_built_group_card(piles[add_ref].top_card(), bg_high)) {
- bool is_reveal_move =
- piles[rem_ref].size() > built_group_height
- && piles[rem_ref][built_group_height].is_face_down();
- moves.emplace_back(move::mtype::built_group, rem_ref, add_ref, built_group_height, is_reveal_move);
- }
+ add_built_group_moves(moves, rem_ref, built_group_height);
+ }
+}
+
+void game_state::add_built_group_moves(vector<move>& moves, pile::ref rem_ref, pile::size_type built_group_height) const {
+ // 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_high = piles[rem_ref][built_group_height - 1];
+ bool base_face_down =
+ piles[rem_ref].size() > built_group_height
+ && piles[rem_ref][built_group_height].is_face_down();
+
+ if (piles[add_ref].empty()) {
+ if (rules.spaces_pol == s_pol::ANY) {
+ add_empty_built_group_moves(moves, rem_ref, add_ref, built_group_height, base_face_down);
+ } else if (rules.spaces_pol == s_pol::KINGS && bg_high.get_rank() == 13) {
+ add_kings_only_built_group_move(moves, rem_ref, add_ref, built_group_height, base_face_down);
}
+ } else {
+ add_non_empty_built_group_move(moves, rem_ref, add_ref, built_group_height, base_face_down);
}
}
}
@@ -378,22 +388,45 @@ pile::size_type game_state::get_built_group_height(pile::ref ref) const {
return i;
}
+bool game_state::is_next_built_group_card(card a, card b) const {
+ return is_next_legal_card(rules.built_group_pol, a, b);
+}
+
// 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 {
- 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));
- } while (piles[rem_ref][card_idx++] != bg_high);
-
- if (card_idx < piles[rem_ref].size() && piles[rem_ref][card_idx].is_face_down())
- moves.back().make_reveal_move();
+ pile::size_type built_group_height, bool base_face_down) const {
+
+ for (pile::size_type card_idx = 1; card_idx < built_group_height; card_idx++) {
+ bool is_reveal_move = card_idx + 1 == built_group_height && base_face_down;
+ moves.emplace_back(move::mtype::built_group, rem_ref, add_ref, card_idx + 1, is_reveal_move);
+ }
}
-bool game_state::is_next_built_group_card(card a, card b) const {
- return is_next_legal_card(rules.built_group_pol, a, b);
+void game_state::add_kings_only_built_group_move(vector<move>& moves, pile::ref rem_ref, pile::ref add_ref,
+ pile::size_type built_group_height, bool base_face_down) const {
+
+ moves.emplace_back(move::mtype::built_group, rem_ref, add_ref, built_group_height, base_face_down);
}
+// Loops through each possible built group move to an empty pile and adds it to the list
+void game_state::add_non_empty_built_group_move(vector<move>& moves, pile::ref rem_ref, pile::ref add_ref,
+ pile::size_type built_group_height, bool base_face_down) const {
+
+ // Given rank of add pile, get card of that rank - 1 in rem pile and see if next bg card
+ // For each card going down from top card in rem pile, check if is next bg card
+ for (pile::ref r = 1; r < built_group_height; r++) {
+ if (is_next_built_group_card(piles[add_ref].top_card(), piles[rem_ref][r])) {
+
+ bool is_reveal_move = r + 1 == built_group_height && base_face_down;
+ moves.emplace_back(move::mtype::built_group, rem_ref, add_ref, r + 1, is_reveal_move);
+ break;
+ }
+ }
+}
+
+
+///////////////////////
+
bool game_state::is_next_legal_card(pol p, card a, card b) const {
// Checks build pol violations
switch(p) {