diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2025-12-14 15:12:36 +0100 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2025-12-14 16:09:31 +0100 |
| commit | bcd52547af58b15868e24f3904e307548d1fd505 (patch) | |
| tree | cda0a871f65575bb15fb4536c7b103c13caf433e /cpp/examples/solve.cpp | |
| parent | 21fdf9697ddfab0a9c082a91ae0a79b1b574774c (diff) | |
| download | nissy-core-bcd52547af58b15868e24f3904e307548d1fd505.tar.gz nissy-core-bcd52547af58b15868e24f3904e307548d1fd505.zip | |
Cleanup, update documentation, fix examples
Diffstat (limited to 'cpp/examples/solve.cpp')
| -rw-r--r-- | cpp/examples/solve.cpp | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/cpp/examples/solve.cpp b/cpp/examples/solve.cpp new file mode 100644 index 0000000..c25a088 --- /dev/null +++ b/cpp/examples/solve.cpp | |||
| @@ -0,0 +1,89 @@ | |||
| 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 | int main() { | ||
| 10 | // Get verbose output | ||
| 11 | nissy::set_logger([](const char* s, void *u) { std::cout << s; }, NULL); | ||
| 12 | |||
| 13 | // Get the scramble from the user | ||
| 14 | std::cout << "Enter scramble: "; | ||
| 15 | std::string scramble; | ||
| 16 | std::getline(std::cin, scramble); | ||
| 17 | |||
| 18 | // Apply scramble to a solved cube | ||
| 19 | nissy::cube c; | ||
| 20 | if (!c.move(scramble).ok()) { | ||
| 21 | std::cout << "Invalid scramble!" << std::endl; | ||
| 22 | return 1; | ||
| 23 | } | ||
| 24 | |||
| 25 | // Ask for a limit on the solution's length | ||
| 26 | int maxmoves; | ||
| 27 | std::cout << "Maximum number of moves: "; | ||
| 28 | std::cin >> maxmoves; | ||
| 29 | |||
| 30 | // Load the solver | ||
| 31 | auto h48h3 = std::get<nissy::solver>(nissy::solver::get("h48h3")); | ||
| 32 | std::filesystem::path tableDir("tables"); | ||
| 33 | std::filesystem::create_directories(tableDir); // Ignored if dir exists | ||
| 34 | std::filesystem::path tableFile("tables/" + h48h3.id); | ||
| 35 | |||
| 36 | // If the table is not present, generate it | ||
| 37 | if (!std::filesystem::exists(tableFile)) { | ||
| 38 | std::cout << "Data for h48h3 solver was not found, " | ||
| 39 | << "generating it..." << std::endl; | ||
| 40 | auto err = h48h3.generate_data(); | ||
| 41 | if (!err.ok()) { | ||
| 42 | std::cout << "Unexpected error! Error code: " | ||
| 43 | << err.value << std::endl; | ||
| 44 | return 1; | ||
| 45 | } | ||
| 46 | std::ofstream ofs(tableFile, std::ios::binary); | ||
| 47 | ofs.write(reinterpret_cast<char *>(h48h3.data.data()), | ||
| 48 | h48h3.size); | ||
| 49 | std::cout << "Table generated and written to " | ||
| 50 | << tableFile << std::endl; | ||
| 51 | ofs.close(); | ||
| 52 | } | ||
| 53 | // Otherwise read it from file | ||
| 54 | else { | ||
| 55 | std::cout << "Loading data table from file" << std::endl; | ||
| 56 | std::ifstream ifs(tableFile, std::ios::binary); | ||
| 57 | h48h3.read_data(ifs); | ||
| 58 | ifs.close(); | ||
| 59 | std::cout << "Data loaded, checking table..." << std::endl; | ||
| 60 | auto err = h48h3.check_data(); | ||
| 61 | if (!err.ok()) { | ||
| 62 | std::cout << "Error reading data table from file! " | ||
| 63 | << "Error code: " << err.value << std::endl; | ||
| 64 | return 1; | ||
| 65 | } | ||
| 66 | std::cout << "Data table loaded" << std::endl; | ||
| 67 | } | ||
| 68 | |||
| 69 | // Solve | ||
| 70 | auto solve_result = h48h3.solve(c, nissy::nissflag::NORMAL, | ||
| 71 | 0, maxmoves, 1, 20, 8, NULL, NULL); | ||
| 72 | |||
| 73 | // Write the result | ||
| 74 | if (!solve_result.err.ok()) { | ||
| 75 | std::cout << "Error solving! Error code: " << | ||
| 76 | solve_result.err.value << std::endl; | ||
| 77 | return 1; | ||
| 78 | } | ||
| 79 | |||
| 80 | if (solve_result.solutions.size() == 0) { | ||
| 81 | std::cout << "No solution found!" << std::endl; | ||
| 82 | } else { | ||
| 83 | auto len = nissy::count_moves(solve_result.solutions).value; | ||
| 84 | std::cout << "Solution (" << len << " moves): " | ||
| 85 | << solve_result.solutions << std::endl; | ||
| 86 | } | ||
| 87 | |||
| 88 | return 0; | ||
| 89 | } | ||
