Created
May 14, 2015 23:06
-
-
Save masotime/efefd23467b3494c63c4 to your computer and use it in GitHub Desktop.
sudoku-generator-WIP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function sudokuGenerator() { | |
'use strict'; | |
var test4 = [ | |
1, 2, 3, 4, | |
3, 4, 1, 2, | |
2, 3, 4, 1, | |
4, 1, 2, 3 | |
]; | |
var test9 = [ | |
8, 3, 5, 4, 1, 6, 9, 2, 7, | |
2, 9, 6, 8, 5, 7, 4, 3, 1, | |
4, 1, 7, 2, 9, 3, 6, 5, 8, | |
5, 6, 9, 1, 3, 4, 7, 8, 2, | |
1, 2, 3, 6, 7, 8, 5, 4, 9, | |
7, 4, 8, 5, 2, 9, 1, 6, 3, | |
6, 5, 2, 7, 8, 1, 3, 9, 4, | |
9, 8, 1, 3, 4, 5, 2, 7, 6, | |
3, 7, 4, 9, 6, 2, 8, 1, 5 | |
]; | |
function getN(square) { | |
return Math.sqrt(Math.sqrt(square.length)); | |
} | |
function getRange(N) { | |
var result = []; | |
for (var i=1; i <= N*N; i++) { | |
result.push(i); | |
} | |
return result; | |
} | |
function squareAccessor(square) { | |
var N = getN(square), | |
size = N * N; | |
function getRow(rowIndex) { | |
var result = []; | |
for (var i=rowIndex * size; i < (rowIndex + 1) * size; i++) { | |
result.push(square[i]); | |
} | |
return result; | |
} | |
function getColumn(columnIndex) { | |
var result = []; | |
for (var i=columnIndex; i < size * size; i+= size) { | |
result.push(square[i]); | |
} | |
return result; | |
} | |
function getSquare(bigRowIndex, bigColumnIndex) { | |
var result = []; | |
// in case I only use a single index e.g. for a 9x9 square, 34 is same as (2,7) | |
if (bigColumnIndex === undefined) { | |
bigColumnIndex = bigRowIndex % N; | |
bigRowIndex = ~~(bigRowIndex / N); // Math.fix | |
} | |
for (var row = bigRowIndex*N; row < (bigRowIndex+1)*N; row++) { | |
for (var i = row*size + bigColumnIndex*N; i < row*size + (bigColumnIndex+1)*N; i++) { | |
result.push(square[i]); | |
} | |
} | |
return result; | |
} | |
return { | |
getRow: getRow, | |
getColumn: getColumn, | |
getSquare: getSquare | |
}; | |
} | |
function squareValidator(square) { | |
var N = getN(square), | |
range = getRange(N), | |
size = N * N, | |
accessor = squareAccessor(square); | |
function getValidatorArray() { | |
var vArr = []; | |
for (var i=1; i <= size; i++) { | |
vArr[i] = false; | |
} | |
vArr[0] = true; | |
return vArr; | |
} | |
var types = { | |
valid: function valid(arr) { | |
return !!arr.reduce(function (acc, element) { | |
return element === undefined ? acc : | |
(!acc ? acc : | |
(acc.indexOf(element) !== -1 ? false : acc.concat(element)) | |
); | |
}, []); | |
}, | |
complete: function complete(arr) { | |
return arr | |
.reduce(function (vArr, element) { | |
if (vArr[element]) { | |
vArr[0] = false; | |
} else { | |
vArr[element] = true; | |
} | |
return vArr; | |
}, getValidatorArray()) | |
.every(function (elem) { | |
return elem; | |
}); | |
} | |
}; | |
function is(type) { | |
return function regarding(aspect) { | |
var result = true; | |
for (var i=0; i<size; i++) { | |
result = result && types[type](accessor['get'+aspect](i)); | |
} | |
return result; | |
}; | |
} | |
function all(type) { | |
return ['Row', 'Column', 'Square'].reduce(function (result, aspect) { | |
return result && is(type)(aspect); | |
}, true); | |
} | |
return { | |
allComplete: all('complete'), | |
allValid: all('valid') | |
}; | |
} | |
function placer(square) { | |
} | |
var accessor = squareAccessor(test9); | |
var validator = squareValidator(test9); | |
[ | |
//accessor4.getRow(2), | |
//accessor4.getColumn(3), | |
//accessor4.getSquare(1,1), | |
validator4.correct('Row'), | |
validator4.correct('Column'), | |
validator4.correct('Square'), | |
validator4.allCorrect() | |
].map(function(x) { console.log(x); }); | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment