├── README.md ├── LICENSE ├── style.css ├── script.js └── index.html /README.md: -------------------------------------------------------------------------------- 1 | # Sudoku-Solver 2 | 3 | ![](https://github.com/sagnikghoshcr7/images/blob/master/Sudoku-Solver.gif) 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Sagnik Ghosh 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /style.css: -------------------------------------------------------------------------------- 1 | #container{ 2 | height: auto; 3 | width: 540px; 4 | background-color: white; 5 | display: flex; 6 | flex-wrap: wrap; 7 | justify-content: space-evenly; 8 | align-content: space-evenly ; 9 | margin: 0 auto; 10 | } 11 | 12 | #generate-sudoku{ 13 | margin:auto; 14 | display:block;; 15 | } 16 | 17 | 18 | #solve{ 19 | margin:auto; 20 | display:block;; 21 | } 22 | 23 | #container div{ 24 | background-color: whitesmoke; 25 | height: 60px; 26 | width: 60px; 27 | box-sizing: border-box; 28 | font-family: sans-serif; 29 | text-align: center; 30 | vertical-align: middle; 31 | line-height: 60px; 32 | font-size: 30px; 33 | color: green; 34 | 35 | } 36 | 37 | #container div:hover{ 38 | background-color: lightskyblue; 39 | } 40 | 41 | .lsb{ 42 | border-left: black 2px solid; 43 | } 44 | 45 | .bsb{ 46 | border-bottom: black 2px solid; 47 | } 48 | 49 | .rsb{ 50 | border-right: black 2px solid; 51 | } 52 | 53 | .tsb{ 54 | border-top: black 2px solid; 55 | } 56 | 57 | 58 | .ldb{ 59 | border-left: black 0.6px dashed; 60 | } 61 | 62 | .bdb{ 63 | border-bottom: black 0.6px dashed; 64 | } 65 | 66 | .rdb{ 67 | border-right: black 0.6px dashed; 68 | } 69 | 70 | .tdb{ 71 | border-top: black 0.6px dashed; 72 | } 73 | -------------------------------------------------------------------------------- /script.js: -------------------------------------------------------------------------------- 1 | var arr = [[], [], [], [], [], [], [], [], []] 2 | var temp = [[], [], [], [], [], [], [], [], []] 3 | 4 | for (var i = 0; i < 9; i++) { 5 | for (var j = 0; j < 9; j++) { 6 | arr[i][j] = document.getElementById(i * 9 + j); 7 | 8 | } 9 | } 10 | 11 | function initializeTemp(temp) { 12 | 13 | for (var i = 0; i < 9; i++) { 14 | for (var j = 0; j < 9; j++) { 15 | temp[i][j] = false; 16 | 17 | } 18 | } 19 | } 20 | 21 | 22 | function setTemp(board, temp) { 23 | 24 | for (var i = 0; i < 9; i++) { 25 | for (var j = 0; j < 9; j++) { 26 | if (board[i][j] != 0) { 27 | temp[i][j] = true; 28 | } 29 | 30 | } 31 | } 32 | } 33 | 34 | 35 | function setColor(temp) { 36 | 37 | for (var i = 0; i < 9; i++) { 38 | for (var j = 0; j < 9; j++) { 39 | if (temp[i][j] == true) { 40 | arr[i][j].style.color = "#DC3545"; 41 | } 42 | 43 | } 44 | } 45 | } 46 | 47 | function resetColor() { 48 | 49 | for (var i = 0; i < 9; i++) { 50 | for (var j = 0; j < 9; j++) { 51 | 52 | arr[i][j].style.color = "green"; 53 | 54 | 55 | } 56 | } 57 | } 58 | 59 | var board = [[], [], [], [], [], [], [], [], []] 60 | 61 | 62 | let button = document.getElementById('generate-sudoku') 63 | let solve = document.getElementById('solve') 64 | 65 | console.log(arr) 66 | function changeBoard(board) { 67 | for (var i = 0; i < 9; i++) { 68 | for (var j = 0; j < 9; j++) { 69 | if (board[i][j] != 0) { 70 | 71 | arr[i][j].innerText = board[i][j] 72 | } 73 | 74 | else 75 | arr[i][j].innerText = '' 76 | } 77 | } 78 | } 79 | 80 | 81 | button.onclick = function () { 82 | var xhrRequest = new XMLHttpRequest() 83 | xhrRequest.onload = function () { 84 | var response = JSON.parse(xhrRequest.response) 85 | console.log(response) 86 | initializeTemp(temp) 87 | resetColor() 88 | 89 | board = response.board 90 | setTemp(board, temp) 91 | setColor(temp) 92 | changeBoard(board) 93 | } 94 | xhrRequest.open('get', 'https://sugoku.herokuapp.com/board?difficulty=easy') 95 | //we can change the difficulty of the puzzle the allowed values of difficulty are easy, medium, hard and random 96 | xhrRequest.send() 97 | } 98 | 99 | //to be completed by student, function should not return anything 100 | // you can make a call to changeboard(board) function to update the state on the screen 101 | //returns a boolean true of false 102 | 103 | function isSafe(board,r,c,no){ 104 | 105 | 106 | //not repeating in the same row or column 107 | for(var i=0;i<9;i++){ 108 | if(board[i][c]==no || board[r][i]==no){ 109 | return false; 110 | } 111 | } 112 | //subgrid 113 | var sx = r - r%3; 114 | var sy = c - c%3; 115 | 116 | for(var x=sx;x 2 | 3 | 4 | 5 | 6 | 7 | Document 8 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 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 | 291 |
292 | 293 |
294 | 295 |
296 | 297 |
298 | 299 |
300 |
301 | 302 |
303 | 304 |
305 | 306 |
307 |
308 | 309 |
310 | 311 |
312 | 313 |
314 |
315 | 316 |
317 | 318 | 319 |
320 |
321 |
322 |      324 | 325 |
326 | 327 | 328 | 329 | --------------------------------------------------------------------------------