-
Notifications
You must be signed in to change notification settings - Fork 1
/
sudoku1.js
59 lines (58 loc) · 1.38 KB
/
sudoku1.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
// "board" is a two dimensional array - contains 9 arrays with contain 9 integers
// returns a boolean value
function checkSolution(board) {
for (var i = 0; i < board.length; i++) {
if (
board[i]
.slice()
.sort()
.join("") !== "123456789"
) {
return false;
}
}
var columns = [];
for (var j = 0; j < board.length; j++) {
for (var k = 0; k < board[j].length; k++) {
columns.push(board[k][j]);
}
}
for (var l = 0; l < 9; l++) {
var number = l * 9;
var column = columns.slice(number, number + 9);
if (
column
.slice()
.sort()
.join("") !== "123456789"
) {
return false;
}
}
var squares = [];
for (var n = 0; n < 81; n += 9) {
squares.push(columns[n + 0]);
squares.push(columns[n + 1]);
squares.push(columns[n + 2]);
}
for (var n = 3; n < 81; n += 9) {
squares.push(columns[n + 0]);
squares.push(columns[n + 1]);
squares.push(columns[n + 2]);
}
for (var n = 6; n < 81; n += 9) {
squares.push(columns[n + 0]);
squares.push(columns[n + 1]);
squares.push(columns[n + 2]);
}
var squareArray = [];
for (var a = 0; a < squares.length; a += 9) {
squareArray.push(squares.slice(a, a + 9));
}
for (var b = 0; b < 9; b++) {
if (squareArray[b].sort().join("") !== "123456789") {
return false;
}
}
return true;
}