#include "Randomizer2Core.h" #include "Puzzle.h" #include "Random.h" #include #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; for (int x=xMin; x Randomizer2Core::CutEdgesInternal(const Puzzle& p, std::vector& edges, size_t numEdges) { auto [colorGrid, numColors] = CreateColorGrid(p); assert(numEdges <= numColors); std::vector cutEdges; for (int i=0; i(edges.size() - 1)); Pos pos = edges[edge]; edges.erase(edges.begin() + edge); int color1 = 0; int color2 = 0; if (pos.x%2 == 0 && pos.y%2 == 1) { // Vertical if (pos.x > 0) color1 = colorGrid[pos.x-1][pos.y]; else color1 = 1; if (pos.x < p.width - 1) color2 = colorGrid[pos.x+1][pos.y]; else color2 = 1; } else { // Horizontal assert(pos.x%2 == 1 && pos.y%2 == 0); if (pos.y > 0) color1 = colorGrid[pos.x][pos.y-1]; else color1 = 1; if (pos.y < p.height - 1) color2 = colorGrid[pos.x][pos.y+1]; else color2 = 1; } // Enforce color1 < color2 if (color1 > color2) std::swap(color1, color2); // Colors mismatch, valid cut if (color1 != color2) { // @Performance... have a lookup table instead? for (int x=0; x>& colorGrid) { for (int y=0; y>& colorGrid, int color, int x, int y) { if (!p.SafeCell(x, y)) return; if (colorGrid[x][y] != 0) return; // Already processed. colorGrid[x][y] = color; FloodFill(p, colorGrid, color, x, y+1); FloodFill(p, colorGrid, color, x, y-1); FloodFill(p, colorGrid, color, x+1, y); FloodFill(p, colorGrid, color, x-1, y); } void Randomizer2Core::FloodFillOutside(const Puzzle& p, std::vector>& colorGrid, int x, int y) { if (!p.SafeCell(x, y)) return; if (colorGrid[x][y] != 0) return; // Already processed. if (x%2 != y%2 && p.grid[x][y].gap != Cell::Gap::FULL) return; // Only flood-fill through full gaps colorGrid[x][y] = 1; // Outside color FloodFillOutside(p, colorGrid, x, y+1); FloodFillOutside(p, colorGrid, x, y-1); FloodFillOutside(p, colorGrid, x+1, y); FloodFillOutside(p, colorGrid, x-1, y); } /* undefined -> 1 (color of outside) or * (any colored cell) or -1 (edge/intersection not part of any region) 0 -> {} (this is a special edge case, which I don't need right now) 1 -> 0 (uncolored / ready to color) 2 -> */ std::tuple>, int> Randomizer2Core::CreateColorGrid(const Puzzle& p) { std::vector> colorGrid; colorGrid.resize(p.width); for (int x=0; x