-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsudoku-board.js
186 lines (164 loc) · 4.5 KB
/
sudoku-board.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
function SudokuBoard(boardArray) {
if(!boardArray) {
this.boardArray = [
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0]
];
} else {
this.boardArray = JSON.parse(JSON.stringify(boardArray)); // deep copies array- but only works with primitives!
}
}
SudokuBoard.arraySatisfiesSolution = function(arr) {
if(arr.length !== 9) {
return false;
}
var uniqueNumsInArr = {};
for (var num of arr) {
if (uniqueNumsInArr[num] || num > 9 || num < 1) {
return false;
} else {
uniqueNumsInArr[num] = 1;
}
}
return true;
}
SudokuBoard.prototype.isSolved = function() {
return (this.allRowsSatisfySolution() && this.allColsSatisfySolution() && this.allBoxesSatisfySolution());
}
SudokuBoard.prototype.toString = function() {
var boardAsString = '';
for (var row of this.boardArray) {
for (var num of row) {
boardAsString += (num + ' ');
}
boardAsString += '\n'
}
return boardAsString;
}
SudokuBoard.prototype.getRows = function() {
return this.boardArray;
}
SudokuBoard.prototype.getCols = function() {
var allCols = [];
for (var col = 0; col < 9; col++) {
allCols[col] = [];
for (var row = 0; row < 9; row++) {
allCols[col].push(this.boardArray[row][col])
}
}
return allCols;
}
SudokuBoard.prototype.allRowsSatisfySolution = function() {
for (var row of this.boardArray) {
if (SudokuBoard.arraySatisfiesSolution(row) == false) {
return false
}
}
return true;
}
SudokuBoard.prototype.allColsSatisfySolution = function() {
for (var col of this.getCols()) {
if (SudokuBoard.arraySatisfiesSolution(col) == false) {
return false
}
}
return true;
}
SudokuBoard.prototype.allBoxesSatisfySolution = function() {
for (var box of this.getBoxes()) {
if (SudokuBoard.arraySatisfiesSolution(box) == false) {
return false
}
}
return true;
}
SudokuBoard.prototype.getBoxes = function() {
var boxRow1 = this.boardArray.filter(function(val, index) {
return index < 3;
});
var boxRow2 = this.boardArray.filter(function(val, index) {
return index > 2 && index < 6;
});
var boxRow3 = this.boardArray.filter(function(val, index) {
return index > 5;
});
var boxRow1Col1 = [];
boxRow1.forEach(function(curRow, index, arr) {
boxRow1Col1.push(curRow[0]);
boxRow1Col1.push(curRow[1]);
boxRow1Col1.push(curRow[2]);
});
var boxRow1Col2 = [];
boxRow1.forEach(function(curRow, index, arr) {
boxRow1Col2.push(curRow[3]);
boxRow1Col2.push(curRow[4]);
boxRow1Col2.push(curRow[5]);
});
var boxRow1Col3 = [];
boxRow1.forEach(function(curRow, index, arr) {
boxRow1Col3.push(curRow[6]);
boxRow1Col3.push(curRow[7]);
boxRow1Col3.push(curRow[8]);
});
var boxRow2Col1 = [];
boxRow2.forEach(function(curRow, index, arr) {
boxRow2Col1.push(curRow[0]);
boxRow2Col1.push(curRow[1]);
boxRow2Col1.push(curRow[2]);
});
var boxRow2Col2 = [];
boxRow2.forEach(function(curRow, index, arr) {
boxRow2Col2.push(curRow[3]);
boxRow2Col2.push(curRow[4]);
boxRow2Col2.push(curRow[5]);
});
var boxRow2Col3 = [];
boxRow2.forEach(function(curRow, index, arr) {
boxRow2Col3.push(curRow[6]);
boxRow2Col3.push(curRow[7]);
boxRow2Col3.push(curRow[8]);
});
var boxRow3Col1 = [];
boxRow3.forEach(function(curRow, index, arr) {
boxRow3Col1.push(curRow[0]);
boxRow3Col1.push(curRow[1]);
boxRow3Col1.push(curRow[2]);
});
var boxRow3Col2 = [];
boxRow3.forEach(function(curRow, index, arr) {
boxRow3Col2.push(curRow[3]);
boxRow3Col2.push(curRow[4]);
boxRow3Col2.push(curRow[5]);
});
var boxRow3Col3 = [];
boxRow3.forEach(function(curRow, index, arr) {
boxRow3Col3.push(curRow[6]);
boxRow3Col3.push(curRow[7]);
boxRow3Col3.push(curRow[8]);
});
var allBoxes = [];
allBoxes.push(boxRow1Col1, boxRow1Col2, boxRow1Col3,
boxRow2Col1, boxRow2Col2, boxRow2Col3,
boxRow3Col1, boxRow3Col2, boxRow3Col3);
return allBoxes;
}
SudokuBoard.prototype.insert = function(arr) {
inputVals = arr.slice(0);
for (var row of this.boardArray) {
for (var index in row) {
if (row[index] === 0) {
row[index] = inputVals.shift(); // shift pops value at 0th index
}
}
}
}
module.exports = {
SudokuBoard: SudokuBoard,
};