Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Humansolver #3

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
102 changes: 102 additions & 0 deletions DigitSet_Constructor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
var GridFactory = require('./grid_constructor');
var testStr = require('./main');
var Viewer = require('./viewer-display');
var currentStr = require('./updater');

var str = currentStr;

var ObjectFactory = function(id) {
var potential = [];
var arr = [];
this.id = id;
this.row = (Math.ceil((id+1)/9));
this.column = (id % 9) + 1;
this.block = Math.floor((this.column-1)/3)*3 + Math.floor((this.row-1)/3);
this.getRow = function(){
var row = getRow(str);
return row[this.id];
};
this.getColumn = function(){
var col = getColumn(getRow(str));
return col[this.id];
};
this.getBlock = function(){
var block = getBlock(getRow(str));
return block[this.id];
};
this.potential = function(){
var possible = [1,2,3,4,5,6,7,8,9];
var column = this.getColumn();
var row = this.getRow();
var block = this.getBlock();
possible = possible.filter(function(val) {
return column.indexOf(val) == -1;});
possible = possible.filter(function(val) {
return row.indexOf(val) == -1;});
possible = possible.filter(function(val) {
return block.indexOf(val) == -1;});
return possible;
};


this.getRow = function(str){
var x = str.split('');
var arr = [];
for(var i = 0; i < x.length; i++){
if (i === 0){
arr.push(str[i]);
}
else if(i % 9 === 0){
arr.push('*');
arr.push(str[i]);

}else {
arr.push(str[i]);
}
}
arr = arr.join('').split('*');
return arr;
};

this.getColumn = function(arr){
col = [];
for(var x = 0; x < arr.length; x++){
col.push('!');
for(var i = 0; i < arr.length; i++){
col.push(arr[i][x]);
}
}
col = col.join('').split('!').slice(1);
return col;
};

this.getBlock = function(row){
var output;
var block = '';
var count1 = -3, count2 = 0;
for (var x = 0; x < 3; x++) {
count1 = count1 + 3;
count2 = count2 + 3;
for (var i = 0; i < 3; i++) {
block += '!';
for (var y = 0; y < 3; y++) {
block += row[y].slice(count1, count2);
}
}
}
output = block.split('!');
output.shift();
return output;
};

};

var cell = new ObjectFactory(12);
console.log(cell);

// console.log(square.getColumn());
// console.log(square.getRow());
// console.log(square.getBlock());
// console.log(square.potential());

module.exports = ObjectFactory;
1,043 changes: 1,043 additions & 0 deletions data_into_testString.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion digitset.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var _ = require('lodash');
// var _ = require('lodash');


function DigitSet(initstr) {
Expand Down
26 changes: 13 additions & 13 deletions extras/async-demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@
var fs = require('fs'); //load node module
var timer = require('./timer'); //load custom module

timer.start()
console.log('readFileSynch:')
timer.start();
console.log('readFileSynch:');
var syncData = fs.readFileSync('sudoku-data', {encoding: 'utf8'});
timer.check('sync loading done');
console.log('-----------');



// Async, Wrong way:
var asyncData = '';
timer.start();
console.log('readFile async:')
fs.readFile('sudoku-data', {encoding: 'utf8'}, function (err, data) {
asyncData = data;
timer.check('async loading done');
console.log('-----------');
});
console.log('Data:',asyncData);
timer.check('Code moving ahead...');
// // Async, Wrong way:
// var asyncData = '';
// timer.start();
// console.log('readFile async:')
// fs.readFile('sudoku-data', {encoding: 'utf8'}, function (err, data) {
// asyncData = data;
// timer.check('async loading done');
// console.log('-----------');
// });
// console.log('Data:',asyncData);
// timer.check('Code moving ahead...');



Expand Down
10 changes: 7 additions & 3 deletions grid.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
var DigitSet = require('./digitset');

var GridFactory = (function() {
function Grid(initstr) {

}
return Grid;
})();

function Grid(initstr) {
// your code here
}

console.log(Grid);// for testing only
var Grid = new GridFactory();

module.exports = Grid;
18 changes: 18 additions & 0 deletions grid_constructor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var testStr = require('./main');
var Viewer = require('./viewer-display');
var ObjectFactory = require('./digitSet_Constructor');
var currentStr = require('./updater');


var cellSet = [];
var newStr = testStr;
var GridFactory = function() {

for (var id = 0; id < 81; id ++ ) {
cellSet[id] = new ObjectFactory(id);
}
};



module.exports = GridFactory;
29 changes: 22 additions & 7 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
var Grid = require('./grid.js');
var Viewer = require('./viewer.js');
var GridFactory = require('./grid_constructor');
var Viewer = require('./viewer-display');
var ObjectFactory = require('./digitSet_Constructor');
var currentStr = require('./updater');

var testStr = '158.2..6.2...8..9..3..7.8.2.6.74......4.6.7......19.5.4.9.3..2..2..5...8.7..9.413';
// consider loading strings from file instead...
var testStr = '7...2......9.3....4.8..1..7.......8....627..5....45..95.3..4.....2...6......7.9.8';
var cellSet = [];

var game = new Grid(testStr);
//function that changes value of currentStr;
var changeCellValue = function(newStr) {
currentStr = newStr;
};

var viewer = new Viewer(game);
viewer.show();
//Display the string:
// Viewer(testStr);


//testing potential values
ObjectFactory();
var values = new ObjectFactory(1);
var old = values.getRow("1");
console.log(values.potential());


module.exports = testStr;
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "sudoku_solver",
"version": "1.0.0",
"name": "CommitSudoku",
"version": "0.0.1",
"description": "A modular sudoku solver project",
"main": "",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"async": "node extras/async-demo.js"
},
"author": "Dan Bauer",
"author": "Matt Kelley",
"license": "http://creativecommons.org/licenses/by-nc-sa/4.0/",
"dependencies": {
"lodash": "^3.10.1",
Expand Down
55 changes: 55 additions & 0 deletions viewer-display.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
var GridFactory = require('./grid_constructor');
var ObjectFactory = require('./digitSet_Constructor');
var currentStr = require('./updater');
var testStr = require('./main');

var cellSet = [];
var Viewer = function(str){
function splitter(str){
var x = str.split('');
var arr = [];
for(var i = 0; i < x.length; i++){
if (i === 0){
arr.push(str[i]);
}
else if(i % 3 === 0){
arr.push('*');
arr.push(str[i]);

}else {
arr.push(str[i]);
}
}
arr = arr.join('').split('*');
return arr;
}

spltArray = (splitter(str));


var plot = spltArray;
function joiner(str){
arr=[];
y = str.split("");
x = y.join(" ");
return x;
}
var arr = plot.map(joiner);
var view = ("+-------+-------+-------+" + "\n"
+"| "+arr[0]+" | "+arr[1]+" | "+arr[2]+" |"+"\n"
+"| "+arr[3]+" | "+arr[4]+" | "+arr[5]+" |"+"\n"
+"| "+arr[6]+" | "+arr[7]+" | "+arr[8]+" |"+"\n"
+"+-------+-------+-------+" + "\n"
+"| "+arr[9]+" | "+arr[10]+" | "+arr[11]+" |"+"\n"
+"| "+arr[12]+" | "+arr[13]+" | "+arr[14]+" |"+"\n"
+"| "+arr[15]+" | "+arr[16]+" | "+arr[17]+" |"+"\n"
+"+-------+-------+-------+" + "\n"
+"| "+arr[18]+" | "+arr[19]+" | "+arr[20]+" |"+"\n"
+"| "+arr[21]+" | "+arr[22]+" | "+arr[23]+" |"+"\n"
+"| "+arr[24]+" | "+arr[25]+" | "+arr[26]+" |"+"\n"
+"+-------+-------+-------+")
console.log(view);
};


module.exports = Viewer;
74 changes: 71 additions & 3 deletions viewer.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,80 @@
// no need for grid module

function SudokuViewer(grid) {
function SudokuViewer(str) {
// your code here


function splitter(str){
var x = str.split('');
var arr = [];
for(var i = 0; i < x.length; i++){
if (i === 0){
arr.push(str[i]);
}
else if(i % 3 === 0){
arr.push('*');
arr.push(str[i]);

}else {
arr.push(str[i]);
}
}
arr = arr.join('').split('*');
return arr;
}

spltArray = (splitter(str));

function columns(arr){
colm = [];
for(var x = 0; x < arr.length; x++){
colm.push('!');
for(var i = 0; i < arr.length; i++){
colm.push(arr[i][x]);
}
}
colm = colm.join('').split('!').slice(1);
}

columns(spltArray);

function blocks(row){
var block = [];
for (var i = 0; i < 3; i++) {
block.push(row[i]);
block.push(row[i+1]);
block.push(row[i+2]) ;
}
}

blocks(spltArray);
var plot = spltArray;
function joiner(str){
arr=[]
y = str.split("");
x = y.join(" ");
return x;
}
var arr = plot.map(joiner)
var view = console.log("+-------+-------+-------+" + "\n"
+"| "+arr[0]+" | "+arr[1]+" | "+arr[2]+" |"+"\n"
+"| "+arr[3]+" | "+arr[4]+" | "+arr[5]+" |"+"\n"
+"| "+arr[6]+" | "+arr[7]+" | "+arr[8]+" |"+"\n"
+"+-------+-------+-------+" + "\n"
+"| "+arr[9]+" | "+arr[10]+" | "+arr[11]+" |"+"\n"
+"| "+arr[12]+" | "+arr[13]+" | "+arr[14]+" |"+"\n"
+"| "+arr[15]+" | "+arr[16]+" | "+arr[17]+" |"+"\n"
+"+-------+-------+-------+" + "\n"
+"| "+arr[18]+" | "+arr[19]+" | "+arr[20]+" |"+"\n"
+"| "+arr[21]+" | "+arr[22]+" | "+arr[23]+" |"+"\n"
+"| "+arr[24]+" | "+arr[25]+" | "+arr[26]+" |"+"\n"
+"+-------+-------+-------+")
return view;
}
SudokuViewer.prototype.show = function() {
console.log('Sudoku appears here!');
}

console.log(SudokuViewer);// for testing only
//
// console.log(SudokuViewer);// for testing only

module.exports = SudokuViewer;