Skip to content

Instantly share code, notes, and snippets.

@christopherhill
Last active December 29, 2015 21:19
Show Gist options
  • Select an option

  • Save christopherhill/7729224 to your computer and use it in GitHub Desktop.

Select an option

Save christopherhill/7729224 to your computer and use it in GitHub Desktop.
Sudoku Validation Class
var testCases = new Array();
testCases[0] = "751843926893625174642179583425316798176982345938754612364297851289531467517468239";
testCases[1] = "751843927893625174642179583425316798176982345938754612364297851289531467517468239";
testCases[2] = "571843926893625174642179583425316798176982345938754612364297851289531467517468239";
testCases[3] = "851743926693825174142679583425316798976182345738954612364297851289531467517468239";
testCases[4] = "223275461161885667779964198782134691447868986543883499854252274298641511551153988";
testCases[5] = "398126745471293685394287156874539162173982456973681245163759248927864531498612375";
var SudokuValidator = function() {
this.testCase = "";
this.isValid = false;
this.tests = { "rows" : false,
"columns" : false,
"boxes" : false,
"invalid" : false,
"unique" : false
};
this.init = function(arg) {
this.testCase = arg;
if (this.isValidCase())
return true;
else
return this.setError("invalid");
};
this.isValidCase = function() {
if (typeof(this.testCase) !== 'string') {
this.setError("invalid");
return false;
} else {
_result = this.testCase.match(/^\d*$/) && this.testCase.length === 81;
if (_result === true) {
return true;
} else {
this.setError("invalid");
return false;
}
}
};
this.getAllRows = function() {
return this.testCase.match(/.{1,9}/g);
};
this.getAllColumns = function() {
var _cols = new Array();
for (var _i = 0; _i < this.testCase.length; _i++) {
var _thisModulo = _i % 9;
var _existingEntry = (_cols[_thisModulo] === undefined) ? '' : _cols[_thisModulo] ;
var _thisEntry = this.testCase[_i];
_cols[_thisModulo] = (_existingEntry + _thisEntry);
}
return _cols;
};
this.getAllBoxes = function() {
var _groupsOfThree = this.testCase.match(/.{1,3}/g);
var _boxes = new Array();
var _curArr = 0;
for (var _h = 0; _h < 27; _h += 9) {
for (var _i = 0; _i < 3; _i++) {
var _addStr = "";
_addStr = _groupsOfThree[(_i + _h)] + _groupsOfThree[(_i + _h + 3)] + _groupsOfThree[(_i + _h + 6)];
_boxes.push(_addStr);
_curArr++;
}
}
return _boxes;
};
this.validateUnique = function(arr) {
// add testing for unique numbers
for (var _h = 0; _h < arr.length; _h++) {
var _unique = [0,0,0,0,0,0,0,0,0];
for (var _i = 0; _i < _unique.length; _i++) {
var _curIndex = parseInt(arr[_h][_i]);
_unique[_curIndex - 1] = _unique[_curIndex - 1] + 1;
}
// validate result
for (var _i = 0; _i < _unique.length; _i++) {
if (_unique[_i] > 1) {
this.setError("unique");
return false;
}
}
}
return true;
};
this.validateResult = function(arr) {
var that = this;
if (that.validateUnique(arr)) {
for (var _i = 0; _i < arr.length; _i++) {
var _cur = arr[_i];
var _sum = 0;
for (var _j = 0; _j < _cur.length; _j++) {
_sum += parseInt(_cur[_j]);
}
_sum === 45 ? that.isValid = true : that.isValid = false
}
return that.isValid;
} else {
return false;
}
};
this.setError = function(arg) {
this.tests[arg] = true;
return false;
};
this.validate = function() {
if (this.isValidCase()) {
r = this.validateResult(this.getAllRows()) === true ? true : this.setError("rows");
c = this.validateResult(this.getAllColumns()) === true ? true : this.setError("columns");
b = this.validateResult(this.getAllBoxes()) === true ? true : this.setError("boxes");
return (r && c && b);
} else {
return this.setError("invalid");
}
};
}
function runTests(testCases) {
for (var i = 0; i < testCases.length; i++) {
var thisSudoku = new SudokuValidator();
thisSudoku.init(testCases[i]);
console.log('validity: ' + thisSudoku.validate());
console.log('errors: ');
console.log(thisSudoku.tests);
}
}
runTests(testCases);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment