-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerator.cpp
More file actions
65 lines (61 loc) · 1.54 KB
/
Copy pathgenerator.cpp
File metadata and controls
65 lines (61 loc) · 1.54 KB
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
/*
Sudoku Generator
Written by David Wiebe
*/
#include <stdexcept>
#include <random>
#include "generator.h"
#include "sudoku.h"
Sudoku *generateSudoku(int size, int seed)
{
srand(seed);
const int numberOfCells = size * size * size * size;
const int groupSize = size * size;
int initialValues[numberOfCells];
for (int i = 0; i < numberOfCells; i++)
{
initialValues[i] = 0;
}
for (int i = 0; i < groupSize; i++)
{
initialValues[i] = i + 1;
}
for (int i = 0; i < groupSize; i++)
{
initialValues[i] = i + 1;
}
for (int i = 0; i < groupSize; i++)
{
for (int j = i + 1; j < groupSize; j++)
{
bool swap = (rand() % 2) == 0;
if (swap)
{
int temp = initialValues[i];
initialValues[i] = initialValues[j];
initialValues[j] = temp;
}
}
}
Sudoku *initialSudoku = new Sudoku(size, initialValues);
Sudoku *solution = initialSudoku->Solution();
delete initialSudoku;
int index = 0;
int x = 0;
int y = 0;
int replacedValue = 0;
while (solution->NumberOfSolutions() == 1)
{
index = rand() % numberOfCells;
x = index % groupSize;
y = index / groupSize;
if (solution->IsCellGiven(x, y))
{
continue;
}
replacedValue = solution->GetCellValue(x, y);
solution->SetCellValue(x, y, UNKNOWN_VALUE);
}
solution->SetCellValue(x, y, replacedValue);
return solution;
}