aboutsummaryrefslogtreecommitdiff
path: root/src/main/game
diff options
context:
space:
mode:
authorIan Gent2018-06-26 16:43:45 +0100
committerIan Gent2018-06-26 16:43:45 +0100
commit0798cfe1ecb92996ed7959383dcc4b8b0d51a1d9 (patch)
treef6f6dbd927012de317feea6b2bb25d466cff1ac3 /src/main/game
parentfirst R file (diff)
parentMerge pull request #71 from thecharlesblake/correct-klondike (diff)
downloadmetasol-0798cfe1ecb92996ed7959383dcc4b8b0d51a1d9.tar.gz
metasol-0798cfe1ecb92996ed7959383dcc4b8b0d51a1d9.zip

Merge branch ‘master’ of github.com:thecharlesblake/Solvitaire into ipg

Diffstat (limited to 'src/main/game')
-rw-r--r--src/main/game/card.cpp36
-rw-r--r--src/main/game/card.h12
-rw-r--r--src/main/game/global_cache.cpp16
-rw-r--r--src/main/game/move.cpp10
-rw-r--r--src/main/game/move.h5
-rw-r--r--src/main/game/search-state/game_state.auto_foundation_moves.cpp14
-rw-r--r--src/main/game/search-state/game_state.cpp137
-rw-r--r--src/main/game/search-state/game_state.h16
-rw-r--r--src/main/game/search-state/game_state.legal_moves.cpp153
-rw-r--r--src/main/game/sol_rules.h6
10 files changed, 335 insertions, 70 deletions
diff --git a/src/main/game/card.cpp b/src/main/game/card.cpp
index cfb94a9..60aaa74 100644
--- a/src/main/game/card.cpp
+++ b/src/main/game/card.cpp
@@ -22,11 +22,13 @@ const card::colour_t card::colour::Red = 1;
// A card can either be created using two int values (rank & suit), or
// using a string (e.g. "AS")
-card::card(suit_t s, rank_t r) : card_rank(r), card_suit(s) {}
+card::card(suit_t s, rank_t r, bool fd) : card_rank(r), card_suit(s), face_down(fd) {}
-card::card(const char* c) : card_rank(rank_from_str(c)), card_suit(suit_from_str(c)) {}
+card::card(const char* c, bool face_down_possible) :
+ card_rank(rank_from_str(c)), card_suit(suit_from_str(c)),
+ face_down(face_down_from_str(c, face_down_possible)) {}
-card::card() : card(0, 0) {}
+card::card() : card(0, 0, false) {}
card::suit_t card::suit_from_str(const char* c) {
switch(tolower(c[strlen(c) - 1])) {
@@ -51,6 +53,18 @@ card::rank_t card::rank_from_str(const char* c) {
}
}
+bool card::face_down_from_str(const char *c, bool possible) {
+ if (!possible) return false;
+
+ switch(c[strlen(c) - 1]) {
+ case 'c': return true;
+ case 'h': return true;
+ case 's': return true;
+ case 'd': return true;
+ default: return false;
+ }
+}
+
card::suit_t card::get_suit() const {
return card_suit;
}
@@ -68,6 +82,8 @@ card::rank_t card::get_rank() const {
}
std::string card::to_string() const {
+ if (face_down) return "##";
+
string s;
switch(card_rank) {
case 1 : s += "A"; break;
@@ -85,7 +101,19 @@ std::string card::to_string() const {
return s;
}
-const card card::divider = card(0, 0);
+bool card::is_face_down() const {
+ return face_down;
+}
+
+void card::turn_face_up() {
+ face_down = false;
+}
+
+void card::turn_face_down() {
+ face_down = true;
+}
+
+const card card::divider = card();
bool operator==(const card& l, const card& r) {
return l.get_rank() == r.get_rank() && l.get_suit() == r.get_suit();
diff --git a/src/main/game/card.h b/src/main/game/card.h
index a4c6b1e..7885d66 100644
--- a/src/main/game/card.h
+++ b/src/main/game/card.h
@@ -31,22 +31,28 @@ public:
// A card can either be created using two int values (rank & suit), or
// using a string (e.g. "AS")
- card(suit_t, rank_t);
- card(const char*);
+ card(suit_t, rank_t, bool face_down=false);
+ card(const char*, bool face_down_possible=false);
card();
suit_t get_suit() const;
colour_t get_colour() const;
rank_t get_rank() const;
+ bool is_face_down() const;
+ void turn_face_up();
+ void turn_face_down();
+
std::string to_string() const;
private:
static suit_t suit_from_str(const char* c);
static rank_t rank_from_str(const char* c);
+ bool face_down_from_str(const char *c, bool possible);
rank_t card_rank : 4;
- suit_t card_suit : 4;
+ suit_t card_suit : 2;
+ bool face_down : 1;
};
bool operator==(const card&, const card&);
diff --git a/src/main/game/global_cache.cpp b/src/main/game/global_cache.cpp
index db0c6de..f688c33 100644
--- a/src/main/game/global_cache.cpp
+++ b/src/main/game/global_cache.cpp
@@ -81,13 +81,7 @@ void cached_game_state::add_card(card c, const game_state& gs) {
// If the game is a 'hole-based' game, or suit-reduction is on, reduces
// the cached suit of the card where possible
- bool is_suit_symmetry = gs.rules.hole;
-#ifdef SUIT_REDUCTION_STREAMLINER
- is_suit_symmetry = true;
-#endif
-#ifdef NO_SUIT_SYMMETRY
- is_suit_symmetry = false;
-#endif
+ bool is_suit_symmetry = gs.streamliners || gs.rules.hole;
if (is_suit_symmetry) {
switch (gs.rules.build_pol) {
@@ -141,13 +135,7 @@ size_t hasher::hash_value(card const& c) const {
// If the game is a 'hole-based' game, or suit-reduction is enabled, hash
// the reduced suit
- bool is_suit_symmetry = init_gs.rules.hole;
-#ifdef SUIT_REDUCTION_STREAMLINER
- is_suit_symmetry = true;
-#endif
-#ifdef NO_SUIT_SYMMETRY
- is_suit_symmetry = false;
-#endif
+ bool is_suit_symmetry = init_gs.streamliners || init_gs.rules.hole;
uint8_t suit_val;
if (is_suit_symmetry) {
diff --git a/src/main/game/move.cpp b/src/main/game/move.cpp
index ff8269c..9e45c66 100644
--- a/src/main/game/move.cpp
+++ b/src/main/game/move.cpp
@@ -4,16 +4,22 @@
#include "move.h"
-move::move(mtype ty, pile::ref f, pile::ref t, pile::size_type i) : type(ty), from(f), to(t), count(i) {
+move::move(mtype ty, pile::ref f, pile::ref t, pile::size_type i, bool reveal_move_) :
+ 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);
#endif
}
+void move::make_reveal_move() {
+ reveal_move = true;
+}
+
bool operator==(const move& a, const move& b) {
return (a.from == b.from )
&& (a.to == b.to )
&& (a.count == b.count)
- && (a.type == b.type );
+ && (a.type == b.type )
+ && (a.reveal_move == b.reveal_move);
}
diff --git a/src/main/game/move.h b/src/main/game/move.h
index 1e3a9d1..8651d1e 100644
--- a/src/main/game/move.h
+++ b/src/main/game/move.h
@@ -10,13 +10,16 @@
struct move {
enum class mtype : uint8_t {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);
+ explicit move(mtype, pile::ref = 255 , pile::ref = 255, pile::size_type = 1, bool reveal_move_ = false);
friend bool operator==(const move&, const move&);
+ void make_reveal_move();
+
mtype type;
pile::ref from;
pile::ref to;
pile::size_type count;
+ bool reveal_move;
};
#endif //SOLVITAIRE_MOVE_H
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 0b62af4..99b43a2 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
@@ -11,9 +11,9 @@ using std::max;
bool game_state::is_valid_auto_foundation_move(pile::ref target_pile) const {
-#ifndef AUTO_FOUNDATIONS_STREAMLINER
- if (rules.build_pol == pol::NO_BUILD || rules.build_pol == pol::SAME_SUIT)
-#endif
+ if (rules.foundations_comp_piles)
+ return false;
+ else if (streamliners || rules.build_pol == pol::NO_BUILD || rules.build_pol == pol::SAME_SUIT)
return true;
card::suit_t target_suit(target_pile - foundations.front());
@@ -86,7 +86,13 @@ 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(move::mtype::dominance, pr, target_foundation);
+ move m(move::mtype::dominance, pr, target_foundation);
+
+ // If this dominance move reveals a card, adds this to the move
+ if (piles[pr].size() > 1 && piles[pr][1].is_face_down())
+ m.make_reveal_move();
+
+ return m;
}
}
#endif
diff --git a/src/main/game/search-state/game_state.cpp b/src/main/game/search-state/game_state.cpp
index 0a20f60..7870b1b 100644
--- a/src/main/game/search-state/game_state.cpp
+++ b/src/main/game/search-state/game_state.cpp
@@ -10,6 +10,7 @@
#include <boost/functional/hash.hpp>
#include <boost/optional.hpp>
+#include <boost/random.hpp>
#include "game_state.h"
#include "document.h"
@@ -17,6 +18,7 @@
#include "../../input-output/output/state_printer.h"
#include "../../input-output/output/log_helper.h"
#include "../move.h"
+#include "../sol_rules.h"
using namespace rapidjson;
using std::vector;
@@ -27,12 +29,14 @@ using std::end;
using std::rbegin;
using std::rend;
using std::runtime_error;
-using std::default_random_engine;
using std::max;
using std::ostream;
+using std::mt19937;
+using std::string;
typedef sol_rules::build_policy pol;
typedef sol_rules::stock_deal_type sdt;
+typedef sol_rules::face_up_policy fu;
//////////////////
// CONSTRUCTORS //
@@ -40,10 +44,12 @@ typedef sol_rules::stock_deal_type sdt;
// A private constructor used by both of the public ones. Initializes all of the
// piles and pile refs specified by the rules
-game_state::game_state(const sol_rules& s_rules) : rules(s_rules),
- stock(255),
- waste(255),
- hole(255) {
+game_state::game_state(const sol_rules& s_rules, bool streamliners_)
+ : rules(s_rules)
+ , streamliners(streamliners_)
+ , stock(255)
+ , waste(255)
+ , hole (255) {
// If there is a hole, creates pile
if (rules.hole) {
piles.emplace_back();
@@ -100,14 +106,14 @@ game_state::game_state(const sol_rules& s_rules) : rules(s_rules),
}
// Constructs an initial game state from a JSON doc
-game_state::game_state(const sol_rules& s_rules, const Document& doc)
- : game_state(s_rules) {
+game_state::game_state(const sol_rules& s_rules, const Document& doc, bool streamliners_)
+ : game_state(s_rules, streamliners_) {
deal_parser::parse(*this, doc);
}
// Constructs an initial game state from a seed
-game_state::game_state(const sol_rules& s_rules, int seed)
- : game_state(s_rules) {
+game_state::game_state(const sol_rules& s_rules, int seed, bool streamliners_)
+ : game_state(s_rules, streamliners_) {
vector<card> deck = gen_shuffled_deck(seed, rules.max_rank, rules.two_decks);
// If there is a hole, moves the ace of spades to it
@@ -152,7 +158,10 @@ game_state::game_state(const sol_rules& s_rules, int seed)
for (int t = 0; !deck.empty(); t++) {
card c = deck.back();
- // Add the randomly generated card to the tableau piles
+ // If only the top cards are face up, initially deals all face down
+ if (rules.face_up == fu::TOP_CARDS) c.turn_face_down();
+
+ // Adds the randomly generated card to the tableau piles
auto p = t % original_tableau_piles.size();
// If we are doing a diagonal deal, each row should have one fewer card.
@@ -173,6 +182,12 @@ game_state::game_state(const sol_rules& s_rules, int seed)
}
}
+ // Now if necessary, turns the top cards face up
+ if (rules.face_up == fu::TOP_CARDS)
+ for (auto& p : piles)
+ if (!p.empty())
+ p[0].turn_face_up();
+
// The size of all piles must equal the deck size
int piles_sz = 0;
for (auto& p : piles) piles_sz += p.size();
@@ -182,11 +197,11 @@ game_state::game_state(const sol_rules& s_rules, int seed)
}
game_state::game_state(const sol_rules& s_rules,
- std::initializer_list<pile> il)
- : game_state(s_rules) {
- pile::ref pr = 0;
- for (const pile& p : il) {
- for (const card c : p.pile_vec) {
+ std::initializer_list<std::initializer_list<string>> il)
+ : game_state(s_rules, false) {pile::ref pr = 0;
+ for (auto& p_il : il) {
+ for (const string& card_str : p_il) {
+ card c(card_str.c_str(), s_rules.face_up == fu::TOP_CARDS);
place_card(pr, c);
}
pr++;
@@ -196,31 +211,38 @@ game_state::game_state(const sol_rules& s_rules,
// Generates a randomly ordered vector of cards
vector<card> game_state::gen_shuffled_deck(int seed, card::rank_t max_rank,
bool two_decks) {
- vector<uint8_t> values;
- vector<uint8_t*> v_ptrs;
- for (int i = 0; i < max_rank * 4; i++) {
- values.emplace_back(i);
- if (two_decks) values.emplace_back(i);
- }
- for (auto& v : values) {
- v_ptrs.emplace_back(&v);
- }
-
- // Randomly shuffle the pointers
- auto rng = default_random_engine(seed+1);
- shuffle(begin(v_ptrs), end(v_ptrs), rng);
-
vector<card> deck;
- for (uint8_t *i : v_ptrs) {
- auto r = static_cast<card::rank_t>(((*i) % max_rank) + 1);
- card::suit_t s = (*i) / max_rank;
- deck.emplace_back(card(s, r));
+
+ for (int deck_count = 1; deck_count <= (two_decks ? 2 : 1); deck_count++) {
+ for (card::rank_t rank = 1; rank <= max_rank; rank++) {
+ for (card::suit_t suit = 0 ; suit < 4; suit++) {
+ deck.emplace_back(suit, rank);
+ }
+ }
}
assert(deck.size() == pile::size_type(max_rank * (two_decks ? 8 : 4)));
+
+ auto rng = mt19937(seed);
+ game_state::shuffle(begin(deck), end(deck), rng);
return deck;
}
+template<class RandomIt, class URBG>
+void game_state::shuffle(RandomIt first, RandomIt last, URBG&& g) {
+ typedef typename std::iterator_traits<RandomIt>::difference_type diff_t;
+ typedef boost::random::uniform_int_distribution<diff_t> distr_t;
+ typedef typename distr_t::param_type param_t;
+
+ distr_t D;
+ diff_t n = last - first;
+ for (diff_t i = n-1; i > 0; --i) {
+ using std::swap;
+ swap(first[i], first[D(g, param_t(0, i))]);
+ }
+}
+
+
////////////////////
// ALTERING STATE //
@@ -248,6 +270,10 @@ void game_state::make_move(const move m) {
assert(false);
break;
}
+
+#ifndef NDEBUG
+ check_face_down_consistent();
+#endif
}
void game_state::undo_move(const move m) {
@@ -272,6 +298,10 @@ void game_state::undo_move(const move m) {
assert(false);
break;
}
+
+#ifndef NDEBUG
+ check_face_down_consistent();
+#endif
}
void game_state::make_regular_move(const move m) {
@@ -280,6 +310,12 @@ void game_state::make_regular_move(const move m) {
place_card(m.to, take_card(m.from));
+ if (m.reveal_move) {
+ assert(!piles[m.from].empty());
+ assert(piles[m.from][0].is_face_down());
+ piles[m.from][0].turn_face_up();
+ }
+
#ifndef NO_AUTO_FOUNDATIONS
update_auto_foundation_moves(m.to);
#endif
@@ -288,6 +324,12 @@ void game_state::make_regular_move(const move m) {
void game_state::undo_regular_move(const move m) {
assert(m.to < piles.size());
+ if (m.reveal_move) {
+ assert(!piles[m.from].empty());
+ assert(!piles[m.from][0].is_face_down());
+ piles[m.from][0].turn_face_down();
+ }
+
place_card(m.from, take_card(m.to));
#ifndef NO_AUTO_FOUNDATIONS
@@ -310,6 +352,12 @@ void game_state::make_built_group_move(move m) {
for (uint8_t rem_count = 0; rem_count < m.count; rem_count++) {
take_card(m.from);
}
+
+ if (m.reveal_move) {
+ assert(!piles[m.from].empty());
+ assert(piles[m.from][0].is_face_down());
+ piles[m.from][0].turn_face_up();
+ }
}
void game_state::undo_built_group_move(move m) {
@@ -317,6 +365,12 @@ void game_state::undo_built_group_move(move m) {
assert(m.to < piles.size());
assert(m.count <= rules.max_rank);
+ if (m.reveal_move) {
+ assert(!piles[m.from].empty());
+ assert(!piles[m.from][0].is_face_down());
+ piles[m.from][0].turn_face_down();
+ }
+
// 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]);
@@ -406,6 +460,23 @@ card game_state::take_card(pile::ref pr) {
return c;
}
+#ifndef NDEBUG
+void game_state::check_face_down_consistent() const {
+ for (auto& p : original_tableau_piles) {
+ if (piles[p].empty()) continue;
+ // Makes sure the top cards of each tableau pile are face up
+ assert(!piles[p].top_card().is_face_down());
+
+ // Makes sure face down cards are never above face down ones
+ bool seen_face_up = false;
+ for (auto& c : piles[p].pile_vec) {
+ seen_face_up = seen_face_up || !c.is_face_down();
+ assert(!(c.is_face_down() && seen_face_up));
+ }
+ }
+}
+#endif
+
////////////////////////
// INSPECT GAME STATE //
diff --git a/src/main/game/search-state/game_state.h b/src/main/game/search-state/game_state.h
index 7c0f9ca..b8b0d03 100644
--- a/src/main/game/search-state/game_state.h
+++ b/src/main/game/search-state/game_state.h
@@ -28,11 +28,11 @@ public:
/* Constructors */
// Creates a game state representation from a JSON doc
- game_state(const sol_rules&, const rapidjson::Document&);
+ game_state(const sol_rules&, const rapidjson::Document&, bool);
// Does the same from a seed
- game_state(const sol_rules&, int seed);
+ game_state(const sol_rules&, int seed, bool);
// Does the same but with an initialiser list (useful for testing)
- game_state(const sol_rules&, std::initializer_list<pile>);
+ game_state(const sol_rules&, std::initializer_list<std::initializer_list<std::string>>);
/* Altering state */
@@ -58,8 +58,9 @@ public:
private:
/* Constructors (& helper function) */
- explicit game_state(const sol_rules&);
+ explicit game_state(const sol_rules&, bool);
static std::vector<card> gen_shuffled_deck(int, card::rank_t, bool);
+ template<class RandomIt, class URBG> static void shuffle(RandomIt, RandomIt, URBG&&);
/* Pile order logic */
@@ -79,6 +80,10 @@ private:
void make_redeal_move();
void undo_redeal_move();
+#ifndef NDEBUG
+ void check_face_down_consistent() const;
+#endif
+
/* Legal move generation */
bool stock_can_deal_tableau() const;
@@ -106,6 +111,8 @@ private:
bool is_next_legal_card(sol_rules::build_policy, card, card) const;
+ void turn_face_down_cards(std::vector<move>&) const;
+
/* Auto-foundation moves */
bool is_valid_auto_foundation_move(pile::ref) const;
@@ -116,6 +123,7 @@ private:
/* Game rules */
const sol_rules rules;
+ bool streamliners;
/* Pile references */
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 f9fc941..9ad8b1d 100644
--- a/src/main/game/search-state/game_state.legal_moves.cpp
+++ b/src/main/game/search-state/game_state.legal_moves.cpp
@@ -3,16 +3,19 @@
//
#include "game_state.h"
+#include "../sol_rules.h"
#include <boost/optional/optional.hpp>
using std::vector;
using std::min;
+using std::list;
using namespace rapidjson;
typedef sol_rules::build_policy pol;
typedef sol_rules::spaces_policy s_pol;
typedef sol_rules::stock_deal_type sdt;
+typedef sol_rules::face_up_policy fu;
////////////////////////////////
@@ -21,6 +24,127 @@ typedef sol_rules::stock_deal_type sdt;
// Note that the moves added last here, are tried first
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
+ // Cell to tableau moves
+ // Tableau to tableau moves
+ // 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
+
+ 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);
+
+ // Tableau 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;
+ else moves.emplace_back(move::mtype::regular, t, empty_cell);
+ }
+ }
+
+ // Foundation to tableau moves
+ if (rules.foundations_removable) {
+ for (auto f : foundations) {
+ if (piles[f].empty() || parent_move.to == f || dominance_blocks_foundation_move(f)) continue;
+
+ for (auto t : tableau_piles) {
+ if (is_valid_tableau_move(f, t)) {
+ moves.emplace_back(move::mtype::regular, f, t);
+ }
+ }
+ }
+ }
+
+ // Reserve to tableau moves
+ for (auto r : reserve) {
+ if (piles[r].empty()) continue;
+
+ for (auto t : tableau_piles) {
+ if (is_valid_tableau_move(r, t)) {
+ moves.emplace_back(move::mtype::regular, r, t);
+ }
+ }
+ }
+
+ // 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);
+ }
+ }
+ }
+
+ // Tableau to tableau moves
+ for (auto t_from : tableau_piles) {
+ if (piles[t_from].empty() || parent_move.to == t_from) continue;
+
+ for (auto t_to : tableau_piles) {
+ if (is_valid_tableau_move(t_from, t_to)) {
+ moves.emplace_back(move::mtype::regular, t_from, t_to);
+ }
+ }
+ }
+
+ // Cell to tableau moves
+ for (auto c : cells) {
+ if (piles[c].empty() || parent_move.to == c) continue;
+
+ for (auto t : tableau_piles) {
+ if (is_valid_tableau_move(c, t)) {
+ moves.emplace_back(move::mtype::regular, c, t);
+ }
+ }
+ }
+
+ // Tableau / cells to hole / foundation moves
+ if (rules.hole || (rules.foundations && !rules.foundations_comp_piles)) {
+ 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());
+
+ for (auto fp : from_piles) {
+ if (piles[fp].empty() || parent_move.to == fp) continue;
+
+ for (auto f : foundations)
+ if (is_valid_foundations_move(fp, f))
+ moves.emplace_back(move::mtype::regular, fp, f);
+ if (rules.hole && is_valid_hole_move(fp))
+ moves.emplace_back(move::mtype::regular, fp, 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);
+
+ if (rules.face_up != fu::ALL)
+ turn_face_down_cards(moves);
+ /*
+
vector<move> moves;
// Attempts stock tableau deal and redeal
@@ -49,10 +173,10 @@ vector<move> game_state::get_legal_moves(move parent_move) {
continue;
}
- if (rules.tableau_pile_count > 0)
- add_tableau_moves(moves, rem_ref);
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))
@@ -64,6 +188,7 @@ vector<move> game_state::get_legal_moves(move parent_move) {
if (rules.foundations_comp_piles) // I.E. Spider-type winning condition
add_foundation_complete_piles_moves(moves);
+*/
return moves;
}
@@ -232,8 +357,12 @@ void game_state::add_built_group_moves(vector<move> &moves) const {
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))
- moves.emplace_back(move::mtype::built_group, rem_ref, add_ref, built_group_height);
+ 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);
+ }
}
}
}
@@ -242,7 +371,9 @@ void game_state::add_built_group_moves(vector<move> &moves) const {
// Finds the size of the built group at the top of a pile
pile::size_type game_state::get_built_group_height(pile::ref ref) const {
pile::size_type i = 1;
- while (i < piles[ref].size() && is_next_built_group_card(piles[ref][i], piles[ref][i-1]))
+ while (i < piles[ref].size()
+ && is_next_built_group_card(piles[ref][i], piles[ref][i-1])
+ && !piles[ref][i].is_face_down())
i++;
return i;
}
@@ -254,6 +385,9 @@ void game_state::add_empty_built_group_moves(vector<move>& moves, pile::ref rem_
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();
}
bool game_state::is_next_built_group_card(card a, card b) const {
@@ -274,3 +408,12 @@ bool game_state::is_next_legal_card(pol p, card a, card b) const {
// Checks rank
return b.get_rank() + 1 == a.get_rank();
}
+
+void game_state::turn_face_down_cards(vector<move>& moves) const {
+ for (auto& m : moves) {
+ bool is_tableau_move = m.from >= original_tableau_piles.front() && m.from <= original_tableau_piles.back();
+ if (is_tableau_move && piles[m.from].size() > 1 && piles[m.from][1].is_face_down()) {
+ m.make_reveal_move();
+ }
+ }
+}
diff --git a/src/main/game/sol_rules.h b/src/main/game/sol_rules.h
index adab00d..a7b9ae6 100644
--- a/src/main/game/sol_rules.h
+++ b/src/main/game/sol_rules.h
@@ -28,6 +28,10 @@ struct sol_rules {
WASTE,
TABLEAU_PILES
};
+ enum class face_up_policy {
+ ALL,
+ TOP_CARDS
+ };
uint8_t tableau_pile_count;
build_policy build_pol;
@@ -49,6 +53,8 @@ struct sol_rules {
bool stock_redeal;
uint8_t reserve_size;
bool reserve_stacked;
+ face_up_policy face_up;
+
};
#endif //SOLVITAIRE_SOL_RULES_H