-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathsudoku solver html+Js code
290 lines (251 loc) · 10.5 KB
/
sudoku solver html+Js code
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<html>
<head>
<title></title>
<script type="text/javascript" src="sudoku.js"></script>
<script type="text/javascript" src="analytics.js"></script>
<style type="text/css">
body { font-family: Calibri, sans-serif; }
#container { text-align: center }
table { border-collapse: collapse; font-size: 2em; margin: 0 auto; }
colgroup, tbody { border: solid medium; }
td { border: solid thin; height: 1.4em; width: 1.4em; text-align: center; padding: 0; }
button { margin-top: 15px; font-size: 1.5em; }
padd{padding-bottom: 100px;}
]
</style>
</head>
<body>
<div id="container">
<img src="gfg.png" style = "height:100px ; width: 400px; align ="left" " >
<h1 class = "padd">Sudoku Solver</h1>
<table id="sudoku-board">
<colgroup><col><col><col>
<colgroup><col><col><col>
<colgroup><col><col><col>
<tbody>
<tr> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td>
<tr> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td>
<tr> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td>
<tbody>
<tr> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td>
<tr> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td>
<tr> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td>
<tbody>
<tr> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td>
<tr> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td>
<tr> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td> <td contenteditable="true"></td>
</table>
<div>
<button id="solve-button">Solve!</button>
</div>
<div>
<button id="clear-button">Clear board</button>
</div>
</div>
</body>
<script type="text/javascript">
document.getElementById("sudoku-board").addEventListener("keyup", function(event) {
if(event.target && event.target.nodeName == "TD") {
var validNum = /[1-9]/;
var tdEl = event.target;
if (tdEl.innerText.length > 0 && validNum.test(tdEl.innerText[0])) {
tdEl.innerText = tdEl.innerText[0];
} else {
tdEl.innerText = "";
}
}
});
document.getElementById("solve-button").addEventListener("click", function(event) {
var boardString = boardToString();
var solution = SudokuSolver.solve(boardString);
if (solution) {
stringToBoard(solution);
} else {
alert("Invalid board!");
}
})
document.getElementById("clear-button").addEventListener("click", clearBoard);
function clearBoard() {
var tds = document.getElementsByTagName("td");
for (var i = 0; i < tds.length; i++) {
tds[i].innerText = "";
}
}
function boardToString() {
var string = "";
var validNum = /[1-9]/;
var tds = document.getElementsByTagName("td");
for (var i = 0; i < tds.length; i++) {
if (validNum.test(tds[i].innerText[0])) {
string += tds[i].innerText;
} else {
string += "-";
}
}
return string;
}
function stringToBoard(string) {
var currentCell;
var validNum = /[1-9]/;
var cells = string.split("");
var tds = document.getElementsByTagName("td");
for (var i = 0; i < tds.length; i++) {
currentCell = cells.shift();
if (validNum.test(currentCell)) {
tds[i].innerText = currentCell;
}
}
}
</script>
</html>
//JS
"use strict";
var EASY_PUZZLE = "1-58-2----9--764-52--4--819-19--73-6762-83-9-----61-5---76---3-43--2-5-16--3-89--";
var MEDIUM_PUZZLE = "-3-5--8-45-42---1---8--9---79-8-61-3-----54---5------78-----7-2---7-46--61-3--5--";
var HARD_PUZZLE = "8----------36------7--9-2---5---7-------457-----1---3---1----68--85---1--9----4--";
// Set this variable to true to publicly expose otherwise private functions inside of SudokuSolver
var TESTABLE = true;
var SudokuSolver = function (testable) {
var solver;
// PUBLIC FUNCTIONS
function solve(boardString) {
var boardArray = boardString.split("");
if (boardIsInvalid(boardArray)) {
return false;
}
return recursiveSolve(boardString);
}
function solveAndPrint(boardString) {
var solvedBoard = solve(boardString);
console.log(toString(solvedBoard.split("")));
return solvedBoard;
}
// PRIVATE FUNCTIONS
function recursiveSolve(boardString) {
var boardArray = boardString.split("");
if (boardIsSolved(boardArray)) {
return boardArray.join("");
}
var cellPossibilities = getNextCellAndPossibilities(boardArray);
var nextUnsolvedCellIndex = cellPossibilities.index;
var possibilities = cellPossibilities.choices;
for (var i = 0; i < possibilities.length; i++) {
boardArray[nextUnsolvedCellIndex] = possibilities[i];
var solvedBoard = recursiveSolve(boardArray.join(""));
if (solvedBoard) {
return solvedBoard;
}
}
return false;
}
function boardIsInvalid(boardArray) {
return !boardIsValid(boardArray);
}
function boardIsValid(boardArray) {
return allRowsValid(boardArray) && allColumnsValid(boardArray) && allBoxesValid(boardArray);
}
function boardIsSolved(boardArray) {
for (var i = 0; i < boardArray.length; i++) {
if (boardArray[i] === "-") {
return false;
}
}
return true;
}
function getNextCellAndPossibilities(boardArray) {
for (var i = 0; i < boardArray.length; i++) {
if (boardArray[i] === "-") {
var existingValues = getAllIntersections(boardArray, i);
var choices = ["1", "2", "3", "4", "5", "6", "7", "8", "9"].filter(function (num) {
return existingValues.indexOf(num) < 0;
});
return { index: i, choices: choices };
}
}
}
function getAllIntersections(boardArray, i) {
return getRow(boardArray, i).concat(getColumn(boardArray, i)).concat(getBox(boardArray, i));
}
function allRowsValid(boardArray) {
return [0, 9, 18, 27, 36, 45, 54, 63, 72].map(function (i) {
return getRow(boardArray, i);
}).reduce(function (validity, row) {
return collectionIsValid(row) && validity;
}, true);
}
function getRow(boardArray, i) {
var startingEl = Math.floor(i / 9) * 9;
return boardArray.slice(startingEl, startingEl + 9);
}
function allColumnsValid(boardArray) {
return [0, 1, 2, 3, 4, 5, 6, 7, 8].map(function (i) {
return getColumn(boardArray, i);
}).reduce(function (validity, row) {
return collectionIsValid(row) && validity;
}, true);
}
function getColumn(boardArray, i) {
var startingEl = Math.floor(i % 9);
return [0, 1, 2, 3, 4, 5, 6, 7, 8].map(function (num) {
return boardArray[startingEl + num * 9];
});
}
function allBoxesValid(boardArray) {
return [0, 3, 6, 27, 30, 33, 54, 57, 60].map(function (i) {
return getBox(boardArray, i);
}).reduce(function (validity, row) {
return collectionIsValid(row) && validity;
}, true);
}
function getBox(boardArray, i) {
var boxCol = Math.floor(i / 3) % 3;
var boxRow = Math.floor(i / 27);
var startingIndex = boxCol * 3 + boxRow * 27;
return [0, 1, 2, 9, 10, 11, 18, 19, 20].map(function (num) {
return boardArray[startingIndex + num];
});
}
function collectionIsValid(collection) {
var numCounts = {};
for(var i = 0; i < collection.length; i++) {
if (collection[i] != "-") {
if (numCounts[collection[i]] === undefined) {
numCounts[collection[i]] = 1;
} else {
return false;
}
}
}
return true;
}
function toString(boardArray) {
return [0, 9, 18, 27, 36, 45, 54, 63, 72].map(function (i) {
return getRow(boardArray, i).join(" ");
}).join("\n");
}
if (testable) {
// These methods will be exposed publicly when testing is on.
solver = {
solve: solve,
solveAndPrint: solveAndPrint,
recursiveSolve: recursiveSolve,
boardIsInvalid: boardIsInvalid,
boardIsValid: boardIsValid,
boardIsSolved: boardIsSolved,
getNextCellAndPossibilities: getNextCellAndPossibilities,
getAllIntersections: getAllIntersections,
allRowsValid: allRowsValid,
getRow: getRow,
allColumnsValid: allColumnsValid,
getColumn: getColumn,
allBoxesValid: allBoxesValid,
getBox: getBox,
collectionIsValid: collectionIsValid,
toString: toString };
} else {
// These will be the only public methods when testing is off.
solver = { solve: solve,
solveAndPrint: solveAndPrint };
}
return solver;
}(TESTABLE);