small sudoku implementation help
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
But binaries are very efficient for Sudokus. For example, with 4x4 grids :
var nmb;// the number of a cell
var col = nmb%4; // its column (from 0 to 3)
var row = (nmb-col)/4 //its row (from 0 to 3)
var reg = (row%2)*2 + (col%2) //its quadrant (from 0 to 3)
- each leer cell can be represent by 11110 = (32-1) 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) :
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).
// The board constructor
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)
Last edited by 007Julien; 04-16-2013 at 12:53 PM.
Sorry I make a mistake a leer cell 11110 is 30 instead of 31 !
32-1 is Math.pow(2,5)-1 or (1<<5)-1 = 11111 and 30 is 11110.
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)