aboutsummaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'src/main')
-rw-r--r--src/main/game/search-state/game_state.cpp51
-rw-r--r--src/main/game/search-state/game_state.dominance_moves.cpp (renamed from src/main/game/search-state/game_state.auto_foundation_moves.cpp)50
-rw-r--r--src/main/game/search-state/game_state.h12
-rw-r--r--src/main/game/search-state/game_state.legal_moves.cpp133
-rw-r--r--src/main/game/sol_rules.cpp8
-rw-r--r--src/main/game/sol_rules.h21
-rw-r--r--src/main/input-output/input/json-parsing/deal_parser.cpp28
-rw-r--r--src/main/input-output/input/json-parsing/rules_parser.cpp94
-rw-r--r--src/main/input-output/input/sol_preset_types.cpp90
-rw-r--r--src/main/solver/solver.cpp1
10 files changed, 354 insertions, 134 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;
}
diff --git a/src/main/game/search-state/game_state.auto_foundation_moves.cpp b/src/main/game/search-state/game_state.dominance_moves.cpp
index b74948a..b7e6e15 100644
--- a/src/main/game/search-state/game_state.auto_foundation_moves.cpp
+++ b/src/main/game/search-state/game_state.dominance_moves.cpp
@@ -3,13 +3,16 @@
//
#include "game_state.h"
+#include "../sol_rules.h"
typedef sol_rules::build_policy pol;
typedef game_state::streamliner_options sos;
+typedef sol_rules::stock_deal_type sdt;
+typedef sol_rules::spaces_policy s_pol;
using std::max;
-
+using boost::optional;
bool game_state::is_valid_auto_foundation_move(pile::ref target_pile) const {
if (rules.foundations_only_comp_piles)
@@ -21,11 +24,12 @@ bool game_state::is_valid_auto_foundation_move(pile::ref target_pile) const {
return true;
card::suit_t target_suit(target_pile - foundations.front());
- card::rank_t target_rank = piles[target_pile].empty()
- ? card::rank_t(0)
- : piles[target_pile].top_card().get_rank();
+ card::rank_t move_up_rank = piles[target_pile].empty()
+ ? card::rank_t(1)
+ : foundation_base_convert(piles[target_pile].top_card().get_rank() + card::rank_t(1));
+
// The card to be moved up
- card move_up_card(target_suit, card::rank_t(target_rank + 1));
+ card move_up_card(target_suit, move_up_rank);
// The max difference between the 'move up' card and the foundations of the
// same colour and the other colour
@@ -36,7 +40,7 @@ bool game_state::is_valid_auto_foundation_move(pile::ref target_pile) const {
card::suit_t s(pr - foundations.front());
card::rank_t r = piles[pr].empty() ? card::rank_t(0)
- : piles[pr].top_card().get_rank();
+ : foundation_base_convert(piles[pr].top_card().get_rank());
card c(s, r);
auto rank_diff = int(move_up_card.get_rank() - r);
@@ -62,8 +66,13 @@ bool game_state::is_valid_auto_foundation_move(pile::ref target_pile) const {
}
// Returns a dominance move if one is available
-boost::optional<move> game_state::get_dominance_move() const {
+optional<move> game_state::get_dominance_move() const {
#ifndef NO_AUTO_FOUNDATIONS
+ if (rules.spaces_pol == s_pol::AUTO_RESERVE_THEN_WASTE) {
+ optional<move> arm = auto_reserve_move();
+ if (arm) return arm;
+ }
+
// If there are 2 decks or no foundations, return
if (!rules.foundations_present || rules.two_decks)
return boost::none;
@@ -71,10 +80,11 @@ boost::optional<move> game_state::get_dominance_move() const {
// Cycles through the piles and sees if any cards can be automatically moved
// to the foundations
for (pile::ref pr = 0; pr < piles.size(); pr++) {
- // Don't move foundation cards, hole or stock cards to the foundations
+ // Don't move foundation cards, hole, waste or stock cards to the foundations
if ((pr >= foundations.front() && pr <= foundations.back())
|| (rules.hole && pr == hole)
|| (rules.stock_size > 0 && pr == stock)
+ || (rules.stock_size > 0 && rules.stock_deal_t == sdt::WASTE && pr == waste)
|| (piles[pr].empty())) {
continue;
}
@@ -85,10 +95,9 @@ boost::optional<move> game_state::get_dominance_move() const {
// If the card is the right rank and the auto-move boolean is true, then
// returns the move
card::rank_t target_rank = piles[target_foundation].empty()
- ? card::rank_t(1) :
- piles[target_foundation].top_card().get_rank()
- + card::rank_t(1);
- if (target_rank == c.get_rank() &&
+ ? card::rank_t(1)
+ : foundation_base_convert(piles[target_foundation].top_card().get_rank() + card::rank_t(1));
+ if (target_rank == foundation_base_convert(c.get_rank()) &&
auto_foundation_moves[c.get_suit()]) {
move m(move::mtype::dominance, pr, target_foundation);
@@ -104,6 +113,17 @@ boost::optional<move> game_state::get_dominance_move() const {
return boost::none;
}
+optional<move> game_state::auto_reserve_move() const {
+ if (rules.spaces_pol == s_pol::AUTO_RESERVE_THEN_WASTE && !piles[reserve.front()].empty()) {
+ for (auto to : tableau_piles) {
+ if (piles[to].empty()) {
+ return move(move::mtype::dominance, reserve.front(), to);
+ }
+ }
+ }
+ return boost::none;
+}
+
bool game_state::is_ordered_pile(pile::ref pr) const {
if (piles[pr].size() != rules.max_rank) return false;
@@ -144,3 +164,9 @@ void game_state::update_auto_foundation_moves(pile::ref target_pile) {
auto_foundation_moves[pr] = is_valid_auto_foundation_move(pr);
}
}
+
+card::rank_t game_state::foundation_base_convert(card::rank_t r) const {
+ card::rank_t s = r - (foundations_base - card::rank_t(1));
+ if (s < 1) s += rules.max_rank;
+ return s;
+}
diff --git a/src/main/game/search-state/game_state.h b/src/main/game/search-state/game_state.h
index cbd72b4..d8d9f12 100644
--- a/src/main/game/search-state/game_state.h
+++ b/src/main/game/search-state/game_state.h
@@ -8,6 +8,7 @@
#include <vector>
#include <list>
#include <string>
+#include <random>
#include <boost/functional/hash.hpp>
#include <boost/optional/optional.hpp>
@@ -62,7 +63,7 @@ private:
/* Constructors (& helper function) */
explicit game_state(const sol_rules&, streamliner_options);
- static std::vector<card> gen_shuffled_deck(int, card::rank_t, bool);
+ static std::vector<card> gen_shuffled_deck(card::rank_t, bool, std::mt19937);
template<class RandomIt, class URBG> static void shuffle(RandomIt, RandomIt, URBG&&);
/* Pile order logic */
@@ -110,30 +111,37 @@ private:
bool is_valid_hole_move(pile::ref) const;
bool is_valid_hole_move(card) const;
+ void add_valid_tableau_moves(std::vector<move>&, pile::ref) const;
void add_built_group_moves(std::vector<move>&) const;
void add_built_group_moves(std::vector<move>&, pile::ref, pile::size_type) const;
+ void add_whole_pile_moves(std::vector<move>&) const;
+ void add_whole_pile_moves(std::vector<move>&, pile::ref, pile::size_type) const;
pile::size_type get_built_group_height(pile::ref) 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;
void add_sequence_moves(std::vector<move>&) const;
+ bool tableau_space_and_auto_reserve() const;
- bool is_next_legal_card(sol_rules::build_policy, card, card) const;
+ bool is_next_legal_card(sol_rules::build_policy, card, card, bool) const;
bool is_next_legal_card(std::vector<sol_rules::accordion_policy>, card, card) const;
void turn_face_down_cards(std::vector<move>&) const;
/* Auto-foundation moves */
+ boost::optional<move> auto_reserve_move() const;
bool is_valid_auto_foundation_move(pile::ref) const;
bool is_ordered_pile(pile::ref) const;
bool dominance_blocks_foundation_move(pile::ref);
void update_auto_foundation_moves(pile::ref);
+ card::rank_t foundation_base_convert(card::rank_t) const;
/* Game rules */
const sol_rules rules;
streamliner_options stream_opts;
+ card::rank_t foundations_base;
/* 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 e1cbc8e..f97814a 100644
--- a/src/main/game/search-state/game_state.legal_moves.cpp
+++ b/src/main/game/search-state/game_state.legal_moves.cpp
@@ -22,6 +22,7 @@ typedef sol_rules::accordion_policy acc_pol;
typedef sol_rules::stock_deal_type sdt;
typedef sol_rules::face_up_policy fu;
typedef sol_rules::direction dir;
+typedef sol_rules::built_group_type bgt;
////////////////////////////////
// MAIN LEGAL MOVE GEN CYCLE ///
@@ -64,18 +65,16 @@ vector<move> game_state::get_legal_moves(move parent_move) {
if (!piles[r].empty())
moves.emplace_back(move::mtype::regular, r, empty_cell);
- if (rules.stock_size > 0 && rules.stock_deal_t == sdt::WASTE && !piles[stock].empty())
+ if (rules.stock_size > 0 && rules.stock_deal_t == sdt::WASTE)
add_stock_to_cell_move(moves, empty_cell);
}
- // Foundation to tableau moves
+ // Foundation to tableau / empty cell 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);
+ add_valid_tableau_moves(moves, f);
if (empty_cell != 255)
moves.emplace_back(move::mtype::regular, f, empty_cell);
@@ -86,28 +85,32 @@ vector<move> game_state::get_legal_moves(move parent_move) {
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);
- }
- }
+ add_valid_tableau_moves(moves, r);
}
- // Stock to tableau moves
- if (rules.stock_size > 0 && rules.stock_deal_t == sdt::WASTE && !piles[stock].empty())
+ // Stock-waste to tableau moves
+ if (rules.stock_size > 0 && rules.stock_deal_t == sdt::WASTE)
add_stock_to_tableau_moves(moves);
- if (rules.move_built_group)
- add_built_group_moves(moves);
+ // Tableau built group moves
+ switch (rules.move_built_group) {
+ case sol_rules::built_group_type::YES:
+ add_built_group_moves(moves);
+ break;
+ case sol_rules::built_group_type::WHOLE_PILE:
+ add_whole_pile_moves(moves);
+ break;
+ case sol_rules::built_group_type::NO:
+ break;
+ }
// Tableau to tableau moves
- for (auto t_from : tableau_piles) {
- if (piles[t_from].empty() || parent_move.to == t_from) continue;
+ // If only whole pile moves are available, doesn't make regular ones
+ if (rules.move_built_group != bgt::WHOLE_PILE) {
+ 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);
- }
+ add_valid_tableau_moves(moves, t_from);
}
}
@@ -120,17 +123,13 @@ vector<move> game_state::get_legal_moves(move parent_move) {
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);
- }
- }
+ add_valid_tableau_moves(moves, c);
}
// Tableau / cells / reserve / stock-waste to hole / foundation moves
if (rules.hole || (rules.foundations_present && !rules.foundations_only_comp_piles)) {
// Stock
- if (rules.stock_size > 0 && rules.stock_deal_t == sdt::WASTE && !piles[stock].empty())
+ if (rules.stock_size > 0 && rules.stock_deal_t == sdt::WASTE)
add_stock_to_hole_foundation_moves(moves);
list<pile::ref> from_piles = tableau_piles;
@@ -187,7 +186,8 @@ set<pair<int8_t, bool>> game_state::generate_k_plus_moves_to_check() const {
set<pair<int8_t, bool>> stock_moves_to_check;
// If the waste isn't empty, adds the move from the top of the current waste
- if (piles[waste].size() != 0) stock_moves_to_check.insert(pair<int8_t, bool>(0, false));
+ if (!piles[waste].empty()) stock_moves_to_check.insert(pair<int8_t, bool>(0, false));
+ else if (piles[stock].empty()) return stock_moves_to_check;
// Adds multiples of deal count (excluding last card)
for (int8_t count = rules.stock_deal_count;
@@ -220,9 +220,14 @@ void game_state::add_stock_to_tableau_moves(std::vector<move>& moves) const {
for (auto k_plus_mv : generate_k_plus_moves_to_check()) {
card from = stock_card_from_count(k_plus_mv.first);
- for (auto t : tableau_piles)
- if (is_valid_tableau_move(from, t))
+ // Obeys the auto-reserve restriction unless the reserve is empty
+ if (!piles[reserve.front()].empty() && tableau_space_and_auto_reserve()) return;
+
+ for (auto t : tableau_piles) {
+ if (is_valid_tableau_move(from, t)) {
moves.emplace_back(move::mtype::stock_k_plus, stock, t, k_plus_mv.first, false, k_plus_mv.second);
+ }
+ }
}
}
@@ -288,7 +293,7 @@ bool game_state::is_valid_tableau_move(const card rem_c,
}
bool game_state::is_next_tableau_card(card a, card b) const {
- return is_next_legal_card(rules.build_pol, a, b);
+ return is_next_legal_card(rules.build_pol, a, b, rules.tableau_wraps);
}
bool game_state::is_valid_foundations_move(const pile::ref rem_ref,
@@ -300,6 +305,8 @@ bool game_state::is_valid_foundations_move(const pile::ref rem_ref,
bool game_state::is_valid_foundations_move(const card rem_c,
const pile::ref add_ref) const {
+ if (piles[add_ref].size() == rules.max_rank) return false;
+
// Checks same suit policy
uint8_t suit_idx = (add_ref - foundations.front()) % uint8_t(4);
if (rem_c.get_suit() != suit_idx)
@@ -307,9 +314,9 @@ bool game_state::is_valid_foundations_move(const card rem_c,
// Checks rank
if (piles[add_ref].empty())
- return rem_c.get_rank() == 1;
+ return rem_c.get_rank() == foundations_base;
else
- return rem_c.get_rank() == piles[add_ref].top_card().get_rank() + 1;
+ return rem_c.get_rank() == (piles[add_ref].top_card().get_rank() % rules.max_rank) + 1;
}
bool game_state::is_valid_hole_move(const pile::ref rem_ref) const {
@@ -332,6 +339,16 @@ bool game_state::is_valid_hole_move(const card c) const {
// BUILT-GROUP MOVE GEN FUNCTIONS //
////////////////////////////////////
+void game_state::add_valid_tableau_moves(std::vector<move>& moves, pile::ref from) const {
+ if (tableau_space_and_auto_reserve()) return;
+
+ for (auto to : tableau_piles) {
+ if (is_valid_tableau_move(from, to)) {
+ moves.emplace_back(move::mtype::regular, from, to);
+ }
+ }
+}
+
void game_state::add_built_group_moves(vector<move>& moves) const {
assert(rules.built_group_pol != pol::NO_BUILD);
@@ -368,6 +385,37 @@ void game_state::add_built_group_moves(vector<move>& moves, pile::ref rem_ref, p
}
}
+void game_state::add_whole_pile_moves(vector<move>& moves) const {
+ assert(rules.built_group_pol != pol::NO_BUILD);
+ if (tableau_space_and_auto_reserve()) return;
+
+ // Cycles through each pile to see if it contains a whole-pile built group
+ for (auto rem_ref : tableau_piles) {
+ auto built_group_height = get_built_group_height(rem_ref);
+
+ if (built_group_height == piles[rem_ref].size()) {
+ add_whole_pile_moves(moves, rem_ref, built_group_height);
+ }
+ }
+}
+
+void game_state::add_whole_pile_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];
+
+ if (piles[add_ref].empty()) {
+ if (rules.spaces_pol == s_pol::ANY || (rules.spaces_pol == s_pol::KINGS && bg_high.get_rank() == 13)) {
+ moves.emplace_back(move::mtype::built_group, rem_ref, add_ref, built_group_height);
+ }
+ } 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);
+ }
+ }
+}
+
// 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;
@@ -379,7 +427,7 @@ pile::size_type game_state::get_built_group_height(pile::ref ref) const {
}
bool game_state::is_next_built_group_card(card a, card b) const {
- return is_next_legal_card(rules.built_group_pol, a, b);
+ return is_next_legal_card(rules.built_group_pol, a, b, rules.tableau_wraps);
}
// Loops through each possible built group move to an empty pile and adds it to the list
@@ -448,7 +496,7 @@ void game_state::add_sequence_moves(std::vector<move>& moves) const {
// Otherwise must agree with left neighbour
else {
card neighbour_card = piles[sequences[space_loc.first]][space_loc.second + 1];
- if (neighbour_card != "AS" && is_next_legal_card(rules.sequence_build_pol, from_card, neighbour_card))
+ if (neighbour_card != "AS" && is_next_legal_card(rules.sequence_build_pol, from_card, neighbour_card, false))
moves.emplace_back(move::mtype::sequence, from_idx, space_idx);
}
}
@@ -462,7 +510,7 @@ void game_state::add_sequence_moves(std::vector<move>& moves) const {
// Otherwise must agree with right neighbour
else {
card neighbour_card = piles[sequences[space_loc.first]][space_loc.second - 1];
- if (neighbour_card != "AS" && is_next_legal_card(rules.sequence_build_pol, neighbour_card, from_card))
+ if (neighbour_card != "AS" && is_next_legal_card(rules.sequence_build_pol, neighbour_card, from_card, false))
moves.emplace_back(move::mtype::sequence, from_idx, space_idx);
}
}
@@ -491,10 +539,17 @@ void game_state::add_accordion_moves(vector<move>& moves) const {
}
}
+// If auto-reserve is enabled and there is a space, returns
+bool game_state::tableau_space_and_auto_reserve() const {
+ if (rules.spaces_pol == s_pol::AUTO_RESERVE_THEN_WASTE)
+ for (auto to : tableau_piles)
+ if (piles[to].empty()) return true;
+ return false;
+}
///////////////////////
-bool game_state::is_next_legal_card(pol p, card a, card b) const {
+bool game_state::is_next_legal_card(pol p, card a, card b, bool wraps) const {
// Checks build pol violations
switch(p) {
case sol_rules::build_policy::SAME_SUIT:
@@ -506,7 +561,11 @@ bool game_state::is_next_legal_card(pol p, card a, card b) const {
default:;
}
// Checks rank
- return b.get_rank() + 1 == a.get_rank();
+ if (wraps) {
+ return (b.get_rank() % rules.max_rank) + 1 == a.get_rank();
+ } else {
+ return b.get_rank() + 1 == a.get_rank();
+ }
}
bool game_state::is_next_legal_card(vector<acc_pol> vp, card a, card b) const {
diff --git a/src/main/game/sol_rules.cpp b/src/main/game/sol_rules.cpp
index 0ea998e..2778241 100644
--- a/src/main/game/sol_rules.cpp
+++ b/src/main/game/sol_rules.cpp
@@ -9,6 +9,8 @@ typedef sol_rules::spaces_policy s_pol;
typedef sol_rules::stock_deal_type sdt;
typedef sol_rules::direction dir;
typedef sol_rules::face_up_policy fu;
+typedef sol_rules::built_group_type bgt;
+typedef sol_rules::foundations_init_type fit;
// Note that the 'default' sol rules are set up by parsing the default json
// rules description. This initialisation is really just for testing purposes.
@@ -16,13 +18,15 @@ sol_rules::sol_rules() :
tableau_pile_count(0),
build_pol(pol::ANY_SUIT),
spaces_pol(s_pol::ANY),
- move_built_group(false),
+ move_built_group(bgt::NO),
built_group_pol(pol::ANY_SUIT),
+ tableau_wraps(false),
two_decks(false),
max_rank(13),
hole(false),
foundations_present(false),
- foundations_init_card(false),
+ foundations_init_cards(fit::NONE),
+ foundations_base(1),
foundations_only_comp_piles(false),
diagonal_deal(false),
cells(0),
diff --git a/src/main/game/sol_rules.h b/src/main/game/sol_rules.h
index 0673771..2384b21 100644
--- a/src/main/game/sol_rules.h
+++ b/src/main/game/sol_rules.h
@@ -7,7 +7,9 @@
#include <string>
#include <vector>
+#include <boost/optional.hpp>
+#include "card.h"
#include "../../../lib/rapidjson/document.h"
struct sol_rules {
@@ -23,7 +25,8 @@ struct sol_rules {
enum class spaces_policy {
NO_BUILD,
KINGS,
- ANY
+ ANY,
+ AUTO_RESERVE_THEN_WASTE
};
enum class accordion_policy {
SAME_RANK,
@@ -44,17 +47,29 @@ struct sol_rules {
RIGHT,
BOTH
};
+ enum class built_group_type {
+ YES,
+ NO,
+ WHOLE_PILE
+ };
+ enum class foundations_init_type {
+ NONE,
+ ONE,
+ ALL
+ };
uint8_t tableau_pile_count;
build_policy build_pol;
spaces_policy spaces_pol;
- bool move_built_group;
+ built_group_type move_built_group;
build_policy built_group_pol;
+ bool tableau_wraps;
bool two_decks;
uint8_t max_rank;
bool hole;
bool foundations_present;
- bool foundations_init_card;
+ foundations_init_type foundations_init_cards;
+ boost::optional<card::rank_t> foundations_base;
bool foundations_removable;
bool foundations_only_comp_piles;
bool diagonal_deal;
diff --git a/src/main/input-output/input/json-parsing/deal_parser.cpp b/src/main/input-output/input/json-parsing/deal_parser.cpp
index 7b8cf18..e5ecae2 100644
--- a/src/main/input-output/input/json-parsing/deal_parser.cpp
+++ b/src/main/input-output/input/json-parsing/deal_parser.cpp
@@ -13,6 +13,7 @@ using namespace rapidjson;
typedef sol_rules::build_policy pol;
typedef sol_rules::stock_deal_type sdt;
typedef sol_rules::face_up_policy fu;
+typedef sol_rules::foundations_init_type fit;
void deal_parser::parse(game_state &gs, const rapidjson::Document& doc) {
// There are two stages to reading in the supplied deal. It is both put
@@ -65,7 +66,7 @@ void deal_parser::parse(game_state &gs, const rapidjson::Document& doc) {
if (gs.rules.foundations_present) {
bool supplied_foundations = parse_foundations(gs, doc);
- if (!supplied_foundations && gs.rules.foundations_init_card) {
+ if (!supplied_foundations && gs.rules.foundations_init_cards == fit::ALL) {
fill_foundations(gs);
}
}
@@ -105,9 +106,7 @@ string deal_parser::deal_schema_json() {
"tableau piles": {
"type": "array", "items": {"$ref": "#/definitions/cardarray"}
},
- "foundations": {
- "type": "array", "items": {"$ref": "#/definitions/cardarray"}
- },
+ "foundations": {"$ref": "#/definitions/cardarray"},
"sequences": {
"type": "array", "items": {"$ref": "#/definitions/cardarraywithempty"}
},
@@ -208,10 +207,6 @@ void deal_parser::parse_reserve(game_state &gs, const Document& doc) {
// We treat a regular reserve like multiple single-card piles,
// but a stacked reserve as a single multiple-card pile
- if (json_reserve_piles.Size() != gs.rules.reserve_size) {
- json_helper::json_parse_err("Incorrect reserve size");
- }
-
for (pile::ref i = 0; i < json_card_arr.Size(); i++) {
assert(json_card_arr[i].IsString());
pile::ref pr = gs.original_reserve[0];
@@ -269,17 +264,16 @@ bool deal_parser::parse_foundations(game_state &gs, const rapidjson::Document& d
const Value& json_foundations = doc["foundations"];
assert(json_foundations.IsArray());
- if (json_foundations.Size() != 4 * (gs.rules.two_decks ? 2 : 1) ) {
- json_helper::json_parse_err("Incorrect number of foundations");
- }
-
for (auto j = begin(json_foundations.GetArray()); j != end(json_foundations.GetArray()); j++) {
+ assert(j->IsString());
+ card c = card(j->GetString());
+ gs.place_card(gs.foundations[c.get_suit()], c);
+ }
- for (auto& json_card : j->GetArray()) {
- assert(json_card.IsString());
- card c = card(json_card.GetString());
- gs.place_card(gs.foundations[c.get_suit()], c);
- }
+ // If the game uses a random base for foundations, assume that the first card in the first foundation is that base
+ if (!gs.rules.foundations_base) {
+ auto& first_found = gs.piles[gs.foundations[0]];
+ gs.foundations_base = first_found[first_found.size() - 1].get_rank();
}
return true;
diff --git a/src/main/input-output/input/json-parsing/rules_parser.cpp b/src/main/input-output/input/json-parsing/rules_parser.cpp
index b70abdf..209153f 100644
--- a/src/main/input-output/input/json-parsing/rules_parser.cpp
+++ b/src/main/input-output/input/json-parsing/rules_parser.cpp
@@ -5,6 +5,8 @@
#include <vector>
#include <algorithm>
+#include "../../../../../lib/rapidjson/error/en.h"
+
#include "rules_parser.h"
#include "json_helper.h"
#include "../sol_preset_types.h"
@@ -24,6 +26,8 @@ typedef sol_rules::accordion_policy acc_pol;
typedef sol_rules::stock_deal_type sdt;
typedef sol_rules::face_up_policy fu;
typedef sol_rules::direction dir;
+typedef sol_rules::built_group_type bgt;
+typedef sol_rules::foundations_init_type fit;
const sol_rules rules_parser::from_file(const string rules_file) {
sol_rules sr = get_default();
@@ -115,6 +119,8 @@ void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) {
sr.spaces_pol = s_pol::NO_BUILD;
} else if (sp_str == "kings") {
sr.spaces_pol = s_pol::KINGS;
+ } else if (sp_str == "auto-reserve-then-waste") {
+ sr.spaces_pol = s_pol::AUTO_RESERVE_THEN_WASTE;
} else {
json_helper::json_parse_err("[tableau piles][spaces policy] is invalid");
}
@@ -125,10 +131,20 @@ void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) {
}
if (d["tableau piles"].HasMember("move built group")) {
- if (d["tableau piles"]["move built group"].IsBool()) {
- sr.move_built_group = d["tableau piles"]["move built group"].GetBool();
+ if (d["tableau piles"]["move built group"].IsString()) {
+ string mbg_str = d["tableau piles"]["move built group"].GetString();
+
+ if (mbg_str == "yes") {
+ sr.move_built_group = bgt::YES;
+ } else if (mbg_str == "no") {
+ sr.move_built_group = bgt::NO;
+ } else if (mbg_str == "whole-pile") {
+ sr.move_built_group = bgt::WHOLE_PILE;
+ } else {
+ json_helper::json_parse_err("[tableau piles][move built group] is invalid");
+ }
} else {
- json_helper::json_parse_err("[tableau piles][move built group] must be a boolean");
+ json_helper::json_parse_err("[tableau piles][move built group] must be a string");
}
}
@@ -183,6 +199,14 @@ void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) {
}
}
+ if (d["tableau piles"].HasMember("wraps")) {
+ if (d["tableau piles"]["wraps"].IsBool()) {
+ sr.tableau_wraps = d["tableau piles"]["wraps"].GetBool();
+ } else {
+ json_helper::json_parse_err("[tableau piles][wraps] must be a boolean");
+ }
+ }
+
} else {
json_helper::json_parse_err("[tableau piles] must be an object");
}
@@ -198,11 +222,37 @@ void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) {
}
}
- if (d["foundations"].HasMember("initial card")) {
- if (d["foundations"]["initial card"].IsBool()) {
- sr.foundations_init_card = d["foundations"]["initial card"].GetBool();
+ if (d["foundations"].HasMember("initial cards")) {
+ if (d["foundations"]["initial cards"].IsString()) {
+ string ic_str = d["foundations"]["initial cards"].GetString();
+
+ if (ic_str == "none") {
+ sr.foundations_init_cards = fit::NONE;
+ } else if (ic_str == "one") {
+ sr.foundations_init_cards = fit::ONE;
+ } else if (ic_str == "all") {
+ sr.foundations_init_cards = fit::ALL;
+ } else {
+ string err = "[foundations][initial cards] is invalid: " + ic_str;
+ json_helper::json_parse_err(err);
+ }
} else {
- json_helper::json_parse_err("[foundations][initial card] must be a boolean");
+ json_helper::json_parse_err("[foundations][initial cards] must be a string");
+ }
+ }
+
+ if (d["foundations"].HasMember("base card")) {
+ if (d["foundations"]["base card"].IsString()) {
+ string base_card_str = d["foundations"]["base card"].GetString();
+
+ if (base_card_str == "random") {
+ sr.foundations_base = boost::none;
+ } else {
+ base_card_str += "H";
+ sr.foundations_base = card(base_card_str.c_str()).get_rank();
+ }
+ } else {
+ json_helper::json_parse_err("[foundations][base card] must be a string");
}
}
@@ -446,7 +496,6 @@ void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) {
"must be true");
}
- rules_schema_json(); // Throws an error if it fails
Document vd;
vd.Parse(rules_schema_json().c_str());
assert(!vd.HasParseError());
@@ -485,15 +534,24 @@ string rules_parser::rules_schema_json() {
"enum": [
"any",
"no-build",
- "kings"
+ "kings",
+ "auto-reserve-then-waste"
]
},
"diagonal deal": {
"type": "boolean"
},
- "move built group": {
+ "wraps": {
"type": "boolean"
},
+ "move built group": {
+ "type": "string",
+ "enum": [
+ "yes",
+ "no",
+ "whole-pile"
+ ]
+ },
"move built group policy": {
"type": "string",
"enum": [
@@ -520,8 +578,20 @@ string rules_parser::rules_schema_json() {
"present": {
"type": "boolean"
},
- "initial card": {
- "type": "boolean"
+ "initial cards": {
+ "type": "string",
+ "enum": [
+ "none",
+ "one",
+ "all"
+ ]
+ },
+ "base card": {
+ "type": "string",
+ "oneOf":[
+ {"pattern": "^(([0-9]|1[0-3]|a|A|j|J|q|Q|k|K))$"},
+ {"enum": ["random"]}
+ ]
},
"removable": {
"type": "boolean"
diff --git a/src/main/input-output/input/sol_preset_types.cpp b/src/main/input-output/input/sol_preset_types.cpp
index 0c96651..0aee219 100644
--- a/src/main/input-output/input/sol_preset_types.cpp
+++ b/src/main/input-output/input/sol_preset_types.cpp
@@ -25,13 +25,15 @@ map<string, string> sol_preset_types::mp = {
"build policy": "any-suit",
"spaces policy": "any",
"diagonal deal": false,
- "move built group": false,
+ "wraps": false,
+ "move built group": "no",
"move built group policy": "same-as-build",
"face up cards": "all"
},
"foundations": {
"present": true,
- "initial card": false,
+ "initial cards": "none",
+ "base card": "A",
"removable": false,
"only complete pile moves": false
},
@@ -200,7 +202,7 @@ map<string, string> sol_preset_types::mp = {
"build policy": "same-suit"
},
"foundations": {
- "initial card": true
+ "initial cards": "all"
},
"stock": {
"size": 36
@@ -219,7 +221,7 @@ map<string, string> sol_preset_types::mp = {
"build policy": "same-suit"
},
"foundations": {
- "initial card": true
+ "initial cards": "all"
},
"stock": {
"size": 3
@@ -274,13 +276,21 @@ map<string, string> sol_preset_types::mp = {
"tableau piles": {
"count": 4,
"build policy": "red-black",
- "move built group": true
+ "move built group": "whole-pile",
+ "spaces policy": "auto-reserve-then-waste",
+ "wraps": true
+ },
+ "foundations": {
+ "initial cards": "one",
+ "base card": "random"
},
"stock": {
- "size": 34
+ "size": 34,
+ "deal count": 3,
+ "redeal": true
},
"reserve": {
- "size": 14,
+ "size": 13,
"stacked": true
}
}
@@ -295,10 +305,18 @@ map<string, string> sol_preset_types::mp = {
"tableau piles": {
"count": 3,
"build policy": "red-black",
- "move built group": true
+ "move built group": "whole-pile",
+ "spaces policy": "auto-reserve-then-waste",
+ "wraps": true
+ },
+ "foundations": {
+ "initial cards": "one",
+ "base card": "random"
},
"stock": {
- "size": 1
+ "size": 6,
+ "deal count": 3,
+ "redeal": true
},
"reserve": {
"size": 2,
@@ -352,10 +370,10 @@ map<string, string> sol_preset_types::mp = {
"tableau piles": {
"count": 12,
"build policy": "same-suit",
- "move built group": true
+ "move built group": "yes"
},
"foundations": {
- "initial card": true
+ "initial cards": "all"
}
}
)"
@@ -369,10 +387,10 @@ map<string, string> sol_preset_types::mp = {
"tableau piles": {
"count": 3,
"build policy": "same-suit",
- "move built group": true
+ "move built group": "yes"
},
"foundations": {
- "initial card": true
+ "initial cards": "all"
},
"max rank": 6
}
@@ -387,7 +405,7 @@ map<string, string> sol_preset_types::mp = {
"tableau piles": {
"count": 10,
"build policy": "any-suit",
- "move built group": true,
+ "move built group": "yes",
"move built group policy": "same-suit"
},
"foundations": {
@@ -410,7 +428,7 @@ map<string, string> sol_preset_types::mp = {
"tableau piles": {
"count": 4,
"build policy": "any-suit",
- "move built group": true,
+ "move built group": "yes",
"move built group policy": "same-suit"
},
"foundations": {
@@ -435,7 +453,7 @@ map<string, string> sol_preset_types::mp = {
"tableau piles": {
"count": 4,
"build policy": "red-black",
- "move built group": true,
+ "move built group": "yes",
"diagonal deal": true,
"face up cards": "top"
},
@@ -465,7 +483,7 @@ map<string, string> sol_preset_types::mp = {
},
"foundations": {
"removable": true,
- "initial card": true
+ "initial cards": "all"
}
}
)"
@@ -481,7 +499,7 @@ map<string, string> sol_preset_types::mp = {
"count": 7,
"build policy": "red-black",
"spaces policy": "kings",
- "move built group": true,
+ "move built group": "yes",
"diagonal deal": true,
"face up cards": "top"
},
@@ -507,7 +525,7 @@ map<string, string> sol_preset_types::mp = {
"count": 7,
"build policy": "red-black",
"spaces policy": "kings",
- "move built group": true,
+ "move built group": "yes",
"diagonal deal": true,
"face up cards": "top"
},
@@ -567,7 +585,7 @@ map<string, string> sol_preset_types::mp = {
"build policy": "same-suit"
},
"foundations": {
- "initial card": true
+ "initial cards": "all"
}
}
)"
@@ -583,7 +601,7 @@ map<string, string> sol_preset_types::mp = {
"build policy": "red-black"
},
"foundations": {
- "initial card": true,
+ "initial cards": "all",
"removable": true
}
}
@@ -600,7 +618,7 @@ map<string, string> sol_preset_types::mp = {
"build policy": "red-black"
},
"foundations": {
- "initial card": true,
+ "initial cards": "all",
"removable": true
},
"cells": 1
@@ -618,7 +636,7 @@ map<string, string> sol_preset_types::mp = {
"build policy": "red-black"
},
"foundations": {
- "initial card": true,
+ "initial cards": "all",
"removable": true
},
"cells": 2
@@ -796,12 +814,12 @@ map<string, string> sol_preset_types::mp = {
"tableau piles": {
"count": 7,
"build policy": "same-suit",
- "move built group": true,
+ "move built group": "yes",
"spaces policy": "kings"
},
"cells": 7,
"foundations": {
- "initial card": true
+ "initial cards": "all"
}
}
)"
@@ -815,7 +833,7 @@ map<string, string> sol_preset_types::mp = {
"tableau piles": {
"count": 10,
"build policy": "same-suit",
- "move built group": true,
+ "move built group": "yes",
"spaces policy": "kings"
},
"cells": 4
@@ -831,10 +849,10 @@ map<string, string> sol_preset_types::mp = {
"tableau piles": {
"count": 4,
"build policy": "red-black",
- "move built group": true
+ "move built group": "yes"
},
"foundations": {
- "initial card": true
+ "initial cards": "all"
},
"reserve": {
"size": 12
@@ -854,7 +872,7 @@ map<string, string> sol_preset_types::mp = {
"tableau piles": {
"count": 3,
"build policy": "any-suit",
- "move built group": true
+ "move built group": "yes"
},
"reserve": {
"size": 13,
@@ -909,10 +927,10 @@ map<string, string> sol_preset_types::mp = {
"tableau piles": {
"count": 6,
"build policy": "red-black",
- "move built group": true
+ "move built group": "yes"
},
"foundations": {
- "initial card": true,
+ "initial cards": "all",
"removable": true
},
"stock": {
@@ -930,7 +948,7 @@ map<string, string> sol_preset_types::mp = {
"tableau piles": {
"count": 7,
"build policy": "red-black",
- "move built group": true,
+ "move built group": "yes",
"face up cards": "top"
},
"foundations": {
@@ -951,7 +969,7 @@ map<string, string> sol_preset_types::mp = {
{
"tableau piles": {
"build policy": "red-black",
- "move built group": true,
+ "move built group": "yes",
"spaces policy": "kings",
"diagonal deal": true
},
@@ -993,7 +1011,7 @@ map<string, string> sol_preset_types::mp = {
"tableau piles": {
"count": 7,
"build policy": "any-suit",
- "move built group": true,
+ "move built group": "yes",
"move built group policy": "same-suit",
"diagonal deal": true
},
@@ -1016,7 +1034,7 @@ map<string, string> sol_preset_types::mp = {
"tableau piles": {
"count": 7,
"build policy": "any-suit",
- "move built group": true,
+ "move built group": "yes",
"move built group policy": "same-suit"
},
"foundations": {
@@ -1038,7 +1056,7 @@ map<string, string> sol_preset_types::mp = {
"tableau piles": {
"count": 10,
"diagonal deal": true,
- "move built group": true,
+ "move built group": "yes",
"move built group policy": "same-suit"
},
"foundations": {
diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp
index 4059b5b..3bfd6bd 100644
--- a/src/main/solver/solver.cpp
+++ b/src/main/solver/solver.cpp
@@ -136,7 +136,6 @@ bool solver::revert_to_last_node_with_children(optional<lru_cache::item_list::it
if (current_node->mv.type != move::mtype::dominance) {
if (cache.get_states_removed_from_cache() == 0) {
- LOG_DEBUG(state); // TODO
assert(cache.contains(state));
}
LOG_DEBUG("(undo move)");