Check Solution

Ask questions about projects relating to: computer science or pure mathematics (such as probability, statistics, geometry, etc...).

Moderators: AmyCowen, kgudger, bfinio, MadelineB, Moderators

Locked
XeneizeRuloso
Posts: 4
Joined: Mon Dec 16, 2013 8:27 pm
Occupation: Student: 8th grade
Project Question: How do you get the JavaScript code working for the Sudoku Checker
Project Due Date: Jan 15 2013
Project Status: I am conducting my experiment

Check Solution

Post by XeneizeRuloso »

Everything in my code is working except for the JavaScript for my check solution button. Could someone help me?

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>
<input name="CheckSolution" onclick="CheckSolution()" value="Check Solution" type="button"><br>
<textarea id="OutputText" rows="10" cols="80" readonly></textarea>
</form>
<SCRIPT LANGUAGE="JavaScript">
<!-- Beginning of JavaScript -

<!-- 2006/02/16 AJO: JavaScript functions for "Program to Check a Sudoku Solution" -->
<!-- old browsers can't handle JavaScript functions so comment them out

var puzzle = new Array(9);

for (i=0; i < 9; i++)
   puzzle[i]=new Array(9);

// clears the Sudoku puzzle
function ClearPuzzle(form)
{
   var cell;
   
   for(i = 1; i <= 9; i++){
      for(j = 1; j <= 9; j++){
         cell = "C" + i + "_" + j;
         document.getElementById(cell).value = "";
      }
   }
    
}

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

// 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).value = puzzle[i-1][j-1];
      }
   }
}

// reads values from the Sudoku form, stores them in the puzzle[][] array, 
// and writes them to the OutputText TEXTAREA
function ReadCells()
{
   var cell;
   
   document.getElementById('OutputText').value = "ReadCells():\n";
   for(i = 1; i <= 9; i++){
      for(j = 1; j <= 9; j++){
         cell = "C" + i + "_" + j;
         puzzle[i-1][j-1] = document.getElementById(cell).value;
      }
      document.getElementById('OutputText').value += puzzle[i-1].join(" ");
      document.getElementById('OutputText').value += "\n";
   }
}
// Checks solution to see if numbers are arranged properly.
// rows
  var cell;
  
for (int i=0; i<9; i++) {
    std::bitset<9> filled;
    for (int j=0; j<9; j++)
        filled.set(grid[i][j] - 1);
    if (filled.count() != 9)
        return false;
}

// ... similar with the loops "swapped" to get the columns
// (or do both in one loop)

for (int i=0; i<9; i += 3)
    for (int j=0; j<9; j += 3) {
        std::bitset<9> filled;
        for (int k=0; k<3; k++)
            for (int l=0; l<3; l++)
                filled.set(grid[i+k][j+l] - 1);
        if (filled.count() != 9)
            return false;
    }

return true;

// end of JavaScript functions -->

// - End of JavaScript - -->
</SCRIPT>
<BODY>
</HTML>
dcnick96
Former Expert
Posts: 533
Joined: Wed Jul 25, 2007 7:59 pm

Re: Check Solution

Post by dcnick96 »

Hello. We noticed there is a separate thread for the same question, and you are receiving excellent advice from our mentors. Therefore, we are closing this thread. Please keep all questions for the same project on one thread, so that our mentors can best help you based on what has already been discussed.

Thanks for using Science Buddies, and good luck with your science project!
Deana
Locked

Return to “Grades 6-8: Math and Computer Science”