forked from PKRoma/LinqSudokuSolver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board.cs
206 lines (192 loc) · 6.22 KB
/
Board.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
using System.Collections.Generic;
namespace SilverlightSudokuHelper
{
internal class Board
{
// Board is 9x9
public const int Size = 9;
// 9x9 array of digits
private Digit[,] _digits;
public Board()
{
// Create blank board
Initialize(null);
}
public Board(Board basis)
{
// Copy specified board
Initialize(basis);
}
private void Initialize(Board basis)
{
// Initialize each digit
_digits = new Digit[Size, Size];
for (var y = 0; y < Size; y++)
{
for (var x = 0; x < Size; x++)
{
_digits[x, y] = new Digit();
}
}
if (null != basis)
{
// Copy the digits of the specified board
for (var y = 0; y < Size; y++)
{
for (var x = 0; x < Size; x++)
{
var digit = basis.GetDigit(x, y);
if (digit.ValueKnown)
{
SetValue(x, y, digit.KnownValue, digit.DigitKind);
}
}
}
}
}
private IEnumerable<string> GetDigitList()
{
foreach (var digit in _digits)
{
yield return digit.ValueKnown ? digit.KnownValue.ToString() : ".";
}
}
public void Solve()
{
var digitList=string.Join("", new List<string>(GetDigitList()).ToArray());
var grid=LinqSudokuSolver.parse_grid(digitList);
var solution=LinqSudokuSolver.search(grid);
var rowIdentifiers="ABCDEFGHI";
for(var row=0; row<Size; row++) {
for(var col=0; col<Size; col++) {
var key = rowIdentifiers[row] + (col+1).ToString();
var solvedValue=int.Parse(solution[key]);
_digits[row, col].ClearValue();
_digits[row, col].SetValue(solvedValue, Digit.Kind.Given);
}
}
}
public Digit GetDigit(int x, int y)
{
// Return the specified digit
return _digits[x, y];
}
public void SetValue(int x, int y, int value, Digit.Kind kind)
{
// Save the current digits in case something goes wrong
var savedDigits = DuplicateDigits();
try
{
try
{
// Attempt to set the specified value
_digits[x, y].SetValue(value, kind);
}
catch (InvalidDigitException)
{
// Digit was invalid, so board would have become invalid
throw new InvalidBoardException(x, y);
}
// Exclude the value from the rest of the row and column
for (var i = 0; i < Size; i++)
{
if (i != x)
{
Exclude(i, y, value);
}
if (i != y)
{
Exclude(x, i, value);
}
}
// Exclude the value from the rest of the block
for (var j = (y / 3) * 3; j < ((y / 3) * 3) + 3; j++)
{
for (var i = (x / 3) * 3; i < ((x / 3) * 3) + 3; i++)
{
if ((i != x) || (j != y))
{
Exclude(i, j, value);
}
}
}
}
catch (InvalidBoardException)
{
// Recover from invalid board and re-throw
_digits = savedDigits;
throw;
}
}
public void ClearValue(int x, int y)
{
// Clear the specified value
_digits[x, y].ClearValue();
// Create a new board to apply all the exclusions properly (vs. attempting to undo them)
var board = new Board(this);
// Take over the new board's digits
_digits = board._digits;
}
public bool Complete
{
get
{
// true iff all values are known
var complete = true;
foreach (Digit digit in _digits)
{
if (!digit.ValueKnown)
{
complete = false;
break;
}
}
return complete;
}
}
private void Exclude(int x, int y, int value)
{
try
{
// Exclude the value from the specified location
_digits[x, y].Exclude(value);
}
catch (InvalidDigitException)
{
// Digit was invalid, so board would have become invalid
throw new InvalidBoardException(x, y);
}
}
private Digit[,] DuplicateDigits()
{
// Return a copy of the _digits array
var digits = new Digit[Size, Size];
for (var y = 0; y < Size; y++)
{
for (var x = 0; x < Size; x++)
{
digits[x, y] = new Digit(_digits[x, y]);
}
}
return digits;
}
public static Board FromString(string basis)
{
// Create a new board from an 81-character string that specifies the board's values left-to-right, top-to-bottom
var board = new Board();
int i = 0;
for (var y = 0; y < Size; y++)
{
for (var x = 0; x < Size; x++)
{
if (' ' != basis[i])
{
board.SetValue(x, y, basis[i] - '0', Digit.Kind.Given);
}
i++;
}
}
return board;
}
}
}