I'm creating Sudoku in JS but can't find the way to extract the numbers from the squares. The numbers from the rows and columns are easy to extract but I can't find the way around the squares. According to users input, I'll validate the column, then the row and finally the squares to see if the numbers are being repeated. Is this best practice?
Globals:
var numIni = 9; //Number of squares per side;
var squared = Math.sqrt(numIni); // I'll need this.
Column check:
function getColumn(){
var num = 30; //USER's INPUT
var colNums = []; //numbers IN the column
var countCol = num; // column anchor
while(countCol > numIni ){
countCol -= numIni
}
colNums[0] = countCol;
for(i=1;i< numIni;i++){
colNums[i]= colNums[i-1]+9;
}
}
Row check:
function getRow(){
var num = 50; //USER's INPUT
var rowAnchor = (Math.ceil(num/numIni))*numIni-(numIni-1);
var rowNums=[rowAnchor]//numbers IN the row
for(i=1;i< numIni;i++){
rowNums[i]= rowAnchor+i;
}
}
These last two work, but the squares are killing me. I'm first extracting the anchors, then building the squares and validating the number against that. It's getting quite long. Is there a better way to write this? Is the logic inefficient? If it's right, I'm stuck anyway.
Squares check (so far):
function getSquare(){
var boxes = [];
var boxesAnchor = [];
boxes[0]=1;
boxesAnchor[0]=1;
for(i=0; i< numIni*numIni ; i++){
boxes[i]= squared + ((i-1)* squared )+1;
}
for(i=0; i< numIni*squared ; i++){
for(j=0; j< squared ; j++){
boxesAnchor[i] = boxes[i];
boxesAnchor[i+j] = boxes[i+j];
boxesAnchor[i+j] = boxes[i+j];
}
i +=(numIni-1);
}
boxesAnchor.clean(undefined);// method added to the Array object
}