0

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?

bob jones
  • 11
  • 2

1 Answers1

0

For your printBoard function, simply loop through the matrix of values and edit each element with an the id corresponding to the position of the current element of the matrix:

var printBoard = function () {
    for (let row = 0; row < sol.length; row += 1) {
        for (let col = 0; col < sol[row].length; col += 1) {
            let id = 'r' + (row + 1) + (col + 1);
            let elem = document.getElementById(id);
            elem.innerHTML = sol[row][col];
        }
    }
};

As for your solve, you've already written the pseudocode for your algorithm. All that's left is to actually implement what you've written. If you find that it is slow or that it does not work, you might need to do some more research into how to go about solving a sudoku puzzle algorithmically. You can find multiple methods of this online.

If you need to learn the necessary JavaScript syntax to implement your solve function, check out w3schools. Since you already have the pseudocode, it's up to you to learn the necessary syntax to implement it. If you have any more specific syntax questions, consult w3schools or come back to StackOverflow if you can't find an answer there.

Robert May
  • 799
  • 6
  • 13
  • THank you so muhc! However I am still having trouble finding a good place to start. I just do not know how to properly implement the pseudocode. I am not sure how to create a for loop that goes through each grid cell. – bob jones Oct 30 '20 at 15:33
  • You can use the same method used in my example `printBoard` function to loop through each grid cell. The outer loop goes through each row, and the nested loop goes through each column in the current row, each column containing one value. You can reference that value with `sol[row][col]`. There are multiple ways to implement the pseudocode, you can definitely find all of the syntax & logic you'll need either on w3schools or on other StackOverflow posts. – Robert May Oct 30 '20 at 18:27
  • Thank you so much! I have filled out most of it, but I am stuck on the line that goes through the numbers 1-9 and checks if a number is valid or not. Is there a source that can help with this? I also made an edit for a reference. – bob jones Oct 30 '20 at 19:04
  • Please help as I am still stuck ur help has been very good – bob jones Oct 30 '20 at 21:19
  • I think this is the right code, but the button does not give an output. – bob jones Oct 30 '20 at 21:26