HI, I have a task to change the javascript part of the code to apply the sudoku rules to this webpage. I already did it for the row but I am struggling for the column and the 3x3 rule. Can you please help me? I can only edit the javascript of the. Here is the code:
table#sudoku {
border-width: 3px;
border-style: solid;
border-color: black
}
table#sudoku td[id^=cell_2] {border-right-width: 2px}
table#sudoku td[id^=cell_5] {border-right-width: 2px}
table#sudoku td[id$=_2] {border-bottom-width: 2px}
table#sudoku td[id$=_5] {border-bottom-width: 2px}
td.selected {background-color: rgb(100%, 70%, 0%)}
td.tofill {color: blue}
</style>
<script>
var current_cell = null; // the currently selected cell
function initialize() {
var col, row;
// Work through all the cells in the table and set
// onclick event handlers and classNames for the empty
// ones.
for (row = 0; row <=8; row++) {
for (col=0; col <= 8; col++) {
var cell = document.getElementById('cell_' + col + '_' + row);
if (!parseInt(cell.innerHTML)) {
// cell is empty
cell.onclick = selectCell;
cell.className = 'tofill';
}
}
}
document.onkeypress = keyPress;
}
// Capture keyboard key presses. If the key pressed is a digit
// then add it to the current cell. If it is a space then empty
// the current cell.
function keyPress(evt) {
if (current_cell == null)
return;
var key;
if (evt)
// firefox or chrome
key = String.fromCharCode(evt.charCode);
else
// IE
key = String.fromCharCode(event.keyCode);
if (key == ' ')
current_cell.innerHTML = '';
else if (key >= 1 && key <= 9)
{
var exists = 0
for (var i=0;i<9;i++)
{
var cell="cell_"+i+"_"+(current_cell.id.substring(current_cell.id.length,7));
if(document.getElementById(cell).innerHTML == key)
{ alert("Cannot enter the same number twice in the same row");
exists= exists + 1
}
The simpler expression to define a Sudoku is a String with 81 digits like this one (*) :
var sdk='000000012000035000000600070700000300000400800100000000000120000080000040050000600';
Copy and paste this value (or all the line) in the textarea to test the script.
Code:
function Board(){
this.cells=new Array();for (var i=0;i<81;i++) this.cells[i]=1022;
this.whrC=99;
}
function SetV(p,v) {
var k,v=1<<v,w=1023-v,x=p%9,y=Math.floor(p/9),r=Math.floor(x/3)*3,s=Math.floor(y/3)*3;
for (k=0;k<9;k++){this.cells[x+k*9]&=w;this.cells[k+y*9]&=w;
this.cells[r+(k%3)+9*(s+Math.floor(k/3))]&=w;}
this.cells[p]=v+1;
}
Board.prototype.setV=SetV;
This solver work with an object Board which use an array of 81 cells (and a auxiliary variable). One leer cell contains 1022, which is the binary 1111111110 (for nine possible values, nine true digits and a false digit for a leer cell.
The function, SetV(p,v), set not exactly the value v (thee first and last lines of the function), but the value 1<<v+1, (a digit v times left shifted and a digit for full cell) in the cell p (which, for example with v=7 contains 0010000001). This function also updates the cells of the same line, column or region (your 3x3 rules) with an &(1023-v) which is, in our example 1110111110 to forbid the 7 in all this cells...
In brief to answer your question, it's always easier to work with unidimensional arrays. Cells are numbered from 0 to 80, the lines and cols are given by the Euclidean division by 9 (quotient Math.floor(n/9) and remainder n%9) and a similar formula (with division by 3) for the 3x3 rules...
Since this solver is very efficient, it full after the given cells are defined, all cells which can contains an unique digit (all other cells are always simultaneously updated), all cells that can only receive a digit in the line, row or 3x3 rule. Then it take the first cell which can contain the minimal number of digit and build new boards to test the different solutions. This new boards are killed in case of impossibility or saved (with two solutions the grid is not valid) until the resolution...
The Time Through Ages
In the Name of Allah, Most Gracious, Most Merciful
1. By the Time,
2. Verily Man is in loss,
3. Except such as have Faith, and do righteous deeds, and (join together) in the mutual enjoining of Truth, and of Patience and Constancy.
Bookmarks