aboutsummaryrefslogtreecommitdiff
path: root/src/main/game
diff options
context:
space:
mode:
authorCharlie Blake2018-06-21 15:13:19 +0100
committerGitHub2018-06-21 15:13:19 +0100
commit4c617d49ce2fc162f25379df7715de34c0bbc349 (patch)
tree49d507a2024cdbbf68be95719ff60d4fee04fc99 /src/main/game
parentMerge pull request #64 from thecharlesblake/solvability-csv-output (diff)
parentFixup randomness (diff)
downloadmetasol-4c617d49ce2fc162f25379df7715de34c0bbc349.tar.gz
metasol-4c617d49ce2fc162f25379df7715de34c0bbc349.zip

Merge pull request #65 from thecharlesblake/solvability-improvements

Solvability improvements

Diffstat (limited to 'src/main/game')
-rw-r--r--src/main/game/global_cache.cpp16
-rw-r--r--src/main/game/search-state/game_state.auto_foundation_moves.cpp4
-rw-r--r--src/main/game/search-state/game_state.cpp66
-rw-r--r--src/main/game/search-state/game_state.h8
4 files changed, 46 insertions, 48 deletions
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/search-state/game_state.auto_foundation_moves.cpp b/src/main/game/search-state/game_state.auto_foundation_moves.cpp
index 0b62af4..1bec3fe 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,7 @@ 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 (streamliners || rules.build_pol == pol::NO_BUILD || rules.build_pol == pol::SAME_SUIT)
return true;
card::suit_t target_suit(target_pile - foundations.front());
diff --git a/src/main/game/search-state/game_state.cpp b/src/main/game/search-state/game_state.cpp
index 0a20f60..82e413b 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"
@@ -27,9 +28,9 @@ 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;
typedef sol_rules::build_policy pol;
typedef sol_rules::stock_deal_type sdt;
@@ -40,10 +41,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 +103,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
@@ -183,7 +186,7 @@ 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) {
+ : game_state(s_rules, false) {
pile::ref pr = 0;
for (const pile& p : il) {
for (const card c : p.pile_vec) {
@@ -196,31 +199,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 //
diff --git a/src/main/game/search-state/game_state.h b/src/main/game/search-state/game_state.h
index 7c0f9ca..f335de1 100644
--- a/src/main/game/search-state/game_state.h
+++ b/src/main/game/search-state/game_state.h
@@ -28,9 +28,9 @@ 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>);
@@ -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 */
@@ -116,6 +117,7 @@ private:
/* Game rules */
const sol_rules rules;
+ bool streamliners;
/* Pile references */