diff options
Diffstat (limited to 'cpp/examples/solve_h48h3k2.cpp')
| -rw-r--r-- | cpp/examples/solve_h48h3k2.cpp | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/cpp/examples/solve_h48h3k2.cpp b/cpp/examples/solve_h48h3k2.cpp new file mode 100644 index 0000000..6639c55 --- /dev/null +++ b/cpp/examples/solve_h48h3k2.cpp | |||
| @@ -0,0 +1,91 @@ | |||
| 1 | /* Simple demo for an H48 solver */ | ||
| 2 | |||
| 3 | #include "../nissy.h" | ||
| 4 | #include <filesystem> | ||
| 5 | #include <fstream> | ||
| 6 | #include <iostream> | ||
| 7 | #include <string> | ||
| 8 | |||
| 9 | extern "C" { | ||
| 10 | long long nissy_setlogger(void (*)(const char *)); | ||
| 11 | } | ||
| 12 | |||
| 13 | int main() { | ||
| 14 | // Get verbose output | ||
| 15 | nissy_setlogger([](const char* s) { std::cout << s; }); | ||
| 16 | |||
| 17 | // Get the scramble from the user | ||
| 18 | std::cout << "Enter scramble: "; | ||
| 19 | std::string scramble; | ||
| 20 | std::getline(std::cin, scramble); | ||
| 21 | |||
| 22 | // Apply scramble to a solved cube | ||
| 23 | nissy::cube c; | ||
| 24 | if (!c.move(scramble).ok()) { | ||
| 25 | std::cout << "Invalid scramble!" << std::endl; | ||
| 26 | return 1; | ||
| 27 | } | ||
| 28 | |||
| 29 | // Ask for a limit on the solution's length | ||
| 30 | int maxmoves; | ||
| 31 | std::cout << "Maximum number of moves: "; | ||
| 32 | std::cin >> maxmoves; | ||
| 33 | |||
| 34 | // Load the solver | ||
| 35 | auto h48h3k2 = std::get<nissy::solver>(nissy::solver::get("h48h3k2")); | ||
| 36 | std::filesystem::path tableDir("tables"); | ||
| 37 | std::filesystem::create_directories(tableDir); // Ignored if dir exists | ||
| 38 | std::filesystem::path tableFile("tables/" + h48h3k2.id); | ||
| 39 | |||
| 40 | // If the table is not present, generate it | ||
| 41 | if (!std::filesystem::exists(tableFile)) { | ||
| 42 | std::cout << "Data for h48h3k2 solver was not found, " | ||
| 43 | << "generating it..." << std::endl; | ||
| 44 | auto err = h48h3k2.generate_data(); | ||
| 45 | if (!err.ok()) { | ||
| 46 | std::cout << "Unexpected error! Error code: " | ||
| 47 | << err.value << std::endl; | ||
| 48 | return 1; | ||
| 49 | } | ||
| 50 | std::ofstream ofs(tableFile, std::ios::binary); | ||
| 51 | ofs.write(reinterpret_cast<char *>(h48h3k2.data.data()), | ||
| 52 | h48h3k2.size); | ||
| 53 | std::cout << "Table generated and written to " | ||
| 54 | << tableFile << std::endl; | ||
| 55 | ofs.close(); | ||
| 56 | } | ||
| 57 | // Otherwise read it from file | ||
| 58 | else { | ||
| 59 | std::cout << "Loading data table from file" << std::endl; | ||
| 60 | std::ifstream ifs(tableFile, std::ios::binary); | ||
| 61 | h48h3k2.read_data(ifs); | ||
| 62 | ifs.close(); | ||
| 63 | std::cout << "Data loaded, checking table..." << std::endl; | ||
| 64 | auto err = h48h3k2.check_data(); | ||
| 65 | if (!err.ok()) { | ||
| 66 | std::cout << "Error reading data table from file! " | ||
| 67 | << "Error code: " << err.value << std::endl; | ||
| 68 | return 1; | ||
| 69 | } | ||
| 70 | std::cout << "Data table loaded" << std::endl; | ||
| 71 | } | ||
| 72 | |||
| 73 | // Solve | ||
| 74 | auto solve_result = h48h3k2.solve(c, nissy::nissflag::NORMAL, | ||
| 75 | 0, maxmoves, 1, -1, 8); | ||
| 76 | |||
| 77 | // Write the result | ||
| 78 | if (!solve_result.err.ok()) { | ||
| 79 | std::cout << "Error solving! Error code: " << | ||
| 80 | solve_result.err.value << std::endl; | ||
| 81 | return 1; | ||
| 82 | } | ||
| 83 | |||
| 84 | if (solve_result.solutions.size() == 0) | ||
| 85 | std::cout << "No solution found!" << std::endl; | ||
| 86 | else | ||
| 87 | std::cout << "Solution: " << | ||
| 88 | solve_result.solutions[0] << std::endl; | ||
| 89 | |||
| 90 | return 0; | ||
| 91 | } | ||
