Files
freecodecamp-sudoku-solver/controllers/sudoku-solver.js
T

86 lines
1.7 KiB
JavaScript
Raw Normal View History

2022-05-20 16:21:08 -05:00
class SudokuSolver {
validate(puzzleString) {
return puzzleString.length == 81 && !puzzleString.match(/[^\.\d]/g);
2022-05-20 16:21:08 -05:00
}
checkRowPlacement(puzzleString, row, column, value) {
// rows
const rows = {
A: [],
B: [],
C: [],
D: [],
E: [],
F: [],
G: [],
H: [],
I: [],
};
// rowKeys
const rowKeys = Object.keys(rows);
// split puzzle strings and assign row values for each row
rowKeys.forEach((key, index) => {
const start = index * 9;
const end = start + 9;
const rowValues = puzzleString.slice(start, end).split('');
rows[key] = rowValues
});
// get row values for placement
const rowValues = rows[row];
// get existing value placed
const existingValue = rowValues[column - 1];
// make sure placement location does not already contain a number
// make sure placements row does not already have value.
return (existingValue == '.' || existingValue == value) && !rowValues.includes(value);
2022-05-20 16:21:08 -05:00
}
checkColPlacement(puzzleString, row, column, value) {
const columns = {
1: [],
2: [],
3: [],
4: [],
5: [],
6: [],
7: [],
8: [],
9: [],
};
const columnKeys = Object.keys(columns);
columnKeys.forEach((key, index) => {
});
2022-05-20 16:21:08 -05:00
}
checkRegionPlacement(puzzleString, row, column, value) {
2022-05-20 16:21:08 -05:00
}
solve(puzzleString) {
}
}
module.exports = SudokuSolver;