From 3ab1012ea8c55f82812b5998fd6b4fdf53dea70e Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Mon, 14 Apr 2025 14:00:44 +0200 Subject: Improved C++ API and added info to README --- cpp/examples/solve_h48h3k2.cpp | 91 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 cpp/examples/solve_h48h3k2.cpp (limited to 'cpp/examples/solve_h48h3k2.cpp') 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 @@ +/* Simple demo for an H48 solver */ + +#include "../nissy.h" +#include +#include +#include +#include + +extern "C" { + long long nissy_setlogger(void (*)(const char *)); +} + +int main() { + // Get verbose output + nissy_setlogger([](const char* s) { std::cout << s; }); + + // 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, -1, 8); + + // 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 + std::cout << "Solution: " << + solve_result.solutions[0] << std::endl; + + return 0; +} -- cgit v1.3