summary refs log tree commit diff stats
path: root/Source
diff options
context:
space:
mode:
Diffstat (limited to 'Source')
-rw-r--r--Source/Random.h14
-rw-r--r--Source/Solver.cpp4
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:
6 static void SetSeed(int seed); 6 static void SetSeed(int seed);
7 static int RandInt(int min, int max); 7 static int RandInt(int min, int max);
8 8
9 template <typename T>
10 static std::vector<T> SelectFromSet(std::vector<T> set, size_t count) {
11 size_t setSize = set.size();
12 assert(count < setSize);
13 std::vector<T> selection;
14 for (int i=0; i<count && i<setSize; i++) {
15 int index = Random::RandInt(0, static_cast<int>(setSize - 1));
16 selection.emplace_back(set[index]);
17 set[index] = set[setSize-1];
18 setSize--;
19 }
20 return selection;
21 }
22
9private: 23private:
10 static uint32_t s_seed; 24 static uint32_t s_seed;
11}; 25};
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<Puzzle> Solver::Solve(Puzzle& p) {
21} 21}
22 22
23void Solver::SolveLoop(Puzzle& p, int x, int y, std::vector<Puzzle>& solutions) { 23void Solver::SolveLoop(Puzzle& p, int x, int y, std::vector<Puzzle>& solutions) {
24 // Stop trying to solve once we reach our goal 24 // Stop trying to solve once we reach our goal
25 if (solutions.size() >= MAX_SOLUTIONS) return; 25 if (solutions.size() >= MAX_SOLUTIONS) return;
26 Cell cell = p.GetCell(x, y); 26 Cell cell = p.GetCell(x, y);
27 if (cell.undefined) return; 27 if (cell.undefined) return;
@@ -30,7 +30,6 @@ void Solver::SolveLoop(Puzzle& p, int x, int y, std::vector<Puzzle>& solutions)
30 if (p.symmetry == Puzzle::Symmetry::NONE) { 30 if (p.symmetry == Puzzle::Symmetry::NONE) {
31 if (cell.color != Cell::Color::NONE) return; // Collided with ourselves 31 if (cell.color != Cell::Color::NONE) return; // Collided with ourselves
32 p.grid[x][y].color = Cell::Color::BLACK; // Otherwise, mark this cell as visited 32 p.grid[x][y].color = Cell::Color::BLACK; // Otherwise, mark this cell as visited
33 p.sequence.emplace_back(x, y);
34 } else { 33 } else {
35 // Get the symmetrical position, and try coloring it 34 // Get the symmetrical position, and try coloring it
36 auto sym = p.GetSymmetricalPos(x, y); 35 auto sym = p.GetSymmetricalPos(x, y);
@@ -44,6 +43,7 @@ void Solver::SolveLoop(Puzzle& p, int x, int y, std::vector<Puzzle>& solutions)
44 } 43 }
45 p.grid[x][y].color = Cell::Color::BLUE; // Otherwise, mark this cell as visited 44 p.grid[x][y].color = Cell::Color::BLUE; // Otherwise, mark this cell as visited
46 } 45 }
46 p.sequence.emplace_back(x, y);
47 47
48 if (cell.end != Cell::Dir::NONE) { 48 if (cell.end != Cell::Dir::NONE) {
49 // Reached an endpoint, validate solution and keep going -- there may be other endpoints 49 // Reached an endpoint, validate solution and keep going -- there may be other endpoints