Files
nd-wfc/demos/nonogram/test_nonogram.cpp
2025-08-22 15:00:50 +09:00

119 lines
3.8 KiB
C++

#include "nonogram.h"
#include <iostream>
#include <vector>
#include <fstream>
void printNonogram(const Nonogram& nonogram) {
std::cout << "Nonogram: " << nonogram.getWidth() << "x" << nonogram.getHeight() << std::endl;
std::cout << "Row hints:" << std::endl;
for (size_t i = 0; i < nonogram.getRowCount(); ++i) {
const auto& hints = nonogram.getRowHints(i);
std::cout << " Row " << i << ":";
for (uint8_t hint : hints) {
std::cout << " " << static_cast<int>(hint);
}
std::cout << std::endl;
}
std::cout << "Column hints:" << std::endl;
for (size_t i = 0; i < nonogram.getColumnCount(); ++i) {
const auto& hints = nonogram.getColumnHints(i);
std::cout << " Col " << i << ":";
for (uint8_t hint : hints) {
std::cout << " " << static_cast<int>(hint);
}
std::cout << std::endl;
}
if (nonogram.hasSolution()) {
std::cout << "Solution:" << std::endl;
for (size_t row = 0; row < nonogram.getHeight(); ++row) {
for (size_t col = 0; col < nonogram.getWidth(); ++col) {
std::cout << (nonogram.getSolutionCell(row, col) ? "1" : "0");
}
std::cout << std::endl;
}
}
}
int main() {
std::cout << "Testing Nonogram Loader..." << std::endl;
// Test loading from file
std::string filepath = "/home/connor/repos/nd-wfc/demos/nonogram/data/db/webpbn/1.non";
std::cout << "Attempting to load from: " << filepath << std::endl;
std::ifstream test_file(filepath);
if (test_file.is_open()) {
std::cout << "File exists and can be opened." << std::endl;
std::string content((std::istreambuf_iterator<char>(test_file)),
std::istreambuf_iterator<char>());
std::cout << "File content length: " << content.length() << std::endl;
std::cout << "File content:\n" << content << std::endl;
test_file.close();
// Try to load from this content
auto nonogram = NonogramLoader::fromString(content);
if (nonogram) {
std::cout << "Successfully loaded nonogram from file content!" << std::endl;
printNonogram(*nonogram);
} else {
std::cout << "Failed to load nonogram from file content." << std::endl;
}
} else {
std::cout << "Cannot open file!" << std::endl;
}
auto nonogram = NonogramLoader::fromFile(filepath);
if (nonogram) {
std::cout << "Successfully loaded nonogram from file!" << std::endl;
printNonogram(*nonogram);
} else {
std::cout << "Failed to load nonogram from file." << std::endl;
}
std::cout << "\nTesting with sample data..." << std::endl;
// Test with sample data
std::string sample_data =
"width 5\n"
"height 10\n"
"\n"
"rows\n"
"2\n"
"2,1\n"
"1,1\n"
"3\n"
"1,1\n"
"1,1\n"
"2\n"
"1,1\n"
"1,2\n"
"2\n"
"\n"
"columns\n"
"2,1\n"
"2,1,3\n"
"7\n"
"1,3\n"
"2,1\n"
"\n"
"goal 01100011010010101110101001010000110010100101111000\n";
auto sample_nonogram = NonogramLoader::fromString(sample_data);
if (sample_nonogram) {
std::cout << "Successfully loaded nonogram from string!" << std::endl;
printNonogram(*sample_nonogram);
} else {
std::cout << "Failed to load nonogram from string." << std::endl;
}
// Test loading from directory
std::cout << "\nTesting directory loading..." << std::endl;
auto puzzles = NonogramLoader::fromDirectory("/home/connor/repos/nd-wfc/demos/nonogram/data/db/webpbn");
std::cout << "Loaded " << puzzles.size() << " puzzles from directory." << std::endl;
return 0;
}