diff options
| author | Tom Harley | 2018-11-16 11:09:45 +0000 |
|---|---|---|
| committer | Tom Harley | 2018-11-16 11:09:45 +0000 |
| commit | 8c819b9e127d6ca837da214667945634725a66ed (patch) | |
| tree | 1a0776ccc38b8c97b10fbefa8e10e93396d596d8 /src | |
| parent | changes in Makefile to turn off some warnings (diff) | |
| download | metasol-8c819b9e127d6ca837da214667945634725a66ed.tar.gz metasol-8c819b9e127d6ca837da214667945634725a66ed.zip | |
wow a thing that works
Diffstat (limited to 'src')
| -rw-r--r-- | src/main/api.cpp | 26 | ||||
| -rw-r--r-- | src/main/api.h | 66 | ||||
| -rw-r--r-- | src/main/game/search-state/game_state.cpp | 22 | ||||
| -rw-r--r-- | src/main/game/search-state/game_state.h | 2 | ||||
| -rw-r--r-- | src/main/solver/solver.h | 4 |
5 files changed, 120 insertions, 0 deletions
diff --git a/src/main/api.cpp b/src/main/api.cpp new file mode 100644 index 0000000..fc3da3a --- /dev/null +++ b/src/main/api.cpp @@ -0,0 +1,26 @@ +#include <cstdint> +#include <vector> +#include <string> + +#include "game/move.h" +#include "game/search-state/game_state.h" +#include "solver/solver.h" + +bool get_moves(std::vector<move>& moves, game_state& gs, + uint64_t cache_capacity, uint64_t timeout); +bool get_moves(std::vector<move>& moves, game_state& gs, + uint64_t cache_capacity, uint64_t timeout) { + moves.clear(); + + solver sol(gs, cache_capacity); + solver::result res = sol.run(std::chrono::milliseconds(timeout)); + if (res.sol_type == solver::result::type::SOLVED) { + std::vector<solver::node> nodes = sol.get_frontier(); + for (auto &it : nodes) { + moves.push_back(it.mv); + } + return true; + } else { + return false; + } +} diff --git a/src/main/api.h b/src/main/api.h new file mode 100644 index 0000000..86e3441 --- /dev/null +++ b/src/main/api.h @@ -0,0 +1,66 @@ +#include <cstdint> +#include <vector> +#include <string> +#include <list> + +#include "game/move.h" +#include "game/sol_rules.h" + +// just for now, until I construct my own rules stuff: +#include "input-output/input/json-parsing/rules_parser.h" + +typedef std::vector<std::vector<std::string>> piles; +typedef std::vector<move> movelist; + +class game_state { + public: + game_state(const sol_rules&, piles); + enum class streamliner_options {NONE, AUTO_FOUNDATIONS, SUIT_SYMMETRY, BOTH}; + void make_move(move); + + friend std::ostream& operator<< (std::ostream&, const game_state&); + + private: + /* Game rules */ + + const sol_rules rules; + streamliner_options stream_opts; + card::rank_t foundations_base; + + /* Pile references */ + + std::list<pile::ref> tableau_piles; + std::list<pile::ref> cells; + pile::ref stock; + pile::ref waste; + std::list<pile::ref> reserve; + std::vector<pile::ref> foundations; + std::vector<pile::ref> sequences; + std::list<pile::ref> accordion; + pile::ref hole; + + /* Pile references of starting/original layout */ + + std::vector<pile::ref> original_tableau_piles; + std::vector<pile::ref> original_cells; + std::vector<pile::ref> original_reserve; + + /* Auto-foundation moves state */ + + std::vector<bool> auto_foundation_moves; + + /* Core piles */ + + std::vector<pile> piles; +}; + +/** + * Populate a move list with a bunch of moves that lead to a solution. + * + * Clears the list, then, if a solution is found, populates it with the moves + * required to realise the solution. + * + * Returns true if a solution was found; false otherwise. + */ +bool 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 6cd1262..a547f7e 100644 --- a/src/main/game/search-state/game_state.cpp +++ b/src/main/game/search-state/game_state.cpp @@ -284,6 +284,28 @@ game_state::game_state(const sol_rules& s_rules, } } +game_state::game_state(const sol_rules& s_rules, + std::vector<std::vector<string>> vec) + : game_state(s_rules, sos::NONE) {pile::ref pr = 0; + for (auto& p_vec : vec) { + for (const string& card_str : p_vec) { + card c(card_str.c_str(), s_rules.face_up == fu::TOP_CARDS); + 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<card> 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 512652f..9f8e3b9 100644 --- a/src/main/game/search-state/game_state.h +++ b/src/main/game/search-state/game_state.h @@ -39,6 +39,8 @@ public: game_state(const sol_rules&, int seed, streamliner_options); // Does the same but with an initialiser list (useful for testing) game_state(const sol_rules&, std::initializer_list<std::initializer_list<std::string>>); + // Does the same but with vectors + game_state(const sol_rules&, std::vector<std::vector<std::string>>); /* Altering state */ diff --git a/src/main/solver/solver.h b/src/main/solver/solver.h index 74c73a8..fec3f4b 100644 --- a/src/main/solver/solver.h +++ b/src/main/solver/solver.h @@ -26,7 +26,11 @@ public: }; struct result { +#ifdef C2NIM + enum type { TIMEOUT, SOLVED, UNSOLVABLE, MEM_LIMIT, TERMINATED }; +#else enum class type { TIMEOUT, SOLVED, UNSOLVABLE, MEM_LIMIT, TERMINATED }; +#endif type sol_type; uint64_t states_searched; |
