diff options
Diffstat (limited to 'cpp')
| -rw-r--r-- | cpp/examples/variations.cpp | 15 | ||||
| -rw-r--r-- | cpp/nissy.cpp | 34 | ||||
| -rw-r--r-- | cpp/nissy.h | 16 |
3 files changed, 65 insertions, 0 deletions
diff --git a/cpp/examples/variations.cpp b/cpp/examples/variations.cpp new file mode 100644 index 0000000..3c6421d --- /dev/null +++ b/cpp/examples/variations.cpp | |||
| @@ -0,0 +1,15 @@ | |||
| 1 | /* A simple example showing how to move a cube and print it in H48 format. */ | ||
| 2 | |||
| 3 | #include "../nissy.h" | ||
| 4 | #include <iostream> | ||
| 5 | |||
| 6 | int main() { | ||
| 7 | std::string moves = "R U' Bw2 M D' x' F B(E2 F D B' Lw2 U2 U' S2 B)"; | ||
| 8 | auto v = std::get<nissy::variation>(nissy::variation::get("lastqt")); | ||
| 9 | auto variations = v.find_variations(moves).solutions; | ||
| 10 | |||
| 11 | std::cout << "Changing the last quarter turn of " << moves << " gives:" | ||
| 12 | << std::endl << variations; | ||
| 13 | |||
| 14 | return 0; | ||
| 15 | } | ||
diff --git a/cpp/nissy.cpp b/cpp/nissy.cpp index 90fcf20..6ca4884 100644 --- a/cpp/nissy.cpp +++ b/cpp/nissy.cpp | |||
| @@ -12,6 +12,7 @@ extern "C" { | |||
| 12 | long long nissy_inverse(const char *, char *); | 12 | long long nissy_inverse(const char *, char *); |
| 13 | long long nissy_applymoves(const char *, const char *, char *); | 13 | long long nissy_applymoves(const char *, const char *, char *); |
| 14 | long long nissy_applytrans(const char *, const char *, char *); | 14 | long long nissy_applytrans(const char *, const char *, char *); |
| 15 | long long nissy_variations(const char *, const char *, size_t, char*); | ||
| 15 | long long nissy_getcube(long long, long long, long long, long long, | 16 | long long nissy_getcube(long long, long long, long long, long long, |
| 16 | long long, const char *, char *); | 17 | long long, const char *, char *); |
| 17 | long long nissy_solverinfo(const char *, char *); | 18 | long long nissy_solverinfo(const char *, char *); |
| @@ -44,6 +45,7 @@ namespace nissy { | |||
| 44 | const error error::INVALID_MOVES{-20}; | 45 | const error error::INVALID_MOVES{-20}; |
| 45 | const error error::INVALID_TRANS{-30}; | 46 | const error error::INVALID_TRANS{-30}; |
| 46 | const error error::INVALID_SOLVER{-50}; | 47 | const error error::INVALID_SOLVER{-50}; |
| 48 | const error error::INVALID_VARIATION{-51}; | ||
| 47 | const error error::NULL_POINTER{-60}; | 49 | const error error::NULL_POINTER{-60}; |
| 48 | const error error::BUFFER_SIZE{-61}; | 50 | const error error::BUFFER_SIZE{-61}; |
| 49 | const error error::DATA{-70}; | 51 | const error error::DATA{-70}; |
| @@ -62,10 +64,42 @@ namespace nissy { | |||
| 62 | constexpr size_t TRANSFORMATION = 12; | 64 | constexpr size_t TRANSFORMATION = 12; |
| 63 | constexpr size_t SOLVE_STATS = 10; | 65 | constexpr size_t SOLVE_STATS = 10; |
| 64 | constexpr size_t DATAID = 255; | 66 | constexpr size_t DATAID = 255; |
| 67 | constexpr size_t MOVES = 1000; | ||
| 65 | } | 68 | } |
| 66 | 69 | ||
| 67 | bool error::ok() const { return value >= 0; } | 70 | bool error::ok() const { return value >= 0; } |
| 68 | 71 | ||
| 72 | variation::variation(const std::string& str) : name{str} {} | ||
| 73 | |||
| 74 | variation::variations_result | ||
| 75 | variation::find_variations(const std::string& moves) | ||
| 76 | { | ||
| 77 | variation::variations_result result; | ||
| 78 | |||
| 79 | const size_t len = 3 * size::MOVES * 16; | ||
| 80 | result.solutions.resize(len); | ||
| 81 | |||
| 82 | auto err = nissy_variations(moves.c_str(), name.c_str(), len, | ||
| 83 | result.solutions.data()); | ||
| 84 | |||
| 85 | int size = result.solutions.find_first_of('\0'); | ||
| 86 | result.solutions.resize(size); | ||
| 87 | result.err = error{err}; | ||
| 88 | |||
| 89 | return result; | ||
| 90 | } | ||
| 91 | |||
| 92 | std::variant<variation, error> | ||
| 93 | variation::get(const std::string& name) { | ||
| 94 | char result[10]; | ||
| 95 | variation s(name); | ||
| 96 | |||
| 97 | if (nissy_variations("", name.c_str(), 10, result) < 0) | ||
| 98 | return error::INVALID_VARIATION; | ||
| 99 | else | ||
| 100 | return s; | ||
| 101 | } | ||
| 102 | |||
| 69 | cube::cube() {} | 103 | cube::cube() {} |
| 70 | 104 | ||
| 71 | error cube::move(const std::string& moves) | 105 | error cube::move(const std::string& moves) |
diff --git a/cpp/nissy.h b/cpp/nissy.h index 199af10..74bbd32 100644 --- a/cpp/nissy.h +++ b/cpp/nissy.h | |||
| @@ -39,6 +39,7 @@ namespace nissy { | |||
| 39 | static const error INVALID_MOVES; | 39 | static const error INVALID_MOVES; |
| 40 | static const error INVALID_TRANS; | 40 | static const error INVALID_TRANS; |
| 41 | static const error INVALID_SOLVER; | 41 | static const error INVALID_SOLVER; |
| 42 | static const error INVALID_VARIATION; | ||
| 42 | static const error NULL_POINTER; | 43 | static const error NULL_POINTER; |
| 43 | static const error BUFFER_SIZE; | 44 | static const error BUFFER_SIZE; |
| 44 | static const error DATA; | 45 | static const error DATA; |
| @@ -63,6 +64,21 @@ namespace nissy { | |||
| 63 | static const compare_result DIFFERENT; | 64 | static const compare_result DIFFERENT; |
| 64 | }; | 65 | }; |
| 65 | 66 | ||
| 67 | class variation { | ||
| 68 | public: | ||
| 69 | struct variations_result { | ||
| 70 | error err; | ||
| 71 | std::string solutions; | ||
| 72 | }; | ||
| 73 | |||
| 74 | const std::string name; | ||
| 75 | |||
| 76 | variations_result find_variations(const std::string&); | ||
| 77 | static std::variant<variation, error> get(const std::string&); | ||
| 78 | private: | ||
| 79 | variation(const std::string&); | ||
| 80 | }; | ||
| 81 | |||
| 66 | class cube { | 82 | class cube { |
| 67 | public: | 83 | public: |
| 68 | cube(); | 84 | cube(); |
