-
Notifications
You must be signed in to change notification settings - Fork 0
/
Generator.hpp
290 lines (239 loc) · 6.76 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
//Generator.h
/* Sudoku generator class */
#pragma once
#include "Solver.hpp"
namespace BackWork
{
class Generator
{
private:
// Sudoku 1D array and must be of size 9 x 9 = 81
int* mGrid;
// Sudoku solution 1D array and must be of size 9 x 9 = 81
int* mGridSolution;
private:
// Set and get element of 1D array using row and col
inline void setElement(const int row, const int col, const int num);
inline int getElement(const int row, const int col) const;
// Swapping
void swapNumbers(const int index1, const int index2);
void swapRows(const int row1, const int row2);
void swapCols(const int col1, const int col2);
void swapRowBlocks(const int rowBlock1, const int rowBlock2);
void swapColBlocks(const int colBlock1, const int colBlock2);
// Fill next row with previous row with n shifts
void fillNextRow(const int previousRow, const int nextRow, const int shifts);
// Copy this grid into grid
void copyGrid(int* grid) const;
// Create a random completed Sudoku array using shuffling
void createCompletedSudoku();
public:
// Constructor for intialisation
Generator();
// Generate Sudoku from completed Sudoku and return a pointer to it
void generate(int* grid, int* solutionGrid, Buttons);
};
};
BackWork::Generator::Generator()
: mGrid(nullptr), mGridSolution(nullptr)
{
}
inline void BackWork::Generator::setElement(const int row, const int col, const int num)
{
mGrid[row * 9 + col] = num;
}
inline int BackWork::Generator::getElement(const int row, const int col) const
{
return mGrid[row * 9 + col];
}
void BackWork::Generator::swapNumbers(const int index1, const int index2)
{
// Only works if they're not from the same index
mGrid[index1] = mGrid[index1] ^ mGrid[index2];
mGrid[index2] = mGrid[index1] ^ mGrid[index2];
mGrid[index1] = mGrid[index1] ^ mGrid[index2];
}
void BackWork::Generator::swapRows(const int row1, const int row2)
{
for (int col = 0; col < 9; col++)
{
swapNumbers(row1 * 9 + col, row2 * 9 + col);
}
}
void BackWork::Generator::swapCols(const int col1, const int col2)
{
for (int row = 0; row < 9; row++)
{
swapNumbers(row * 9 + col1, row * 9 + col2);
}
}
void BackWork::Generator::swapRowBlocks(const int rowBlock1, const int rowBlock2)
{
int startRow1 = rowBlock1 * 3;
int startRow2 = rowBlock2 * 3;
for (int i = 0; i < 3; i++)
{
swapRows(startRow1 + i, startRow2 + i);
}
}
void BackWork::Generator::swapColBlocks(const int colBlock1, const int colBlock2)
{
int startCol1 = colBlock1 * 3;
int startCol2 = colBlock2 * 3;
for (int i = 0; i < 3; i++)
{
swapCols(startCol1 + i, startCol2 + i);
}
}
void BackWork::Generator::fillNextRow(const int previousRow, const int nextRow, const int shifts)
{
for (int col = 0; col < (9 - shifts); col++)
{
setElement(nextRow, col, getElement(previousRow, col + shifts));
}
for (int col = (9 - shifts); col < 9; col++)
{
setElement(nextRow, col, getElement(previousRow, col - 9 + shifts));
}
}
void BackWork::Generator::copyGrid(int* grid) const
{
for (int i = 0; i < 81; i++)
{
grid[i] = mGrid[i];
}
}
void BackWork::Generator::createCompletedSudoku()
{
// Set random seed using time
srand((unsigned int)time(NULL));
// 1. Fill first row with numbers 1 to 9
for (int i = 0; i < 9; i++)
{
mGrid[i] = i + 1;
}
// 2. Shuffle first row of 9 numbers
int swaps = 50;
for (int i = 0; i < swaps; i++)
{
int randIndex1 = rand() % 9;
int randIndex2 = rand() % 9;
if (randIndex1 != randIndex2)
{
swapNumbers(randIndex1, randIndex2);
}
}
// 3. Fill second and third row by previous row but shifted by 3
fillNextRow(0, 1, 3);
fillNextRow(1, 2, 3);
// 4. Fill fourth row by previous row but shifted by 1
fillNextRow(2, 3, 1);
// 5. Fill fith and sixth row by previous row but shifted by 3
fillNextRow(3, 4, 3);
fillNextRow(4, 5, 3);
// 6. Fill seventh row by previous row but shifted by 1
fillNextRow(5, 6, 1);
// 7. Fill eith and ninth row by previous row but shifted by 3
fillNextRow(6, 7, 3);
fillNextRow(7, 8, 3);
// 8. Shuffle rows within every row block
int shuffles = 10;
for (int rowBlock = 0; rowBlock < 3; rowBlock++)
{
for (int shuffle = 0; shuffle < shuffles; shuffle++)
{
int randRow1 = rowBlock * 3 + (rand() % 3);
int randRow2 = rowBlock * 3 + (rand() % 3);
if (randRow1 != randRow2)
{
swapRows(randRow1, randRow2);
}
}
}
// 9. Shuffle cols within every col block
for (int colBlock = 0; colBlock < 3; colBlock++)
{
for (int shuffle = 0; shuffle < shuffles; shuffle++)
{
int randCol1 = colBlock * 3 + (rand() % 3);
int randCol2 = colBlock * 3 + (rand() % 3);
if (randCol1 != randCol2)
{
swapCols(randCol1, randCol2);
}
}
}
// 10. Shuffle row blocks
for (int shuffle = 0; shuffle < shuffles; shuffle++)
{
int randRowBlock1 = rand() % 3;
int randRowBlock2 = rand() % 3;
if (randRowBlock1 != randRowBlock2)
{
swapRowBlocks(randRowBlock1, randRowBlock2);
}
}
// 11. Shuffle col blocks
for (int shuffle = 0; shuffle < shuffles; shuffle++)
{
int randColBlock1 = rand() % 3;
int randColBlock2 = rand() % 3;
if (randColBlock1 != randColBlock2)
{
swapColBlocks(randColBlock1, randColBlock2);
}
}
// 12. Store solution in solution grid
copyGrid(mGridSolution);
}
void BackWork::Generator::generate(int* grid, int* solutionGrid, Buttons LEVEL)
{
// Set the Sudoku grid and solution grid
mGrid = grid;
mGridSolution = solutionGrid;
// Create completed Sudoku
createCompletedSudoku();
// Set random seed using time
srand((unsigned int)time(NULL));
// Create Sudoku solver object
Solver SS;
// Set the Sudoku solver to have the generator modifier
SS.setGenModifier(true);
// Create grid of bool types to track if elements have been removed from the main grid
bool removed[81] = { };
// Create a temporary duplicate grid
int duplicateGrid[81];
// Current number to be determined to be removed
int removingNumber = 0;
// Elements to remove for each level
int toRemove;
if(LEVEL == Buttons::HARD)
toRemove = 50;
else if(LEVEL == Buttons::MEDIUM)
toRemove = 44;
else if(LEVEL == Buttons::EASY)
toRemove = 37;
while (toRemove)
{
// 1. Pick a random number you haven't tried removing before
int randRow = rand() % 9;
int randCol = rand() % 9;
if (!removed[randRow * 9 + randCol])
{
// 2. Remove the number, then run solver without the number to be determined to be removed
removingNumber = getElement(randRow, randCol);
copyGrid(duplicateGrid);
duplicateGrid[randRow * 9 + randCol] = 0;
Ignore numToIgnore = { removingNumber, randRow, randCol };
SS.setGrid(duplicateGrid, numToIgnore);
// 3. If the solver does not find a solution, then remove number
if (!SS.solve())
{
setElement(randRow, randCol, 0);
removed[randRow * 9 + randCol] = true;
toRemove--;
}
}
// 4. Repeat, until enough numbers removed
}
}