aboutsummaryrefslogtreecommitdiff
path: root/cpp/examples
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2025-04-14 14:00:44 +0200
committerSebastiano Tronto <sebastiano@tronto.net>2025-04-14 14:00:44 +0200
commit3ab1012ea8c55f82812b5998fd6b4fdf53dea70e (patch)
treed18552619a364fcb816d5e2d2331822edd7e2045 /cpp/examples
parent7b8c1e4634784b57bb6223cd780d5c67f5381d43 (diff)
downloadnissy-core-3ab1012ea8c55f82812b5998fd6b4fdf53dea70e.tar.gz
nissy-core-3ab1012ea8c55f82812b5998fd6b4fdf53dea70e.zip
Improved C++ API and added info to README
Diffstat (limited to 'cpp/examples')
-rw-r--r--cpp/examples/move_convert.cpp24
-rw-r--r--cpp/examples/solve_h48h3k2.cpp91
2 files changed, 115 insertions, 0 deletions
diff --git a/cpp/examples/move_convert.cpp b/cpp/examples/move_convert.cpp
new file mode 100644
index 0000000..11398df
--- /dev/null
+++ b/cpp/examples/move_convert.cpp
@@ -0,0 +1,24 @@
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
6int main() {
7 nissy::cube c;
8 if (!c.move("R' U' F").ok()) {
9 std::cout << "Error moving the cube!" << std::endl;
10 return 1;
11 }
12
13 auto string_or_error = c.to_string("H48");
14 if (std::holds_alternative<nissy::error>(string_or_error)) {
15 std::cout << "Error converting cube!" << std::endl;
16 return 1;
17 }
18
19 auto str = std::get<std::string>(string_or_error);
20 std::cout << "Cube in H48 format after R' U' F:" << std::endl
21 << str << std::endl;
22
23 return 0;
24}
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
9extern "C" {
10 long long nissy_setlogger(void (*)(const char *));
11}
12
13int 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}

Generated with cgit - Back to sebastiano.tronto.net