Skip to content

Commit

Permalink
implement new output function which Fixes #2
Browse files Browse the repository at this point in the history
  • Loading branch information
Radu-Bogdan Croitoru committed Apr 21, 2014
1 parent 8900453 commit d363653
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
36 changes: 34 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Dependencies
var sets = require('simplesets');
var sets = require('simplesets')
, vsprintf = require('sprintf').vsprintf;

var sudokuSolver = {};

Expand All @@ -21,11 +22,42 @@ global.sameBlock = function(i, j) {
return (Math.floor(i/27) == Math.floor(j/27) && Math.floor(i%9/3) == Math.floor(j%9/3));
}

// Output the problem in the terminal
global.output = function(problem, style) {
// Check if style is false or true
style == false ? console.log(problem) : stylishOutput(problem);

// This is called when style == true
function stylishOutput(problem) {

// Make a new array of strings where each string have 9 elements
var new_data = problem.match(/(.........)/g);

// Print one formated line
function printLine() {
console.log(vsprintf('%s %s %s | %s %s %s | %s %s %s', new_data.shift().match(/(.)/g)));
}

// Print delimter
function printDelimiter() {
console.log('------+-------+------');
}

// Build the output
for(var i = 0; i < 13; i++) {
i % 4 == 0 ? printDelimiter() : printLine();
}
}
}

sudokuSolver.solve = function (options) {

// Expect at least one problem
var problem = options.problem;

// Take the style from options
var style = options.style || false;

if (problem && problem.constructor == String) {
// Replace spaces with nothing
problem = problem.replace (/ /g, "");
Expand All @@ -49,7 +81,7 @@ sudokuSolver.solve = function (options) {
var i = problem.indexOf("0");

// If i is negative that means we're done
(i == -1) ? console.log(problem) : 'err'
(i == -1) ? output(problem, style) : 'err'

// Create a set to save excluded elements
var excludedElements = new sets.Set();
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "Solve sudoku problems using Node.js and output the solution in the terminal",
"main": "index.js",
"dependencies": {
"simplesets": "*"
"simplesets": "*",
"sprintf": "*"
},
"repository": {
"type": "git",
Expand Down
1 change: 1 addition & 0 deletions test/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ SudokuSolver.solve({
+ '. . 2 6 . 9 5 . .'
+ '8 . . 2 . 3 . . 9'
+ '. . 5 . 1 . 3 . .'
, style: true
});

2 comments on commit d363653

@IonicaBizau
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something is strange with this commit: is git config correctly set on your machine?

Invalid author email. ...

@radubogdan
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No.. I fixed it

Please sign in to comment.