From 7273471d430737dd18fcabba70bef83a36b31fe4 Mon Sep 17 00:00:00 2001 From: Joshua Moerman Date: Tue, 19 Apr 2016 22:45:29 +0100 Subject: [PATCH] (committing very old stuff) No clue what I've changed here --- CMakeLists.txt | 2 +- include/data.hpp | 15 +++++----- include/dynamic_grid.hpp | 7 +++-- lib/data.cpp | 6 ++-- lib/dynamic_grid.cpp | 6 +++- src/generate.cpp | 10 ++++++- src/pack.cpp | 63 ++++++++++++++++++++++++++++++++++++++++ src/replay.cpp | 6 ++-- 8 files changed, 98 insertions(+), 17 deletions(-) create mode 100644 src/pack.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index ccc5364..e84de43 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,7 +4,7 @@ cmake_minimum_required(VERSION 2.8) include_directories("${PROJECT_SOURCE_DIR}/include/") add_definitions(-std=c++1y) -find_package(Boost REQUIRED COMPONENTS program_options serialization system) +find_package(Boost REQUIRED COMPONENTS filesystem program_options serialization system) include_directories(SYSTEM ${Boost_INCLUDE_DIRS}) set(libs ${libs} ${Boost_LIBRARIES}) diff --git a/include/data.hpp b/include/data.hpp index a45f849..5d49a63 100644 --- a/include/data.hpp +++ b/include/data.hpp @@ -6,6 +6,7 @@ #include #include #include +#include struct BasicRulesBase { unsigned int min_size; @@ -51,23 +52,23 @@ struct DynamicGridBase { int W; int H; std::vector grid; - std::vector positions; DynamicGridBase() = default; DynamicGridBase(int width, int height, std::vector && data) : W(width), H(height), grid(std::move(data)) - {} + { + assert(W*H == grid.size()); + } private: friend class boost::serialization::access; template void serialize(Archive & ar, const unsigned int /*version*/){ - ar & W & H & grid & positions; - // archiving positions is redundant (I'm being lazy here) + ar & W & H & grid; + assert(W*H == grid.size()); // consider make_nvp for readable json } }; -struct DynamicGrid; -AnalyzedGrid grid_from_file(std::string file_path); -void grid_to_file(AnalyzedGrid const & grid, std::string file_path); +AnalyzedGrid grid_from_file(std::string file_path); +void grid_to_file(AnalyzedGrid const & grid, std::string file_path); diff --git a/include/dynamic_grid.hpp b/include/dynamic_grid.hpp index c76348e..5dc20a6 100644 --- a/include/dynamic_grid.hpp +++ b/include/dynamic_grid.hpp @@ -16,9 +16,9 @@ struct DynamicGrid : public DynamicGridBase { using Position = DynamicGridBase::Position; - DynamicGrid(int width, int height, std::vector && data); - DynamicGrid() = default; + DynamicGrid(DynamicGridBase&& b); + DynamicGrid(int width, int height, std::vector && data); //! \brief Returns an array of all valid positions (may be empty) auto const & all_positions() const{ @@ -71,6 +71,9 @@ struct DynamicGrid : public DynamicGridBase { size_t hash() const; private: + // Optimization: caching the positions for the functions all_positions. + std::vector positions; + //! \brief Single vertical collapsing step auto vcollapse(); diff --git a/lib/data.cpp b/lib/data.cpp index 3c2e577..e3469dd 100644 --- a/lib/data.cpp +++ b/lib/data.cpp @@ -5,16 +5,16 @@ #include #include -AnalyzedGrid grid_from_file(std::string file_path){ +AnalyzedGrid grid_from_file(std::string file_path){ std::ifstream file(file_path); boost::archive::text_iarchive ar(file); - AnalyzedGrid ret; + AnalyzedGrid ret; ar >> ret; return ret; } -void grid_to_file(const AnalyzedGrid& grid, std::string file_path){ +void grid_to_file(const AnalyzedGrid& grid, std::string file_path){ std::ofstream file(file_path); boost::archive::text_oarchive ar(file); diff --git a/lib/dynamic_grid.cpp b/lib/dynamic_grid.cpp index 0329955..1444800 100644 --- a/lib/dynamic_grid.cpp +++ b/lib/dynamic_grid.cpp @@ -13,9 +13,13 @@ static auto all_positions_impl(int W, int H) { return r; } +DynamicGrid::DynamicGrid(DynamicGridBase&& b) +: DynamicGridBase(std::move(b)){ + positions = all_positions_impl(W, H); +} + DynamicGrid::DynamicGrid(int width, int height, std::vector && data) : DynamicGridBase(width, height, std::move(data)) { - assert(grid.size() == W*H); positions = all_positions_impl(W, H); } diff --git a/src/generate.cpp b/src/generate.cpp index dbbcc9c..4fb344a 100644 --- a/src/generate.cpp +++ b/src/generate.cpp @@ -8,6 +8,14 @@ #include #include +static Solutions slice(Solutions && in){ + return {std::move(in.traces)}; +} + +static AnalyzedGrid slice(AnalyzedGrid && in){ + return {std::move(in.grid), slice(std::move(in.solutions)), std::move(in.rules)}; +} + int main(int argc, char** argv){ namespace po = boost::program_options; @@ -68,7 +76,7 @@ int main(int argc, char** argv){ std::cout << filename << "\n\n"; } - grid_to_file({std::move(field), std::move(solutions), rules}, filename); + grid_to_file(slice({std::move(field), std::move(solutions), rules}), filename); } std::cout << solvable << " solvable (= " << 100 * solvable / double(solvable + unsolvable) << "%)\n"; diff --git a/src/pack.cpp b/src/pack.cpp new file mode 100644 index 0000000..5b3ba2a --- /dev/null +++ b/src/pack.cpp @@ -0,0 +1,63 @@ +#include "clusters.hpp" +#include "rules.hpp" +#include "data.hpp" + +#include +#include +#include + +#include +#include + +int main(int argc, char** argv){ + namespace po = boost::program_options; + namespace fs = boost::filesystem; + + // Describe program options + po::options_description opts; + opts.add_options() + ("input,i", "input directory") + ("output,o", po::value(), "output file") + ("add-unsolvables,u", po::bool_switch(), "also adds the unsolvable levels to the pack") + ("help", po::bool_switch(), "show this help"); + + po::positional_options_description file_opts; + file_opts.add("input", 1); + + // Parse and store them in a vm + po::variables_map vm; + po::store(po::command_line_parser(argc, argv).options(opts).positional(file_opts).run(), vm); + po::notify(vm); + + if(vm["help"].as()){ + std::cout << "Puzzle Wuzzle Level Packer, version " << __DATE__ << std::endl; + std::cout << "pack [-u] [-o ] \n"; + std::cout << opts << std::endl; + return 0; + } + + fs::path directory = vm["input"].as(); + fs::path output_file = directory.parent_path().string() + ".pck"; + if(vm.count("output")) output_file = vm["output"].as(); + + std::vector> pack; + std::cout << "Packing " << directory << " into " << output_file << std::endl; + bool unsolvables = vm["add-unsolvables"].as(); + + fs::directory_iterator eod; + for(fs::directory_iterator it(directory); it != eod; ++it){ + auto && path = it->path(); + if(path.extension() != ".lvl") continue; + + auto level = grid_from_file(path.string()); + if(!unsolvables && level.solutions.traces.empty()) continue; + + pack.emplace_back(std::move(level)); + } + + std::ofstream file(output_file.string()); + boost::archive::text_oarchive ar(file); + ar << pack; + + std::cout << "Packed " << pack.size() << " levels" << std::endl; +} diff --git a/src/replay.cpp b/src/replay.cpp index 89805f0..34a7dfe 100644 --- a/src/replay.cpp +++ b/src/replay.cpp @@ -26,20 +26,22 @@ int main(int argc, char** argv){ if(vm["help"].as()){ std::cout << "Puzzle Wuzzle Replayer, version " << __DATE__ << std::endl; + std::cout << "replay \n"; std::cout << opts << std::endl; return 0; } std::cout << "= The puzzle =\n"; auto level = grid_from_file(vm["file"].as()); - level.grid.print(std::cout); + auto original_grid = DynamicGrid{std::move(level.grid)}; + original_grid.print(std::cout); std::cout << "has " << level.solutions.traces.size() << " solutions\n\n"; int count = 1; for(auto && solution : level.solutions.traces){ std::cout << "= Solution " << count++ << " =\n"; - auto grid = level.grid; + auto grid = original_grid; grid.print(std::cout); for(auto && tap : solution){