Page 1 of 1

Sudoku Checker

Posted: Mon Dec 16, 2013 9:10 pm
by XeneizeRuloso
I am working on the Sudoku checker experiment and am having problems with the JavaScript. Can someone help me with the code for the buttons
Here it is:

Code: Select all

<HTML>
<HEAD>
<title> Sudoku Checker </title>
</HEAD>
<BODY>
<form method="post" name="Sudoku">
<input id="C1_1" maxlength="1" size="1" style="BACKGROUND-COLOR:WHITE;" value="8" type="TEXT">
<input id="C1_2" maxlength="1" size="1" style="BACKGROUND-COLOR:WHITE;" value="2" type="TEXT">
<input id="C1_3" maxlength="1" size="1" style="BACKGROUND-COLOR:WHITE;" value="3" type="TEXT">
<input id="C1_4" maxlength="1" size="1" style="BACKGROUND-COLOR:silver;" value="" type="TEXT">
<input id="C1_5" maxlength="1" size="1" style="BACKGROUND-COLOR:SILVER;" value="" type="TEXT">
<input id="C1_6" maxlength="1" size="1" style="BACKGROUND-COLOR:SILVER;" value="" type="TEXT">
<input id="C1_7" maxlength="1" size="1" style="BACKGROUND-COLOR:WHITE;" value="7" type="TEXT">
<input id="C1_8" maxlength="1" size="1" style="BACKGROUND-COLOR:WHITE;" value="1" type="TEXT">
<input id="C1_9" maxlength="1" size="1" style="BACKGROUND-COLOR:WHITE;" value="5" type="TEXT">
<br>
<input id="C2_1" maxlength="1" size="1" style="BACKGROUND-COLOR:WHITE;" value="" type="TEXT">
<input id="C2_2" maxlength="1" size="1" style="BACKGROUND-COLOR:WHITE;" value="" type="TEXT">
<input id="C2_3" maxlength="1" size="1" style="BACKGROUND-COLOR:WHITE;" value="" type="TEXT">
<input id="C2_4" maxlength="1" size="1" style="BACKGROUND-COLOR:SILVER;" value="8" type="TEXT">
<input id="C2_5" maxlength="1" size="1" style="BACKGROUND-COLOR:SILVER;" value="" type="TEXT">
<input id="C2_6" maxlength="1" size="1" style="BACKGROUND-COLOR:SILVER;" value="5" type="TEXT">
<input id="C2_7" maxlength="1" size="1" style="BACKGROUND-COLOR:WHITE;" value="" type="TEXT">
<input id="C2_8" maxlength="1" size="1" style="BACKGROUND-COLOR:WHITE;" value="" type="TEXT">
<input id="C2_9" maxlength="1" size="1" style="BACKGROUND-COLOR:WHITE;" value="" type="TEXT">
<br>
<input id="C3_1" maxlength="1" size="1" style="BACKGROUND-COLOR:WHITE;" value="" type="TEXT">
<input id="C3_2" maxlength="1" size="1" style="BACKGROUND-COLOR:WHITE;" value="6" type="TEXT">
<input id="C3_3" maxlength="1" size="1" style="BACKGROUND-COLOR:WHITE;" value="" type="TEXT">
<input id="C3_4" maxlength="1" size="1" style="BACKGROUND-COLOR:SILVER;" value="" type="TEXT">
<input id="C3_5" maxlength="1" size="1" style="BACKGROUND-COLOR:SILVER;" value="" type="TEXT">
<input id="C3_6" maxlength="1" size="1" style="BACKGROUND-COLOR:SILVER;" value="" type="TEXT">
<input id="C3_7" maxlength="1" size="1" style="BACKGROUND-COLOR:WHITE;" value="" type="TEXT">
<input id="C3_8" maxlength="1" size="1" style="BACKGROUND-COLOR:WHITE;" value="2" type="TEXT">
<input id="C3_9" maxlength="1" size="1" style="BACKGROUND-COLOR:WHITE;" value="" type="TEXT">
<br>
<input id="C4_1" maxlength="1" size="1" style="BACKGROUND-COLOR:SILVER;" value="" type="TEXT">
<input id="C4_2" maxlength="1" size="1" style="BACKGROUND-COLOR:SILVER;" value="3" type="TEXT">
<input id="C4_3" maxlength="1" size="1" style="BACKGROUND-COLOR:SILVER;" value="5" type="TEXT">
<input id="C4_4" maxlength="1" size="1" style="BACKGROUND-COLOR:WHITE;" value="" type="TEXT">
<input id="C4_5" maxlength="1" size="1" style="BACKGROUND-COLOR:WHITE;" value="7" type="TEXT">
<input id="C4_6" maxlength="1" size="1" style="BACKGROUND-COLOR:WHITE;" value="" type="TEXT">
<input id="C4_7" maxlength="1" size="1" style="BACKGROUND-COLOR:SILVER;" value="6" type="TEXT">
<input id="C4_8" maxlength="1" size="1" style="BACKGROUND-COLOR:SILVER;" value="4" type="TEXT">
<input id="C4_9" maxlength="1" size="1" style="BACKGROUND-COLOR:SILVER;" value="" type="TEXT">
<br>
<input id="C5_1" maxlength="1" size="1" style="BACKGROUND-COLOR:SILVER;" value="" type="TEXT">
<input id="C5_2" maxlength="1" size="1" style="BACKGROUND-COLOR:SILVER;" value="" type="TEXT">
<input id="C5_3" maxlength="1" size="1" style="BACKGROUND-COLOR:SILVER;" value="" type="TEXT">
<input id="C5_4" maxlength="1" size="1" style="BACKGROUND-COLOR:WHITE;" value="4" type="TEXT">
<input id="C5_5" maxlength="1" size="1" style="BACKGROUND-COLOR:WHITE;" value="" type="TEXT">
<input id="C5_6" maxlength="1" size="1" style="BACKGROUND-COLOR:WHITE;" value="6" type="TEXT">
<input id="C5_7" maxlength="1" size="1" style="BACKGROUND-COLOR:SILVER;" value="" type="TEXT">
<input id="C5_8" maxlength="1" size="1" style="BACKGROUND-COLOR:SILVER;" value="" type="TEXT">
<input id="C5_9" maxlength="1" size="1" style="BACKGROUND-COLOR:SILVER;" value="" type="TEXT">
<br>
<input id="C6_1" maxlength="1" size="1" style="BACKGROUND-COLOR:SILVER;" value="" type="TEXT">
<input id="C6_2" maxlength="1" size="1" style="BACKGROUND-COLOR:SILVER;" value="4" type="TEXT">
<input id="C6_3" maxlength="1" size="1" style="BACKGROUND-COLOR:SILVER;" value="7" type="TEXT">
<input id="C6_4" maxlength="1" size="1" style="BACKGROUND-COLOR:WHITE;" value="" type="TEXT">
<input id="C6_5" maxlength="1" size="1" style="BACKGROUND-COLOR:WHITE;" value="8" type="TEXT">
<input id="C6_6" maxlength="1" size="1" style="BACKGROUND-COLOR:WHITE;" value="" type="TEXT">
<input id="C6_7" maxlength="1" size="1" style="BACKGROUND-COLOR:SILVER;" value="1" type="TEXT">
<input id="C6_8" maxlength="1" size="1" style="BACKGROUND-COLOR:SILVER;" value="3" type="TEXT">
<input id="C6_9" maxlength="1" size="1" style="BACKGROUND-COLOR:SILVER;" value="" type="TEXT">
<br>
<input id="C7_1" maxlength="1" size="1" style="BACKGROUND-COLOR:WHITE;" value="" type="TEXT">
<input id="C7_2" maxlength="1" size="1" style="BACKGROUND-COLOR:WHITE;" value="8" type="TEXT">
<input id="C7_3" maxlength="1" size="1" style="BACKGROUND-COLOR:WHITE;" value="" type="TEXT">
<input id="C7_4" maxlength="1" size="1" style="BACKGROUND-COLOR:SILVER;" value="" type="TEXT">
<input id="C7_5" maxlength="1" size="1" style="BACKGROUND-COLOR:SILVER;" value="" type="TEXT">
<input id="C7_6" maxlength="1" size="1" style="BACKGROUND-COLOR:SILVER;" value="" type="TEXT">
<input id="C7_7" maxlength="1" size="1" style="BACKGROUND-COLOR:WHITE;" value="" type="TEXT">
<input id="C7_8" maxlength="1" size="1" style="BACKGROUND-COLOR:WHITE;" value="9" type="TEXT">
<input id="C7_9" maxlength="1" size="1" style="BACKGROUND-COLOR:WHITE;" value="" type="TEXT">
<br>
<input id="C8_1" maxlength="1" size="1" style="BACKGROUND-COLOR:WHITE;" value="" type="TEXT">
<input id="C8_2" maxlength="1" size="1" style="BACKGROUND-COLOR:WHITE;" value="" type="TEXT">
<input id="C8_3" maxlength="1" size="1" style="BACKGROUND-COLOR:WHITE;" value="" type="TEXT">
<input id="C8_4" maxlength="1" size="1" style="BACKGROUND-COLOR:SILVER;" value="2" type="TEXT">
<input id="C8_5" maxlength="1" size="1" style="BACKGROUND-COLOR:SILVER;" value="" type="TEXT">
<input id="C8_6" maxlength="1" size="1" style="BACKGROUND-COLOR:SILVER;" value="3" type="TEXT">
<input id="C8_7" maxlength="1" size="1" style="BACKGROUND-COLOR:WHITE;" value="" type="TEXT">
<input id="C8_8" maxlength="1" size="1" style="BACKGROUND-COLOR:WHITE;" value="" type="TEXT">
<input id="C8_9" maxlength="1" size="1" style="BACKGROUND-COLOR:WHITE;" value="" type="TEXT">
<br>
<input id="C9_1" maxlength="1" size="1" style="BACKGROUND-COLOR:WHITE;" value="9" type="TEXT">
<input id="C9_2" maxlength="1" size="1" style="BACKGROUND-COLOR:WHITE;" value="1" type="TEXT">
<input id="C9_3" maxlength="1" size="1" style="BACKGROUND-COLOR:WHITE;" value="2" type="TEXT">
<input id="C9_4" maxlength="1" size="1" style="BACKGROUND-COLOR:SILVER;" value="" type="TEXT">
<input id="C9_5" maxlength="1" size="1" style="BACKGROUND-COLOR:SILVER;" value="" type="TEXT">
<input id="C9_6" maxlength="1" size="1" style="BACKGROUND-COLOR:SILVER;" value="" type="TEXT">
<input id="C9_7" maxlength="1" size="1" style="BACKGROUND-COLOR:WHITE;" value="3" type="TEXT">
<input id="C9_8" maxlength="1" size="1" style="BACKGROUND-COLOR:WHITE;" value="5" type="TEXT">
<input id="C9_9" maxlength="1" size="1" style="BACKGROUND-COLOR:WHITE;" value="7" type="TEXT">
<br>
<br>
<input name="ReadCellsButton" onclick="ReadCells()" value="Read Cells" type="button">
<input name="RestoreCellsButton" onclick="RestoreCells()" value="Restore Cells" type="button">
<input name="ClearButton1" onclick="ClearPuzzle()" value="Clear Puzzle" type="button">
<input name="ClearButton2" onclick="ClearTextArea()" value="Clear Text Area" type="button"><br>
<textarea id="OutputText" rows="10" cols="80" readonly></textarea>
</form>
<SCRIPT LANGUAGE="JavaScript">
<!-- Beginning of JavaScript -

var puzzle = new Array(9);

for (i=0; i < 9; i++)
   puzzle[i]=new Array(9);
// reads values from the Sudoku form, stores them in the puzzle[][] array, 
// and writes them to the OutputText TEXTAREAfunction ReadCells()
{
   var cell;

   document.getElementById('OutputText').innerHtml = "ReadCells():"<br/>";
   for(i = 1; i <= 9; i++){
      for(j = 1; j <= 9; j++){
         cell = "C" + i + "_" + j;
         puzzle[i-1][j-1] = document.getElementById(cell).innerHtml;
      }
      document.getElementById('OutputText').innerHtml += puzzle[i-1].join(" ");
      document.getElementById('OutputText').innerHtml += "<br/>";
   }
}

// writes values from the puzzle[][] array to the Sudoku form
 function RestoreCells()
 {
    var cell;

    for(i = 1; i <= 9; i++){
       for(j = 1; j <= 9; j++){
          cell = "C" + i + "_" + j;
          document.getElementById(cell).innerHtml = puzzle[i-1][j-1];
       }
    }
 }

// clears the Sudoku form
 function ClearPuzzle(form)
 {
    var cell;

    for(i = 1; i <= 9; i++){
       for(j = 1; j <= 9; j++){
          cell = "C" + i + "_" + j;
          document.getElementById(cell).innerHtml = "";
       }
    }

 }

// clears the TEXTAREA below the Sudoku puzzle
 function ClearTextArea(form){
    document.getElementById('OutputText').innerHtmlS = "";
 }

// - End of JavaScript - -->
</SCRIPT>
<BODY>
</HTML>

Re: Sudoku Checker

Posted: Tue Dec 17, 2013 4:48 pm
by hhemken
XeneizeRuloso,

On the "Program to Check a Sudoku Solution" project page (https://www.sciencebuddies.org/science- ... p023.shtml), if you use your browser's "View Source" function you'll find that the page uses this code at line 68 or so:

Code: Select all

<script src="https://www.sciencebuddies.org/Dirs/6/6/js/CompSci_p023.js" type="text/javascript"></script>
to pull in this javascript file: http://www.cdn.sciencebuddies.org/Dirs/ ... ci_p023.js

Have a look at that javascript and I think your questions will be answered.

Good luck!

Re: Sudoku Checker

Posted: Tue Dec 17, 2013 5:58 pm
by XeneizeRuloso
Hmm I cant open it... something about a compilation error

Re: Sudoku Checker

Posted: Tue Dec 17, 2013 7:00 pm
by hhemken
What browser are you using? It should just display the javascript code, not execute it. It works for me in Firefox and Chrome on Windows 7.

Re: Sudoku Checker

Posted: Tue Dec 17, 2013 8:48 pm
by XeneizeRuloso
Oh ya, it was because I was using Internet Explorer. Also, not to bother you but do you know of a way to incorporate a Check Solution button? I do know the HTML for it, but what about the JavaScript.

Re: Sudoku Checker

Posted: Fri Dec 20, 2013 12:42 am
by Aaron_03
Nice post!