#include "Randomizer2.h" #include "Puzzle.h" #include "Random.h" #include "Solver.h" #include "Memory.h" 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; 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); } // 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) { 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]; p.grid[pos.x][pos.y].gap = Cell::Gap::FULL; edges.erase(edges.begin() + edge); if (FloodFill(p)) { 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; } 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) { 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]; p.grid[pos.x][pos.y].gap = Cell::Gap::FULL; edges.erase(edges.begin() + edge); if (FloodFill(p)) { 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; 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; } switch (Random::RandInt(1, 3)) { case 1: // Horiz (6) [5/7][4/6/8] p.grid[Random::RandInt(0, 1)*2 + 5][Random::RandInt(0, 2)*2 + 4].start = true; break; case 2: // Verti (6) [4/6/8][5/7] p.grid[Random::RandInt(0, 2)*2 + 4][Random::RandInt(0, 1)*2 + 5].start = true; break; case 3: // Inter (9) [4/6/8][4/6/8] p.grid[Random::RandInt(0, 2)*2 + 4][Random::RandInt(0, 2)*2 + 4].start = true; break; } auto solutions = Solver::Solve(p); if (solutions.size() > 0) break; } PuzzleSerializer(_memory).WritePuzzle(p, 0x295); } void Randomizer2::RandomizeKeep() { Puzzle p; p.width = 9; p.height = 10; p.grid.clear(); p.grid.resize(p.width); for (int x=0; xWritePanelData(panel, POSITION, {x, y, 19.1f}); float z, w; if (X%2 == 0 && Y%2 == 1) { // Horizontal z = -0.1f; w = 1.0f; } else if (X%2 == 1 && Y%2 == 0) { // Vertical z = -.77f; w = .63f; } else { assert(false); return; } _memory->WritePanelData(panel, ORIENTATION, {0.0f, 0.0f, z, w}); }