-
Notifications
You must be signed in to change notification settings - Fork 0
/
sudoku.js
256 lines (220 loc) · 7.69 KB
/
sudoku.js
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
const SOLVER_SOLVED = 'solved',
SOLVER_VIA_ELIMINATION = 'eliminations',
SOLVER_SPECULATION_STARTED = 'speculation started...',
SOLVER_SPECULATION_FAILED = 'speculation failed...',
SOLVER_IMPOSSIBLE = 'sudoku impossible'
class Cell {
constructor(sudoku, $elm, x, y, digit = 0, possibilities = null, fixed = false, marked = false) {
this.digit = digit
this.x = x
this.y = y
this.id = sudoku.size * x + y
this.sudoku = sudoku
this.fixed = fixed
this.marked = marked
if (possibilities != null) {
this.possibilities = possibilities
} else {
this.possibilities = new Set()
for (let d = 1; d <= sudoku.size; d++)
this.possibilities.add(d)
}
this.$elm = $elm
}
updateUI() {
this.$elm.innerText = this.digit == 0 ? '' : this.digit.toString()
this.$elm.className = this.fixed ? 'fixed' : (this.marked ? 'marked' : '')
}
copy() {
return new Cell(this.sudoku, this.$elm, this.x, this.y, this.digit, new Set(this.possibilities.values()), this.fixed, this.marked)
}
}
class Sudoku {
constructor($table, size = 9) {
this.size = size
this.grid = []
this.$table = $table
this.speculations = []
this.digits = []
for (let d = 1; d <= this.size; d++)
this.digits.push(d)
for (let x = 0; x < this.size; x++) {
let row = []
let $row = document.createElement('div')
$row.className = 'row'
for (let y = 0; y < this.size; y++) {
let $cell = document.createElement('div')
$cell.dataset.x = x
$cell.dataset.y = y
row.push(new Cell(this, $cell, x, y))
$row.appendChild($cell)
}
this.grid.push(row)
this.$table.appendChild($row)
}
this.units = []
for (let x = 0; x < this.size; x++) {
let row = [], col = []
for (let y = 0; y < this.size; y++) {
row.push({ x: x, y: y })
col.push({ x: y, y: x })
}
this.units.push(row)
this.units.push(col)
}
let subsquares = Math.floor(Math.sqrt(this.size))
console.assert(subsquares * subsquares == this.size)
for (let i = 0; i < subsquares; i++) {
for (let j = 0; j < subsquares; j++) {
let subsquare = []
for (let di = 0; di < subsquares; di++) {
for (let dj = 0; dj < subsquares; dj++) {
let x = i * subsquares + di, y = j * subsquares + dj
subsquare.push({ x: x, y: y })
}
}
this.units.push(subsquare)
}
}
this.cell2units = new Array(this.size*this.size).fill(0).map(_ => [])
for (let unit of this.units) {
for (let pos of unit) {
let id = this.size * pos.x + pos.y
this.cell2units[id].push(unit)
}
}
}
init(game) {
console.assert(game.length == this.size)
for (let i = 0; i < game.length; i++) {
console.assert(game[i].length == this.size)
for (let j = 0; j < game[i].length; j++) {
let cell = this.grid[i][j]
cell.digit = game[i][j]
if (cell.digit != 0)
cell.fixed = true
}
}
this.removePossibilities()
this.updateUI()
}
removePossibilities() {
let changed = false
for (let unit of this.units) {
for (let pos of unit) {
let cell = this.grid[pos.x][pos.y]
if (cell.digit == 0)
continue
for (let peer of unit)
if (peer != pos)
changed |= this.grid[peer.x][peer.y].possibilities.delete(cell.digit)
}
}
return changed
}
placeDigit(cell, digit) {
cell.digit = digit
let changed = false
for (let unit of this.cell2units[cell.id]) {
for (let peer of unit) {
if (!(peer.x == cell.x && peer.y == cell.y)) {
changed ||= this.grid[peer.x][peer.y].possibilities.delete(digit)
}
}
}
return changed
}
placeDigits() {
let changed = false
for (let row of this.grid) {
for (let cell of row) {
if (cell.digit == 0 && cell.possibilities.size == 1) {
let digit = cell.possibilities.values().next().value
cell.digit = digit
changed = true
// this.placeDigit(cell, digit)
} else if (cell.possibilities.size == 0) {
return { changed, failed: true }
}
}
}
return { changed, failed: false }
}
updateUI() {
for (let row of this.grid) {
for (let cell of row) {
cell.updateUI()
}
}
}
check() {
let solved = true
for (let row of this.grid) {
for (let cell of row) {
if (cell.digit == 0)
solved = false
}
}
let digits = new Array(this.size)
for (let unit of this.units) {
digits.fill(0)
for (let pos of unit) {
let cell = this.grid[pos.x][pos.y]
if (cell.digit != 0) {
digits[cell.digit] += 1
if (digits[cell.digit] == 2) {
return { solved, failed: true }
}
}
}
}
return { solved, failed: false }
}
solve() {
this.removePossibilities()
let { changed, failed } = this.placeDigits()
this.updateUI()
if (changed && !failed)
return SOLVER_VIA_ELIMINATION
let status
if (failed || (status = this.check()).failed) {
let speculation = this.speculations.pop()
if (speculation == null)
return SOLVER_IMPOSSIBLE
speculation.try += 1
this.grid = speculation.originalGrid.map(row => row.map(cell => cell.copy()))
let cell0 = this.grid[speculation.x][speculation.y]
cell0.digit = speculation.possibilities[speculation.try]
cell0.possibilities = new Set([cell0.digit])
if (speculation.try + 1 < speculation.possibilities.length)
this.speculations.push(speculation)
this.updateUI()
return SOLVER_SPECULATION_FAILED
}
if (status.solved)
return SOLVER_SOLVED
// Find cell with the lowest amount of possible values...
let cell0 = null
for (let row of this.grid) {
for (let cell of row) {
if (cell.digit == 0 && (cell0 == null || cell.possibilities.size < cell0.possibilities.size)) {
cell0 = cell
}
}
}
// Start a new speculation...
let speculation = {
x: cell0.x,
y: cell0.y,
possibilities: [...cell0.possibilities.values()],
try: 0,
originalGrid: this.grid.map(row => row.map(cell => cell.copy()))
}
this.speculations.push(speculation)
cell0.digit = speculation.possibilities[0]
cell0.possibilities = new Set([cell0.digit])
cell0.marked = true
cell0.updateUI()
return SOLVER_SPECULATION_STARTED
}
}