From 1ccd3488a0c946b2ad4c0e525d91dcf96d1ca707 Mon Sep 17 00:00:00 2001 From: jbzdarkid Date: Wed, 4 Dec 2019 17:44:38 -0800 Subject: helper func + bug fix --- Source/Random.h | 14 ++++++++++++++ Source/Solver.cpp | 4 ++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/Source/Random.h b/Source/Random.h index e809c1e..36b6dc1 100644 --- a/Source/Random.h +++ b/Source/Random.h @@ -6,6 +6,20 @@ public: static void SetSeed(int seed); static int RandInt(int min, int max); + template + static std::vector SelectFromSet(std::vector set, size_t count) { + size_t setSize = set.size(); + assert(count < setSize); + std::vector selection; + for (int i=0; i(setSize - 1)); + selection.emplace_back(set[index]); + set[index] = set[setSize-1]; + setSize--; + } + return selection; + } + private: static uint32_t s_seed; }; diff --git a/Source/Solver.cpp b/Source/Solver.cpp index 2813027..74fa099 100644 --- a/Source/Solver.cpp +++ b/Source/Solver.cpp @@ -21,7 +21,7 @@ std::vector Solver::Solve(Puzzle& p) { } void Solver::SolveLoop(Puzzle& p, int x, int y, std::vector& solutions) { - // Stop trying to solve once we reach our goal + // Stop trying to solve once we reach our goal if (solutions.size() >= MAX_SOLUTIONS) return; Cell cell = p.GetCell(x, y); if (cell.undefined) return; @@ -30,7 +30,6 @@ void Solver::SolveLoop(Puzzle& p, int x, int y, std::vector& solutions) if (p.symmetry == Puzzle::Symmetry::NONE) { if (cell.color != Cell::Color::NONE) return; // Collided with ourselves p.grid[x][y].color = Cell::Color::BLACK; // Otherwise, mark this cell as visited - p.sequence.emplace_back(x, y); } else { // Get the symmetrical position, and try coloring it auto sym = p.GetSymmetricalPos(x, y); @@ -44,6 +43,7 @@ void Solver::SolveLoop(Puzzle& p, int x, int y, std::vector& solutions) } p.grid[x][y].color = Cell::Color::BLUE; // Otherwise, mark this cell as visited } + p.sequence.emplace_back(x, y); if (cell.end != Cell::Dir::NONE) { // Reached an endpoint, validate solution and keep going -- there may be other endpoints -- cgit 1.4.1