-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerator.hpp
106 lines (93 loc) · 2.21 KB
/
generator.hpp
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
105
#pragma once
#include "grid.hpp"
#include "utils.hpp"
#include "solver.hpp"
#include <cassert>
#include <vector>
#define EASY 1
#define MEDIUM 2
#define HARD 3
#define EASY_NUM 24
#define MEDIUM_NUM 30
#define HARD_NUM 40
#define PRESETS 10
class Generator{
public:
static Grid getGrid(int diff) {
Grid g = Grid();
initializeGrid(g, diff);
for(int i=0; i< 9; i++) {
for(int j=0; j<9; j++) {
int val = g[i][j];
if(val != -10) {
g.set(i,j,-val);
}
}
}
return g;
}
private:
static void initializeGrid(Grid &g, int diff) {
setBlanks(g);
srand(time(NULL));
createValidGrid(g, PRESETS);
/* TODO: find a way to store the solution
Grid solution = g;
std::cout << "Default solution: \n";
solution.print();
*/
eraseCells(g, diff);
}
// Removes random spots on the grid based on difficulty rating
static void eraseCells(Grid &g, int diff) {
int count = 0;
if (diff == EASY) {
count = EASY_NUM;
} else if (diff == MEDIUM) {
count = MEDIUM_NUM;
} else {
count = HARD_NUM;
}
while (count > 0) {
int x = std::rand()%9;
int y = std::rand()%9;
if (g[x][y] != -10) {
g.set(x,y, -10);
count--;
}
}
}
// fill the grid will a full, valid sudoku
static void createValidGrid(Grid &g, int presets) {
while (presets > 0) {
int x = std::rand()%9;
int y = std::rand()%9;
if (g[x][y] != -10) continue;
// choose an empty cell to set
std::vector<int> vec = getRandom();
for (int i = 0; i < 9; i++) {
if (Solver::isValidMove(vec[i], g, x, y)) {
g.set(x,y,vec[i]);
if (Solver::hasSolution(g)) {
presets--;
break;
}
g.set(x,y,-10);
}
}
}
g = Solver::getSolution(g);
}
static void setBlanks(Grid &g) {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
g.set(i,j,-10);
}
}
}
static void setRow(Grid &g, std::vector<int> vec, int index) {
for (int i = 0; i < 9; i++) {
g.set(index,i,vec[i]*-1);
}
}
};