From bcd52547af58b15868e24f3904e307548d1fd505 Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Sun, 14 Dec 2025 15:12:36 +0100 Subject: Cleanup, update documentation, fix examples --- cpp/examples/solve.cpp | 89 ++++++++++++++++++++++++++++++++++++++++++ cpp/examples/solve_h48h3k2.cpp | 89 ------------------------------------------ 2 files changed, 89 insertions(+), 89 deletions(-) create mode 100644 cpp/examples/solve.cpp delete mode 100644 cpp/examples/solve_h48h3k2.cpp (limited to 'cpp/examples') 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 @@ +/* Simple demo for an H48 solver */ + +#include "../nissy.h" +#include +#include +#include +#include + +int main() { + // Get verbose output + nissy::set_logger([](const char* s, void *u) { std::cout << s; }, NULL); + + // Get the scramble from the user + std::cout << "Enter scramble: "; + std::string scramble; + std::getline(std::cin, scramble); + + // Apply scramble to a solved cube + nissy::cube c; + if (!c.move(scramble).ok()) { + std::cout << "Invalid scramble!" << std::endl; + return 1; + } + + // Ask for a limit on the solution's length + int maxmoves; + std::cout << "Maximum number of moves: "; + std::cin >> maxmoves; + + // Load the solver + auto h48h3 = std::get(nissy::solver::get("h48h3")); + std::filesystem::path tableDir("tables"); + std::filesystem::create_directories(tableDir); // Ignored if dir exists + std::filesystem::path tableFile("tables/" + h48h3.id); + + // If the table is not present, generate it + if (!std::filesystem::exists(tableFile)) { + std::cout << "Data for h48h3 solver was not found, " + << "generating it..." << std::endl; + auto err = h48h3.generate_data(); + if (!err.ok()) { + std::cout << "Unexpected error! Error code: " + << err.value << std::endl; + return 1; + } + std::ofstream ofs(tableFile, std::ios::binary); + ofs.write(reinterpret_cast(h48h3.data.data()), + h48h3.size); + std::cout << "Table generated and written to " + << tableFile << std::endl; + ofs.close(); + } + // Otherwise read it from file + else { + std::cout << "Loading data table from file" << std::endl; + std::ifstream ifs(tableFile, std::ios::binary); + h48h3.read_data(ifs); + ifs.close(); + std::cout << "Data loaded, checking table..." << std::endl; + auto err = h48h3.check_data(); + if (!err.ok()) { + std::cout << "Error reading data table from file! " + << "Error code: " << err.value << std::endl; + return 1; + } + std::cout << "Data table loaded" << std::endl; + } + + // Solve + auto solve_result = h48h3.solve(c, nissy::nissflag::NORMAL, + 0, maxmoves, 1, 20, 8, NULL, NULL); + + // Write the result + if (!solve_result.err.ok()) { + std::cout << "Error solving! Error code: " << + solve_result.err.value << std::endl; + return 1; + } + + if (solve_result.solutions.size() == 0) { + std::cout << "No solution found!" << std::endl; + } else { + auto len = nissy::count_moves(solve_result.solutions).value; + std::cout << "Solution (" << len << " moves): " + << solve_result.solutions << std::endl; + } + + return 0; +} diff --git a/cpp/examples/solve_h48h3k2.cpp b/cpp/examples/solve_h48h3k2.cpp deleted file mode 100644 index 2a226ba..0000000 --- a/cpp/examples/solve_h48h3k2.cpp +++ /dev/null @@ -1,89 +0,0 @@ -/* Simple demo for an H48 solver */ - -#include "../nissy.h" -#include -#include -#include -#include - -int main() { - // Get verbose output - nissy::set_logger([](const char* s, void *u) { std::cout << s; }, NULL); - - // Get the scramble from the user - std::cout << "Enter scramble: "; - std::string scramble; - std::getline(std::cin, scramble); - - // Apply scramble to a solved cube - nissy::cube c; - if (!c.move(scramble).ok()) { - std::cout << "Invalid scramble!" << std::endl; - return 1; - } - - // Ask for a limit on the solution's length - int maxmoves; - std::cout << "Maximum number of moves: "; - std::cin >> maxmoves; - - // Load the solver - auto h48h3k2 = std::get(nissy::solver::get("h48h3k2")); - std::filesystem::path tableDir("tables"); - std::filesystem::create_directories(tableDir); // Ignored if dir exists - std::filesystem::path tableFile("tables/" + h48h3k2.id); - - // If the table is not present, generate it - if (!std::filesystem::exists(tableFile)) { - std::cout << "Data for h48h3k2 solver was not found, " - << "generating it..." << std::endl; - auto err = h48h3k2.generate_data(); - if (!err.ok()) { - std::cout << "Unexpected error! Error code: " - << err.value << std::endl; - return 1; - } - std::ofstream ofs(tableFile, std::ios::binary); - ofs.write(reinterpret_cast(h48h3k2.data.data()), - h48h3k2.size); - std::cout << "Table generated and written to " - << tableFile << std::endl; - ofs.close(); - } - // Otherwise read it from file - else { - std::cout << "Loading data table from file" << std::endl; - std::ifstream ifs(tableFile, std::ios::binary); - h48h3k2.read_data(ifs); - ifs.close(); - std::cout << "Data loaded, checking table..." << std::endl; - auto err = h48h3k2.check_data(); - if (!err.ok()) { - std::cout << "Error reading data table from file! " - << "Error code: " << err.value << std::endl; - return 1; - } - std::cout << "Data table loaded" << std::endl; - } - - // Solve - auto solve_result = h48h3k2.solve(c, nissy::nissflag::NORMAL, - 0, maxmoves, 1, 20, 8, NULL, NULL); - - // Write the result - if (!solve_result.err.ok()) { - std::cout << "Error solving! Error code: " << - solve_result.err.value << std::endl; - return 1; - } - - if (solve_result.solutions.size() == 0) { - std::cout << "No solution found!" << std::endl; - } else { - auto len = nissy::count_moves(solve_result.solutions).value; - std::cout << "Solution (" << len << " moves): " - << solve_result.solutions << std::endl; - } - - return 0; -} -- cgit v1.3