aboutsummaryrefslogtreecommitdiff
path: root/src/main/game/search-state/game_state.cpp
diff options
context:
space:
mode:
authorCharlie Blake2018-07-24 16:17:44 +0100
committerGitHub2018-07-24 16:17:44 +0100
commit6ffee36ea973433723359321dfffd43c65febf88 (patch)
treecb3841d736ff353c5d8aea6a4faf81861ef83f5c /src/main/game/search-state/game_state.cpp
parentMerge pull request #77 from thecharlesblake/add-accordion (diff)
parentFix canfield to pass all unit tests (diff)
downloadmetasol-6ffee36ea973433723359321dfffd43c65febf88.tar.gz
metasol-6ffee36ea973433723359321dfffd43c65febf88.zip

Merge pull request #78 from thecharlesblake/add-canfield

Add canfield

Diffstat (limited to 'src/main/game/search-state/game_state.cpp')
-rw-r--r--src/main/game/search-state/game_state.cpp51
1 files changed, 39 insertions, 12 deletions
diff --git a/src/main/game/search-state/game_state.cpp b/src/main/game/search-state/game_state.cpp
index 0388d9c..17fdc29 100644
--- a/src/main/game/search-state/game_state.cpp
+++ b/src/main/game/search-state/game_state.cpp
@@ -4,7 +4,6 @@
#include <vector>
#include <ostream>
-#include <random>
#include <algorithm>
#include <functional>
@@ -38,6 +37,7 @@ typedef sol_rules::build_policy pol;
typedef sol_rules::stock_deal_type sdt;
typedef sol_rules::face_up_policy fu;
typedef game_state::streamliner_options sos;
+typedef sol_rules::foundations_init_type fit;
//////////////////
// CONSTRUCTORS //
@@ -48,6 +48,7 @@ typedef game_state::streamliner_options sos;
game_state::game_state(const sol_rules& s_rules, streamliner_options stream_opts_)
: rules(s_rules)
, stream_opts(stream_opts_)
+ , foundations_base(card::rank_t(1))
, stock(255)
, waste(255)
, hole (255) {
@@ -130,7 +131,8 @@ game_state::game_state(const sol_rules& s_rules, const Document& doc, streamline
// Constructs an initial game state from a seed
game_state::game_state(const sol_rules& s_rules, int seed, streamliner_options s_opts)
: game_state(s_rules, s_opts) {
- vector<card> deck = gen_shuffled_deck(seed, rules.max_rank, rules.two_decks);
+ auto rng = mt19937(seed);
+ vector<card> deck = gen_shuffled_deck(rules.max_rank, rules.two_decks, rng);
// If there is a hole, moves the ace of spades to it
if (rules.hole) {
@@ -138,14 +140,30 @@ game_state::game_state(const sol_rules& s_rules, int seed, streamliner_options s
place_card(hole, card("AS"));
}
+ // If the foundation base is random, selects a random rank and suit
+ card::suit_t rand_suit = 0;
+ if (!rules.foundations_base) {
+ boost::random::uniform_int_distribution<card::rank_t> distr_rank(1, rules.max_rank);
+ foundations_base = distr_rank(rng);
+ boost::random::uniform_int_distribution<card::suit_t> distr_suit(0, 3);
+ rand_suit = distr_suit(rng);
+ }
+
// If the foundations begin filled, then fills them
- if (rules.foundations_init_card) {
- for (uint8_t f_idx = 0; f_idx < 4*(rules.two_decks ? 2:1); f_idx++) {
- card c = card(f_idx % uint8_t(4), 1);
+ uint8_t founds_to_fill = 0;
+ switch (rules.foundations_init_cards) {
+ case sol_rules::foundations_init_type::NONE:
+ break;
+ case sol_rules::foundations_init_type::ONE:
+ founds_to_fill = 1; break;
+ case sol_rules::foundations_init_type::ALL:
+ founds_to_fill = static_cast<uint8_t>(4 * (rules.two_decks ? 2 : 1)); break;
+ }
+ for (uint8_t f_idx = 0; f_idx < founds_to_fill; f_idx++) {
+ card c = card((f_idx + rand_suit) % uint8_t(4), foundations_base);
- deck.erase(find(begin(deck), end(deck), c));
- place_card(foundations[f_idx], c);
- }
+ deck.erase(find(begin(deck), end(deck), c));
+ place_card(foundations[(f_idx + rand_suit)], c);
}
// If there is a stock, deals to it and set up a waste pile too
@@ -245,11 +263,21 @@ game_state::game_state(const sol_rules& s_rules,
}
pr++;
}
+
+ // Sets foundations base appropriately
+ if (rules.foundations_present && rules.foundations_base == boost::none) {
+ for (auto f : foundations) {
+ if (!piles[f].empty()) {
+ foundations_base = piles[f].top_card().get_rank();
+ return;
+ }
+ }
+ }
}
// 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<card> game_state::gen_shuffled_deck(card::rank_t max_rank,
+ bool two_decks, mt19937 rng) {
vector<card> deck;
for (int deck_count = 1; deck_count <= (two_decks ? 2 : 1); deck_count++) {
@@ -262,7 +290,6 @@ vector<card> game_state::gen_shuffled_deck(int seed, card::rank_t max_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;
}
@@ -613,7 +640,7 @@ bool game_state::is_solved() const {
} else if (rules.sequence_count > 0) {
for (pile::ref i = 0; i < sequences.size() && solved; i++) {
for (pile::ref j = piles[sequences[i]].size(); j-- > 2;) {
- if (!is_next_legal_card(rules.sequence_build_pol, piles[sequences[i]][j-1], piles[sequences[i]][j])) {
+ if (!is_next_legal_card(rules.sequence_build_pol, piles[sequences[i]][j-1], piles[sequences[i]][j], false)) {
solved = false;
break;
}