From 1ef39e9ba6ce1a622c53f38929d1e77b4f2518fc Mon Sep 17 00:00:00 2001 From: Tom Harley Date: Thu, 24 Jan 2019 12:10:15 +0000 Subject: It works with fortune's favor It seems to be consistently running to completion successfully! It's been renamed to `metasol` for now, since that's a better name than `test`. Next step will probably be eliminating the namespace pollution in `metasol.cpp`. --- src/main/api.cpp | 30 +++++++++++++++++++++++++----- src/main/api.h | 16 ++++++++++++++-- src/main/game/search-state/game_state.cpp | 30 ++++++++++++++++++++++++++++++ src/main/game/search-state/game_state.h | 4 ++++ src/main/solver/solver.cpp | 2 +- 5 files changed, 74 insertions(+), 8 deletions(-) (limited to 'src/main') diff --git a/src/main/api.cpp b/src/main/api.cpp index fc3da3a..1709a49 100644 --- a/src/main/api.cpp +++ b/src/main/api.cpp @@ -1,14 +1,21 @@ #include + #include #include +#include + #include "game/move.h" #include "game/search-state/game_state.h" #include "solver/solver.h" -bool get_moves(std::vector& moves, game_state& gs, +enum finished_state { + SOLUTION_FOUND, NO_SOLUTION, TIMEOUT, SOLVER_OOM, CANCELLED +}; + +finished_state get_moves(std::vector& moves, game_state& gs, uint64_t cache_capacity, uint64_t timeout); -bool get_moves(std::vector& moves, game_state& gs, +finished_state get_moves(std::vector& moves, game_state& gs, uint64_t cache_capacity, uint64_t timeout) { moves.clear(); @@ -19,8 +26,21 @@ bool get_moves(std::vector& moves, game_state& gs, for (auto &it : nodes) { moves.push_back(it.mv); } - return true; - } else { - return false; + } + + switch (res.sol_type) { + case solver::result::type::SOLVED: + return SOLUTION_FOUND; + case solver::result::type::UNSOLVABLE: + return NO_SOLUTION; + case solver::result::type::TIMEOUT: + return TIMEOUT; + case solver::result::type::MEM_LIMIT: + return SOLVER_OOM; + case solver::result::type::TERMINATED: + return CANCELLED; + default: + assert(false); + return NO_SOLUTION; } } diff --git a/src/main/api.h b/src/main/api.h index 86e3441..e454816 100644 --- a/src/main/api.h +++ b/src/main/api.h @@ -5,6 +5,7 @@ #include "game/move.h" #include "game/sol_rules.h" +#include "game/card.h" // just for now, until I construct my own rules stuff: #include "input-output/input/json-parsing/rules_parser.h" @@ -12,9 +13,20 @@ typedef std::vector> piles; typedef std::vector movelist; +enum finished_state { + SOLUTION_FOUND, NO_SOLUTION, TIMEOUT, SOLVER_OOM, CANCELLED +}; + class game_state { public: game_state(const sol_rules&, piles); + game_state(const sol_rules&, + std::vector>, + std::vector>, + std::vector>); + + bool is_solved() const; + enum class streamliner_options {NONE, AUTO_FOUNDATIONS, SUIT_SYMMETRY, BOTH}; void make_move(move); @@ -62,5 +74,5 @@ class game_state { * * Returns true if a solution was found; false otherwise. */ -bool get_moves(movelist& moves, game_state& gs, uint64_t cache_capacity, - uint64_t timeout); +finished_state get_moves(movelist& moves, game_state& gs, + uint64_t cache_capacity, uint64_t timeout); diff --git a/src/main/game/search-state/game_state.cpp b/src/main/game/search-state/game_state.cpp index a547f7e..6ea550c 100644 --- a/src/main/game/search-state/game_state.cpp +++ b/src/main/game/search-state/game_state.cpp @@ -306,6 +306,36 @@ game_state::game_state(const sol_rules& s_rules, } } +game_state::game_state(const sol_rules& s_rules, + std::vector> suits, + std::vector> ranks, + std::vector> face_down) + : game_state(s_rules, sos::NONE) {pile::ref pr = 0; + assert(suits.size() == ranks.size()); + assert(ranks.size() == face_down.size()); + for (std::vector>::size_type i = 0; + i < suits.size(); ++i) { + assert(suits[i].size() == ranks[i].size()); + assert(ranks[i].size() == face_down[i].size()); + for (std::vector::size_type j = 0; j < suits[i].size(); + ++j) { + card c(suits[i][j], ranks[i][j], face_down[i][j]); + place_card(pr, c); + } + pr++; + } + + // Sets foundations base appropriately + if (rules.foundations_present && rules.foundations_base == boost::none) { + for (auto f : foundations) { + if (!piles[f].empty()) { + foundations_base = piles[f].top_card().get_rank(); + return; + } + } + } +} + // Generates a randomly ordered vector of cards vector game_state::gen_shuffled_deck(card::rank_t max_rank, bool two_decks, mt19937 rng) { diff --git a/src/main/game/search-state/game_state.h b/src/main/game/search-state/game_state.h index 9f8e3b9..1698122 100644 --- a/src/main/game/search-state/game_state.h +++ b/src/main/game/search-state/game_state.h @@ -41,6 +41,10 @@ public: game_state(const sol_rules&, std::initializer_list>); // Does the same but with vectors game_state(const sol_rules&, std::vector>); + // Does the same but with lots of vectors + game_state(const sol_rules&, std::vector>, + std::vector>, + std::vector>); /* Altering state */ diff --git a/src/main/solver/solver.cpp b/src/main/solver/solver.cpp index c4e8c84..e10396f 100644 --- a/src/main/solver/solver.cpp +++ b/src/main/solver/solver.cpp @@ -61,7 +61,7 @@ solver::node::node(const move m) solver::result solver::run(boost::optional timeout) { // Set interrupt handler - signal(SIGINT, sigint_handler); + /* signal(SIGINT, sigint_handler); */ // Set timings const clock::time_point start_time = clock::now(); -- cgit v1.2.1