aboutsummaryrefslogtreecommitdiff
path: root/src/main/game/search-state
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/search-state
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/search-state')
-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
4 files changed, 274 insertions, 46 deletions
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();
+ }
+ }
+}