Skip to content

Instantly share code, notes, and snippets.

@james0r
Created January 13, 2018 05:20
Show Gist options
  • Save james0r/32f70502bf8d2cfdcc69b359e6742647 to your computer and use it in GitHub Desktop.
Save james0r/32f70502bf8d2cfdcc69b359e6742647 to your computer and use it in GitHub Desktop.
function doneOrNot(board){
// var boardForRegBuilder = [];
// boardForRegBuilder = deepCopy(board);
// console.log("boardForRegBuilder is " + boardForRegBuilder);
// function deepCopy(obj) {
// if (Object.prototype.toString.call(obj) === '[object Array]') {
// var out = [], i = 0, len = obj.length;
// for ( ; i < len; i++ ) {
// out[i] = arguments.callee(obj[i]);
// }
// return out;
// }
// if (typeof obj === 'object') {
// var out = {}, i;
// for ( i in obj ) {
// out[i] = arguments.callee(obj[i]);
// }
// return out;
// }
// return obj;
// }
function isValidBoard(newBoard) {
console.log(newBoard);
if (newBoard.length == 9) {
console.log("newBoard has 9 arrays");
for (i = 0; i < 9; i++ ) {
if (newBoard[i].length == 9) {
continue;
} else {
console.log("A subarray of newBoard didn't have 9 elements at subarray " + i);
//Board did not have the correct number of elements in sub-arrays.
return false;
}
}
//Board has passed the for-loop checking number of elements in sub-arrays.
return true;
} else {
//Board did not have the correct number of arrays (elements) in parent array.
console.log("board did not haev the correct number of arrays (elements) in parent array." + newBoard.length);
return false;
}
}
//Rows and columns are checked for containing 1,...9.
function isBoardCorrect(validBoard) {
for (xCoord = 0; xCoord < 9; xCoord++) {
for (numIter = 1; numIter < 10; numIter++ ) {
if (validBoard[xCoord].indexOf(numIter) >= 0) {
continue;
} else {
console.log("Board found incorrect during row check. With numIter being " + numIter + " and xCoord being " + xCoord);
return false;
}
}
}
for (yCoord = 0; yCoord < 9; yCoord++) {
var yAcc = [];
for (row = 0; row < 9; row++) {
yAcc.push(validBoard[row][yCoord]);
}
for (numIter2 = 1; numIter2 < 10; numIter2++ ) {
if (yAcc.indexOf(numIter2) >= 0) {
continue;
} else {
console.log("Board found incorrect during column check at column " + (yCoord+1));
console.log("yAcc var yeilds " + yAcc);
return false;
}
}
//Clear Y-Coordinate accumulator for next column accumulation.
yAcc = [];
}
return true;
}
//New array is built containing subarrays populated with 3x3 regions.
function regionBuilder(validBoard) {
var regParentArr = [];
var regChildArr = [];
while (validBoard.length > 1) {
for (regX = 0; regX < 3; regX++) {
for (regY = 0; regY < 3; regY++) {
regChildArr.push(validBoard[regX][regY]);
if (regY == 2) {
validBoard[regX].splice(0,3);
}
}
if (regX == 2) {
regParentArr.push(regChildArr);
regChildArr = [];
if (validBoard[2].length < 1) {
validBoard.splice(0,3);
}
}
}
}
return regParentArr;
}
//Checking arrays populated with region numbers for containing 1,...9.
function isRegionCorrect(regArrays) {
console.log(regArrays)
for (k = 0; k < 9; k++) {
for (l = 1; l < 10; l++) {
if (regArrays[k].indexOf(l) >= 0) {
continue;
} else {
console.log("One of the regions was not correct.");
return false;
}
}
}
return true;
}
//Main logic using above helper functions.
if (isValidBoard(board) == true && isBoardCorrect(board) && isRegionCorrect(regionBuilder(board))) {
console.log("Board found to be valid and correct!");
console.log("Regions were found to be correct!");
return "Finished!";
} else {
console.log("Board did not pass correct test.");
return "Try again!";
}
}
doneOrNot([ [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
[ 2, 3, 4, 5, 6, 7, 8, 9, 1 ],
[ 3, 4, 5, 6, 7, 8, 9, 1, 2 ],
[ 4, 5, 6, 7, 8, 9, 1, 2, 3 ],
[ 5, 6, 7, 8, 9, 1, 2, 3, 4 ],
[ 6, 7, 8, 9, 1, 2, 3, 4, 5 ],
[ 7, 8, 9, 1, 2, 3, 4, 5, 6 ],
[ 8, 9, 1, 2, 3, 4, 5, 6, 7 ],
[ 9, 1, 2, 3, 4, 5, 6, 7, 8 ] ]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment