-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsudoku.cpp
234 lines (207 loc) · 5.53 KB
/
sudoku.cpp
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
#include "sudoku.h"
#include <Arduino.h>
using namespace std;
Sudoku::Sudoku(difficulty level) { this->level = level; }
void Sudoku::newGame(difficulty level) {
this->level = level;
time = 0;
// initialize the grid
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
this->grid[i][i] = 0;
}
}
initiate_grid();
getRandomNumberList();
getRandomSpaceList();
solver();
// print_grid();
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
solution[i][j] = this->grid[i][j];
}
}
generator();
}
void Sudoku::initiate_grid() {
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
grid[i][j] = 0;
}
bool Sudoku::isExist(int row, int colum, int num) {
for (int i = 0; i < 9; i++) {
if (this->grid[row][i] == num) {
return true;
}
}
for (int i = 0; i < 9; i++) {
if (this->grid[i][colum] == num) {
return true;
}
}
int square_row = row / 3;
int square_colum = colum / 3;
for (int i = square_row * 3; i < (square_row + 1) * 3; i++) {
for (int j = square_colum * 3; j < (square_colum + 1) * 3; j++) {
if (this->grid[i][j] == num) {
return true;
}
}
}
return false;
}
void Sudoku::getRandomNumberList() {
for (int i = 0; i < 9; i++) {
NumberList[i] = i + 1;
}
for (int i = 8; i >= 0; i--) {
int randomIndex = random(1, i + 1); // 1 to 9
int tmp = NumberList[randomIndex];
NumberList[randomIndex] = NumberList[i];
NumberList[i] = tmp;
}
}
void Sudoku::getRandomSpaceList() {
for (int i = 0; i < 81; i++) {
SpaceList[i] = i;
}
for (int i = 80; i >= 0; i--) {
int randomIndex = random(1, i + 1);
int tmp = SpaceList[randomIndex];
SpaceList[randomIndex] = SpaceList[i];
SpaceList[i] = tmp;
}
}
bool Sudoku::findEmpty(int &row, int &colum) {
for (row = 0; row < 9; row++) {
for (colum = 0; colum < 9; colum++) {
if (this->grid[row][colum] == 0) {
return true;
}
}
}
return false;
}
bool Sudoku::solver() {
// to generate a sudoku game
this->mode = 1;
isFound = 0;
dfs_find_solution();
isFound = 0;
}
void Sudoku::dfs_find_solution() {
int row, colum;
// mode 1 generate a game
if (this->mode == 1) {
if (isFound)
return;
if (!findEmpty(row, colum)) {
isFound = 1;
return; // a solution is found
}
}
// mode 2 generate puzzles
if (this->mode == 2) {
if (puzzleNum > 1)
return;
if (!findEmpty(row, colum)) {
puzzleNum++;
return;
}
}
for (int i = 0; i < 9; i++) {
if (!isExist(row, colum, NumberList[i])) {
this->grid[row][colum] = NumberList[i];
dfs_find_solution();
if (isFound)
return;
this->grid[row][colum] = 0;
}
}
}
void Sudoku::getTips(int &grid_y, int &grid_x, int &num) {
int randNumber = random(1, this->leftNum + 1);
int count = 0;
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++) {
if (this->grid[i][j] == 0) {
count++;
if (count == randNumber) {
fillGrid(i, j, this->solution[i][j]);
grid_y = i;
grid_x = j;
num = solution[i][j];
return;
}
}
}
}
void Sudoku::generator() {
int preservedNum;
if (this->level == TOUGH)
preservedNum = 30;
if (this->level == NORMAL)
preservedNum = 35;
if (this->level == EASY)
preservedNum = 40;
this->leftNum = 81 - preservedNum;
int count = 81;
for (int i = 0; i < 81 && count > preservedNum; i++) {
int x = (this->SpaceList[i]) / 9;
int y = (this->SpaceList[i]) % 9;
int temp = this->grid[x][y];
this->grid[x][y] = 0;
this->mode = 2;
this->puzzleNum = 0;
dfs_find_solution();
if (this->puzzleNum == 1) {
count--;
} else {
this->grid[x][y] = temp;
}
}
}
int Sudoku::getValue(int x, int y) { return this->grid[x][y]; }
int Sudoku::getSolution(int x, int y) { return this->solution[x][y]; }
bool Sudoku::isModifiable(int x, int y) {
if (this->grid[x][y] == 0) {
return true;
}
return false;
}
void Sudoku::fillGrid(int x, int y, int num) { this->grid[x][y] = num; }
void Sudoku::setResumeGrid(int x, int y, int num) { this->grid[x][y] = num; }
void Sudoku::setResumeSolution(int x, int y, int num) {
this->solution[x][y] = num;
}
void Sudoku::setResumeLevel(int x) {
if (x == 0) {
level = EASY;
} else if (x == 1) {
level = NORMAL;
} else {
level = TOUGH;
}
}
bool Sudoku::countResult() {
this->leftNum--;
if (this->leftNum == 0)
return true;
else
return false;
}
void Sudoku::print_grid() {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
Serial.print(this->grid[i][j]);
Serial.print(" ");
}
Serial.println();
}
// system("pause");
return;
}
void Sudoku::setTime(unsigned long time) { this->time = time; }
unsigned long Sudoku::getTime() { return time; }
int Sudoku::getLevel() { return level; }
void Sudoku::setLevel(int l) { level = l; }