-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sudoku.cpp
318 lines (292 loc) · 10.7 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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Sudoku.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
bool a = false;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::fillGrid(){
std::vector<std::vector<int>> tmp(9, std::vector<int>(9));
std::vector<std::vector<int>> tmpCorr(9, std::vector<int>(9));
srand(time(NULL));
Sudoku *sudoku = new Sudoku();
//int t = 0;
//do {
sudoku->createSeed();
sudoku->genPuzzle();
sudoku->calculateDifficulty();
//t++;
//} while (t < 400);
//ShowMessage(IntToStr(sudoku->getDifficultyLevel()));
tmp = sudoku->getGrid();
tmpCorr = sudoku->getSolnGrid();
Cell *tmpCell;
std::vector<Cell> tmpV;
for (int i = 0; i < 9; i++) {
tmpV = std::vector<Cell>();
gridCells.push_back(tmpV);
for (int j = 0; j < 9; j++) {
tmpCell = new Cell(tmp[i][j], tmpCorr[i][j]);
if (tmpCell->value == 0) {
tmpCell->isRedactable = true;
}
gridCells[i].push_back(*tmpCell);
}
}
}
void __fastcall TForm1::FormCreate(TObject *Sender)
{
//init();
StringGrid1->DefaultDrawing = false;
StringGrid1->DoubleBuffered = true;
StringGrid1->GridLineWidth = 0;
//(TColor) RGB(46, 46, 46);
fillGrid();
fillFromCells();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::StringGrid1DrawCell(TObject *Sender, int ACol, int ARow, TRect &Rect,
TGridDrawState State)
{
if (a) {
ShowMessage("Ôóíêöèÿ âêëþ÷åíà/âûêëþ÷åíà");
a = false;
}
validateGrid();
TCanvas *a = this->StringGrid1->Canvas;
a->Pen->Width = 1;
a->Brush->Color = clWhite;
// íà÷àëüíûå
if (!gridCells[ARow][ACol].isRedactable)
a->Brush->Color = (TColor) RGB(214, 214, 214);
// ïîäñâåòêà
if (gridCells[ARow][ACol].isHighlited)
a->Brush->Color = (TColor) RGB(197, 227, 244);
// äóáëèêàòû â ñòðîêàõ/ñòîáöàõ/çîíàõ
if (CheckBox2->State == cbChecked)
if (gridCells[ARow][ACol].isDupe)
a->Brush->Color = (TColor) RGB(255, 156, 156);
// ñâåðêà ñ ðåøåíèåì
if (CheckBox3->State == cbChecked)
if (!gridCells[ARow][ACol].isCorrect() && gridCells[ARow][ACol].value != 0)
a->Brush->Color = (TColor) RGB(255, 156, 156);
// âûäåëåíèå êëåòêè
if (State.Contains(gdSelected))
a->Brush->Color = (TColor) RGB(154, 206, 235);
a->FillRect(Rect);
TRect r = Rect;
r.left += 2;
r.top += 2;
a->Pen->Color = (TColor) RGB(105, 105, 105); //HEX for this color is #696969
//NICE
a->Rectangle(Rect.left,Rect.top,Rect.right,Rect.bottom);
a->Font->Size = 16;
DrawText(a->Handle, this->StringGrid1->Cells[ACol][ARow].t_str(), -1, (TRect*)&r, DT_CENTER);
a->Pen->Width = 4;
a->Pen->Color = clBlack;
a->MoveTo(150, 0);
a->LineTo(150, 455);
a->MoveTo(300, 0);
a->LineTo(300, 455);
a->MoveTo(0, 150);
a->LineTo(455, 150);
a->MoveTo(0, 300);
a->LineTo(455, 300);
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
void __fastcall TForm1::StringGrid1SetEditText(TObject *Sender, int ACol, int ARow,
const UnicodeString Value)
{
// if (!gridCells[ARow][ACol].isRedactable) {
// //ShowMessage("nope");
// StringGrid1->Cells[ACol][ARow] = IntToStr(gridCells[ARow][ACol].value);
// return;
// }
if (Value != "") {
const auto r = std::regex(R"([1-9]{1})");
AnsiString tmp = Value;
StringGrid1->Cells[ACol][ARow] = std::regex_match(tmp.c_str(), r) ? Value : "";
gridCells[ARow][ACol].value = std::regex_match(tmp.c_str(), r) ? StrToInt(Value) : 0;
//ShowMessage(StringGrid1->Cells[ACol][ARow]);
//StringGrid1->Cells[0][0] = "hi";
} else {
gridCells[ARow][ACol].value = 0;
}
}
void __fastcall TForm1::validateGrid() {
//ShowMessage(StringGrid1->Cells[ACol][ARow]);
//int value = StrToInt(StringGrid1->Cells[ACol][ARow]);
// for (int i = 0; i < 8; i++) {
// for (int j = 0; j < 8; j++) {
// Cell currentCell = gridCells[i][j];
// int value = currentCell.value;
//
// }
// }
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
gridCells[i][j].isDupe = false;
}
}
for (int col_num = 0; col_num < 9; col_num++) {
bool nums[10]={false};
bool toDupe[10]={false};
for (int cell_num=0; cell_num<9; ++cell_num) {
int curr_num = gridCells[cell_num][col_num].value;
if(curr_num!=0 && nums[curr_num]==true) {
toDupe[curr_num] = true;
//gridCells[((int)(block_num/3))*3 + (cell_num/3)][((int)(block_num%3))*3 + (cell_num%3)].isDupe = true;
//grid_status=false;
//return;
}
nums[curr_num] = true;
}
for (int cell_num=0; cell_num<9; ++cell_num) {
int curr_num = gridCells[cell_num][col_num].value;
if (toDupe[curr_num]) {
gridCells[cell_num][col_num].isDupe = true;
}
}
}
for (int row_num = 0; row_num < 9; row_num++) {
bool nums[10]={false};
bool toDupe[10]={false};
for (int cell_num=0; cell_num<9; ++cell_num) {
int curr_num = gridCells[row_num][cell_num].value;
if(curr_num!=0 && nums[curr_num]==true) {
toDupe[curr_num] = true;
//gridCells[((int)(block_num/3))*3 + (cell_num/3)][((int)(block_num%3))*3 + (cell_num%3)].isDupe = true;
//grid_status=false;
//return;
}
nums[curr_num] = true;
}
for (int cell_num=0; cell_num<9; ++cell_num) {
int curr_num = gridCells[row_num][cell_num].value;
if (toDupe[curr_num]) {
gridCells[row_num][cell_num].isDupe = true;
}
}
}
for (int block_num=0; block_num<9; ++block_num) {
bool nums[10]={false};
bool toDupe[10]={false};
for (int cell_num=0; cell_num<9; ++cell_num) {
int curr_num = gridCells[((int)(block_num/3))*3 + (cell_num/3)][((int)(block_num%3))*3 + (cell_num%3)].value;
if(curr_num!=0 && nums[curr_num]==true) {
toDupe[curr_num] = true;
//gridCells[((int)(block_num/3))*3 + (cell_num/3)][((int)(block_num%3))*3 + (cell_num%3)].isDupe = true;
//grid_status=false;
//return;
}
nums[curr_num] = true;
}
for (int cell_num=0; cell_num<9; ++cell_num) {
int curr_num = gridCells[((int)(block_num/3))*3 + (cell_num/3)][((int)(block_num%3))*3 + (cell_num%3)].value;
if (toDupe[curr_num]) {
gridCells[((int)(block_num/3))*3 + (cell_num/3)][((int)(block_num%3))*3 + (cell_num%3)].isDupe = true;
}
}
}
// for (int i = 0; i < 9 && !gridCells[ARow][ACol].isDupe; i++) {
//
// }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::StringGrid1SelectCell(TObject *Sender, int ACol, int ARow,
bool &CanSelect)
{
//StringGrid1SetEditText(Sender, ACol, ARow, StringGrid1->Cells[ACol][ARow]);
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
gridCells[i][j].isHighlited = false;
}
}
if (CheckBox1->State == cbChecked && gridCells[ARow][ACol].value != 0) {
highlightGrid(gridCells[ARow][ACol].value);
}
CanSelect = gridCells[ARow][ACol].isRedactable;
}
void __fastcall TForm1::highlightGrid(int val) {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
gridCells[i][j].isHighlited = gridCells[i][j].value == val;
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::N2Click(TObject *Sender)
{
if (SaveDialog1->Execute()) {
AnsiString ans = SaveDialog1->FileName;
saveGameToFile(gridCells, ans.c_str());
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::N3Click(TObject *Sender)
{
if (OpenDialog1->Execute()) {
AnsiString ans = OpenDialog1->FileName;
readSavedGame(gridCells, ans.c_str());
fillFromCells();
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::fillFromCells() {
for (int i = 0; i < 9; i++){
for (int j = 0; j < 9; j++){
StringGrid1->Cells[j][i] = gridCells[i][j].value == 0 ? "" : IntToStr(gridCells[i][j].value);
}
}
}
void __fastcall TForm1::N4Click(TObject *Sender)
{
MessageDlg("Èãðîâîå ïîëå ïðåäñòàâëÿåò ñîáîé êâàäðàò ðàçìåðîì 9x9, ðàçäåë¸ííûé íà ìåíüøèå êâàäðàòû ñî ñòîðîíîé â 3 êëåòêè.\n íèõ óæå â íà÷àëå èãðû ñòîÿò íåêîòîðûå ÷èñëà (îò 1 äî 9).\nÎò èãðîêà òðåáóåòñÿ çàïîëíèòü ñâîáîäíûå êëåòêè öèôðàìè îò 1 äî 9 òàê, ÷òîáû â êàæäîé ñòðîêå, â êàæäîì ñòîëáöå è â êàæäîì ìàëîì êâàäðàòå 3x3 êàæäàÿ öèôðà âñòðå÷àëàñü áû òîëüêî îäèí ðàç.", mtInformation, TMsgDlgButtons() << mbOK, 0);
}
//---------------------------------------------------------------------------
//void __fastcall TForm1::Button2Click(TObject *Sender)
//{
// isToBeHighlighted = !isToBeHighlighted;
// Button2->Caption = isToBeHighlighted ? "Âûêëþ÷èòü ïîäñâåòêó" : "Âêëþ÷èòü ïîäñâåòêó";
//}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender) {
bool ans = true;
for (int i = 0; ans && i < 9; i++) {
for (int j = 0; ans && j < 9; j++) {
ans &= gridCells[i][j].isCorrect();
}
}
if (ans) {
ShowMessage("Ïîçäðàâëÿåì! Âû ïîáåäèëè!");
} else {
ShowMessage("×òî-òî íå ñõîäèòñÿ(");
}
}
void __fastcall TForm1::CheckBox3Click(TObject *Sender)
{
StringGrid1->SetFocus();
a = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::CheckBox1Click(TObject *Sender)
{
StringGrid1->SetFocus();
a = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::CheckBox2Click(TObject *Sender)
{
StringGrid1->SetFocus();
a = true;
}
//---------------------------------------------------------------------------