diff options
| author | Ian Gent | 2018-06-26 16:43:45 +0100 |
|---|---|---|
| committer | Ian Gent | 2018-06-26 16:43:45 +0100 |
| commit | 0798cfe1ecb92996ed7959383dcc4b8b0d51a1d9 (patch) | |
| tree | f6f6dbd927012de317feea6b2bb25d466cff1ac3 /src | |
| parent | first R file (diff) | |
| parent | Merge pull request #71 from thecharlesblake/correct-klondike (diff) | |
| download | metasol-0798cfe1ecb92996ed7959383dcc4b8b0d51a1d9.tar.gz metasol-0798cfe1ecb92996ed7959383dcc4b8b0d51a1d9.zip | |
Merge branch ‘master’ of github.com:thecharlesblake/Solvitaire into ipg
Diffstat (limited to 'src')
28 files changed, 986 insertions, 276 deletions
diff --git a/src/main/evaluation/benchmark.cpp b/src/main/evaluation/benchmark.cpp index 367caf5..83207fb 100644 --- a/src/main/evaluation/benchmark.cpp +++ b/src/main/evaluation/benchmark.cpp @@ -13,7 +13,7 @@ using namespace std; typedef chrono::microseconds microsec; -void benchmark::run(const sol_rules &rules, uint64_t cache_capacity) { +void benchmark::run(const sol_rules &rules, uint64_t cache_capacity, bool streamliners) { cout << "Seed " "| Median/Mean Solution Time(μs)" "| Median/Mean States Searched " @@ -27,7 +27,7 @@ void benchmark::run(const sol_rules &rules, uint64_t cache_capacity) { int total_unsolvable = 0; for(int seed = 1; seed <= 1000; seed++) { - game_state gs(rules, seed); + game_state gs(rules, seed, streamliners); solver sol(gs, cache_capacity); auto start = chrono::steady_clock::now(); diff --git a/src/main/evaluation/benchmark.h b/src/main/evaluation/benchmark.h index 83c9638..70a5b76 100644 --- a/src/main/evaluation/benchmark.h +++ b/src/main/evaluation/benchmark.h @@ -11,7 +11,7 @@ class benchmark { public: - static void run(const sol_rules &rules, uint64_t); + static void run(const sol_rules &rules, uint64_t, bool); }; diff --git a/src/main/evaluation/binomial_ci.cpp b/src/main/evaluation/binomial_ci.cpp new file mode 100644 index 0000000..c381c05 --- /dev/null +++ b/src/main/evaluation/binomial_ci.cpp @@ -0,0 +1,45 @@ +// +// Created by thecharlesblake on 20/06/18. +// + +#include <cmath> +#include <algorithm> +#include "binomial_ci.h" + +using std::pow; +using std::min; +using std::max; +using std::pair; + +pair<double, double> binomial_ci::wilson(int s_, int f_) { + auto s = static_cast<double>(s_); + auto f = static_cast<double>(f_); + double z = 1.96; + double z2 = pow(z, 2); + double n = s + f; + double p = (s + (z2 / 2.0)) / (n + z2); + double half_ci = (z / (n + z2)) * sqrt(((s * f) / n) + (z2 / 4.0)); + return { p - half_ci + , p + half_ci }; +} + +pair<double, double> binomial_ci::wilson(int s, int f, int i) { + pair<double, double> a = wilson(s, f + i); + pair<double, double> b = wilson(s + i, f); + return {a.first, b.second}; +} + +pair<double, double> binomial_ci::agresti_coull(int s, int f) { + double z = 1.96; + double n_ = s + f + pow(z, 2); + double p = (s + pow(z, 2)/2)/n_; + double half_ci = z * sqrt((p - pow(p, 2))/n_); + return { max(0.0, p - half_ci) + , min(1.0, p + half_ci) }; +} + +pair<double, double> binomial_ci::agresti_coull(int s, int f, int i) { + pair<double, double> a = agresti_coull(s, f + i); + pair<double, double> b = agresti_coull(s + i, f); + return {a.first, b.second}; +} diff --git a/src/main/evaluation/binomial_ci.h b/src/main/evaluation/binomial_ci.h new file mode 100644 index 0000000..6e2dceb --- /dev/null +++ b/src/main/evaluation/binomial_ci.h @@ -0,0 +1,19 @@ +// +// Created by thecharlesblake on 20/06/18. +// + +#ifndef SOLVITAIRE_BINOMIAL_CI_H +#define SOLVITAIRE_BINOMIAL_CI_H + + +#include <utility> + +struct binomial_ci { + static std::pair<double, double> wilson(int, int); + static std::pair<double, double> wilson(int, int, int); + static std::pair<double, double> agresti_coull(int, int); + static std::pair<double, double> agresti_coull(int, int, int); +}; + + +#endif //SOLVITAIRE_BINOMIAL_CI_H diff --git a/src/main/evaluation/solvability_calc.cpp b/src/main/evaluation/solvability_calc.cpp index ffe883b..b576548 100644 --- a/src/main/evaluation/solvability_calc.cpp +++ b/src/main/evaluation/solvability_calc.cpp @@ -4,12 +4,13 @@ #include <future> #include <iomanip> #include <iostream> -#include <cmath> #include "solvability_calc.h" #include "../solver/solver.h" +#include "binomial_ci.h" using namespace std; +using boost::optional; /////////////////// // SETUP METHODS // @@ -23,15 +24,31 @@ solvability_calc::solvability_calc(const sol_rules& r, uint64_t cache_capacity_) // PRINTING METHODS // ////////////////////// -void solvability_calc::print_header(long t) const { +void solvability_calc::print_header(long t, bool streamliners) const { cout << "Calculating solvability percentage...\n\n" "Lower Bound" ", Upper Bound" ", Solvable" ", Unsolvable" - ", Intractable" - ", Attempted Seed" - ", Seed State" + ", Intractable, "; + if (streamliners) { + cout << "(Streamliner Results:) " + "Attempted Seed" + ", Outcome" + ", Time Taken(ms)" + ", States Searched" + ", Unique States Searched" + ", Backtracks" + ", Dominance Moves" + ", States Removed From Cache" + ", Final States In Cache" + ", Final Buckets In Cache" + ", Maximum Search Depth" + ", Final Search Depth" + "(Non-Streamliner Results:) "; + } + cout << "Attempted Seed" + ", Outcome" ", Time Taken(ms)" ", States Searched" ", Unique States Searched" @@ -47,20 +64,21 @@ void solvability_calc::print_header(long t) const { cout << fixed << setprecision(3); } -void solvability_calc::print_row(const seed_results& seed_res, sol_result res, set<int>& seeds_in_progress) { - double lower_bound = sol_lower_bound(seed_res.solvable, - seed_res.unsolvable, - seed_res.intractable); - double upper_bound = sol_upper_bound(seed_res.solvable, - seed_res.unsolvable, - seed_res.intractable); +void solvability_calc::print_general_info(const seed_results& seed_res) { + pair<double, double> interval = binomial_ci::wilson( + seed_res.solvable, + seed_res.unsolvable, + seed_res.intractable); - cout << lower_bound * 100 - << ", " << upper_bound * 100 + cout << interval.first * 100 + << ", " << interval.second * 100 << ", " << seed_res.solvable << ", " << seed_res.unsolvable - << ", " << seed_res.intractable - << ", " << res.seed; + << ", " << seed_res.intractable; +} + +void solvability_calc::print_seed_info(sol_result res) { + cout << ", " << res.seed; switch (res.sol_type) { case sol_result::type::timeout: cout << ", timed-out"; @@ -73,7 +91,7 @@ void solvability_calc::print_row(const seed_results& seed_res, sol_result res, s break; } cout << ", " << res.time.count(); - + cout << ", " << res.sol_info.states_searched << ", " << res.sol_info.unique_states_searched << ", " << res.sol_info.backtracks @@ -83,7 +101,13 @@ void solvability_calc::print_row(const seed_results& seed_res, sol_result res, s << ", " << res.sol_info.cache_bucket_count << ", " << res.sol_info.max_depth << ", " << res.sol_info.depth; +} + +void solvability_calc::print_null_seed_info() { + cout << ", , , , , , , , , , , , "; +} +void solvability_calc::print_seeds_in_prog(std::set<int>& seeds_in_progress) { for(auto iter = begin(seeds_in_progress); iter != end(seeds_in_progress); ++iter) { cout << ", " << *iter; } @@ -99,7 +123,8 @@ void solvability_calc::print_row(const seed_results& seed_res, sol_result res, s // SOLVING METHODS // ///////////////////// -void solvability_calc::calculate_solvability_percentage(int timeout_, uint cores, const vector<int>& resume) { +void solvability_calc::calculate_solvability_percentage(uint64_t timeout_, int seed_count, uint cores, + bool streamliners, const vector<int>& resume) { vector<int> resume_seeds(begin(resume) + 3, end(resume)); sort(begin(resume_seeds), end(resume_seeds)); @@ -111,7 +136,7 @@ void solvability_calc::calculate_solvability_percentage(int timeout_, uint cores mutex results_mutex; millisec timeout(timeout_); - print_header(timeout.count()); + print_header(timeout.count(), streamliners); // Spin off 'cores' threads. Each one runs solve_seed and then print_row repeatedly, taking the seed from am // atomic int @@ -123,25 +148,47 @@ void solvability_calc::calculate_solvability_percentage(int timeout_, uint cores for (uint i = 0; i < cores; i++) { futures.push_back(async( launch::async, - [¤t_seed, &seed_res, &seeds_in_progress, &results_mutex, timeout, sr, cc, i, resume_seeds](){ + [¤t_seed, &seed_res, &seeds_in_progress, &results_mutex, seed_count, timeout, sr, cc, i, + resume_seeds, streamliners](){ int my_seed = resume_seeds.size() > i ? resume_seeds[i] : current_seed++; - while (my_seed < INT_MAX) { + while (my_seed < seed_count) { results_mutex.lock(); seeds_in_progress.insert(my_seed); results_mutex.unlock(); - sol_result res = solve_seed(my_seed, timeout, sr, cc); + optional<sol_result> stream_res, no_stream_res; + sol_result final_res; + + if (streamliners) { + stream_res = solve_seed(my_seed, timeout, sr, cc, true); + if (stream_res->sol_type == sol_result::type::unsolvable) { + no_stream_res = solve_seed(my_seed, timeout, sr, cc, false); + final_res = *no_stream_res; + } else { + final_res = *stream_res; + } + } else { + no_stream_res = solve_seed(my_seed, timeout, sr, cc, false); + final_res = *no_stream_res; + } results_mutex.lock(); + seeds_in_progress.erase(my_seed); - switch (res.sol_type) { - case sol_result::type::solved: seed_res.solvable ++; break; - case sol_result::type::unsolvable: seed_res.unsolvable ++; break; - case sol_result::type::timeout: seed_res.intractable++; break; + seed_res.add_result(final_res.sol_type); + + print_general_info(seed_res); + if (streamliners) { + print_seed_info(*stream_res); + if (no_stream_res) print_seed_info(*no_stream_res); + else print_null_seed_info(); + } else { + print_seed_info(*no_stream_res); } - print_row(seed_res, res, seeds_in_progress); + print_seeds_in_prog(seeds_in_progress); + results_mutex.unlock(); my_seed = current_seed++; @@ -149,12 +196,13 @@ void solvability_calc::calculate_solvability_percentage(int timeout_, uint cores } )); } - futures[0].wait(); // Should wait indefinitely + + for (auto& f : futures) f.wait(); } -solvability_calc::sol_result solvability_calc::solve_seed(int seed, millisec timeout, - const sol_rules& rules, uint64_t cache_capacity) { - game_state gs(rules, seed); +solvability_calc::sol_result solvability_calc::solve_seed(int seed, millisec timeout, const sol_rules& rules, + uint64_t cache_capacity, bool streamliners) { + game_state gs(rules, seed, streamliners); solver sol(gs, cache_capacity); atomic<bool> terminate_solver(false); @@ -234,56 +282,3 @@ void solvability_calc::seed_results::add_result(sol_result::type t) { break; } } - - -///////////////////////// -// CALCULATION METHODS // -///////////////////////// - -double solvability_calc::sol_lower_bound(int solvables, - int unsolvables, - int intractables) { - int x = solvables; - int n = solvables + unsolvables + intractables; - bool lower_bound = true; - return agresti_coull(x, n, lower_bound); -} - -double solvability_calc::sol_upper_bound(int solvables, - int unsolvables, - int intractables) { - int x = solvables + intractables; - int n = solvables + unsolvables + intractables; - bool lower_bound = false; - return agresti_coull(x, n, lower_bound); -} - -double solvability_calc::sol_ci_size(int solvables, - int unsolvables, - int intractables) { - return sol_upper_bound(solvables, unsolvables, intractables) - - sol_lower_bound(solvables, unsolvables, intractables); -} - -double solvability_calc::agresti_coull(int x, int n, bool lower_bound) { - double z = 1.96; - double n_ = n + pow(z, 2); - double p = (x + pow(z, 2)/2)/n_; - double half_ci = z * sqrt((p - pow(p, 2))/n_); - if (lower_bound) { - double ans = p - half_ci; - return ans < 0 ? 0 : ans; - } else { - double ans = p + half_ci; - return ans > 1 ? 1 : ans; - } -} - -double solvability_calc::agresti_coull_mean(int x, int n) { - double z = 1.96; - double n_ = n + pow(z, 2); - double p = (x + pow(z, 2)/2)/n_; - return p; -} - -int solvability_calc::rnd(double d) { return static_cast<int>(lround(d)); } diff --git a/src/main/evaluation/solvability_calc.h b/src/main/evaluation/solvability_calc.h index ef3492c..9307649 100644 --- a/src/main/evaluation/solvability_calc.h +++ b/src/main/evaluation/solvability_calc.h @@ -17,7 +17,7 @@ class solvability_calc { public: explicit solvability_calc(const sol_rules&, uint64_t); - void calculate_solvability_percentage(int timeout, uint cores, const std::vector<int>&); + void calculate_solvability_percentage(uint64_t, int, uint, bool, const std::vector<int>&); private: typedef std::chrono::milliseconds millisec; @@ -32,7 +32,7 @@ private: }; struct seed_results { - seed_results(std::vector<int>); + explicit seed_results(std::vector<int>); void add_result(sol_result::type); std::atomic<int> solvable; @@ -43,19 +43,14 @@ private: }; // Printing methods - void print_header(long) const; - static void print_row(const seed_results&, sol_result, std::set<int>&); + void print_header(long, bool) const; + static void print_general_info(const seed_results&); + static void print_seed_info(sol_result); + static void print_null_seed_info(); + static void print_seeds_in_prog(std::set<int>&); // Solving methods - static sol_result solve_seed(int, millisec, const sol_rules&, uint64_t); - - // Calculation methods - static double sol_lower_bound(int, int, int); - static double sol_upper_bound(int, int, int); - static double sol_ci_size(int, int, int); - static double agresti_coull(int, int, bool); - static double agresti_coull_mean(int, int); - static int rnd(double); + static sol_result solve_seed(int, millisec, const sol_rules&, uint64_t, bool); const sol_rules& rules; const uint64_t cache_capacity; 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 diff --git a/src/main/input-output/input/command_line_helper.cpp b/src/main/input-output/input/command_line_helper.cpp index 2abef54..ec9a375 100644 --- a/src/main/input-output/input/command_line_helper.cpp +++ b/src/main/input-output/input/command_line_helper.cpp @@ -41,14 +41,17 @@ command_line_helper::command_line_helper() ("cache-capacity", po::value<uint64_t>(), "sets an upper bound on the number of states allowed in " "the cache") ("solvability", po::value<int>(), "calculates the solvability " - "percentage of the supplied solitaire game, given a timeout in milliseconds. Must supply " - "either 'random', 'benchmark', 'solvability' or list of deals to be " - "solved.") + "percentage of the supplied solitaire game, given a limit for the number of seeds. Must supply " + "either 'random', 'benchmark', 'solvability' or list of deals to be solved.") + ("timeout", po::value<uint64_t>(), "adds a per-game timeout to the solvability percentage generation") ("resume", po::value<vector<int>>()->multitoken(), "resumes the solvability percentage calculation from a " "previous run. Must be supplied with the solvability option. " "Syntax: [sol unsol intract in-progress-1 in-progress-2 ...]") ("cores", po::value<uint>(), "the number of cores for the solvability percentages to be run across. " - "Must be supplied with the solvability option.") + "Must be supplied with the solvability option.") + ("streamliners", "applies any relevant streamliners to the search. If the --solvability option is supplied," + " runs the streamliner version of the search first and then if unsuccessful, the full" + " search for each seed.") ("benchmark", "outputs performance statistics for the solver on the " "supplied solitaire game. Must supply " "either 'random', 'benchmark', 'solvability' or list of deals to be " @@ -121,6 +124,12 @@ bool command_line_helper::parse(int argc, const char* argv[]) { solvability = -1; } + if (vm.count("timeout")) { + timeout = vm["timeout"].as<uint64_t>(); + } else { + timeout = 604800000; // 1 week in milliseconds + } + if (vm.count("cores")) { cores = vm["cores"].as<uint>(); } else { @@ -134,6 +143,7 @@ bool command_line_helper::parse(int argc, const char* argv[]) { for (uint i = 0; i < cores; i++) resume.push_back(i); } + streamliners = (vm.count("streamliners") != 0); benchmark = (vm.count("benchmark") != 0); @@ -248,6 +258,10 @@ int command_line_helper::get_solvability() { return solvability; } +uint64_t command_line_helper::get_timeout() { + return timeout; +} + vector<int> command_line_helper::get_resume() { return resume; } @@ -267,3 +281,7 @@ string command_line_helper::get_describe_game_rules() { bool command_line_helper::get_benchmark() { return benchmark; } + +bool command_line_helper::get_streamliners() { + return streamliners; +} diff --git a/src/main/input-output/input/command_line_helper.h b/src/main/input-output/input/command_line_helper.h index 3ce239d..c12bb69 100644 --- a/src/main/input-output/input/command_line_helper.h +++ b/src/main/input-output/input/command_line_helper.h @@ -21,9 +21,11 @@ public: uint get_cores(); bool get_available_game_types(); bool get_benchmark(); + bool get_streamliners(); std::vector<int> get_resume(); uint64_t get_cache_capacity(); std::string get_describe_game_rules(); + uint64_t get_timeout(); private: bool assess_errors(); @@ -50,7 +52,9 @@ private: uint cores; bool available_game_types; bool benchmark; + bool streamliners; uint64_t cache_capacity; + uint64_t timeout; }; #endif //SOLVITAIRE_COMMAND_LINE_HELPER_H 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 29ac3b1..e776cbd 100644 --- a/src/main/input-output/input/json-parsing/deal_parser.cpp +++ b/src/main/input-output/input/json-parsing/deal_parser.cpp @@ -5,11 +5,13 @@ #include "deal_parser.h" #include "../../../game/search-state/game_state.h" #include "json_helper.h" +#include "../../../game/sol_rules.h" using namespace std; using namespace rapidjson; typedef sol_rules::build_policy pol; +typedef sol_rules::face_up_policy fu; 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 @@ -100,7 +102,7 @@ void deal_parser::parse_tableau_piles(game_state &gs, const rapidjson::Document& for (auto& json_card : p.first->GetArray()) { assert(json_card.IsString()); - gs.place_card(*p.second, card(json_card.GetString())); + gs.place_card(*p.second, card(json_card.GetString(), gs.rules.face_up != fu::ALL)); } } } 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 d4879de..29e755b 100644 --- a/src/main/input-output/input/json-parsing/rules_parser.cpp +++ b/src/main/input-output/input/json-parsing/rules_parser.cpp @@ -9,6 +9,7 @@ #include "json_helper.h" #include "../sol_preset_types.h" #include "../../../game/card.h" +#include "../../../game/sol_rules.h" using namespace std; using namespace rapidjson; @@ -20,6 +21,7 @@ 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; const sol_rules rules_parser::from_file(const string rules_file) { sol_rules sr = get_default(); @@ -160,6 +162,25 @@ void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) { } } + string face_up_str; + if (d["tableau piles"].HasMember("face up cards")) { + if (d["tableau piles"]["face up cards"].IsString()) { + face_up_str = d["tableau piles"]["face up cards"].GetString(); + + if (face_up_str == "all") { + sr.face_up = fu::ALL; + } else if (face_up_str == "top") { + sr.face_up = fu::TOP_CARDS; + } else { + string err = "[tableau piles][face up cards] is invalid: " + face_up_str; + json_helper::json_parse_err(err); + } + + } else { + json_helper::json_parse_err("[tableau piles][face up cards] must be a string"); + } + } + } else { json_helper::json_parse_err("[tableau piles] must be an object"); } @@ -318,7 +339,8 @@ string rules_parser::rules_schema_json() { "spaces policy": {"type": "string", "enum": ["any", "no-build", "kings"]}, "diagonal deal": {"type": "boolean"}, "move built group": {"type": "boolean"}, - "move built group policy": {"type": "string", "enum": ["same-as-build", "any-suit", "red-black", "same-suit", "no-build"]} + "move built group policy": {"type": "string", "enum": ["same-as-build", "any-suit", "red-black", "same-suit", "no-build"]}, + "face up cards": {"type": "string", "enum": ["all", "top"]} }, "additionalProperties": false }, "max rank": {"type": "integer", "minimum": 1, "maximum": 13}, diff --git a/src/main/input-output/input/sol_preset_types.cpp b/src/main/input-output/input/sol_preset_types.cpp index 53141a8..a10df54 100644 --- a/src/main/input-output/input/sol_preset_types.cpp +++ b/src/main/input-output/input/sol_preset_types.cpp @@ -26,7 +26,8 @@ map<string, string> sol_preset_types::mp = { "spaces policy": "any", "diagonal deal": false, "move built group": false, - "move built group policy": "same-as-build" + "move built group policy": "same-as-build", + "face up cards": "all" }, "max rank": 13, "two decks": false, @@ -388,7 +389,8 @@ map<string, string> sol_preset_types::mp = { "build policy": "red-black", "spaces policy": "kings", "move built group": true, - "diagonal deal": true + "diagonal deal": true, + "face up cards": "top" }, "stock size": 24, "stock deal count": 1, @@ -408,7 +410,8 @@ map<string, string> sol_preset_types::mp = { "build policy": "red-black", "spaces policy": "kings", "move built group": true, - "diagonal deal": true + "diagonal deal": true, + "face up cards": "top" }, "stock size": 24, "stock deal count": 3, @@ -928,6 +931,22 @@ map<string, string> sol_preset_types::mp = { "max rank": 9 } )" + }, + { + "simple-simon", + + R"( +{ + "tableau piles": { + "count": 10, + "diagonal deal": true, + "move built group": true, + "move built group policy": "same-suit" + }, + "foundations complete piles": true +} +)" + } }; diff --git a/src/main/main.cpp b/src/main/main.cpp index a492c26..a6ed318 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -19,9 +19,9 @@ using namespace boost; namespace po = boost::program_options; const optional<sol_rules> gen_rules(command_line_helper&); -void solve_random_game(int, const sol_rules&, bool, uint64_t); -void solve_input_files(vector<string>, const sol_rules&, bool, uint64_t); -void solve_game(const game_state&, bool, uint64_t); +void solve_random_game(int, const sol_rules&, command_line_helper&); +void solve_input_files(vector<string>, const sol_rules&, command_line_helper&); +void solve_game(const game_state&, command_line_helper&); // Decides what to do given supplied command-line options int main(int argc, const char* argv[]) { @@ -51,15 +51,16 @@ int main(int argc, const char* argv[]) { // If the user has asked for a solvability percentage, calculates it if (clh.get_solvability() > 0) { solvability_calc solv_c(*rules, clh.get_cache_capacity()); - solv_c.calculate_solvability_percentage(clh.get_solvability(), clh.get_cores(), clh.get_resume()); + solv_c.calculate_solvability_percentage(clh.get_timeout(), clh.get_solvability(), clh.get_cores(), + clh.get_streamliners(), clh.get_resume()); } // If a random deal seed has been supplied, solves it else if (clh.get_random_deal() != -1) { - solve_random_game(clh.get_random_deal(), *rules, clh.get_classify(), clh.get_cache_capacity()); + solve_random_game(clh.get_random_deal(), *rules, clh); } // If the benchmark option has been supplied, generates it else if (clh.get_benchmark()) { - benchmark::run(*rules, clh.get_cache_capacity()); + benchmark::run(*rules, clh.get_cache_capacity(), clh.get_streamliners()); } // Otherwise there are supplied input files which should be solved else { @@ -67,8 +68,8 @@ int main(int argc, const char* argv[]) { // If there are no input files, solve a random deal based on the // supplied seed - assert (!input_files.empty()); - solve_input_files(input_files, *rules, clh.get_classify(), clh.get_cache_capacity()); + assert(!input_files.empty()); + solve_input_files(input_files, *rules, clh); } return EXIT_SUCCESS; @@ -90,24 +91,23 @@ const optional<sol_rules> gen_rules(command_line_helper& clh) { } } -void solve_random_game(int seed, const sol_rules& rules, bool classify, uint64_t cache_capacity) { +void solve_random_game(int seed, const sol_rules& rules, command_line_helper& clh) { LOG_INFO ("Attempting to solve with seed: " << seed << "..."); - game_state gs(rules, seed); - solve_game(gs, classify, cache_capacity); + game_state gs(rules, seed, clh.get_streamliners()); + solve_game(gs, clh); } -void solve_input_files(const vector<string> input_files, const sol_rules& rules, - bool classify, uint64_t cache_capacity) { +void solve_input_files(const vector<string> input_files, const sol_rules& rules, command_line_helper& clh) { for (const string& input_file : input_files) { try { // Reads in the input file to a json doc const Document in_doc = json_helper::get_file_json(input_file); // Attempts to create a game state object from the json - game_state gs(rules, in_doc); + game_state gs(rules, in_doc, clh.get_streamliners()); LOG_INFO ("Attempting to solve " << input_file << "..."); - solve_game(gs, classify, cache_capacity); + solve_game(gs, clh); } catch (const runtime_error& error) { string errmsg = "Error parsing deal file: "; @@ -117,16 +117,16 @@ void solve_input_files(const vector<string> input_files, const sol_rules& rules, } } -void solve_game(const game_state& gs, bool classify, uint64_t cache_capacity) { - solver solv(gs, cache_capacity); +void solve_game(const game_state& gs, command_line_helper& clh) { + solver solv(gs, clh.get_cache_capacity()); bool solution = solv.run() == solver::sol_state::solved; if (solution) { - if (!classify) solv.print_solution(); + if (!clh.get_classify()) solv.print_solution(); cout << "Solved\n"; } else { - if (!classify) cout << "Deal:\n" << gs << "\n"; + if (!clh.get_classify()) cout << "Deal:\n" << gs << "\n"; cout << "No Possible Solution\n"; } diff --git a/src/test/test_helper.cpp b/src/test/test_helper.cpp index eaa0aba..1bc1f24 100644 --- a/src/test/test_helper.cpp +++ b/src/test/test_helper.cpp @@ -14,15 +14,17 @@ #define ASSERT_(t_f, statement) ASSERT_TRUE((statement) == (t_f)) using namespace rapidjson; -using std::initializer_list; using std::vector; using std::ostream; +typedef std::initializer_list<std::initializer_list<std::string>> string_il; + + bool test_helper::is_solvable(const std::string& input_file, const std::string& preset_type) { const Document in_doc = json_helper::get_file_json(input_file); const sol_rules rules = rules_parser::from_preset(preset_type); - game_state gs(rules, in_doc); + game_state gs(rules, in_doc, false); solver sol(gs, 1000000); return sol.run() == solver::sol_state::solved; @@ -35,7 +37,7 @@ void test_helper::run_foundations_dominance_test(sol_rules::build_policy policy, rules.foundations_removable = false; rules.cells = 0; - game_state gs = game_state(rules, std::initializer_list<pile>{ + game_state gs = game_state(rules, string_il{ {}, {}, {}, @@ -62,9 +64,9 @@ void test_helper::run_foundations_dominance_test(sol_rules::build_policy policy, ASSERT_TRUE(i->child_moves.empty()); } -void test_helper::run_built_group_test(sol_rules sr, - initializer_list<pile> piles, - vector<move> exp_moves) { +void test_helper::expected_moves_test(sol_rules sr, + string_il piles, + vector<move> exp_moves) { game_state gs(sr, piles); vector<move> actual_moves = gs.get_legal_moves(); @@ -88,6 +90,7 @@ ostream& operator <<(ostream& os, const move& m) { return os << "move:(" << int(m.from) << "," << int(m.to) << "," << int(m.count) + << "," << m.reveal_move << ")"; } diff --git a/src/test/test_helper.h b/src/test/test_helper.h index 463a935..3df9ead 100644 --- a/src/test/test_helper.h +++ b/src/test/test_helper.h @@ -17,7 +17,7 @@ public: static bool is_solvable(const std::string&, const std::string&); static void run_foundations_dominance_test(sol_rules::build_policy policy, std::vector<card> cards); - static void run_built_group_test(sol_rules sr, std::initializer_list<pile>, std::vector<move>); + static void expected_moves_test(sol_rules sr, std::initializer_list<std::initializer_list<std::string>>, std::vector<move>); static bool moves_eq(std::vector<move>&, std::vector<move>&); }; diff --git a/src/test/unit_tests/built_group_move_gen_test.cpp b/src/test/unit_tests/built_group_move_gen_test.cpp index 34e2b07..37a2cf0 100644 --- a/src/test/unit_tests/built_group_move_gen_test.cpp +++ b/src/test/unit_tests/built_group_move_gen_test.cpp @@ -20,13 +20,13 @@ TEST(BuiltGroupMoveGen, SameSuitEmpty) { sr.built_group_pol = pol::SAME_SUIT; sr.move_built_group = true; - test_helper::run_built_group_test( + test_helper::expected_moves_test( sr, { {}, - {"3C","2C","AC"}, // Same suit - {"3H","2S","AH"}, // Red black - {"3D","2D","AS"}, // Any suit + {"3C", "2C", "AC"}, // Same suit + {"3H", "2S", "AH"}, // Red black + {"3D", "2D", "AS"}, // Any suit {} }, { @@ -53,13 +53,13 @@ TEST(BuiltGroupMoveGen, RedBlackEmpty) { sr.built_group_pol = pol::RED_BLACK; sr.move_built_group = true; - test_helper::run_built_group_test( + test_helper::expected_moves_test( sr, { {}, - {"3C","2C","AC"}, // Same suit - {"3H","2S","AH"}, // Red black - {"3D","2D","AS"}, // Any suit + {"3C", "2C", "AC"}, // Same suit + {"3H", "2S", "AH"}, // Red black + {"3D", "2D", "AS"}, // Any suit {} }, { @@ -89,13 +89,13 @@ TEST(BuiltGroupMoveGen, AnySuitEmpty) { sr.built_group_pol = pol::ANY_SUIT; sr.move_built_group = true; - test_helper::run_built_group_test( + test_helper::expected_moves_test( sr, { {}, - {"3C","2C","AC"}, // Same suit - {"3H","2S","AH"}, // Red black - {"3D","2D","AS"}, // Any suit + {"3C", "2C", "AC"}, // Same suit + {"3H", "2S", "AH"}, // Red black + {"3D", "2D", "AS"}, // Any suit {} }, { @@ -132,13 +132,13 @@ TEST(BuiltGroupMoveGen, CantMove) { sr.built_group_pol = pol::ANY_SUIT; sr.move_built_group = false; - test_helper::run_built_group_test( + test_helper::expected_moves_test( sr, { {}, - {"3C","2C","AC"}, // Same suit - {"3H","2S","AH"}, // Red black - {"3D","2D","AS"}, // Any suit + {"3C", "2C", "AC"}, // Same suit + {"3H", "2S", "AH"}, // Red black + {"3D", "2D", "AS"}, // Any suit {} }, { @@ -159,13 +159,13 @@ TEST(BuiltGroupMoveGen, SameSuitOccupied) { sr.built_group_pol = pol::SAME_SUIT; sr.move_built_group = true; - test_helper::run_built_group_test( + test_helper::expected_moves_test( sr, { {"4C"}, - {"3C","2C","AC"}, // Same suit - {"3H","2S","AH"}, // Red black - {"3D","2D","AS"}, // Any suit + {"3C", "2C", "AC"}, // Same suit + {"3H", "2S", "AH"}, // Red black + {"3D", "2D", "AS"}, // Any suit {} }, { @@ -189,13 +189,13 @@ TEST(BuiltGroupMoveGen, RedBlackOccupied) { sr.built_group_pol = pol::RED_BLACK; sr.move_built_group = true; - test_helper::run_built_group_test( + test_helper::expected_moves_test( sr, { {"4C"}, - {"3C","2C","AC"}, // Same suit - {"3H","2S","AH"}, // Red black - {"3D","2D","AS"}, // Any suit + {"3C", "2C", "AC"}, // Same suit + {"3H", "2S", "AH"}, // Red black + {"3D", "2D", "AS"}, // Any suit {} }, { @@ -220,13 +220,13 @@ TEST(BuiltGroupMoveGen, AnySuitOccupied) { sr.built_group_pol = pol::ANY_SUIT; sr.move_built_group = true; - test_helper::run_built_group_test( + test_helper::expected_moves_test( sr, { {"4C"}, - {"3C","2C","AC"}, // Same suit - {"3H","2S","AH"}, // Red black - {"3D","2D","AS"}, // Any suit + {"3C", "2C", "AC"}, // Same suit + {"3H", "2S", "AH"}, // Red black + {"3D", "2D", "AS"}, // Any suit {} }, { diff --git a/src/test/unit_tests/face_up_cards_test.cpp b/src/test/unit_tests/face_up_cards_test.cpp new file mode 100644 index 0000000..5bf65cb --- /dev/null +++ b/src/test/unit_tests/face_up_cards_test.cpp @@ -0,0 +1,321 @@ +// +// Created by thecharlesblake on 1/11/18. +// + +#include <gtest/gtest.h> + +#include "../test_helper.h" +#include "../../main/game/search-state/game_state.h" +#include "../../main/game/sol_rules.h" +#include "../../main/game/global_cache.h" + +typedef sol_rules::build_policy pol; +typedef sol_rules::face_up_policy fu; +typedef move::mtype mt; + +using std::vector; +using std::ostream; + +typedef std::initializer_list<std::initializer_list<std::string>> string_il; + +#define ASSERT_FACE_UP(card) ASSERT_FALSE(card.is_face_down()) << card.to_string() << " should be face up" +#define ASSERT_FACE_DOWN(card) ASSERT_TRUE(card.is_face_down()) << card.to_string() << " should be face down" + + +TEST(FaceUpCards, CapsFaceUpIfFaceUpNotAll) { + sol_rules sr; + sr.tableau_pile_count = 3; + sr.built_group_pol = pol::SAME_SUIT; + sr.build_pol = pol::SAME_SUIT; + sr.move_built_group = true; + sr.face_up = fu::TOP_CARDS; + + string_il piles = { + {"3S"}, + {"2s","AS"}, + {} + }; + + game_state gs(sr, piles); + + ASSERT_FACE_UP(gs.get_data()[0][0]); + ASSERT_FACE_UP(gs.get_data()[1][0]); + ASSERT_FACE_DOWN(gs.get_data()[1][1]); +} + +TEST(FaceUpCards, IgnoreCapsIfFaceUpAll) { + sol_rules sr; + sr.tableau_pile_count = 3; + sr.built_group_pol = pol::SAME_SUIT; + sr.build_pol = pol::SAME_SUIT; + sr.move_built_group = true; + sr.face_up = fu::ALL; + + string_il piles = { + {"3S"}, + {"2s","AS"}, + {} + }; + + game_state gs(sr, piles); + + ASSERT_FACE_UP(gs.get_data()[0][0]); + ASSERT_FACE_UP(gs.get_data()[1][0]); + ASSERT_FACE_UP(gs.get_data()[1][1]); +} + +TEST(FaceUpCards, MoveTurnsFaceUp) { + sol_rules sr; + sr.tableau_pile_count = 3; + sr.built_group_pol = pol::SAME_SUIT; + sr.build_pol = pol::SAME_SUIT; + sr.move_built_group = true; + sr.face_up = fu::TOP_CARDS; + + string_il piles = { + {"3S"}, + {"2s","AS"}, // from + {} // to + }; + + test_helper::expected_moves_test( + sr, + piles, + { + // Regular moves + move(move::mtype::regular, 0, 2, 1, false), + move(move::mtype::regular, 1, 2, 1, true) + } + ); + + game_state gs(sr, piles); + gs.make_move(move(mt::regular, 1, 2, 1, true)); + + ASSERT_FACE_UP(gs.get_data()[0][0]); + ASSERT_FACE_UP(gs.get_data()[1][0]); + ASSERT_FACE_UP(gs.get_data()[2][0]); +} + +TEST(FaceUpCards, BuiltGroupMoveTurnsFaceUp) { + sol_rules sr; + sr.tableau_pile_count = 2; + sr.built_group_pol = pol::SAME_SUIT; + sr.build_pol = pol::SAME_SUIT; + sr.move_built_group = true; + sr.face_up = fu::TOP_CARDS; + + string_il piles = { + {"3s","2S","AS"}, // from + {} // to + }; + + test_helper::expected_moves_test( + sr, + piles, + { + // Regular moves + move(move::mtype::regular, 0, 1, 1, false), + // Built-group moves + move(move::mtype::built_group, 0, 1, 2, true) + } + ); + + game_state gs(sr, piles); + gs.make_move(move(mt::built_group, 0, 1, 2, true)); + + ASSERT_FACE_UP(gs.get_data()[0][0]); + ASSERT_FACE_UP(gs.get_data()[1][0]); + ASSERT_FACE_UP(gs.get_data()[1][1]); +} + +TEST(FaceUpCards, MoveAlreadyFaceUpNoChange) { + sol_rules sr; + sr.tableau_pile_count = 3; + sr.built_group_pol = pol::SAME_SUIT; + sr.build_pol = pol::SAME_SUIT; + sr.move_built_group = true; + sr.face_up = fu::TOP_CARDS; + + string_il piles = { + {"3S"}, + {"2s","AS"}, // from + {} // to + }; + + test_helper::expected_moves_test( + sr, + piles, + { + // Regular moves + move(move::mtype::regular, 0, 2, 1, false), + move(move::mtype::regular, 1, 2, 1, true) + } + ); + + game_state gs(sr, piles); + gs.make_move(move(mt::regular, 1, 2, 1, true)); + + ASSERT_FACE_UP(gs.get_data()[0][0]); + ASSERT_FACE_UP(gs.get_data()[1][0]); + ASSERT_FACE_UP(gs.get_data()[2][0]); +} + +TEST(FaceUpCards, FaceDownNoBuiltGroup) { + sol_rules sr; + sr.tableau_pile_count = 3; + sr.built_group_pol = pol::SAME_SUIT; + sr.build_pol = pol::SAME_SUIT; + sr.move_built_group = true; + sr.face_up = fu::TOP_CARDS; + + test_helper::expected_moves_test( + sr, + { + {"3S"}, + {"2s","AS"}, + {} + }, + { + // Regular moves + move(move::mtype::regular, 0, 2, 1, false), + move(move::mtype::regular, 1, 2, 1, true), + } + ); +} + +TEST(FaceUpCards, FaceUpBuiltGroup) { + sol_rules sr; + sr.tableau_pile_count = 3; + sr.built_group_pol = pol::SAME_SUIT; + sr.build_pol = pol::SAME_SUIT; + sr.move_built_group = true; + sr.face_up = fu::TOP_CARDS; + + test_helper::expected_moves_test( + sr, + { + {"3S"}, + {"2S","AS"}, + {} + }, + { + // Regular moves + move(mt::regular, 0, 2, 1), + move(mt::regular, 1, 2, 1), + + // Built group moves + move(mt::built_group, 1, 0, 2), + move(mt::built_group, 1, 2, 2) + } + ); +} + +TEST(FaceUpCards, FaceUpBuiltGroup2Not3) { + sol_rules sr; + sr.tableau_pile_count = 3; + sr.built_group_pol = pol::SAME_SUIT; + sr.build_pol = pol::SAME_SUIT; + sr.move_built_group = true; + sr.face_up = fu::TOP_CARDS; + + test_helper::expected_moves_test( + sr, + { + {"4S"}, + {"3s","2S","AS"}, + {} + }, + { + // Regular moves + move(mt::regular, 0, 2, 1, false), + move(mt::regular, 1, 2, 1, false), + + // Built group moves + move(mt::built_group, 1, 2, 2, true) + } + ); +} + +TEST(FaceUpCards, UndoTurnsBackFaceDown) { + sol_rules sr; + sr.tableau_pile_count = 3; + sr.built_group_pol = pol::SAME_SUIT; + sr.build_pol = pol::SAME_SUIT; + sr.move_built_group = true; + sr.face_up = fu::TOP_CARDS; + + string_il piles = { + {"3S"}, + {"2s","AS"}, // from + {} // to + }; + + game_state gs(sr, piles); + move m = move(mt::regular, 1, 2, 1, true); + gs.make_move(m); + gs.undo_move(m); + + ASSERT_FACE_UP(gs.get_data()[0][0]); + ASSERT_FACE_UP(gs.get_data()[1][0]); + ASSERT_FACE_DOWN(gs.get_data()[1][1]); +} + +TEST(FaceUpCards, UndoBuiltGroupTurnsBackFaceDown) { + sol_rules sr; + sr.tableau_pile_count = 3; + sr.built_group_pol = pol::SAME_SUIT; + sr.build_pol = pol::SAME_SUIT; + sr.move_built_group = true; + sr.face_up = fu::TOP_CARDS; + + string_il piles = { + {"4S"}, + {"3s","2S","AS"}, // from + {} // to + }; + + game_state gs(sr, piles); + move m = move(mt::built_group, 1, 2, 2, true); + gs.make_move(m); + gs.undo_move(m); + + ASSERT_FACE_UP(gs.get_data()[0][0]); + ASSERT_FACE_UP(gs.get_data()[1][0]); + ASSERT_FACE_UP(gs.get_data()[1][1]); + ASSERT_FACE_DOWN(gs.get_data()[1][2]); +} + +TEST(FaceUpCards, CacheEncodesFaceDown) { + sol_rules sr; + sr.tableau_pile_count = 3; + sr.built_group_pol = pol::RED_BLACK; + sr.build_pol = pol::RED_BLACK; + sr.move_built_group = true; + sr.face_up = fu::TOP_CARDS; + + string_il piles = { + {"2C"}, + {"2s","AH"}, + {} + }; + + game_state gs(sr, piles); + lru_cache cache(gs, 1000); + bool new_state; + + gs.make_move(move(mt::regular, 1, 2, 1, true)); + new_state = cache.insert(gs).second; + ASSERT_TRUE(new_state) << "AH to empty pile"; + + gs.make_move(move(mt::regular, 2, 1, 1, false)); + new_state = cache.insert(gs).second; + ASSERT_TRUE(new_state) << "AH back to face-up 2S"; + + gs.make_move(move(mt::regular, 1, 0, 1, false)); + new_state = cache.insert(gs).second; + ASSERT_TRUE(new_state) << "AH to 2C"; + + gs.make_move(move(mt::regular, 0, 1, 1, false)); + new_state = cache.insert(gs).second; + ASSERT_FALSE(new_state) << "AH to 2S"; +} diff --git a/src/test/unit_tests/global_cache_test.cpp b/src/test/unit_tests/global_cache_test.cpp index cd3c686..93ef105 100644 --- a/src/test/unit_tests/global_cache_test.cpp +++ b/src/test/unit_tests/global_cache_test.cpp @@ -9,69 +9,70 @@ #include "../../main/game/global_cache.h" typedef sol_rules::build_policy pol; +typedef std::initializer_list<std::initializer_list<std::string>> string_il; TEST(GlobalCache, CommutativeTableauPiles) { sol_rules rules; rules.tableau_pile_count = 3; rules.build_pol = sol_rules::build_policy::SAME_SUIT; - game_state gs(rules, std::initializer_list<pile>{{},{},{}}); + game_state gs(rules, string_il{{},{},{}}); lru_cache cache(gs, 1000); - cache.insert (game_state(rules, {{card("AC")},{card("2D")},{card("3H")}})); - ASSERT_TRUE (cache.contains(game_state(rules, {{card("2D")},{card("3H")},{card("AC")}}))); - ASSERT_FALSE(cache.contains(game_state(rules, {{card("3H")},{card("2D")},{card("3H")}}))); + cache.insert (game_state(rules, {{"AC"},{"2D"},{"3H"}})); + ASSERT_TRUE (cache.contains(game_state(rules, {{"2D"},{"3H"},{"AC"}}))); + ASSERT_FALSE(cache.contains(game_state(rules, {{"3H"},{"2D"},{"3H"}}))); // Test with empty piles cache.clear(); - cache.insert (game_state(rules, {{card("4C")},{card("5D")},{}})); - ASSERT_TRUE (cache.contains(game_state(rules, {{},{card("4C")},{card("5D")}}))); - ASSERT_FALSE(cache.contains(game_state(rules, {{},{card("4C")},{}}))); + cache.insert (game_state(rules, {{"4C"},{"5D"},{}})); + ASSERT_TRUE (cache.contains(game_state(rules, {{},{"4C"},{"5D"}}))); + ASSERT_FALSE(cache.contains(game_state(rules, {{},{"4C"},{}}))); // Test with piles of size > 1 cache.clear(); - cache.insert (game_state(rules, {{card("6C"),card("7D")},{card("8C")},{card("9D")}})); - ASSERT_TRUE (cache.contains(game_state(rules, {{card("8C")},{card("6C"),card("7D")},{card("9D")}}))); - ASSERT_FALSE(cache.contains(game_state(rules, {{card("8C")},{card("6C"),card("KD")},{card("9D")}}))); + cache.insert (game_state(rules, {{"6C","7D"},{"8C"},{"9D"}})); + ASSERT_TRUE (cache.contains(game_state(rules, {{"8C"},{"6C","7D"},{"9D"}}))); + ASSERT_FALSE(cache.contains(game_state(rules, {{"8C"},{"6C","KD"},{"9D"}}))); } TEST(GlobalCache, CommutativeReserve) { sol_rules rules; rules.reserve_size = 3; rules.build_pol = sol_rules::build_policy::SAME_SUIT; - game_state gs(rules, std::initializer_list<pile>{{},{},{}}); + game_state gs(rules, string_il{{},{},{}}); lru_cache cache(gs, 1000); - cache.insert (game_state(rules, {{card("AC")},{card("2D")},{card("3H")}})); - ASSERT_TRUE (cache.contains(game_state(rules, {{card("2D")},{card("3H")},{card("AC")}}))); - ASSERT_FALSE(cache.contains(game_state(rules, {{card("3H")},{card("2D")},{card("3H")}}))); + cache.insert (game_state(rules, {{"AC"},{"2D"},{"3H"}})); + ASSERT_TRUE (cache.contains(game_state(rules, {{"2D"},{"3H"},{"AC"}}))); + ASSERT_FALSE(cache.contains(game_state(rules, {{"3H"},{"2D"},{"3H"}}))); // Test with empty piles cache.clear(); - cache.insert (game_state(rules, {{card("4C")},{card("5D")},{}})); - ASSERT_TRUE (cache.contains(game_state(rules, {{},{card("4C")},{card("5D")}}))); - ASSERT_FALSE(cache.contains(game_state(rules, {{},{card("4C")},{}}))); + cache.insert (game_state(rules, {{"4C"},{"5D"},{}})); + ASSERT_TRUE (cache.contains(game_state(rules, {{},{"4C"},{"5D"}}))); + ASSERT_FALSE(cache.contains(game_state(rules, {{},{"4C"},{}}))); // Test with piles of size > 1 cache.clear(); - cache.insert (game_state(rules, {{card("6C"),card("7D")},{card("8C")},{card("9D")}})); - ASSERT_TRUE (cache.contains(game_state(rules, {{card("8C")},{card("6C"),card("7D")},{card("9D")}}))); - ASSERT_FALSE(cache.contains(game_state(rules, {{card("8C")},{card("6C"),card("KD")},{card("9D")}}))); + cache.insert (game_state(rules, {{"6C","7D"},{"8C"},{"9D"}})); + ASSERT_TRUE (cache.contains(game_state(rules, {{"8C"},{"6C","7D"},{"9D"}}))); + ASSERT_FALSE(cache.contains(game_state(rules, {{"8C"},{"6C","KD"},{"9D"}}))); } TEST(GlobalCache, CommutativeCells) { sol_rules rules; rules.cells = 3; rules.build_pol = sol_rules::build_policy::SAME_SUIT; - game_state gs(rules, std::initializer_list<pile>{{},{},{}}); + game_state gs(rules, string_il{{},{},{}}); lru_cache cache(gs, 1000); - cache.insert (game_state(rules, {{card("AC")},{card("2D")},{card("3H")}})); - ASSERT_TRUE (cache.contains(game_state(rules, {{card("2D")},{card("3H")},{card("AC")}}))); - ASSERT_FALSE(cache.contains(game_state(rules, {{card("3H")},{card("2D")},{card("3H")}}))); + cache.insert (game_state(rules, {{"AC"},{"2D"},{"3H"}})); + ASSERT_TRUE (cache.contains(game_state(rules, {{"2D"},{"3H"},{"AC"}}))); + ASSERT_FALSE(cache.contains(game_state(rules, {{"3H"},{"2D"},{"3H"}}))); // Test with empty piles cache.clear(); - cache.insert (game_state(rules, {{card("4C")},{card("5D")},{}})); - ASSERT_TRUE (cache.contains(game_state(rules, {{},{card("4C")},{card("5D")}}))); - ASSERT_FALSE(cache.contains(game_state(rules, {{},{card("4C")},{}}))); + cache.insert (game_state(rules, {{"4C"},{"5D"},{}})); + ASSERT_TRUE (cache.contains(game_state(rules, {{},{"4C"},{"5D"}}))); + ASSERT_FALSE(cache.contains(game_state(rules, {{},{"4C"},{}}))); } diff --git a/src/test/unit_tests/legal_move_gen_test.cpp b/src/test/unit_tests/legal_move_gen_test.cpp index f9ff52e..6ea73d5 100644 --- a/src/test/unit_tests/legal_move_gen_test.cpp +++ b/src/test/unit_tests/legal_move_gen_test.cpp @@ -15,14 +15,16 @@ typedef sol_rules::stock_deal_type sdt; using std::vector; using std::ostream; -using std::initializer_list; + +typedef std::initializer_list<std::initializer_list<std::string>> string_il; + TEST(LegalMoveGen, BuildPolAnySuit) { sol_rules sr; sr.build_pol = pol::ANY_SUIT; sr.tableau_pile_count = 5; - game_state gs(sr, initializer_list<pile>{ + game_state gs(sr, string_il{ {"2C"}, {"2H"}, {"2D"}, @@ -46,7 +48,7 @@ TEST(LegalMoveGen, BuildPolRedBlack) { sr.build_pol = pol::RED_BLACK; sr.tableau_pile_count = 5; - game_state gs(sr, initializer_list<pile>{ + game_state gs(sr, string_il{ {"2C"}, {"2H"}, {"2S"}, @@ -68,7 +70,7 @@ TEST(LegalMoveGen, BuildPolSameSuit) { sr.build_pol = pol::SAME_SUIT; sr.tableau_pile_count = 5; - game_state gs(sr, initializer_list<pile>{ + game_state gs(sr, string_il{ {"2C"}, {"2H"}, {"2S"}, @@ -89,7 +91,7 @@ TEST(LegalMoveGen, BuildPolNoBuild) { sr.build_pol = pol::NO_BUILD; sr.tableau_pile_count = 5; - game_state gs(sr, initializer_list<pile>{ + game_state gs(sr, string_il{ {"2C"}, {"2H"}, {"2S"}, @@ -109,7 +111,7 @@ TEST(LegalMoveGen, SpacesPolAny) { sr.spaces_pol = s_pol::ANY; sr.tableau_pile_count = 2; - game_state gs(sr, initializer_list<pile>{ + game_state gs(sr, string_il{ {}, {"AC"} }); @@ -127,7 +129,7 @@ TEST(LegalMoveGen, SpacesPolKings) { sr.spaces_pol = s_pol::KINGS; sr.tableau_pile_count = 3; - game_state gs(sr, initializer_list<pile>{ + game_state gs(sr, string_il{ {}, {"AC"}, {"KD"} @@ -146,7 +148,7 @@ TEST(LegalMoveGen, SpacesPolNoBuild) { sr.spaces_pol = s_pol::NO_BUILD; sr.tableau_pile_count = 2; - game_state gs(sr, initializer_list<pile>{ + game_state gs(sr, string_il{ {}, {"AC"} }); @@ -163,7 +165,7 @@ TEST(LegalMoveGen, MoveBuiltGroupTrue) { sr.move_built_group = true; sr.tableau_pile_count = 2; - game_state gs(sr, initializer_list<pile>{ + game_state gs(sr, string_il{ {}, {"2C", "AC"} }); @@ -182,7 +184,7 @@ TEST(LegalMoveGen, MoveBuiltGroupFalse) { sr.move_built_group = false; sr.tableau_pile_count = 2; - game_state gs(sr, initializer_list<pile>{ + game_state gs(sr, string_il{ {}, {"AC", "2C"} }); @@ -200,7 +202,7 @@ TEST(LegalMoveGen, Foundations) { sr.foundations = true; sr.tableau_pile_count = 4; - game_state gs(sr, initializer_list<pile>{ + game_state gs(sr, string_il{ {}, {}, {}, {}, {"AC"}, {"AH"}, @@ -225,7 +227,7 @@ TEST(LegalMoveGen, FoundationsRemovable) { sr.foundations_removable = true; sr.tableau_pile_count = 1; - game_state gs(sr, initializer_list<pile>{ + game_state gs(sr, string_il{ {"AC","2C","3C"}, // Without a few cards here dominance logic blocks {}, // removing from foundation {}, @@ -246,14 +248,13 @@ TEST(LegalMoveGen, Cells) { sr.cells = 2; sr.tableau_pile_count = 1; - game_state gs(sr, initializer_list<pile>{ + game_state gs(sr, string_il{ {"3D"}, {}, // The cells {"4H"} }); vector<move> actual_moves = gs.get_legal_moves(); vector<move> exp_moves = { - move(move::mtype::regular, 0, 1), move(move::mtype::regular, 0, 2), move(move::mtype::regular, 2, 1) }; @@ -268,7 +269,7 @@ TEST(LegalMoveGen, StockDealToWaste) { sr.stock_size = 2; sr.stock_deal_t = sdt::WASTE; - game_state gs(sr, initializer_list<pile>{ + game_state gs(sr, string_il{ {}, {}, {}, {}, // The foundations {"AC", "AD"}, // The stock {}, // The waste @@ -290,7 +291,7 @@ TEST(LegalMoveGen, StockDealToTableau) { sr.stock_size = 2; sr.stock_deal_t = sdt::TABLEAU_PILES; - game_state gs(sr, initializer_list<pile>{ + game_state gs(sr, string_il{ {}, {}, {}, {}, // The foundations {"AC", "AD"}, // The stock {"3H"}, {"5D"}, {"7C"} // The tableau piles |
