-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
73 lines (68 loc) · 2.01 KB
/
index.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
'use strict';
function SudokuSolver() {
var puzzle_table = [];
/*
* Check if the number is a legal candidate
* for the given cell (by Sudoku rules).
*/
function check_candidate(num, row, col) {
for (var i = 0; i < 9; i++) {
var b_index = ((Math.floor(row / 3) * 3) + Math.floor(i / 3)) * 9 + (Math.floor(col / 3) * 3) + (i % 3);
if (num == puzzle_table[(row * 9) + i] ||
num == puzzle_table[col + (i * 9)] ||
num == puzzle_table[b_index]) {
return false;
}
}
return true;
}
/*
* Recursively test all possible numbers for a given cell until
* the puzzle is solved.
*/
function get_candidate(index) {
if (index >= puzzle_table.length) {
return true;
} else if (puzzle_table[index] != 0) {
return get_candidate(index + 1);
}
for (var i = 1; i <= 9; i++) {
if (check_candidate(i, Math.floor(index / 9), index % 9)) {
puzzle_table[index] = i;
if (get_candidate(index + 1)) {
return true;
}
}
}
puzzle_table[index] = 0;
return false;
}
/*
* Split result of puzzle into chunks by 9.
*/
function chunk_in_groups(arr) {
var result = [];
for (var i = 0; i < arr.length; i += 9) {
result.push(arr.slice(i, i + 9));
}
return result;
}
/*
* Start solving the game for provided puzzle and options.
*/
this.solve = function (puzzle, options) {
options = options || {};
var result = options.result || 'string';
puzzle_table = puzzle.split('').map(function (v) { return isNaN(v) ? 0 : +v });
if (puzzle.length !== 81) return 'Puzzle is not valid.'
return !get_candidate(0) ? 'No solution found.' : result === 'chunks' ? chunk_in_groups(puzzle_table) : result === 'array' ? puzzle_table : puzzle_table.join('');
}
}
if (typeof exports !== 'undefined') {
if (typeof module !== 'undefined' && module.exports) {
exports = module.exports = SudokuSolver;
}
exports.SudokuSolver = SudokuSolver;
} else {
window.SudokuSolver = SudokuSolver;
}