wfc compiled

This commit is contained in:
cdemeyer-teachx
2025-08-24 17:14:46 +09:00
parent de5317967d
commit 76ad49b05a
31 changed files with 973 additions and 1785 deletions

View File

@@ -39,20 +39,38 @@ add_executable(sudoku_demo
sudoku.cpp
)
# Set output directory
# Create WFC demo executable
add_executable(sudoku_wfc_demo
sudoku_wfc.cpp
)
# Set output directory for sudoku_demo
set_target_properties(sudoku_demo PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
)
# Set output directory and properties for sudoku_wfc_demo
set_target_properties(sudoku_wfc_demo PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
CXX_STANDARD 20
CXX_STANDARD_REQUIRED ON
)
# Include directories
target_include_directories(sudoku_demo PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
target_include_directories(sudoku_wfc_demo PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/../../include
)
# Optional: Enable optimizations for release builds
if(CMAKE_BUILD_TYPE STREQUAL "Release")
if(MSVC)
target_compile_options(sudoku_demo PRIVATE /O2)
target_compile_options(sudoku_wfc_demo PRIVATE /O2)
else()
target_compile_options(sudoku_demo PRIVATE -O3 -march=native)
target_compile_options(sudoku_wfc_demo PRIVATE -O3 -march=native)
endif()
endif()
@@ -97,9 +115,9 @@ endif()
# Installation (optional)
if(HAS_GTEST AND HAS_BENCHMARK)
install(TARGETS sudoku_demo sudoku_tests sudoku_benchmarks DESTINATION bin)
install(TARGETS sudoku_demo sudoku_wfc_demo sudoku_tests sudoku_benchmarks DESTINATION bin)
elseif(HAS_GTEST)
install(TARGETS sudoku_demo sudoku_tests DESTINATION bin)
install(TARGETS sudoku_demo sudoku_wfc_demo sudoku_tests DESTINATION bin)
else()
install(TARGETS sudoku_demo DESTINATION bin)
install(TARGETS sudoku_demo sudoku_wfc_demo DESTINATION bin)
endif()

19
demos/sudoku/main.cpp Normal file
View File

@@ -0,0 +1,19 @@
#include "sudoku.h"
#include <iostream>
int main()
{
std::cout << "Sudoku Demo" << std::endl;
// Create a simple sudoku puzzle
Sudoku sudoku("530070000600195000098000060800060003400803001700020006060000280000419005000080079");
if (sudoku.isValid()) {
std::cout << "Loaded valid sudoku puzzle:" << std::endl;
sudoku.print();
} else {
std::cout << "Invalid sudoku puzzle!" << std::endl;
}
return 0;
}

View File

@@ -0,0 +1,58 @@
#include <nd-wfc/wfc.hpp>
#include <nd-wfc/worlds.hpp>
#include <iostream>
int main()
{
std::cout << "Running Sudoku WFC" << std::endl;
auto sudokuSolver = WFC::Builder<WFC::Array2D<uint8_t, 9, 9>, uint8_t>()
.DefineIDs<1, 2, 3, 4, 5, 6, 7, 8, 9>()
.Variable<1, 2, 3, 4, 5, 6, 7, 8, 9>([](WFC::Array2D<uint8_t, 9, 9>&, size_t index, WFC::WorldValue<uint8_t> val, auto& constrainer) {
size_t x = index % 9;
size_t y = index / 9;
// Add row constraints (same row, different columns)
for (size_t i = 0; i < 9; ++i) {
if (i != x) constrainer.Exclude(val, i + y * 9);
}
// Add column constraints (same column, different rows)
for (size_t i = 0; i < 9; ++i) {
if (i != y) constrainer.Exclude(val,x + i * 9);
}
// Add box constraints (3x3 box)
int box_x = (x / 3) * 3;
int box_y = (y / 3) * 3;
for (size_t j = 0; j < 3; ++j) {
for (size_t k = 0; k < 3; ++k) {
int cell_x = box_x + j;
int cell_y = box_y + k;
size_t cell_index = cell_x + cell_y * 9;
if (cell_index != index) {
constrainer.Exclude(val, cell_index);
}
}
}
})
.build();
WFC::Array2D<uint8_t, 9, 9> sudokuWorld;
bool success = sudokuSolver.Run(sudokuWorld);
if (success) {
std::cout << "Sudoku solved successfully!" << std::endl;
// Print the solved sudoku
for (size_t y = 0; y < 9; ++y) {
for (size_t x = 0; x < 9; ++x) {
std::cout << static_cast<int>(sudokuWorld.at(x, y)) << " ";
if (x == 2 || x == 5) std::cout << "| ";
}
std::cout << std::endl;
if (y == 2 || y == 5) std::cout << "------+-------+------" << std::endl;
}
} else {
std::cout << "Failed to solve sudoku!" << std::endl;
}
}

BIN
demos/sudoku/sudoku_wfc_demo Executable file

Binary file not shown.

BIN
demos/sudoku/sudoku_wfc_manual Executable file

Binary file not shown.