From 859cfdb86d57bb230f1200c3a4c76c55ca920199 Mon Sep 17 00:00:00 2001 From: jbzdarkid Date: Thu, 21 Nov 2019 10:03:34 -0800 Subject: Well, got started on randomization anyways --- Source/Randomizer2.cpp | 67 ++++++++++++++++++++++++++++------------------ Source/Randomizer2Core.cpp | 28 ++++++++++++------- Source/Randomizer2Core.h | 8 +++--- 3 files changed, 65 insertions(+), 38 deletions(-) (limited to 'Source') diff --git a/Source/Randomizer2.cpp b/Source/Randomizer2.cpp index e4f2b9f..cc23c7d 100644 --- a/Source/Randomizer2.cpp +++ b/Source/Randomizer2.cpp @@ -25,7 +25,7 @@ void Randomizer2::RandomizeTutorial() { p.grid[0][8].start = true; p.grid[8][0].end = Cell::Dir::UP; - for (Pos pos : Randomizer2Core::CutEdges(p, 14, true)) { + for (Pos pos : Randomizer2Core::CutEdges(p, 14)) { p.grid[pos.x][pos.y].gap = Cell::Gap::FULL; } _serializer.WritePuzzle(p, 0x293); @@ -36,24 +36,18 @@ void Randomizer2::RandomizeTutorial() { p.NewGrid(6, 6); // @Bug: Mid-segment endpoints are not yet supported. - switch (Random::RandInt(1, 4)) { - case 1: - p.grid[Random::RandInt(0, p.width-1)][0].end = Cell::Dir::UP; - break; - case 2: - p.grid[Random::RandInt(0, p.width-1)][p.height-1].end = Cell::Dir::DOWN; - break; - case 3: - p.grid[0][Random::RandInt(0, p.height-1)].end = Cell::Dir::LEFT; - break; - case 4: - p.grid[p.width-1][Random::RandInt(0, p.height-1)].end = Cell::Dir::RIGHT; - break; - } + int x = Random::RandInt(0, (p.width-1)/2)*2; + int y = Random::RandInt(0, (p.height-1)/2)*2; + int rng = Random::RandInt(1, 4); + if (rng == 1) p.grid[x][0].end = Cell::Dir::UP; + else if (rng == 2) p.grid[x][p.height-1].end = Cell::Dir::DOWN; + else if (rng == 3) p.grid[0][y].end = Cell::Dir::LEFT; + else if (rng == 4) p.grid[p.width-1][y].end = Cell::Dir::RIGHT; + // [4/6/8][4/6/8] p.grid[Random::RandInt(0, 2)*2 + 4][Random::RandInt(0, 2)*2 + 4].start = true; - for (Pos pos : Randomizer2Core::CutEdges(p, 35, true)) { + for (Pos pos : Randomizer2Core::CutEdges(p, 35)) { p.grid[pos.x][pos.y].gap = Cell::Gap::FULL; } @@ -67,7 +61,7 @@ void Randomizer2::RandomizeTutorial() { p.grid[0][20].start = true; p.grid[20][0].end = Cell::Dir::RIGHT; - for (Pos pos : Randomizer2Core::CutEdges(p, 96, true)) { + for (Pos pos : Randomizer2Core::CutEdges(p, 96)) { p.grid[pos.x][pos.y].gap = Cell::Gap::FULL; } _serializer.WritePuzzle(p, 0x2C2); @@ -81,7 +75,7 @@ void Randomizer2::RandomizeTutorial() { p.grid[12][0].end = Cell::Dir::RIGHT; p.grid[12][12].end = Cell::Dir::RIGHT; - for (Pos pos : Randomizer2Core::CutEdges(p, 27, true)) { + for (Pos pos : Randomizer2Core::CutEdges(p, 27)) { p.grid[pos.x][pos.y].gap = Cell::Gap::BREAK; } _serializer.WritePuzzle(p, 0xA3B5); @@ -153,7 +147,7 @@ void Randomizer2::RandomizeTutorial() { p.grid[pos.x][pos.y].gap = Cell::Gap::BREAK; } - for (Pos pos : Randomizer2Core::CutEdges(p, 30 - cuts.size(), true)) { + for (Pos pos : Randomizer2Core::CutEdges(p, 30 - cuts.size())) { p.grid[pos.x][pos.y].gap = Cell::Gap::BREAK; } _serializer.WritePuzzle(p, 0xA3B2); @@ -161,7 +155,28 @@ void Randomizer2::RandomizeTutorial() { } void Randomizer2::RandomizeSymmetry() { - { // + // Back wall + { + Puzzle p; + p.NewGrid(3, 3); + p.symmetry = Puzzle::Symmetry::X; + p.grid[0][6].start = true; + p.grid[6][6].start = true; + p.grid[2][0].end = Cell::Dir::UP; + p.grid[4][0].end = Cell::Dir::UP; + + std::vector cutEdges = Randomizer2Core::CutSymmetricalEdgePairs(p, 2); + for (Pos pos : cutEdges) { + Pos sym = p.GetSymmetricalPos(pos.x, pos.y); + p.grid[pos.x][pos.y].gap = Cell::Gap::BREAK; + p.grid[sym.x][sym.y].gap = Cell::Gap::BREAK; + } + _serializer.WritePuzzle(p, 0x86); + } + { + Puzzle p; + p.NewGrid(4, 4); + p.symmetry = Puzzle::Symmetry::X; } } @@ -186,7 +201,7 @@ void Randomizer2::RandomizeKeep() { p.grid[4][8].start = true; p.grid[6][0].end = Cell::Dir::UP; - std::vector cutEdges = Randomizer2Core::CutEdges(p, 5, false); + std::vector cutEdges = Randomizer2Core::CutInsideEdges(p, 5); Puzzle copy = p; std::vector gates = {0x00344, 0x00488, 0x00489, 0x00495, 0x00496}; for (int i=0; i cutEdges = Randomizer2Core::CutEdges(p, 7, false); + std::vector cutEdges = Randomizer2Core::CutInsideEdges(p, 7); for (Pos pos : cutEdges) { p.grid[pos.x][pos.y].gap = Cell::Gap::FULL; } @@ -231,7 +246,7 @@ void Randomizer2::RandomizeKeep() { q.grid[pos.x][pos.y].gap = Cell::Gap::FULL; } // Cut to 6 of 9 additional edges - for (Pos pos : Randomizer2Core::CutEdges(q, 6, false)) { + for (Pos pos : Randomizer2Core::CutInsideEdges(q, 6)) { q.grid[pos.x][pos.y].gap = Cell::Gap::FULL; } _serializer.WritePuzzle(q, 0x19DC); @@ -254,7 +269,7 @@ void Randomizer2::RandomizeKeep() { p.grid[0][8].start = true; p.grid[8][2].end = Cell::Dir::RIGHT; - std::vector cutEdges = Randomizer2Core::CutEdges(p, 7, false); + std::vector cutEdges = Randomizer2Core::CutInsideEdges(p, 7); for (Pos pos : cutEdges) { p.grid[pos.x][pos.y].gap = Cell::Gap::BREAK; } @@ -286,7 +301,7 @@ void Randomizer2::RandomizeKeep() { p.grid[0][8].start = true; p.grid[4][0].end = Cell::Dir::UP; - std::vector cutEdges = Randomizer2Core::CutEdges(p, 2, false); + std::vector cutEdges = Randomizer2Core::CutInsideEdges(p, 2); for (Pos pos : cutEdges) { p.grid[pos.x][pos.y].gap = Cell::Gap::FULL; } @@ -300,7 +315,7 @@ void Randomizer2::RandomizeKeep() { for (Pos pos : cutEdges) { q.grid[pos.x][pos.y].gap = Cell::Gap::FULL; } - for (Pos pos : Randomizer2Core::CutEdges(q, 7, false)) { + for (Pos pos : Randomizer2Core::CutInsideEdges(q, 7)) { q.grid[pos.x][pos.y].gap = Cell::Gap::FULL; } _serializer.WritePuzzle(q, 0x1A0F); diff --git a/Source/Randomizer2Core.cpp b/Source/Randomizer2Core.cpp index f8d1312..dcb9fd2 100644 --- a/Source/Randomizer2Core.cpp +++ b/Source/Randomizer2Core.cpp @@ -6,13 +6,25 @@ #include #include -std::vector Randomizer2Core::CutEdges(const Puzzle& p, size_t numEdges, bool allowEdges) { - std::vector edges; - int xMin = allowEdges ? 0 : 1; - int xMax = allowEdges ? p.width : p.width-1; - int yMin = allowEdges ? 0 : 1; - int yMax = allowEdges ? p.height : p.height-1; +std::vector Randomizer2Core::CutEdges(const Puzzle& p, size_t numEdges) { + return CutEdgesInternal(p, 0, p.width, 0, p.height, numEdges); +} +std::vector Randomizer2Core::CutInsideEdges(const Puzzle& p, size_t numEdges) { + return CutEdgesInternal(p, 1, p.width-1, 1, p.height-1, numEdges); +} + +std::vector Randomizer2Core::CutSymmetricalEdgePairs(const Puzzle& p, size_t numEdges) { + assert(p.symmetry != Puzzle::Symmetry::NONE); + if (p.symmetry == Puzzle::Symmetry::X) { + return CutEdgesInternal(p, 0, (p.width-1)/2, 0, p.height, numEdges); + } + assert(false); + return {}; +} + +std::vector Randomizer2Core::CutEdgesInternal(const Puzzle& p, int xMin, int xMax, int yMin, int yMax, size_t numEdges) { + std::vector edges; for (int x=xMin; x Randomizer2Core::CutEdges(const Puzzle& p, size_t numEdges, boo edges.emplace_back(x, y); } } - return CutEdgesInternal(p, edges, numEdges); -} + assert(numEdges <= edges.size()); -std::vector Randomizer2Core::CutEdgesInternal(const Puzzle& p, std::vector& edges, size_t numEdges) { auto [colorGrid, numColors] = CreateColorGrid(p); assert(numEdges <= numColors); diff --git a/Source/Randomizer2Core.h b/Source/Randomizer2Core.h index 443f893..674e4ea 100644 --- a/Source/Randomizer2Core.h +++ b/Source/Randomizer2Core.h @@ -6,11 +6,13 @@ class Puzzle; class Randomizer2Core { public: - // CAUTION: Does not actually cut edges, just returns a list of suggested cuts. - static std::vector CutEdges(const Puzzle& p, size_t numEdges, bool allowEdges); + // CAUTION: These do not actually cut edges, they just returns a list of suggested cuts. + static std::vector CutEdges(const Puzzle& p, size_t numEdges); + static std::vector CutInsideEdges(const Puzzle& p, size_t numEdges); + static std::vector CutSymmetricalEdgePairs(const Puzzle& p, size_t numEdges); private: - static std::vector CutEdgesInternal(const Puzzle& p, std::vector& edges, size_t numEdges); + static std::vector CutEdgesInternal(const Puzzle& p, int xMin, int xMax, int yMin, int yMax, size_t numEdges); static void DebugColorGrid(const std::vector>& colorGrid); static void FloodFill(const Puzzle& p, std::vector>& colorGrid, int color, int x, int y); static void FloodFillOutside(const Puzzle& p, std::vector>& colorGrid, int x, int y); -- cgit 1.4.1