From fea5d4c3a84e3d83ed761579a7a7cc31279cc165 Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Wed, 20 Jun 2018 15:43:21 +0100 Subject: Set solver seed limit --- src/main/evaluation/solvability_calc.cpp | 10 ++++++---- src/main/evaluation/solvability_calc.h | 2 +- src/main/input-output/input/command_line_helper.cpp | 15 +++++++++++++-- src/main/input-output/input/command_line_helper.h | 2 ++ src/main/main.cpp | 3 ++- 5 files changed, 24 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/main/evaluation/solvability_calc.cpp b/src/main/evaluation/solvability_calc.cpp index ffe883b..3fcdf44 100644 --- a/src/main/evaluation/solvability_calc.cpp +++ b/src/main/evaluation/solvability_calc.cpp @@ -99,7 +99,7 @@ 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& resume) { +void solvability_calc::calculate_solvability_percentage(uint64_t timeout_, int seed_count, uint cores, const vector& resume) { vector resume_seeds(begin(resume) + 3, end(resume)); sort(begin(resume_seeds), end(resume_seeds)); @@ -123,11 +123,12 @@ 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](){ 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(); @@ -149,7 +150,8 @@ 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, diff --git a/src/main/evaluation/solvability_calc.h b/src/main/evaluation/solvability_calc.h index ef3492c..eabeb99 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&); + void calculate_solvability_percentage(uint64_t, int, uint, const std::vector&); private: typedef std::chrono::milliseconds millisec; diff --git a/src/main/input-output/input/command_line_helper.cpp b/src/main/input-output/input/command_line_helper.cpp index 2abef54..21eec88 100644 --- a/src/main/input-output/input/command_line_helper.cpp +++ b/src/main/input-output/input/command_line_helper.cpp @@ -38,12 +38,13 @@ command_line_helper::command_line_helper() "'solvability', 'benchmark' or list of deals to be solved.") ("classify", "outputs a simple 'solvable/not solvable' " "classification") - ("cache-capacity", po::value(), "sets an upper bound on the number of states allowed in " + ("cache-capacity", po::value(), "sets an upper bound on the number of states allowed in " "the cache") ("solvability", po::value(), "calculates the solvability " - "percentage of the supplied solitaire game, given a timeout in milliseconds. Must supply " + "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(), "adds a per-game timeout to the solvability percentage generation") ("resume", po::value>()->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 ...]") @@ -121,6 +122,12 @@ bool command_line_helper::parse(int argc, const char* argv[]) { solvability = -1; } + if (vm.count("timeout")) { + timeout = vm["timeout"].as(); + } else { + timeout = 604800000; // 1 week in milliseconds + } + if (vm.count("cores")) { cores = vm["cores"].as(); } else { @@ -248,6 +255,10 @@ int command_line_helper::get_solvability() { return solvability; } +uint64_t command_line_helper::get_timeout() { + return timeout; +} + vector command_line_helper::get_resume() { return resume; } diff --git a/src/main/input-output/input/command_line_helper.h b/src/main/input-output/input/command_line_helper.h index 3ce239d..744935f 100644 --- a/src/main/input-output/input/command_line_helper.h +++ b/src/main/input-output/input/command_line_helper.h @@ -24,6 +24,7 @@ public: std::vector get_resume(); uint64_t get_cache_capacity(); std::string get_describe_game_rules(); + uint64_t get_timeout(); private: bool assess_errors(); @@ -51,6 +52,7 @@ private: bool available_game_types; bool benchmark; uint64_t cache_capacity; + uint64_t timeout; }; #endif //SOLVITAIRE_COMMAND_LINE_HELPER_H diff --git a/src/main/main.cpp b/src/main/main.cpp index a492c26..d5c5dcc 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -51,7 +51,8 @@ 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_resume()); } // If a random deal seed has been supplied, solves it else if (clh.get_random_deal() != -1) { -- cgit v1.2.1 From dbb2dc6f3acadca6d1744a595fe0cd755367c62c Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Wed, 20 Jun 2018 16:30:12 +0100 Subject: Switch to wilson interval --- src/main/evaluation/binomial_ci.cpp | 45 ++++++++++++++++++++ src/main/evaluation/binomial_ci.h | 19 +++++++++ src/main/evaluation/solvability_calc.cpp | 71 ++++---------------------------- src/main/evaluation/solvability_calc.h | 8 ---- 4 files changed, 72 insertions(+), 71 deletions(-) create mode 100644 src/main/evaluation/binomial_ci.cpp create mode 100644 src/main/evaluation/binomial_ci.h (limited to 'src') 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 +#include +#include "binomial_ci.h" + +using std::pow; +using std::min; +using std::max; +using std::pair; + +pair binomial_ci::wilson(int s_, int f_) { + auto s = static_cast(s_); + auto f = static_cast(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 binomial_ci::wilson(int s, int f, int i) { + pair a = wilson(s, f + i); + pair b = wilson(s + i, f); + return {a.first, b.second}; +} + +pair 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 binomial_ci::agresti_coull(int s, int f, int i) { + pair a = agresti_coull(s, f + i); + pair 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 + +struct binomial_ci { + static std::pair wilson(int, int); + static std::pair wilson(int, int, int); + static std::pair agresti_coull(int, int); + static std::pair 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 3fcdf44..b246e68 100644 --- a/src/main/evaluation/solvability_calc.cpp +++ b/src/main/evaluation/solvability_calc.cpp @@ -4,10 +4,10 @@ #include #include #include -#include #include "solvability_calc.h" #include "../solver/solver.h" +#include "binomial_ci.h" using namespace std; @@ -48,15 +48,13 @@ void solvability_calc::print_header(long t) const { } void solvability_calc::print_row(const seed_results& seed_res, sol_result res, set& 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); - - cout << lower_bound * 100 - << ", " << upper_bound * 100 + pair interval = binomial_ci::wilson( + seed_res.solvable, + seed_res.unsolvable, + seed_res.intractable); + + cout << interval.first * 100 + << ", " << interval.second * 100 << ", " << seed_res.solvable << ", " << seed_res.unsolvable << ", " << seed_res.intractable @@ -236,56 +234,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(lround(d)); } diff --git a/src/main/evaluation/solvability_calc.h b/src/main/evaluation/solvability_calc.h index eabeb99..6ca640d 100644 --- a/src/main/evaluation/solvability_calc.h +++ b/src/main/evaluation/solvability_calc.h @@ -49,14 +49,6 @@ private: // 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); - const sol_rules& rules; const uint64_t cache_capacity; }; -- cgit v1.2.1 From abae8e0529cf2e6048e9b2eb8248434e1f9551de Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Wed, 20 Jun 2018 17:37:22 +0100 Subject: Add streamliner command line option (inc for spgs) --- src/main/evaluation/benchmark.cpp | 4 +- src/main/evaluation/benchmark.h | 2 +- src/main/evaluation/solvability_calc.cpp | 55 +++++++++++++++------- src/main/evaluation/solvability_calc.h | 10 ++-- src/main/game/global_cache.cpp | 16 +------ .../game_state.auto_foundation_moves.cpp | 4 +- src/main/game/search-state/game_state.cpp | 20 ++++---- src/main/game/search-state/game_state.h | 7 +-- .../input-output/input/command_line_helper.cpp | 15 ++++-- src/main/input-output/input/command_line_helper.h | 2 + src/main/main.cpp | 37 +++++++-------- 11 files changed, 95 insertions(+), 77 deletions(-) (limited to 'src') 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/solvability_calc.cpp b/src/main/evaluation/solvability_calc.cpp index b246e68..7c83ae8 100644 --- a/src/main/evaluation/solvability_calc.cpp +++ b/src/main/evaluation/solvability_calc.cpp @@ -10,6 +10,7 @@ #include "binomial_ci.h" using namespace std; +using boost::optional; /////////////////// // SETUP METHODS // @@ -23,7 +24,7 @@ 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" @@ -31,8 +32,9 @@ void solvability_calc::print_header(long t) const { ", Unsolvable" ", Intractable" ", Attempted Seed" - ", Seed State" - ", Time Taken(ms)" + ", Seed State"; + if (streamliners) cout << ", Streamliner Results"; + cout << ", Time Taken(ms)" ", States Searched" ", Unique States Searched" ", Backtracks" @@ -47,7 +49,8 @@ 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& seeds_in_progress) { +void solvability_calc::print_row(const seed_results& seed_res, sol_result res, + optional streamliner_result, set& seeds_in_progress) { pair interval = binomial_ci::wilson( seed_res.solvable, seed_res.unsolvable, @@ -70,6 +73,19 @@ void solvability_calc::print_row(const seed_results& seed_res, sol_result res, s cout << ", unsolvable"; break; } + if (streamliner_result) { + switch (*streamliner_result) { + case sol_result::type::timeout: + cout << ", timed-out"; + break; + case sol_result::type::solved: + cout << ", solved"; + break; + case sol_result::type::unsolvable: + cout << ", unsolvable"; + break; + } + } cout << ", " << res.time.count(); cout << ", " << res.sol_info.states_searched @@ -97,7 +113,8 @@ void solvability_calc::print_row(const seed_results& seed_res, sol_result res, s // SOLVING METHODS // ///////////////////// -void solvability_calc::calculate_solvability_percentage(uint64_t timeout_, int seed_count, uint cores, const vector& resume) { +void solvability_calc::calculate_solvability_percentage(uint64_t timeout_, int seed_count, uint cores, + bool streamliners, const vector& resume) { vector resume_seeds(begin(resume) + 3, end(resume)); sort(begin(resume_seeds), end(resume_seeds)); @@ -109,7 +126,7 @@ void solvability_calc::calculate_solvability_percentage(uint64_t timeout_, int s 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 @@ -122,7 +139,7 @@ void solvability_calc::calculate_solvability_percentage(uint64_t timeout_, int s futures.push_back(async( launch::async, [¤t_seed, &seed_res, &seeds_in_progress, &results_mutex, seed_count, timeout, sr, cc, i, - resume_seeds](){ + resume_seeds, streamliners](){ int my_seed = resume_seeds.size() > i ? resume_seeds[i] : current_seed++; @@ -131,16 +148,20 @@ void solvability_calc::calculate_solvability_percentage(uint64_t timeout_, int s seeds_in_progress.insert(my_seed); results_mutex.unlock(); - sol_result res = solve_seed(my_seed, timeout, sr, cc); + sol_result res = solve_seed(my_seed, timeout, sr, cc, streamliners); + + optional streamliner_result; + if (streamliners) { + streamliner_result = res.sol_type; + if (res.sol_type == sol_result::type::unsolvable) { + res = solve_seed(my_seed, timeout, sr, cc, false); + } + } 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; - } - print_row(seed_res, res, seeds_in_progress); + seed_res.add_result(res.sol_type); + print_row(seed_res, res, streamliner_result, seeds_in_progress); results_mutex.unlock(); my_seed = current_seed++; @@ -152,9 +173,9 @@ void solvability_calc::calculate_solvability_percentage(uint64_t timeout_, int s 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 terminate_solver(false); diff --git a/src/main/evaluation/solvability_calc.h b/src/main/evaluation/solvability_calc.h index 6ca640d..1c9e800 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(uint64_t, int, uint, const std::vector&); + void calculate_solvability_percentage(uint64_t, int, uint, bool, const std::vector&); private: typedef std::chrono::milliseconds millisec; @@ -32,7 +32,7 @@ private: }; struct seed_results { - seed_results(std::vector); + explicit seed_results(std::vector); void add_result(sol_result::type); std::atomic solvable; @@ -43,11 +43,11 @@ private: }; // Printing methods - void print_header(long) const; - static void print_row(const seed_results&, sol_result, std::set&); + void print_header(long, bool) const; + static void print_row(const seed_results&, sol_result, boost::optional, std::set&); // Solving methods - static sol_result solve_seed(int, millisec, const sol_rules&, uint64_t); + 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/global_cache.cpp b/src/main/game/global_cache.cpp index db0c6de..f688c33 100644 --- a/src/main/game/global_cache.cpp +++ b/src/main/game/global_cache.cpp @@ -81,13 +81,7 @@ void cached_game_state::add_card(card c, const game_state& gs) { // If the game is a 'hole-based' game, or suit-reduction is on, reduces // the cached suit of the card where possible - bool is_suit_symmetry = gs.rules.hole; -#ifdef SUIT_REDUCTION_STREAMLINER - is_suit_symmetry = true; -#endif -#ifdef NO_SUIT_SYMMETRY - is_suit_symmetry = false; -#endif + bool is_suit_symmetry = gs.streamliners || gs.rules.hole; if (is_suit_symmetry) { switch (gs.rules.build_pol) { @@ -141,13 +135,7 @@ size_t hasher::hash_value(card const& c) const { // If the game is a 'hole-based' game, or suit-reduction is enabled, hash // the reduced suit - bool is_suit_symmetry = init_gs.rules.hole; -#ifdef SUIT_REDUCTION_STREAMLINER - is_suit_symmetry = true; -#endif -#ifdef NO_SUIT_SYMMETRY - is_suit_symmetry = false; -#endif + bool is_suit_symmetry = init_gs.streamliners || init_gs.rules.hole; uint8_t suit_val; if (is_suit_symmetry) { diff --git a/src/main/game/search-state/game_state.auto_foundation_moves.cpp b/src/main/game/search-state/game_state.auto_foundation_moves.cpp index 0b62af4..1bec3fe 100644 --- a/src/main/game/search-state/game_state.auto_foundation_moves.cpp +++ b/src/main/game/search-state/game_state.auto_foundation_moves.cpp @@ -11,9 +11,7 @@ using std::max; bool game_state::is_valid_auto_foundation_move(pile::ref target_pile) const { -#ifndef AUTO_FOUNDATIONS_STREAMLINER - if (rules.build_pol == pol::NO_BUILD || rules.build_pol == pol::SAME_SUIT) -#endif + if (streamliners || rules.build_pol == pol::NO_BUILD || rules.build_pol == pol::SAME_SUIT) return true; card::suit_t target_suit(target_pile - foundations.front()); diff --git a/src/main/game/search-state/game_state.cpp b/src/main/game/search-state/game_state.cpp index 0a20f60..5d80d3e 100644 --- a/src/main/game/search-state/game_state.cpp +++ b/src/main/game/search-state/game_state.cpp @@ -40,10 +40,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 +102,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 deck = gen_shuffled_deck(seed, rules.max_rank, rules.two_decks); // If there is a hole, moves the ace of spades to it @@ -183,7 +185,7 @@ game_state::game_state(const sol_rules& s_rules, int seed) game_state::game_state(const sol_rules& s_rules, std::initializer_list il) - : game_state(s_rules) { + : game_state(s_rules, false) { pile::ref pr = 0; for (const pile& p : il) { for (const card c : p.pile_vec) { diff --git a/src/main/game/search-state/game_state.h b/src/main/game/search-state/game_state.h index 7c0f9ca..a0a74ea 100644 --- a/src/main/game/search-state/game_state.h +++ b/src/main/game/search-state/game_state.h @@ -28,9 +28,9 @@ public: /* Constructors */ // Creates a game state representation from a JSON doc - game_state(const sol_rules&, const rapidjson::Document&); + game_state(const sol_rules&, const rapidjson::Document&, bool); // Does the same from a seed - game_state(const sol_rules&, int seed); + game_state(const sol_rules&, int seed, bool); // Does the same but with an initialiser list (useful for testing) game_state(const sol_rules&, std::initializer_list); @@ -58,7 +58,7 @@ public: private: /* Constructors (& helper function) */ - explicit game_state(const sol_rules&); + explicit game_state(const sol_rules&, bool); static std::vector gen_shuffled_deck(int, card::rank_t, bool); /* Pile order logic */ @@ -116,6 +116,7 @@ private: /* Game rules */ const sol_rules rules; + bool streamliners; /* Pile references */ diff --git a/src/main/input-output/input/command_line_helper.cpp b/src/main/input-output/input/command_line_helper.cpp index 21eec88..ec9a375 100644 --- a/src/main/input-output/input/command_line_helper.cpp +++ b/src/main/input-output/input/command_line_helper.cpp @@ -38,18 +38,20 @@ command_line_helper::command_line_helper() "'solvability', 'benchmark' or list of deals to be solved.") ("classify", "outputs a simple 'solvable/not solvable' " "classification") - ("cache-capacity", po::value(), "sets an upper bound on the number of states allowed in " + ("cache-capacity", po::value(), "sets an upper bound on the number of states allowed in " "the cache") ("solvability", po::value(), "calculates the solvability " "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.") + "either 'random', 'benchmark', 'solvability' or list of deals to be solved.") ("timeout", po::value(), "adds a per-game timeout to the solvability percentage generation") ("resume", po::value>()->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(), "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 " @@ -141,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); @@ -278,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 744935f..c12bb69 100644 --- a/src/main/input-output/input/command_line_helper.h +++ b/src/main/input-output/input/command_line_helper.h @@ -21,6 +21,7 @@ public: uint get_cores(); bool get_available_game_types(); bool get_benchmark(); + bool get_streamliners(); std::vector get_resume(); uint64_t get_cache_capacity(); std::string get_describe_game_rules(); @@ -51,6 +52,7 @@ private: uint cores; bool available_game_types; bool benchmark; + bool streamliners; uint64_t cache_capacity; uint64_t timeout; }; diff --git a/src/main/main.cpp b/src/main/main.cpp index d5c5dcc..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 gen_rules(command_line_helper&); -void solve_random_game(int, const sol_rules&, bool, uint64_t); -void solve_input_files(vector, 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, 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[]) { @@ -52,15 +52,15 @@ int main(int argc, const char* argv[]) { if (clh.get_solvability() > 0) { solvability_calc solv_c(*rules, clh.get_cache_capacity()); solv_c.calculate_solvability_percentage(clh.get_timeout(), clh.get_solvability(), clh.get_cores(), - clh.get_resume()); + 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 { @@ -68,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; @@ -91,24 +91,23 @@ const optional 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 input_files, const sol_rules& rules, - bool classify, uint64_t cache_capacity) { +void solve_input_files(const vector 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: "; @@ -118,16 +117,16 @@ void solve_input_files(const vector 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"; } -- cgit v1.2.1 From 495ccca135cc139221b4205c7443499fee5ab63e Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Thu, 21 Jun 2018 11:53:11 +0100 Subject: Make randomness consistent --- src/main/game/search-state/game_state.cpp | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/main/game/search-state/game_state.cpp b/src/main/game/search-state/game_state.cpp index 5d80d3e..8303516 100644 --- a/src/main/game/search-state/game_state.cpp +++ b/src/main/game/search-state/game_state.cpp @@ -27,9 +27,9 @@ using std::end; using std::rbegin; using std::rend; using std::runtime_error; -using std::default_random_engine; using std::max; using std::ostream; +using std::mt19937; typedef sol_rules::build_policy pol; typedef sol_rules::stock_deal_type sdt; @@ -198,28 +198,20 @@ game_state::game_state(const sol_rules& s_rules, // Generates a randomly ordered vector of cards vector game_state::gen_shuffled_deck(int seed, card::rank_t max_rank, bool two_decks) { - vector values; - vector 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 deck; - for (uint8_t *i : v_ptrs) { - auto r = static_cast(((*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); + shuffle(begin(deck), end(deck), rng); return deck; } -- cgit v1.2.1 From 982c29136530be549f84af290e20523688d7fbdd Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Thu, 21 Jun 2018 15:07:07 +0100 Subject: Fixup randomness --- src/main/game/search-state/game_state.cpp | 18 +++++++++++++++++- src/main/game/search-state/game_state.h | 1 + 2 files changed, 18 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/main/game/search-state/game_state.cpp b/src/main/game/search-state/game_state.cpp index 8303516..82e413b 100644 --- a/src/main/game/search-state/game_state.cpp +++ b/src/main/game/search-state/game_state.cpp @@ -10,6 +10,7 @@ #include #include +#include #include "game_state.h" #include "document.h" @@ -211,10 +212,25 @@ vector 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); - shuffle(begin(deck), end(deck), rng); + game_state::shuffle(begin(deck), end(deck), rng); return deck; } +template +void game_state::shuffle(RandomIt first, RandomIt last, URBG&& g) { + typedef typename std::iterator_traits::difference_type diff_t; + typedef boost::random::uniform_int_distribution distr_t; + typedef typename distr_t::param_type param_t; + + distr_t D; + diff_t n = last - first; + for (diff_t i = n-1; i > 0; --i) { + using std::swap; + swap(first[i], first[D(g, param_t(0, i))]); + } +} + + //////////////////// // ALTERING STATE // diff --git a/src/main/game/search-state/game_state.h b/src/main/game/search-state/game_state.h index a0a74ea..f335de1 100644 --- a/src/main/game/search-state/game_state.h +++ b/src/main/game/search-state/game_state.h @@ -60,6 +60,7 @@ private: explicit game_state(const sol_rules&, bool); static std::vector gen_shuffled_deck(int, card::rank_t, bool); + template static void shuffle(RandomIt, RandomIt, URBG&&); /* Pile order logic */ -- cgit v1.2.1 From 933554c4c2b1f1925fdbf3ce9ccd9b632c8f172a Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Fri, 22 Jun 2018 16:02:51 +0100 Subject: Change move order generation --- .../game/search-state/game_state.legal_moves.cpp | 126 ++++++++++++++++++++- src/test/test_helper.cpp | 2 +- src/test/unit_tests/legal_move_gen_test.cpp | 1 - 3 files changed, 124 insertions(+), 5 deletions(-) (limited to 'src') 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..5fb48df 100644 --- a/src/main/game/search-state/game_state.legal_moves.cpp +++ b/src/main/game/search-state/game_state.legal_moves.cpp @@ -8,6 +8,7 @@ using std::vector; using std::min; +using std::list; using namespace rapidjson; typedef sol_rules::build_policy pol; @@ -21,6 +22,125 @@ typedef sol_rules::stock_deal_type sdt; // Note that the moves added last here, are tried first vector 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 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) { + list 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); + + /* + vector moves; // Attempts stock tableau deal and redeal @@ -49,10 +169,10 @@ vector 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,7 +184,7 @@ vector 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; } diff --git a/src/test/test_helper.cpp b/src/test/test_helper.cpp index eaa0aba..ab9e0cb 100644 --- a/src/test/test_helper.cpp +++ b/src/test/test_helper.cpp @@ -22,7 +22,7 @@ bool test_helper::is_solvable(const std::string& input_file, const std::string& 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; diff --git a/src/test/unit_tests/legal_move_gen_test.cpp b/src/test/unit_tests/legal_move_gen_test.cpp index f9ff52e..de6733e 100644 --- a/src/test/unit_tests/legal_move_gen_test.cpp +++ b/src/test/unit_tests/legal_move_gen_test.cpp @@ -253,7 +253,6 @@ TEST(LegalMoveGen, Cells) { vector actual_moves = gs.get_legal_moves(); vector exp_moves = { - move(move::mtype::regular, 0, 1), move(move::mtype::regular, 0, 2), move(move::mtype::regular, 2, 1) }; -- cgit v1.2.1 From 3a781e332c2a2d0d03b1eb35f1538929851aedaa Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Fri, 22 Jun 2018 16:21:37 +0100 Subject: Add simple simon --- .../search-state/game_state.auto_foundation_moves.cpp | 4 +++- src/main/game/search-state/game_state.legal_moves.cpp | 2 +- src/main/input-output/input/sol_preset_types.cpp | 16 ++++++++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) (limited to 'src') 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 1bec3fe..794c040 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,7 +11,9 @@ using std::max; bool game_state::is_valid_auto_foundation_move(pile::ref target_pile) const { - if (streamliners || rules.build_pol == pol::NO_BUILD || rules.build_pol == pol::SAME_SUIT) + 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()); 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 5fb48df..b7b910e 100644 --- a/src/main/game/search-state/game_state.legal_moves.cpp +++ b/src/main/game/search-state/game_state.legal_moves.cpp @@ -117,7 +117,7 @@ vector game_state::get_legal_moves(move parent_move) { } // Tableau / cells to hole / foundation moves - if (rules.hole || rules.foundations) { + if (rules.hole || (rules.foundations && !rules.foundations_comp_piles)) { list 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()); diff --git a/src/main/input-output/input/sol_preset_types.cpp b/src/main/input-output/input/sol_preset_types.cpp index 53141a8..5b751af 100644 --- a/src/main/input-output/input/sol_preset_types.cpp +++ b/src/main/input-output/input/sol_preset_types.cpp @@ -928,6 +928,22 @@ map 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 +} +)" + } }; -- cgit v1.2.1 From fc5009619fd3639359af26aa7fca7006e263799d Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Fri, 22 Jun 2018 17:21:34 +0100 Subject: Add full streamliner results to solvability --- src/main/evaluation/solvability_calc.cpp | 87 +++++++++++++++++++++----------- src/main/evaluation/solvability_calc.h | 5 +- 2 files changed, 61 insertions(+), 31 deletions(-) (limited to 'src') diff --git a/src/main/evaluation/solvability_calc.cpp b/src/main/evaluation/solvability_calc.cpp index 7c83ae8..b576548 100644 --- a/src/main/evaluation/solvability_calc.cpp +++ b/src/main/evaluation/solvability_calc.cpp @@ -30,11 +30,26 @@ void solvability_calc::print_header(long t, bool streamliners) const { ", Upper Bound" ", Solvable" ", Unsolvable" - ", Intractable" - ", Attempted Seed" - ", Seed State"; - if (streamliners) cout << ", Streamliner Results"; - cout << ", Time Taken(ms)" + ", 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" ", Backtracks" @@ -49,8 +64,7 @@ void solvability_calc::print_header(long t, bool streamliners) const { cout << fixed << setprecision(3); } -void solvability_calc::print_row(const seed_results& seed_res, sol_result res, - optional streamliner_result, set& seeds_in_progress) { +void solvability_calc::print_general_info(const seed_results& seed_res) { pair interval = binomial_ci::wilson( seed_res.solvable, seed_res.unsolvable, @@ -60,8 +74,11 @@ void solvability_calc::print_row(const seed_results& seed_res, sol_result res, << ", " << 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,21 +90,8 @@ void solvability_calc::print_row(const seed_results& seed_res, sol_result res, cout << ", unsolvable"; break; } - if (streamliner_result) { - switch (*streamliner_result) { - case sol_result::type::timeout: - cout << ", timed-out"; - break; - case sol_result::type::solved: - cout << ", solved"; - break; - case sol_result::type::unsolvable: - cout << ", unsolvable"; - break; - } - } cout << ", " << res.time.count(); - + cout << ", " << res.sol_info.states_searched << ", " << res.sol_info.unique_states_searched << ", " << res.sol_info.backtracks @@ -97,7 +101,13 @@ void solvability_calc::print_row(const seed_results& seed_res, sol_result res, << ", " << 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& seeds_in_progress) { for(auto iter = begin(seeds_in_progress); iter != end(seeds_in_progress); ++iter) { cout << ", " << *iter; } @@ -148,20 +158,37 @@ void solvability_calc::calculate_solvability_percentage(uint64_t timeout_, int s seeds_in_progress.insert(my_seed); results_mutex.unlock(); - sol_result res = solve_seed(my_seed, timeout, sr, cc, streamliners); + optional stream_res, no_stream_res; + sol_result final_res; - optional streamliner_result; if (streamliners) { - streamliner_result = res.sol_type; - if (res.sol_type == sol_result::type::unsolvable) { - res = solve_seed(my_seed, timeout, sr, cc, false); + 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); - seed_res.add_result(res.sol_type); - print_row(seed_res, res, streamliner_result, seeds_in_progress); + 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_seeds_in_prog(seeds_in_progress); + results_mutex.unlock(); my_seed = current_seed++; diff --git a/src/main/evaluation/solvability_calc.h b/src/main/evaluation/solvability_calc.h index 1c9e800..9307649 100644 --- a/src/main/evaluation/solvability_calc.h +++ b/src/main/evaluation/solvability_calc.h @@ -44,7 +44,10 @@ private: // Printing methods void print_header(long, bool) const; - static void print_row(const seed_results&, sol_result, boost::optional, std::set&); + 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&); // Solving methods static sol_result solve_seed(int, millisec, const sol_rules&, uint64_t, bool); -- cgit v1.2.1 From a33fce2a22243fc3d29571c88be6f3c3cef91bec Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Mon, 25 Jun 2018 10:50:50 +0100 Subject: Add face up cards json rule --- src/main/input-output/input/sol_preset_types.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/main/input-output/input/sol_preset_types.cpp b/src/main/input-output/input/sol_preset_types.cpp index 5b751af..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 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 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 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, -- cgit v1.2.1 From 2b92783762c2824e519b881afbf5d66a606c394f Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Mon, 25 Jun 2018 11:50:34 +0100 Subject: Add card face down unit tests --- src/main/game/card.cpp | 10 + src/main/game/card.h | 4 + src/main/game/sol_rules.h | 6 + src/test/test_helper.cpp | 6 +- src/test/test_helper.h | 2 +- src/test/unit_tests/built_group_move_gen_test.cpp | 56 ++--- src/test/unit_tests/face_up_cards_test.cpp | 273 ++++++++++++++++++++++ 7 files changed, 325 insertions(+), 32 deletions(-) create mode 100644 src/test/unit_tests/face_up_cards_test.cpp (limited to 'src') diff --git a/src/main/game/card.cpp b/src/main/game/card.cpp index cfb94a9..710b394 100644 --- a/src/main/game/card.cpp +++ b/src/main/game/card.cpp @@ -85,6 +85,16 @@ std::string card::to_string() const { return s; } +bool card::is_face_up() const { + return true; +} + +void card::turn_face_up() { +} + +void card::turn_face_down() { +} + const card card::divider = card(0, 0); bool operator==(const card& l, const card& r) { diff --git a/src/main/game/card.h b/src/main/game/card.h index a4c6b1e..ad14d2b 100644 --- a/src/main/game/card.h +++ b/src/main/game/card.h @@ -39,6 +39,10 @@ public: colour_t get_colour() const; rank_t get_rank() const; + bool is_face_up() const; + void turn_face_up(); + void turn_face_down(); + std::string to_string() const; private: 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/test/test_helper.cpp b/src/test/test_helper.cpp index ab9e0cb..e1aa0a7 100644 --- a/src/test/test_helper.cpp +++ b/src/test/test_helper.cpp @@ -62,9 +62,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 piles, - vector exp_moves) { +void test_helper::expected_moves_test(sol_rules sr, + initializer_list piles, + vector exp_moves) { game_state gs(sr, piles); vector actual_moves = gs.get_legal_moves(); diff --git a/src/test/test_helper.h b/src/test/test_helper.h index 463a935..0690d0e 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 cards); - static void run_built_group_test(sol_rules sr, std::initializer_list, std::vector); + static void expected_moves_test(sol_rules sr, std::initializer_list, std::vector); static bool moves_eq(std::vector&, std::vector&); }; 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..6e2d6a3 --- /dev/null +++ b/src/test/unit_tests/face_up_cards_test.cpp @@ -0,0 +1,273 @@ +// +// Created by thecharlesblake on 1/11/18. +// + +#include + +#include "../test_helper.h" +#include "../../main/game/search-state/game_state.h" +#include "../../main/game/sol_rules.h" + +typedef sol_rules::build_policy pol; +typedef sol_rules::face_up_policy fu; +typedef move::mtype mt; + +using std::vector; +using std::ostream; +using std::initializer_list; + +#define ASSERT_FACE_UP(card) ASSERT_TRUE(card.is_face_up()) << card.to_string() << " should be face up" +#define ASSERT_FACE_DOWN(card) ASSERT_FALSE(card.is_face_up()) << 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; + + initializer_list 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; + + initializer_list 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; + + initializer_list piles = { + {"3S"}, + {"2s","AS"}, // from + {} // to + }; + + game_state gs(sr, piles); + gs.make_move(move(mt::regular, 1, 2)); + + 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 = 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; + + initializer_list piles = { + {"4S"}, + {"3s","2S","AS"}, // from + {} // to + }; + + game_state gs(sr, piles); + gs.make_move(move(mt::built_group, 1, 2, 2)); + + ASSERT_FACE_UP(gs.get_data()[0][0]); + ASSERT_FACE_UP(gs.get_data()[1][0]); + ASSERT_FACE_UP(gs.get_data()[2][0]); + ASSERT_FACE_UP(gs.get_data()[2][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; + + initializer_list piles = { + {"3S"}, + {"2S","AS"}, // from + {} // to + }; + + game_state gs(sr, piles); + gs.make_move(move(mt::regular, 1, 2)); + + 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; + + initializer_list piles = { + {"3S"}, + {"2s","AS"}, + {} + }; + + test_helper::expected_moves_test( + sr, + { + {"3S"}, + {"2s","AS"}, + {} + }, + { + // Regular moves + move(move::mtype::regular, 0, 2, 1), + move(move::mtype::regular, 1, 2, 1), + } + ); +} + +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; + + initializer_list piles = { + {"3S"}, + {"2s","AS"}, + {} + }; + + 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; + + initializer_list piles = { + {"3S"}, + {"2s","AS"}, + {} + }; + + test_helper::expected_moves_test( + sr, + { + {"4S"}, + {"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, 2, 2) + } + ); +} + +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; + + initializer_list piles = { + {"3S"}, + {"2s","AS"}, // from + {} // to + }; + + game_state gs(sr, piles); + move m = move(mt::regular, 1, 2); + 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; + + initializer_list piles = { + {"4S"}, + {"3s","2S","AS"}, // from + {} // to + }; + + game_state gs(sr, piles); + move m = move(mt::built_group, 1, 2, 2); + 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]); +} -- cgit v1.2.1 From 92d5705cb81e4fa4d971c8965b4cae7bc93f3eca Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Mon, 25 Jun 2018 11:54:07 +0100 Subject: Update rules schema to allow face up cards option --- src/main/input-output/input/json-parsing/rules_parser.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src') 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..e55c313 100644 --- a/src/main/input-output/input/json-parsing/rules_parser.cpp +++ b/src/main/input-output/input/json-parsing/rules_parser.cpp @@ -318,7 +318,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}, -- cgit v1.2.1 From dbfd7469b1d62d3cd32325af6aeb01e975db0111 Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Mon, 25 Jun 2018 11:57:56 +0100 Subject: Add face up card rules parser --- .../input-output/input/json-parsing/rules_parser.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'src') 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 e55c313..91f6549 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,22 @@ 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(); + } else { + json_helper::json_parse_err("[tableau piles][face up cards] must be a string"); + } + } + if (face_up_str == "all") { + sr.face_up = fu::ALL; + } else if (face_up_str == "top") { + sr.face_up = fu::TOP_CARDS; + } else { + json_helper::json_parse_err("[tableau piles][face up cards] is invalid"); + } + } else { json_helper::json_parse_err("[tableau piles] must be an object"); } -- cgit v1.2.1 From 8167f678fcbfc34982458b0311346bbeb04da9af Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Mon, 25 Jun 2018 12:25:02 +0100 Subject: Add face down deal parsing --- src/main/game/card.cpp | 9 +++++---- src/main/game/card.h | 7 ++++--- src/main/input-output/input/json-parsing/deal_parser.cpp | 4 +++- 3 files changed, 12 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/main/game/card.cpp b/src/main/game/card.cpp index 710b394..21bbd2f 100644 --- a/src/main/game/card.cpp +++ b/src/main/game/card.cpp @@ -22,11 +22,12 @@ 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_possible) {} // TODO: CHANGE! -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])) { @@ -95,7 +96,7 @@ void card::turn_face_up() { void card::turn_face_down() { } -const card card::divider = card(0, 0); +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 ad14d2b..ea808b4 100644 --- a/src/main/game/card.h +++ b/src/main/game/card.h @@ -31,8 +31,8 @@ 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; @@ -50,7 +50,8 @@ private: static rank_t rank_from_str(const char* c); 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/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)); } } } -- cgit v1.2.1 From a99b32bf9f62c4efa6c959bdd6ee4d4e52054bd0 Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Mon, 25 Jun 2018 12:30:23 +0100 Subject: Add game state face down random gen --- src/main/game/search-state/game_state.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/main/game/search-state/game_state.cpp b/src/main/game/search-state/game_state.cpp index 82e413b..8a1298b 100644 --- a/src/main/game/search-state/game_state.cpp +++ b/src/main/game/search-state/game_state.cpp @@ -18,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; @@ -34,6 +35,7 @@ using std::mt19937; typedef sol_rules::build_policy pol; typedef sol_rules::stock_deal_type sdt; +typedef sol_rules::face_up_policy fu; ////////////////// // CONSTRUCTORS // @@ -155,7 +157,10 @@ game_state::game_state(const sol_rules& s_rules, int seed, bool streamliners_) 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. @@ -176,6 +181,12 @@ game_state::game_state(const sol_rules& s_rules, int seed, bool streamliners_) } } + // 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(); -- cgit v1.2.1 From b3cd52e5b844414f66a2e9fde996ddeaf0b68fec Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Mon, 25 Jun 2018 12:31:41 +0100 Subject: Add state printing for face down cards --- src/main/game/card.cpp | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src') diff --git a/src/main/game/card.cpp b/src/main/game/card.cpp index 21bbd2f..12e577f 100644 --- a/src/main/game/card.cpp +++ b/src/main/game/card.cpp @@ -69,6 +69,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; -- cgit v1.2.1 From b9822b9fe08f423a32d5218ec8ce8bbcbf4c3a98 Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Mon, 25 Jun 2018 12:38:18 +0100 Subject: Correct face up rules parsing bug --- .../input-output/input/json-parsing/rules_parser.cpp | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'src') 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 91f6549..29e755b 100644 --- a/src/main/input-output/input/json-parsing/rules_parser.cpp +++ b/src/main/input-output/input/json-parsing/rules_parser.cpp @@ -166,17 +166,20 @@ void rules_parser::modify_sol_rules(sol_rules& sr, Document& d) { 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"); } } - if (face_up_str == "all") { - sr.face_up = fu::ALL; - } else if (face_up_str == "top") { - sr.face_up = fu::TOP_CARDS; - } else { - json_helper::json_parse_err("[tableau piles][face up cards] is invalid"); - } } else { json_helper::json_parse_err("[tableau piles] must be an object"); -- cgit v1.2.1 From 36d103ffa36e712b3d4eac08d608929a8a8987c1 Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Mon, 25 Jun 2018 14:02:49 +0100 Subject: Add test to make sure cache encodes face up --- src/test/unit_tests/face_up_cards_test.cpp | 36 ++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'src') diff --git a/src/test/unit_tests/face_up_cards_test.cpp b/src/test/unit_tests/face_up_cards_test.cpp index 6e2d6a3..7c6228b 100644 --- a/src/test/unit_tests/face_up_cards_test.cpp +++ b/src/test/unit_tests/face_up_cards_test.cpp @@ -7,6 +7,7 @@ #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; @@ -271,3 +272,38 @@ TEST(FaceUpCards, UndoBuiltGroupTurnsBackFaceDown) { 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; + + initializer_list 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)); + new_state = cache.insert(gs).second; + ASSERT_TRUE(new_state) << "AH to empty pile"; + + gs.make_move(move(mt::regular, 2, 1)); + new_state = cache.insert(gs).second; + ASSERT_TRUE(new_state) << "AH back to face-up 2S"; + + gs.make_move(move(mt::regular, 1, 0)); + new_state = cache.insert(gs).second; + ASSERT_TRUE(new_state) << "AH to 2C"; + + gs.make_move(move(mt::regular, 0, 1)); + new_state = cache.insert(gs).second; + ASSERT_FALSE(new_state) << "AH to 2S"; +} -- cgit v1.2.1 From 797400ea32ac1f6588e834fc1bcb42aa67fa6863 Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Tue, 26 Jun 2018 10:37:54 +0100 Subject: Finished klondike correct impl --- src/main/game/card.cpp | 21 +++- src/main/game/card.h | 3 +- src/main/game/move.cpp | 10 +- src/main/game/move.h | 5 +- .../game_state.auto_foundation_moves.cpp | 8 +- src/main/game/search-state/game_state.cpp | 60 +++++++++++- src/main/game/search-state/game_state.h | 8 +- .../game/search-state/game_state.legal_moves.cpp | 31 +++++- src/test/test_helper.cpp | 9 +- src/test/test_helper.h | 2 +- src/test/unit_tests/face_up_cards_test.cpp | 108 ++++++++++++--------- src/test/unit_tests/global_cache_test.cpp | 55 +++++------ src/test/unit_tests/legal_move_gen_test.cpp | 32 +++--- 13 files changed, 240 insertions(+), 112 deletions(-) (limited to 'src') diff --git a/src/main/game/card.cpp b/src/main/game/card.cpp index 12e577f..60aaa74 100644 --- a/src/main/game/card.cpp +++ b/src/main/game/card.cpp @@ -25,7 +25,8 @@ const card::colour_t card::colour::Red = 1; card::card(suit_t s, rank_t r, bool fd) : card_rank(r), card_suit(s), face_down(fd) {} 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_possible) {} // TODO: CHANGE! + 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, false) {} @@ -52,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; } @@ -88,14 +101,16 @@ std::string card::to_string() const { return s; } -bool card::is_face_up() const { - return true; +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(); diff --git a/src/main/game/card.h b/src/main/game/card.h index ea808b4..7885d66 100644 --- a/src/main/game/card.h +++ b/src/main/game/card.h @@ -39,7 +39,7 @@ public: colour_t get_colour() const; rank_t get_rank() const; - bool is_face_up() const; + bool is_face_down() const; void turn_face_up(); void turn_face_down(); @@ -48,6 +48,7 @@ public: 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 : 2; 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 794c040..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 @@ -86,7 +86,13 @@ boost::optional 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 8a1298b..7870b1b 100644 --- a/src/main/game/search-state/game_state.cpp +++ b/src/main/game/search-state/game_state.cpp @@ -32,6 +32,7 @@ using std::runtime_error; 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; @@ -196,11 +197,11 @@ game_state::game_state(const sol_rules& s_rules, int seed, bool streamliners_) } game_state::game_state(const sol_rules& s_rules, - std::initializer_list il) - : game_state(s_rules, false) { - pile::ref pr = 0; - for (const pile& p : il) { - for (const card c : p.pile_vec) { + std::initializer_list> 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++; @@ -269,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) { @@ -293,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) { @@ -301,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 @@ -309,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 @@ -331,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) { @@ -338,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]); @@ -427,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 f335de1..b8b0d03 100644 --- a/src/main/game/search-state/game_state.h +++ b/src/main/game/search-state/game_state.h @@ -32,7 +32,7 @@ public: // Does the same from a 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); + game_state(const sol_rules&, std::initializer_list>); /* Altering state */ @@ -80,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; @@ -107,6 +111,8 @@ private: bool is_next_legal_card(sol_rules::build_policy, card, card) const; + void turn_face_down_cards(std::vector&) const; + /* Auto-foundation moves */ bool is_valid_auto_foundation_move(pile::ref) const; 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 b7b910e..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,6 +3,7 @@ // #include "game_state.h" +#include "../sol_rules.h" #include @@ -14,6 +15,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; //////////////////////////////// @@ -136,9 +138,11 @@ vector game_state::get_legal_moves(move parent_move) { if (rules.move_built_group) add_built_group_moves(moves); - if (rules.foundations_comp_piles) // I.E. Spider-type winning condition + 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 moves; @@ -185,6 +189,7 @@ vector 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; } @@ -352,8 +357,12 @@ void game_state::add_built_group_moves(vector &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); + } } } } @@ -362,7 +371,9 @@ void game_state::add_built_group_moves(vector &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; } @@ -374,6 +385,9 @@ void game_state::add_empty_built_group_moves(vector& moves, pile::ref rem_ do { moves.emplace_back(move::mtype::built_group, rem_ref, add_ref, static_cast(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 { @@ -394,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& 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/test/test_helper.cpp b/src/test/test_helper.cpp index e1aa0a7..1bc1f24 100644 --- a/src/test/test_helper.cpp +++ b/src/test/test_helper.cpp @@ -14,10 +14,12 @@ #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> 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); @@ -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{ + game_state gs = game_state(rules, string_il{ {}, {}, {}, @@ -63,7 +65,7 @@ void test_helper::run_foundations_dominance_test(sol_rules::build_policy policy, } void test_helper::expected_moves_test(sol_rules sr, - initializer_list piles, + string_il piles, vector exp_moves) { game_state gs(sr, piles); vector 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 0690d0e..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 cards); - static void expected_moves_test(sol_rules sr, std::initializer_list, std::vector); + static void expected_moves_test(sol_rules sr, std::initializer_list>, std::vector); static bool moves_eq(std::vector&, std::vector&); }; diff --git a/src/test/unit_tests/face_up_cards_test.cpp b/src/test/unit_tests/face_up_cards_test.cpp index 7c6228b..5bf65cb 100644 --- a/src/test/unit_tests/face_up_cards_test.cpp +++ b/src/test/unit_tests/face_up_cards_test.cpp @@ -15,10 +15,11 @@ typedef move::mtype mt; using std::vector; using std::ostream; -using std::initializer_list; -#define ASSERT_FACE_UP(card) ASSERT_TRUE(card.is_face_up()) << card.to_string() << " should be face up" -#define ASSERT_FACE_DOWN(card) ASSERT_FALSE(card.is_face_up()) << card.to_string() << " should be face down" +typedef std::initializer_list> 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) { @@ -29,7 +30,7 @@ TEST(FaceUpCards, CapsFaceUpIfFaceUpNotAll) { sr.move_built_group = true; sr.face_up = fu::TOP_CARDS; - initializer_list piles = { + string_il piles = { {"3S"}, {"2s","AS"}, {} @@ -50,7 +51,7 @@ TEST(FaceUpCards, IgnoreCapsIfFaceUpAll) { sr.move_built_group = true; sr.face_up = fu::ALL; - initializer_list piles = { + string_il piles = { {"3S"}, {"2s","AS"}, {} @@ -71,14 +72,24 @@ TEST(FaceUpCards, MoveTurnsFaceUp) { sr.move_built_group = true; sr.face_up = fu::TOP_CARDS; - initializer_list piles = { + 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)); + 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]); @@ -87,25 +98,34 @@ TEST(FaceUpCards, MoveTurnsFaceUp) { TEST(FaceUpCards, BuiltGroupMoveTurnsFaceUp) { sol_rules sr; - sr.tableau_pile_count = 3; + 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; - initializer_list piles = { - {"4S"}, + 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, 1, 2, 2)); + 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()[2][0]); - ASSERT_FACE_UP(gs.get_data()[2][1]); + ASSERT_FACE_UP(gs.get_data()[1][1]); } TEST(FaceUpCards, MoveAlreadyFaceUpNoChange) { @@ -116,14 +136,24 @@ TEST(FaceUpCards, MoveAlreadyFaceUpNoChange) { sr.move_built_group = true; sr.face_up = fu::TOP_CARDS; - initializer_list piles = { + string_il piles = { {"3S"}, - {"2S","AS"}, // from + {"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)); + 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]); @@ -138,12 +168,6 @@ TEST(FaceUpCards, FaceDownNoBuiltGroup) { sr.move_built_group = true; sr.face_up = fu::TOP_CARDS; - initializer_list piles = { - {"3S"}, - {"2s","AS"}, - {} - }; - test_helper::expected_moves_test( sr, { @@ -153,8 +177,8 @@ TEST(FaceUpCards, FaceDownNoBuiltGroup) { }, { // Regular moves - move(move::mtype::regular, 0, 2, 1), - move(move::mtype::regular, 1, 2, 1), + move(move::mtype::regular, 0, 2, 1, false), + move(move::mtype::regular, 1, 2, 1, true), } ); } @@ -167,12 +191,6 @@ TEST(FaceUpCards, FaceUpBuiltGroup) { sr.move_built_group = true; sr.face_up = fu::TOP_CARDS; - initializer_list piles = { - {"3S"}, - {"2s","AS"}, - {} - }; - test_helper::expected_moves_test( sr, { @@ -200,12 +218,6 @@ TEST(FaceUpCards, FaceUpBuiltGroup2Not3) { sr.move_built_group = true; sr.face_up = fu::TOP_CARDS; - initializer_list piles = { - {"3S"}, - {"2s","AS"}, - {} - }; - test_helper::expected_moves_test( sr, { @@ -215,11 +227,11 @@ TEST(FaceUpCards, FaceUpBuiltGroup2Not3) { }, { // Regular moves - move(mt::regular, 0, 2, 1), - move(mt::regular, 1, 2, 1), + move(mt::regular, 0, 2, 1, false), + move(mt::regular, 1, 2, 1, false), // Built group moves - move(mt::built_group, 1, 2, 2) + move(mt::built_group, 1, 2, 2, true) } ); } @@ -232,14 +244,14 @@ TEST(FaceUpCards, UndoTurnsBackFaceDown) { sr.move_built_group = true; sr.face_up = fu::TOP_CARDS; - initializer_list piles = { + string_il piles = { {"3S"}, {"2s","AS"}, // from {} // to }; game_state gs(sr, piles); - move m = move(mt::regular, 1, 2); + move m = move(mt::regular, 1, 2, 1, true); gs.make_move(m); gs.undo_move(m); @@ -256,14 +268,14 @@ TEST(FaceUpCards, UndoBuiltGroupTurnsBackFaceDown) { sr.move_built_group = true; sr.face_up = fu::TOP_CARDS; - initializer_list piles = { + string_il piles = { {"4S"}, {"3s","2S","AS"}, // from {} // to }; game_state gs(sr, piles); - move m = move(mt::built_group, 1, 2, 2); + move m = move(mt::built_group, 1, 2, 2, true); gs.make_move(m); gs.undo_move(m); @@ -281,7 +293,7 @@ TEST(FaceUpCards, CacheEncodesFaceDown) { sr.move_built_group = true; sr.face_up = fu::TOP_CARDS; - initializer_list piles = { + string_il piles = { {"2C"}, {"2s","AH"}, {} @@ -291,19 +303,19 @@ TEST(FaceUpCards, CacheEncodesFaceDown) { lru_cache cache(gs, 1000); bool new_state; - gs.make_move(move(mt::regular, 1, 2)); + 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)); + 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)); + 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)); + 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> 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{{},{},{}}); + 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{{},{},{}}); + 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{{},{},{}}); + 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 de6733e..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> 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{ + 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{ + 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{ + 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{ + 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{ + 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{ + 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{ + 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{ + 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{ + 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{ + 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{ + game_state gs(sr, string_il{ {"AC","2C","3C"}, // Without a few cards here dominance logic blocks {}, // removing from foundation {}, @@ -246,7 +248,7 @@ TEST(LegalMoveGen, Cells) { sr.cells = 2; sr.tableau_pile_count = 1; - game_state gs(sr, initializer_list{ + game_state gs(sr, string_il{ {"3D"}, {}, // The cells {"4H"} }); @@ -267,7 +269,7 @@ TEST(LegalMoveGen, StockDealToWaste) { sr.stock_size = 2; sr.stock_deal_t = sdt::WASTE; - game_state gs(sr, initializer_list{ + game_state gs(sr, string_il{ {}, {}, {}, {}, // The foundations {"AC", "AD"}, // The stock {}, // The waste @@ -289,7 +291,7 @@ TEST(LegalMoveGen, StockDealToTableau) { sr.stock_size = 2; sr.stock_deal_t = sdt::TABLEAU_PILES; - game_state gs(sr, initializer_list{ + game_state gs(sr, string_il{ {}, {}, {}, {}, // The foundations {"AC", "AD"}, // The stock {"3H"}, {"5D"}, {"7C"} // The tableau piles -- cgit v1.2.1