This repository has been archived by the owner on Mar 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
xSolver.cs
executable file
·437 lines (386 loc) · 16.1 KB
/
xSolver.cs
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
using System;
using System.Collections.Generic;
using System.Text;
namespace Numbrella
{
public class xSolver
{
public xBoard b;
public delegate void solverEvent(object Sender, EventArgs e);
public delegate void bruteForceEvent(object Sender, BruteForceEventArgs e);
public event solverEvent solutionComplete;
public event solverEvent puttingObviousNumbers;
public event bruteForceEvent bruteForceLeveled;
private int bruteForceLevel;
public xSolver(xBoard B)
{
b = B;
}
public void solve()
{
fillObviousNumbers(ref b, true);
bruteForce(ref b);
solutionComplete(this, new EventArgs());
}
private void fillObviousNumbers(ref xBoard Board, bool RaiseEvent)
{
bool cont = true;
if (RaiseEvent) puttingObviousNumbers(this, new EventArgs());
if (!validate(ref Board)) return;
while (cont)
{
xBoard former = new xBoard();
for (int x = 0; x < 9; x++) for (int y = 0; y < 9; y++) former.cells[x, y].value = Board.cells[x, y].value;
fill8Boxes(ref Board);
fill8Columns(ref Board);
fill8Rows(ref Board);
plusScan(ref Board);
cont = false;
for (int x = 0; x < 9; x++) for (int y = 0; y < 9; y++) if (former.cells[x, y].value != Board.cells[x, y].value) cont = true;
}
}
/// <summary>
/// Recursive bir þekilde araya sayýlar atmayý dener
/// </summary>
public void bruteForce(ref xBoard Board)
{
xBoard lBoard = new xBoard();
xBoard lBoardBefore = new xBoard();
Board.copyTo(ref lBoard);
bruteForceLevel++;
bruteForceLeveled(this, new BruteForceEventArgs(bruteForceLevel, lBoard));
fillObviousNumbers(ref lBoard, false);
for (byte x = 0; x < 9; x++) for (byte y = 0; y < 9; y++)
{
if (lBoard.cells[x, y].value == 0)
{
for (byte a = 1; a <= 9; a++)
{
lBoardBefore = new xBoard();
lBoard.copyTo(ref lBoardBefore);
lBoard.cells[x, y].value = a;
lBoard.cells[x, y].source = xCell.SOURCE.BRUTE;
if (validate(ref lBoard))
{
lBoard.cells[x, y].source = xCell.SOURCE.COMPUTER;
bruteForce(ref lBoard);
}
else
{
lBoardBefore.copyTo(ref lBoard);
}
}
}
}
if (validate(ref lBoard))
{
fillObviousNumbers(ref lBoard, false);
lBoard.copyTo(ref Board);
}
bruteForceLevel--;
}
/// <summary>
/// Kutulardaki eksik sayýlarý tespit eder ve yukarýdan aþaðýya, saðdan sola bakýp dolabilecek sayýlarý doldurur
/// </summary>
public void plusScan(ref xBoard Board)
{
byte[] missing;
string[,] curBox;
string[,] curBoxInitial;
// Her bir kutu için...
for (byte bx = 0; bx < 3; bx++)
{
for (byte by = 0; by < 3; by++)
{
// Initialize
missing = new byte[10];
for (byte n = 1; n <= 9; n++) missing[n] = n;
curBox = new string[3, 3];
curBoxInitial = new string[3, 3];
for (byte x = 0; x < 3; x++) for (byte y = 0; y < 3; y++) if (Board.cells[(bx * 3) + x, (by * 3) + y].value != 0) curBoxInitial[x, y] = "."; else curBoxInitial[x, y] = "";
// Kutudaki eksik sayýlarý tespit edelim
for (byte x = 0; x < 3; x++) for (byte y = 0; y < 3; y++)
{
byte no = Board.cells[(bx * 3) + x, (by * 3) + y].value;
if (no != 0) missing[no] = 0;
}
// Her bir eksik sayý için...
for (byte n = 1; n <= 9; n++)
{
byte curno = missing[n];
for (byte x = 0; x < 3; x++) for (byte y = 0; y < 3; y++) curBox[x, y] = curBoxInitial[x, y];
if (curno != 0)
{
// Sütunu sabit tutup, sol ve saðýndaki bütün satýrlarý tarayalým
for (byte bx2 = 0; bx2 < 3; bx2++)
{
if (bx2 != bx)
{
for (byte x = 0; x < 3; x++) for (byte y = 0; y < 3; y++)
{
if (Board.cells[(bx2 * 3) + x, (by * 3) + y].value == curno)
{
for (byte a = 0; a < 3; a++) curBox[a, y] = ".";
}
}
}
}
// Satýrý sabit tutup, üst ve altýndaki bütün satýrlarý tarayalým
for (byte by2 = 0; by2 < 3; by2++)
{
if (by2 != by)
{
for (byte x = 0; x < 3; x++) for (byte y = 0; y < 3; y++)
{
if (Board.cells[(bx * 3) + x, (by2 * 3) + y].value == curno)
{
for (byte a = 0; a < 3; a++) curBox[x, a] = ".";
}
}
}
}
// Hem yukarýdan aþaðýya, hem soldan saða tarama yapalým
for (byte bx2 = 0; bx2 < 3; bx2++) for (byte by2 = 0; by2 < 3; by2++)
{
if ((bx2 != bx && by2 == by) || (bx2 == bx && by2 != by))
{
for (byte x = 0; x < 3; x++) for (byte y = 0; y < 3; y++)
{
if (Board.cells[(bx2 * 3) + x, (by * 3) + y].value == curno)
{
if (bx2 == bx)
{
for (byte a = 0; a < 3; a++) curBox[x, a] = ".";
}
else
{
for (byte a = 0; a < 3; a++) curBox[a, y] = ".";
}
}
}
}
}
// Eðer geriye tek bir boþluk kaldýysa, söz konusu sayý ancak oraya gelebilir
byte c = 0;
for (byte x = 0; x < 3; x++) for (byte y = 0; y < 3; y++) if (curBox[x, y] == ".") c++;
if (c == 8) for (byte x = 0; x < 3; x++) for (byte y = 0; y < 3; y++) if (curBox[x, y] != ".")
{
Board.cells[(bx * 3) + x, (by * 3) + y].value = curno;
Board.cells[(bx * 3) + x, (by * 3) + y].source = xCell.SOURCE.COMPUTER;
}
}
}
}
}
}
/// <summary>
/// Tahtayý kontrol eder ve hatalýlarý iþaretler
/// </summary>
public bool validate(ref xBoard Board)
{
for (byte x = 0; x < 9; x++) for (byte y = 0; y < 9; y++) Board.cells[x, y].error = false;
bool b1 = validateBoxes(ref Board);
bool b2 = validateRows(ref Board);
bool b3 = validateColumns(ref Board);
return (b1 && b2 && b3);
}
/// <summary>
/// Sütun içeriklerini valide eder
/// </summary>
public bool validateColumns(ref xBoard Board)
{
byte[] no;
bool ret = true;
for (byte y = 0; y < 9; y++)
{
no = new byte[9];
for (byte x = 0; x < 9; x++)
{
byte v = Board.cells[x, y].value;
if (v != 0) no[v - 1]++;
}
for (byte n = 0; n < 9; n++)
{
if (no[n] > 1)
{
ret = false;
for (byte x = 0; x < 9; x++) if (Board.cells[x, y].value == n + 1) Board.cells[x, y].error = true;
}
}
}
return ret;
}
/// <summary>
/// Satýr içeriklerini valide eder
/// </summary>
public bool validateRows(ref xBoard Board)
{
byte[] no;
bool ret = true;
for (byte x = 0; x < 9; x++)
{
no = new byte[9];
for (byte y = 0; y < 9; y++)
{
byte v = Board.cells[x, y].value;
if (v != 0) no[v - 1]++;
}
for (byte n = 0; n < 9; n++)
{
if (no[n] > 1)
{
ret = false;
for (byte y = 0; y < 9; y++) if (Board.cells[x, y].value == n + 1) Board.cells[x, y].error = true;
}
}
}
return ret;
}
/// <summary>
/// Kutu içeriklerini valide eder
/// </summary>
public bool validateBoxes(ref xBoard Board)
{
byte[] no;
bool ret = true;
for (byte bx = 0; bx < 3; bx++) for (byte by = 0; by < 3; by++)
{
no = new byte[9];
for (byte x = 0; x < 3; x++) for (byte y = 0; y < 3; y++)
{
byte v = Board.cells[(bx * 3) + x, (by * 3) + y].value;
if (v != 0) no[v - 1]++;
}
for (byte n = 0; n < 9; n++)
{
if (no[n] > 1)
{
ret = false;
for (byte x = 0; x < 3; x++) for (byte y = 0; y < 3; y++) if (Board.cells[(bx * 3) + x, (by * 3) + y].value == n + 1) Board.cells[(bx * 3) + x, (by * 3) + y].error = true;
}
}
}
return ret;
}
/// <summary>
/// 8 sayý içeren satýrlara 9. sayýyý yazar
/// </summary>
public void fill8Rows(ref xBoard Board)
{
bool[] no;
byte trueCount;
byte falseX, falseY;
for (byte y = 0; y < 9; y++)
{
no = new bool[9];
trueCount = falseX = falseY = 0;
for (byte x = 0; x < 9; x++)
{
byte v = Board.cells[x, y].value;
if (v != 0)
{
no[v - 1] = true;
trueCount++;
}
else
{
falseX = x;
falseY = y;
}
}
if (trueCount == 8)
{
for (byte n = 0; n < 9; n++)
{
if (!no[n])
{
Board.cells[falseX, falseY].value = Convert.ToByte(n + 1);
Board.cells[falseX, falseY].source = xCell.SOURCE.COMPUTER;
break;
}
}
}
}
}
/// <summary>
/// 8 sayý içeren sütunlara 9. sayýyý yazar
/// </summary>
public void fill8Columns(ref xBoard Board)
{
bool[] no;
byte trueCount;
byte falseX, falseY;
for (byte x = 0; x < 9; x++)
{
no = new bool[9];
trueCount = falseX = falseY = 0;
for (byte y = 0; y < 9; y++)
{
byte v = Board.cells[x, y].value;
if (v != 0)
{
no[v - 1] = true;
trueCount++;
}
else
{
falseX = x;
falseY = y;
}
}
if (trueCount == 8)
{
for (byte n = 0; n < 9; n++)
{
if (!no[n])
{
Board.cells[falseX, falseY].value = Convert.ToByte(n + 1);
Board.cells[falseX, falseY].source = xCell.SOURCE.COMPUTER;
break;
}
}
}
}
}
/// <summary>
/// 8 sayý içeren kutulara 9. sayýyý yazar
/// </summary>
public void fill8Boxes(ref xBoard Board)
{
bool[] no;
byte trueCount;
byte falseX, falseY;
for (byte bx = 0; bx < 3; bx++) for (byte by = 0; by < 3; by++)
{
no = new bool[9];
trueCount = falseX = falseY = 0;
for (byte x = 0; x < 3; x++) for (byte y = 0; y < 3; y++)
{
byte v = Board.cells[(bx * 3) + x, (by * 3) + y].value;
if (v != 0)
{
no[v - 1] = true;
trueCount++;
}
else
{
falseX = x;
falseY = y;
}
}
if (trueCount == 8)
{
for (byte n = 0; n < 9; n++)
{
if (!no[n])
{
Board.cells[(bx * 3) + falseX, (by * 3) + falseY].value = Convert.ToByte(n + 1);
Board.cells[(bx * 3) + falseX, (by * 3) + falseY].source = xCell.SOURCE.COMPUTER;
break;
}
}
}
}
}
}
}