forked from RubinderS/Sudoku-Solver-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSudoku.js
150 lines (131 loc) · 5.12 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
/**
*
* ██████╗ ██╗ ██╗██████╗ ██╗███╗ ██╗██████╗ ███████╗██████╗
* ██╔══██╗██║ ██║██╔══██╗██║████╗ ██║██╔══██╗██╔════╝██╔══██╗
* ██████╔╝██║ ██║██████╔╝██║██╔██╗ ██║██║ ██║█████╗ ██████╔╝
* ██╔══██╗██║ ██║██╔══██╗██║██║╚██╗██║██║ ██║██╔══╝ ██╔══██╗
* ██║ ██║╚██████╔╝██████╔╝██║██║ ╚████║██████╔╝███████╗██║ ██║
* ╚═╝ ╚═╝ ╚═════╝ ╚═════╝ ╚═╝╚═╝ ╚═══╝╚═════╝ ╚══════╝╚═╝ ╚═╝
*
* written by Rubinder Singh 4-Nov-2018
* copyright ©
*
* @param gameArr
* returns the solved sudoku puzzle
* note: in case there are multiple solutions this function will only return the first solution
*/
function solveSudoku(gameArr) {
var emptySpot = nextEmtySpot(gameArr);
var r = emptySpot[0];
var c = emptySpot[1];
// if the game is unsolvable don't even try to solve it
if (!isValidSudoku(gameArr)) return gameArr;
// if no vacant spot is left, board is solved
if (r === -1) {
return gameArr;
};
var possArr = possiblities(r, c, gameArr);
for (var k = 0; k < possArr.length && nextEmtySpot(gameArr)[0] !== -1; k++) {
gameArr[r][c] = possArr[k];
solveSudoku(gameArr);
}
// if no possible value leads to a solution reset this value
if (nextEmtySpot(gameArr)[0] !== -1) gameArr[r][c] = 0;
return gameArr;
}
function nextEmtySpot(gameArr) {
for (var i = 0; i < 9; i++) {
for (var j = 0; j < 9; j++) {
if (gameArr[i][j] === 0) return [i, j];
}
}
return [-1, -1];
}
function possiblities(r, c, gameArr) {
var possArr = [];
var row = [];
var col = [];
var quad = [];
var k = 0;
var l = 0;
if (r <= 2) k = 0; else if (r <= 5) k = 3; else k = 6;
if (c <= 2) l = 0; else if (c <= 5) l = 3; else l = 6;
for (var i = 0; i < 9; i++) {
row.push(gameArr[i][c]);
}
for (var j = 0; j < 9; j++) {
col.push(gameArr[r][j]);
}
for (var i = k; i < k + 3; i++) {
for (var j = l; j < l + 3; j++) {
quad.push(gameArr[i][j]);
}
}
for (var n = 1; n < 10; n++) {
if (row.indexOf(n) === -1 && col.indexOf(n) === -1 && quad.indexOf(n) === -1) {
possArr.push(n);
}
}
return possArr;
}
function checkQuadrant(r, c, gameArr) {
var qudarantArr = [];
for (var i = r; i < r + 3; i++) {
for (var j = c; j < c + 3; j++) {
if (qudarantArr.indexOf(gameArr[i][j]) === -1 || gameArr[i][j] === 0) {
qudarantArr.push(gameArr[i][j]);
} else {
return false;
}
}
}
return true;
}
function isValidSudoku(gameArr) {
if (!checkQuadrant(0, 0, gameArr)) return false;
if (!checkQuadrant(0, 3, gameArr)) return false;
if (!checkQuadrant(0, 6, gameArr)) return false;
if (!checkQuadrant(3, 0, gameArr)) return false;
if (!checkQuadrant(3, 3, gameArr)) return false;
if (!checkQuadrant(3, 6, gameArr)) return false;
if (!checkQuadrant(6, 0, gameArr)) return false;
if (!checkQuadrant(6, 3, gameArr)) return false;
if (!checkQuadrant(6, 6, gameArr)) return false;
for (var i = 0; i < gameArr.length; i++) {
var rowNumbers = [];
for (var j = 0; j < gameArr.length; j++) {
if (rowNumbers.indexOf(gameArr[i][j]) === -1 || gameArr[i][j] === 0) {
rowNumbers.push(gameArr[i][j]);
} else {
return false;
}
}
}
for (var i = 0; i < gameArr.length; i++) {
var colNumbers = [];
for (var j = 0; j < gameArr.length; j++) {
if (colNumbers.indexOf(gameArr[j][i]) === -1 || gameArr[j][i] === 0) {
colNumbers.push(gameArr[j][i]);
} else {
return false;
}
}
}
return true;
}
//---------------------------------------------------Test-----------------------------------------------------------------
var gameArr = [
[2, 0, 3, 0, 0, 8, 6, 0, 7],
[1, 4, 0, 7, 2, 6, 0, 0, 9],
[5, 0, 7, 1, 3, 9, 4, 2, 8],
[0, 2, 5, 0, 8, 1, 9, 0, 4],
[4, 1, 0, 9, 0, 3, 2, 0, 5],
[0, 7, 9, 2, 0, 5, 0, 3, 6],
[6, 0, 2, 0, 1, 0, 0, 9, 3],
[7, 0, 0, 5, 0, 2, 0, 0, 1],
[0, 8, 1, 3, 6, 7, 0, 4, 0]
];
solveSudoku(gameArr);
console.log('Solved');
console.log(gameArr);
console.log(isValidSudoku(gameArr));