I have to fill out a sudoku grid. Here is index.html:
<! DOCTYPE html >
<html >
<head >
<link rel ="stylesheet" href ="sudoku.css"/>
</head >
<body>
<table >
<tr >
<td id='r11'> </td >
<td id='r12'> </td >
<td id='r13'> </td >
<td id='r14'> </td >
<td id='r15'> </td >
<td id='r16'> </td >
<td id='r17'> </td >
<td id='r18'> </td >
<td id='r19'> </td >
</tr >
<tr >
<td id='r21' > </td >
<td id='r22' > </td >
<td id='r23' > </td >
<td id='r24' > </td >
<td id='r25' > </td >
<td id='r26' > </td >
<td id='r27' > </td >
<td id='r28' > </td >
<td id='r29' > </td >
</tr >
<tr >
<td id='r31'> </td >
<td id='r32'> </td >
<td id='r33'> </td >
<td id='r34'> </td >
<td id='r35'> </td >
<td id='r36'> </td >
<td id='r37'> </td >
<td id='r38'> </td >
<td id='r39'> </td >
</tr >
<tr >
<td id='r41'> </td >
<td id='r42'> </td >
<td id='r43'> </td >
<td id='r44'> </td >
<td id='r45'> </td >
<td id='r46'> </td >
<td id='r47'> </td >
<td id='r48'> </td >
<td id='r49'> </td >
</tr >
<tr >
<td id='r51'> </td >
<td id='r52'> </td >
<td id='r53'> </td >
<td id='r54'> </td >
<td id='r55'> </td >
<td id='r56'> </td >
<td id='r57'> </td >
<td id='r58'> </td >
<td id='r59'> </td >
</tr >
<tr >
<td id='r61'> </td >
<td id='r62'> </td >
<td id='r63'> </td >
<td id='r64'> </td >
<td id='r65'> </td >
<td id='r66'> </td >
<td id='r67'> </td >
<td id='r68'> </td >
<td id='r69'> </td >
</tr >
<tr >
<td id='r71'> </td >
<td id='r72'> </td >
<td id='r73'> </td >
<td id='r74'> </td >
<td id='r75'> </td >
<td id='r76'> </td >
<td id='r77'> </td >
<td id='r78'> </td >
<td id='r79'> </td >
</tr >
<tr >
<td id='r81'> </td >
<td id='r82'> </td >
<td id='r83'> </td >
<td id='r84'> </td >
<td id='r85'> </td >
<td id='r86'> </td >
<td id='r87'> </td >
<td id='r88'> </td >
<td id='r89'> </td >
</tr >
<tr >
<td id='r91' > </td >
<td id='r92' > </td >
<td id='r93' > </td >
<td id='r94' > </td >
<td id='r95' > </td >
<td id='r96' > </td >
<td id='r97' > </td >
<td id='r98' > </td >
<td id='r99' > </td >
</tr >
</table >
<button onclick="solve()">Solve</button>
</body >
<script src ="sudoku.js" > </script >
</html >
and here is the sudoku.js file:
//global variable
//accesible to all functions
var sol =
[[0, 7, 0, 2, 3, 8, 0, 0, 0],
[0, 0, 0, 7, 4, 0, 8, 0, 9],
[0, 6, 8, 1, 0, 9, 0, 0, 2],
[0, 3, 5, 4, 0, 0, 0, 0, 8],
[6, 0, 7, 8, 0, 2, 5, 0, 1],
[8, 0, 0, 0, 0, 5, 7, 6, 0],
[2, 0, 0, 6, 0, 3, 1, 9, 0],
[7, 0, 9, 0, 2, 1, 0, 0, 0],
[0, 0, 0, 9, 7, 4, 0, 8, 0]];
//this function prints the board
var printBoard = function () {
//code goes here
};
var solve = function() {
for //each grid cell
if //grid cell is empty
//try possible numbers
for //numbers 1 – 9
//check if number satisfies Sudoku requirements
if //number satisfies Sudoku requirements
set //grid cell to number
if(solve() == true) //make a recursive call to solve
return true; //we continue
else
set grid cell back to 0 //try next number
return false; //this is returned if no number works so we backtrack
return true; //returns true when all cells are filled with correct values
};
printBoard();
As you can see, I have to create a for loop that goes through each element in the sol array, then place it in the corresponding grid. But I am not sure how to implement the code. I just need a little boost, as I am very lost right now. I also need help with solving the puzzle after printing it, using depth first search. I am just very lost right now as I literally a Fine Arts major and I have no solid knowledge of CS, so some help will be greatly appreciated. Thank you!
EDIT: Here is what i Have done so far and have a problem with:
function isValid(sol, row, col, k) {
for (let i = 0; i < 9; i++) {
const m = 3 * Math.floor(row / 3) + Math.floor(i / 3);
const n = 3 * Math.floor(col / 3) + i % 3;
if (sol[row][i] == k || sol[i][col] == k || sol[m][n] == k) {
return false;
}
}
return true;
}
var solve = function() {
for (let row = 0; row < 9; row++) {
for (let col = 0; col < 9; col++) {
if (sol[row][col] = 0) {
for (let k = 1; k <= 9; k++) {
if (isValid(sol, row, col, k)) {
sol[row][col] = `${k}`;
if (solve(sol)) {
return true;
} else {
sol[row][col] = 0;
}
}
}
return false;
}
}
}
return true;
}
As you can see here, I created a new function, isValid(), that checks if a number is valid to put or not. However, when I run the code, it fails to solve it. WHere does the error lie?