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 | |
| parent | changes in Makefile to turn off some warnings (diff) | |
| download | metasol-8c819b9e127d6ca837da214667945634725a66ed.tar.gz metasol-8c819b9e127d6ca837da214667945634725a66ed.zip | |
wow a thing that works
| -rw-r--r-- | CMakeLists.txt | 6 | ||||
| -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 | ||||
| -rw-r--r-- | test.cpp | 120 |
7 files changed, 246 insertions, 0 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index b428bb5..121c475 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,6 +16,7 @@ check_ipo_supported(RESULT supported OUTPUT error) set(main src/main/main.cpp) set(sources src/main/input-output/input/command_line_helper.cpp + src/main/api.cpp src/main/input-output/input/command_line_helper.h src/main/input-output/output/log_helper.h src/main/game/card.h @@ -162,6 +163,11 @@ add_executable(solvitaire ${main} ${sources}) set_property(TARGET solvitaire PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE) # For -flto flag target_link_libraries(solvitaire LINK_PUBLIC ${Boost_LIBRARIES}) +add_library(api STATIC ${sources}) +set_target_properties(api PROPERTIES PUBLIC_HEADER src/main/api.h) +set_property(TARGET api PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE) # For -flto flag +target_link_libraries(api LINK_PUBLIC ${Boost_LIBRARIES}) + # add a separete executable for the 'no pile symmetry' version of the solver #add_executable(solvitaire-no-pile-symmetry ${main} ${sources}) #target_link_libraries(solvitaire-no-pile-symmetry LINK_PUBLIC ${Boost_LIBRARIES}) 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; diff --git a/test.cpp b/test.cpp new file mode 100644 index 0000000..de14630 --- /dev/null +++ b/test.cpp @@ -0,0 +1,120 @@ +#include <cstdint> +#include <iostream> +#include <algorithm> +#include <random> +#include <string> +#include <cstdio> + +#include "src/main/api.h" + +typedef std::vector<std::string> card_pile; + +card_pile& make_deck(card_pile& space, int deck_count); +card_pile& make_deck(card_pile& space, int deck_count = 1) { + space.clear(); + for (int i = 0; i < deck_count; ++i) { + for (std::string suit : { "S", "C", "D", "H" }) { + for (std::string rank : { "A", "2", "3", "4", "5", "6", "7", "8", + "9", "10", "J", "Q", "K" }) { + space.push_back(rank + suit); + } + } + } + return space; +} + +std::random_device rd; +auto rng = std::default_random_engine{rd()}; +card_pile& shuffle(card_pile& cards); +card_pile& shuffle(card_pile& cards) { + std::shuffle(std::begin(cards), std::end(cards), rng); + return cards; +} + +std::vector<card_pile>& split(card_pile& in, std::vector<card_pile>& out, + std::vector<std::size_t> numbers); +std::vector<card_pile>& split(card_pile& in, std::vector<card_pile>& out, + std::vector<std::size_t> numbers) { + out.clear(); + out.resize(numbers.size()); + + std::size_t acc = 0; + auto i = 0; + for (auto& n : numbers) { + card_pile cp(in.begin() + acc, in.begin() + acc + n); + out[i] = cp; + acc += n; + ++i; + } + + assert(acc == in.size()); + return out; +} + +std::vector<card_pile>& split_even(card_pile& in, std::vector<card_pile>& out, + std::size_t n_piles); +std::vector<card_pile>& split_even(card_pile& in, std::vector<card_pile>& out, + std::size_t n_piles) { + std::vector<std::size_t> nums; + + auto t = in.size(); + for (int i = 0; i < (int)in.size(); ++i) { + auto w = t / (n_piles - i); + nums.push_back(w); + t -= w; + } + + return split(in, out, nums); +} + +typedef struct scard { + enum class suit { + HEARTS, SPADES, CLUBS, DIAMONDS + } suit; + std::uint8_t rank; + bool hidden; +} scard; + +int main() { + auto rules = rules_parser::from_preset("canfield"); + + card_pile cp; + std::vector<card_pile> t; + + make_deck(cp); + shuffle(cp); + + std::cerr << cp.size(); + + split(cp, t, {13, 1, 1, 1, 1, 1, 34}); + + movelist ml; + + piles ps = piles{ + t[1],{},{},{}, // Foundations + t[6],{}, // Stock-waste + t[0], // Reserve + t[2],t[3],t[4],t[5] // Tableau + }; + + game_state gs(rules, ps); + + std::cerr << get_moves(ml, gs, 1024u, 102400u) << std::endl; + std::cerr << ml.size() << std::endl; + + bool first = true; + for (auto& m : ml) { + printf("%d->%d\n", m.from, m.to); + + if (!first) { + gs.make_move(m); + } + first = false; + } + + std::cerr << gs << std::endl; + + std::cerr << "woa" << std::endl; + + return 0; +} |
