
small sudoku implementation help
Hi,
I am new to javascript so please be gentle. I am implementing a small 4x4 sudoku (16 cells total). I figured out how to make the tables and how to put buttons inside the cells to allow the user to pick a number. I am stuck on one thing however. How would I clear all the buttons within a cell once a user picks one and just have that number appear in plain text? here's what it looks like so far,
http://i.imgur.com/CLuPSkJ.jpg
For example, after I click on button 1, I want to check if it's valid then clear all the buttons and put "1" into that cell in plain text. Thanks guys.

Well, it is a pretty picture, but if you want some help you will need to supply the code you created it with.

Actually after hours of researching and failures, I figured most of my problems out. I have one last thing that just has to do with the algorithm. I know how to check if there's the same number in the row or column but i can't seem to figure out how to check if a number is in the same quadrant.. The table basically is 4x4 and there are 4 quadrants, how would you go about checking if the same number appears in the quadrant already?

You have questions, but without some code to look at anything I could say would be a pure SWAG.

Use the division of two intergers (by 4 and by 2) with quotient and remainder to get the row, column and quadrant of the cells (from 0 to 15) of your grid. Then the code is something like this
Code:
var nmb;// the number of a cell
var col = nmb%4; // its column (from 0 to 3)
var row = (nmbcol)/4 //its row (from 0 to 3)
var reg = (row%2)*2 + (col%2) //its quadrant (from 0 to 3)
But binaries are very efficient for Sudokus. For example, with 4x4 grids :
 each leer cell can be represent by 11110 = (321) which means that each value 1, 2, 3, 4 is possible
 each given cell by 00011 (for 1), 00101 (for 2), 01001 (for 3) and 00011 (for 4).
Then a new object Board could be build and update like this (not tested, only a transcription of this script) :
Code:
// The board constructor
function Board(){
this.cells=new Array();for (var i=0;i<16;i++) this.cells[i]=31;
}
// A method to place the value v in the cell c (used at first with the given number)
Board.prototype.setV=function(c,v){
var k,v=1<<v,w=32v,x=c%4,y=Math.floor(c/4),r=Math.floor(x/2)*2,s=Math.floor(y/2)*2;
for (k=0;k<4;k++){this.cells[x+k*4]&=w;this.cells[k+y*4]&=w;
this.cells[r+(k%2)+4*(s+Math.floor(k/2))]&=w;}
this.cells[c]=v+1;}
}
Then the Board gives immediately for each cell the number (if the bit of range 0 is 1) or possible numbers (the bit of range 0 is null).
Last edited by 007Julien; 04162013 at 11:53 AM.

Sorry I make a mistake a leer cell 11110 is 30 instead of 31 !
321 is Math.pow(2,5)1 or (1<<5)1 = 11111 and 30 is 11110.
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

"
"
