about summary refs log tree commit diff stats
path: root/Source/Randomizer2.cpp
blob: fcfec9ac8362b7bbdf5ab7093432df97dbd80b04 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "Randomizer2.h"
#include "Puzzle.h"
#include "Random.h"
#include "Solver.h"

Randomizer2::Randomizer2(const std::shared_ptr<Memory>& memory) : _memory(memory) {}

void Randomizer2::Randomize() {
    // 4x4
    // 14 gaps
    // start (x=0, y=8)
    // end (x=8, y=0) Up
    // 1 solution
    Puzzle p;
    int attemptCount = 0;
    while (true) {
        attemptCount++;
        p.NewGrid(4, 4);

        std::vector<Pos> corners;
        std::vector<Pos> cells;
        std::vector<Pos> edges;
        for (int x=0; x<p.width; x++) {
            for (int y=0; y<p.height; y++) {
                if (x%2 == 0 && y%2 == 0) corners.emplace_back(Pos{x, y});
                else if (x%2 == 1 && y%2 == 1) cells.emplace_back(Pos{x, y});
                else edges.emplace_back(Pos{x, y});
            }
        }

        for (int i=0; i<14; i++) {
            int edge = Random::RandInt(0, static_cast<int>(edges.size() - 1));
            Pos pos = edges[edge];
            p.grid[pos.x][pos.y].gap = Cell::Gap::FULL;
            edges.erase(edges.begin() + edge);
        }

        p.grid[0][8].start = true;
        p.grid[8][0].end = Cell::Dir::UP;

        auto solutions = Solver::Solve(p);
        if (solutions.size() == 1) {
            auto solution = solutions[0];
            int solutionLength = 0;
            for (int x=0; x<solution.width; x++) {
                for (int y=0; y<solution.height; y++) {
                    if (solution.grid[x][y].color == Cell::Color::BLACK) solutionLength++;
                }
            }
            if (solutionLength == 25) break;
        }
    }
    PuzzleSerializer(_memory).WritePuzzle(p, 0x293);




    // 7x7
    // 35 gaps
    // start (x=8, y=8)
    // end (x=4, y=0) Up
    // 2 solutions, 37 & 39
    attemptCount = 0;
    while (true) {
        attemptCount++;
        p.NewGrid(7, 7);

        std::vector<Pos> corners;
        std::vector<Pos> cells;
        std::vector<Pos> edges;
        for (int x=0; x<p.width; x++) {
            for (int y=0; y<p.height; y++) {
                if (x%2 == 0 && y%2 == 0) corners.emplace_back(Pos{x, y});
                else if (x%2 == 1 && y%2 == 1) cells.emplace_back(Pos{x, y});
                else edges.emplace_back(Pos{x, y});
            }
        }

        for (int i=0; i<35; i++) {
            int edge = Random::RandInt(0, static_cast<int>(edges.size() - 1));
            Pos pos = edges[edge];
            p.grid[pos.x][pos.y].gap = Cell::Gap::FULL;
            edges.erase(edges.begin() + edge);
        }

        p.grid[8][8].start = true;
        p.grid[4][0].end = Cell::Dir::UP;

        auto solutions = Solver::Solve(p);
        if (solutions.size() > 0) break;
        if (solutions.size() > 0 && solutions.size() < 5) {
            auto solution = solutions[0];
            int solutionLength = 0;
            for (int x=0; x<solution.width; x++) {
                for (int y=0; y<solution.height; y++) {
                    if (solution.grid[x][y].color == Cell::Color::BLACK) solutionLength++;
                }
            }
            if (solutionLength > 30 && solutionLength < 40) break;
        }
    }
    PuzzleSerializer(_memory).WritePuzzle(p, 0x295);

}