-
Notifications
You must be signed in to change notification settings - Fork 0
/
sudokuPuzzle.js
78 lines (64 loc) · 1.63 KB
/
sudokuPuzzle.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import MasterList from './masterList.js';
export default class SudokuPuzzle {
constructor(){
this.finders = [];
this.rows = [];
this.columns = [];
this.groups = [];
this.masterList = new MasterList();
this.logging = false;
}
getCellById = id =>{
return this.masterList.cells[id];
}
addFinderAlgorithm(finder,toStart=0){
if(toStart === 0) {
this.finders.push(finder);
}else{
this.finders.unshift(finder)
}
this[finder.NAME] = finder;
}
removeFinderAlgorithm(toStart=0){
let finder;
if(toStart === 0) {
finder = this.finders.pop(finder);
}else{
finder = this.finders.shift(finder)
}
delete this[finder.NAME]
}
findPossible(){
this.finders.forEach(finder=>{
finder.findPossible(this,this.getCellById)
})
}
solved(){
const empty = []
this.masterList.forEach(cell=>{
if(cell.value === 0){
empty.push(cell);
}
})
return (empty.length === 0)
}
solveLayer(){
this.masterList.forEach(cell=>{
if(cell.potentials.size === 1){
let value = cell.potentials.values().next().value;
if(value !== 0){
cell.value = value
}
}
})
}
solvePuzzle(){
let runs = 0
while(this.solved() === false && runs < 50){
runs++
this.findPossible();
this.solveLayer();
}
return runs;
}
}