Skip to content

Commit 004f531

Browse files
committed
Merge pull request #13 from rdubrock/master
Added deep copy to duple finder
2 parents 0b4ea18 + 114de64 commit 004f531

File tree

1 file changed

+34
-87
lines changed

1 file changed

+34
-87
lines changed

sudoku.js

Lines changed: 34 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ var checkBlock = require('./checkBlock.js');
88
var printer = require('./boardprinter.js');
99
var boardString = "8 2 5 97 4 25 1 9 2 7 96 3 1 52 6 1 8 4 73 7 65 3 8 9"
1010
var boardArray = boardString.split('');
11-
12-
var cycleCount = 0;
13-
var cycleCountLimit = 20;
11+
12+
var cycleCount = 0;
13+
var cycleCountLimit = 50;
1414
var masterCycleCount = 0;
15-
var masterCycleCountLimit = 200;
15+
var masterCycleCountLimit = 1000;
16+
1617
var arrayDepot = [];
1718
var arrayDepotIndex = 0;
1819

@@ -31,8 +32,9 @@ function findOptions(fullArray){
3132
var solvedSquares = 0;
3233
masterCycleCount = masterCycleCount + 1;
3334
if (masterCycleCount >= masterCycleCountLimit){
34-
return console.log('Master Cycle Limit Exceeded');
35-
}
35+
console.log('Master Cycle Limit Exceeded');
36+
37+
return }
3638
for (row=0; row<9; row++) {
3739
for (col=0; col<9; col++) {
3840
if (fullArray[row][col].value === null) {
@@ -68,81 +70,11 @@ function insertSingletonValue(fullArray){
6870
if (cycleCount === cycleCountLimit) {
6971
cycleCount = 0;
7072
console.log('calling findOptions from arrayDepot')
71-
7273
arrayDepotIndex = arrayDepotIndex + 1;
73-
var arrayToPass = arrayDepotIndex - 1;
74-
buildPrinterString(arrayDepot[arrayToPass]);
75-
findOptions(arrayDepot[arrayToPass]);
76-
return;
77-
78-
79-
};
80-
81-
for (row=0; row<9; row++) {
82-
for (col=0; col<9; col++) {
83-
if (fullArray[row][col].possibles.length === 1 && fullArray[row][col].value === null) {
84-
possibleLengths = 1;
85-
fullArray[row][col].value = fullArray[row][col].possibles[0];
86-
console.log('Singleton assigned at: ' + (col + 1) + ', ' + (9-row));
87-
console.log('Block is: ' + fullArray[row][col].block);
88-
console.log('Possibles Array for location is ' + fullArray[row][col].possibles[0]);
89-
buildPrinterString(fullArray);
90-
91-
92-
//after finding ONE singleton, display something
93-
if (possibleLengths === 0) {
94-
console.log('NO NEW POSSIBLES ARRAYS WITH A LENGTH OF 1');
95-
buildPrinterString(fullArray);
96-
insertDupleValues(fullArray);
97-
}
98-
99-
//after finding ONE singleton, find new options
100-
console.log("Calling findOptions after one singleton possibles added.")
101-
findOptions(fullArray);
10274

75+
buildPrinterString(arrayDepot[0]);
76+
findOptions(arrayDepot.shift());
10377

104-
}
105-
//solve for possibles array length of 2
106-
else if (fullArray[row][col].possibles.length === 2 && fullArray[row][col].value === null) {
107-
108-
//insert the first value of the 2 possibles
109-
fullArray[row][col].value = fullArray[row][col].possibles[0];
110-
console.log('Possibles=Two - Solving for Position Zero ');
111-
console.log('Singleton assigned at: ' + (col + 1) + ', ' + (9-row));
112-
console.log('Block is: ' + fullArray[row][col].block);
113-
console.log('Possibles Array for location is ' + fullArray[row][col].possibles[0] +
114-
" and " + fullArray[row][col].possibles[1] );
115-
buildPrinterString(fullArray);
116-
117-
console.log("Calling findOptions after one singleton possibles added.")
118-
findOptions(fullArray);
119-
}
120-
121-
122-
}
123-
}
124-
125-
126-
};
127-
128-
129-
//inserts all singleton values prior to recalculation?
130-
function insertSingletonValues(fullArray){
131-
var possibleLengths = 0;
132-
var row, col;
133-
cycleCount = cycleCount +1;
134-
135-
if (cycleCount === cycleCountLimit) {
136-
cycleCount = 0;
137-
console.log('calling findOptions from arrayDepot')
138-
139-
arrayDepotIndex = arrayDepotIndex + 1;
140-
var arrayToPass = arrayDepotIndex - 1;
141-
buildPrinterString(arrayDepot[arrayToPass]);
142-
findOptions(arrayDepot[arrayToPass]);
143-
return;
144-
145-
14678
};
14779

14880
for (row=0; row<9; row++) {
@@ -154,6 +86,7 @@ function insertSingletonValues(fullArray){
15486
console.log('Block is: ' + fullArray[row][col].block);
15587
console.log('Possibles Array for location is ' + fullArray[row][col].possibles[0]);
15688
buildPrinterString(fullArray);
89+
findOptions(fullArray);
15790
}
15891
}
15992
}
@@ -162,25 +95,26 @@ function insertSingletonValues(fullArray){
16295
buildPrinterString(fullArray);
16396
insertDupleValues(fullArray);
16497
}
165-
findOptions(fullArray);
16698
};
16799

168-
169-
170-
171-
172100
function insertDupleValues(fullArray){
173101
for (row=0; row<9; row++) {
174102
for (col=0; col<9; col++) {
175103
if (fullArray[row][col].possibles.length === 2 && fullArray[row][col].value === null) {
176-
fullArray[row][col].value = fullArray[row][col].possibles[1];
177-
arrayDepot.push(fullArray);
104+
105+
//clone fullArray
106+
var secondChoice = clone(fullArray);
107+
//set secondchoice value to SECOND possibility
108+
secondChoice[row][col].value = secondChoice[row][col].possibles[1];
109+
//send full Array clone to arrayDepot
110+
arrayDepot.push(secondChoice);
111+
//set fullArray value to FIRST possibility
178112
fullArray[row][col].value = fullArray[row][col].possibles[0];
179113
console.log('duple assigned at ' + (col + 1) + ', ' + (9-row));
180114
buildPrinterString(fullArray);
115+
console.log('Other dupel possible ');
116+
buildPrinterString(arrayDepot[arrayDepot.length-1]);
181117
findOptions(fullArray);
182-
183-
return;
184118
}
185119
}
186120
}
@@ -242,3 +176,16 @@ function boxDeletePossibles(possiblesList, callback){
242176
})
243177
};
244178

179+
function clone (existingArray) {
180+
var newObj = (existingArray instanceof Array) ? [] : {};
181+
for (i in existingArray) {
182+
if (i == 'clone') continue;
183+
if (existingArray[i] && typeof existingArray[i] == "object") {
184+
newObj[i] = clone(existingArray[i]);
185+
} else {
186+
newObj[i] = existingArray[i]
187+
}
188+
}
189+
return newObj;
190+
}
191+

0 commit comments

Comments
 (0)