implement /api/solve route
This commit is contained in:
@@ -6,7 +6,11 @@ class SudokuSolver {
|
|||||||
|
|
||||||
validate(puzzleString) {
|
validate(puzzleString) {
|
||||||
|
|
||||||
return puzzleString.length == 81 && !puzzleString.match(/[^\.\d]/g);
|
if (puzzleString.length != 81) return 'Expected puzzle to be 81 characters long';
|
||||||
|
|
||||||
|
if (puzzleString.match(/[^\.\d]/g)) return 'Invalid characters in puzzle';
|
||||||
|
|
||||||
|
return true;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -173,7 +177,7 @@ class SudokuSolver {
|
|||||||
|
|
||||||
solve(puzzleString) {
|
solve(puzzleString) {
|
||||||
|
|
||||||
if (!this.validate(puzzleString)) return false;
|
if (this.validate(puzzleString) != true) return false;
|
||||||
|
|
||||||
// create character array
|
// create character array
|
||||||
let puzzle = puzzleString.split('');
|
let puzzle = puzzleString.split('');
|
||||||
@@ -311,7 +315,8 @@ class SudokuSolver {
|
|||||||
// get the solution for the given puzzle
|
// get the solution for the given puzzle
|
||||||
let board = generateBoard(puzzle);
|
let board = generateBoard(puzzle);
|
||||||
let solution = solveFromCell(board, 0, 0);
|
let solution = solveFromCell(board, 0, 0);
|
||||||
|
|
||||||
|
if (solution == false) return false;
|
||||||
return solution.flat().join('');
|
return solution.flat().join('');
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+11
-2
@@ -1,6 +1,6 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const SudokuSolver = require('../controllers/sudoku-solver.js');
|
const SudokuSolver = require('../controllers/sudoku-solver');
|
||||||
|
|
||||||
module.exports = function (app) {
|
module.exports = function (app) {
|
||||||
|
|
||||||
@@ -15,9 +15,18 @@ module.exports = function (app) {
|
|||||||
.post((req, res) => {
|
.post((req, res) => {
|
||||||
|
|
||||||
const puzzleString = req.body.puzzle;
|
const puzzleString = req.body.puzzle;
|
||||||
console.log(puzzleString);
|
|
||||||
|
|
||||||
|
if (puzzleString == undefined) return res.status(200).json({error: 'Required field missing' });
|
||||||
|
|
||||||
|
const validate = solver.validate(puzzleString);
|
||||||
|
|
||||||
|
if (validate != true) return res.status(200).json({error: validate});
|
||||||
|
|
||||||
|
const solution = solver.solve(puzzleString);
|
||||||
|
|
||||||
|
if (solution == false) return res.status(200).json({error: 'Puzzle cannot be solved'});
|
||||||
|
|
||||||
|
return res.status(200).json({solution: solution});
|
||||||
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
@@ -20,7 +20,7 @@ suite('UnitTests', () => {
|
|||||||
|
|
||||||
const input = puzzlesAndSolutions[0][0].replace(/\./g, '?');
|
const input = puzzlesAndSolutions[0][0].replace(/\./g, '?');
|
||||||
|
|
||||||
assert.equal(solver.validate(input), false);
|
assert.equal(solver.validate(input), 'Invalid characters in puzzle');
|
||||||
done();
|
done();
|
||||||
|
|
||||||
});
|
});
|
||||||
@@ -29,7 +29,7 @@ suite('UnitTests', () => {
|
|||||||
|
|
||||||
const input = puzzlesAndSolutions[0][0] + '0';
|
const input = puzzlesAndSolutions[0][0] + '0';
|
||||||
|
|
||||||
assert.equal(solver.validate(input), false);
|
assert.equal(solver.validate(input), 'Expected puzzle to be 81 characters long');
|
||||||
done();
|
done();
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user