From bff40e55c9c55fbc8439bb225d1937b2d805e629 Mon Sep 17 00:00:00 2001 From: jbzdarkid Date: Mon, 18 Nov 2019 09:16:16 -0800 Subject: Cleanup & progress on tutorial --- Source/Randomizer2.cpp | 197 +++++++++++++------------------------------------ 1 file changed, 53 insertions(+), 144 deletions(-) (limited to 'Source/Randomizer2.cpp') diff --git a/Source/Randomizer2.cpp b/Source/Randomizer2.cpp index d9c00c0..7a50c7b 100644 --- a/Source/Randomizer2.cpp +++ b/Source/Randomizer2.cpp @@ -1,128 +1,39 @@ +#include "Memory.h" #include "Randomizer2.h" +#include "Randomizer2Core.h" #include "Puzzle.h" #include "Random.h" #include "Solver.h" -#include "Memory.h" -#include "Randomizer2Core.h" -#include "PuzzlerSerializer.h" + #include #include -void FloodFillInternal(const Puzzle& p, std::vector>& reached, int x, int y) { - if (x%2 == 1 && y%2 == 1) return; - auto cell = p.GetCell(x, y); - if (cell.undefined) return; - if (cell.gap != Cell::Gap::NONE) return; - if (reached[x][y]) return; +#pragma warning (disable: 26451) - reached[x][y] = true; - FloodFillInternal(p, reached, x-1, y); - FloodFillInternal(p, reached, x+1, y); - FloodFillInternal(p, reached, x, y-1); - FloodFillInternal(p, reached, x, y+1); -} +Randomizer2::Randomizer2(const std::shared_ptr& memory) : _memory(memory), _serializer(PuzzleSerializer(_memory)) {} -// Returns true: All nodes reachable / false: Some node disconnected -bool FloodFill(const Puzzle& p) { - std::vector> reached; - reached.resize(p.width); - for (int x=0; x& memory) : _memory(memory) {} - -void Randomizer2::Randomize() { - // 4x4 - // 14 gaps - // start (x=0, y=8) - // end (x=8, y=0) Up - // 1 solution - Puzzle p; - while (true) { +void Randomizer2::RandomizeTutorial() { + { // Far center + Puzzle p; p.NewGrid(4, 4); - - std::vector corners; - std::vector cells; - std::vector edges; - for (int x=0; x(edges.size() - 1)); - Pos pos = edges[edge]; - edges.erase(edges.begin() + edge); - - if (FloodFill(p)) { - p.grid[pos.x][pos.y].gap = Cell::Gap::FULL; - break; - } else { - p.grid[pos.x][pos.y].gap = Cell::Gap::NONE; - } - } - } - p.grid[0][8].start = true; p.grid[8][0].end = Cell::Dir::UP; - auto solutions = Solver::Solve(p); - if (solutions.size() > 0) break; + for (Pos pos : Randomizer2Core::CutEdges(p, 14)) { + p.grid[pos.x][pos.y].gap = Cell::Gap::FULL; + } + _serializer.WritePuzzle(p, 0x293); } - PuzzleSerializer(_memory).WritePuzzle(p, 0x293); - // 7x7 - // 35 gaps - // start (x=8, y=8) - // end (x=4, y=0) Up - // 2 solutions, 37 & 39 - while (true) { + { + Puzzle p; p.NewGrid(7, 7); - std::vector corners; - std::vector cells; - std::vector edges; - for (int x=0; x(edges.size() - 1)); - Pos pos = edges[edge]; - edges.erase(edges.begin() + edge); - - if (FloodFill(p)) { - p.grid[pos.x][pos.y].gap = Cell::Gap::FULL; - break; - } else { - p.grid[pos.x][pos.y].gap = Cell::Gap::NONE; - } - } - } - switch (Random::RandInt(1, 4)) { case 1: p.grid[Random::RandInt(0, p.width-1)][0].end = Cell::Dir::UP; @@ -148,17 +59,17 @@ void Randomizer2::Randomize() { p.grid[Random::RandInt(0, 2)*2 + 4][Random::RandInt(0, 2)*2 + 4].start = true; break; } + + for (Pos pos : Randomizer2Core::CutEdges(p, 35)) { + p.grid[pos.x][pos.y].gap = Cell::Gap::FULL; + } - auto solutions = Solver::Solve(p); - if (solutions.size() > 0) break; + _serializer.WritePuzzle(p, 0x295); } - PuzzleSerializer(_memory).WritePuzzle(p, 0x295); - } void Randomizer2::RandomizeKeep() { - // *** Hedges 1 *** - { + { // Hedges 1 Puzzle p; p.NewGrid(4, 4); @@ -177,8 +88,7 @@ void Randomizer2::RandomizeKeep() { p.grid[4][8].start = true; p.grid[6][0].end = Cell::Dir::UP; - std::vector cutEdges = Randomizer2Core::CutEdgesToBeUnique(p); - assert(cutEdges.size() == 5); + std::vector cutEdges = Randomizer2Core::CutEdges2(p, 5); Puzzle copy = p; std::vector gates = {0x00344, 0x00488, 0x00489, 0x00495, 0x00496}; for (int i=0; i cutEdges = Randomizer2Core::CutEdgesToBeUnique(p); - assert(cutEdges.size() == 7); + std::vector cutEdges = Randomizer2Core::CutEdges2(p, 7); for (Pos pos : cutEdges) { p.grid[pos.x][pos.y].gap = Cell::Gap::FULL; } - auto solutions = Solver::Solve(p); - assert(solutions.size() == 1); + auto solution = GetUniqueSolution(p); Puzzle q; q.NewGrid(4, 4); q.grid[0][8].start = true; q.grid[8][0].end = Cell::Dir::RIGHT; - q.sequence = solutions[0].sequence; + q.sequence = solution.sequence; for (Pos pos : cutEdges) { q.grid[pos.x][pos.y].gap = Cell::Gap::FULL; } - // Cut to 4 of 9 additional edges (total: 11) - Randomizer2Core::CutEdgesNotOutsideNotBreakingSequence(q, 4); - PuzzleSerializer(_memory).WritePuzzle(q, 0x19DC); + // Cut to 6 of 9 additional edges + for (Pos pos : Randomizer2Core::CutEdges2(q, 6)) { + q.grid[pos.x][pos.y].gap = Cell::Gap::FULL; + } + _serializer.WritePuzzle(q, 0x19DC); } - // *** Hedges 3 ** - { + { // Hedges 3 [WIP] Puzzle p; p.NewGrid(4, 4); @@ -251,17 +158,14 @@ void Randomizer2::RandomizeKeep() { std::vector pebbleMarkers = {0x034a9, 0x034b1, 0x034be, 0x034c4}; - - std::vector cutEdges = Randomizer2Core::CutEdgesToBeUnique(p); - assert(cutEdges.size() == 7); + std::vector cutEdges = Randomizer2Core::CutEdges2(p, 7); for (Pos pos : cutEdges) { p.grid[pos.x][pos.y].gap = Cell::Gap::BREAK; } - PuzzleSerializer(_memory).WritePuzzle(p, 0x19E7); + // _serializer.WritePuzzle(p, 0x19E7); } - // *** Hedges 4 *** - { + { // Hedges 4 Puzzle p; p.NewGrid(4, 4); @@ -283,28 +187,33 @@ void Randomizer2::RandomizeKeep() { p.grid[0][8].start = true; p.grid[4][0].end = Cell::Dir::UP; - std::vector cutEdges = Randomizer2Core::CutEdgesToBeUnique(p); - assert(cutEdges.size() == 2); + std::vector cutEdges = Randomizer2Core::CutEdges2(p, 2); for (Pos pos : cutEdges) { p.grid[pos.x][pos.y].gap = Cell::Gap::FULL; } - auto solutions = Solver::Solve(p); - assert(solutions.size() == 1); + auto solution = GetUniqueSolution(p); Puzzle q; q.NewGrid(4, 4); q.grid[0][8].start = true; q.grid[4][0].end = Cell::Dir::UP; - q.sequence = solutions[0].sequence; + q.sequence = solution.sequence; for (Pos pos : cutEdges) { q.grid[pos.x][pos.y].gap = Cell::Gap::FULL; } - // 9 cuts, -2 from existing cuts - Randomizer2Core::CutEdgesNotOutsideNotBreakingSequence(q, 7); - PuzzleSerializer(_memory).WritePuzzle(q, 0x1A0F); + for (Pos pos : Randomizer2Core::CutEdges2(q, 7)) { + q.grid[pos.x][pos.y].gap = Cell::Gap::FULL; + } + _serializer.WritePuzzle(q, 0x1A0F); } } +Puzzle Randomizer2::GetUniqueSolution(Puzzle& p) { + auto solutions = Solver::Solve(p); + assert(solutions.size() == 1); + return solutions[0]; +} + void Randomizer2::SetGate(int panel, int X, int Y) { float x, y, z, w; if (X%2 == 0 && Y%2 == 1) { // Horizontal -- cgit 1.4.1