-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
157 lines (136 loc) · 3.24 KB
/
script.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
let posibilities = {};
function setCellValue(cell, cellValue) {
document.getElementById(cell).value = cellValue;
}
function isSolved() {
let solved = true;
for (let cell = 1; cell < 82; cell++) {
if (getCellValue(cell) == '') {
solved = false;
}
}
return solved;
}
function getCellValue(cell) {
if (document.getElementById(cell).value) {
return parseInt((document.getElementById(cell).value || false));
} else {
return '';
}
}
function getAllValues() {
let values = {};
for (let cell = 1; cell < 82; cell++) {
values[cell] = getCellValue(cell);
}
return values;
}
function randomSudoku() {
return sudokuTestData[Math.floor(Math.random() * sudokuTestData.length)];
}
function fillData() {
let dataset = randomSudoku();
for (let cell = 1; cell < 82; cell++) {
if (dataset[cell - 1] != 0) {
setCellValue(cell, dataset[cell - 1]);
posibilities[cell] = dataset[cell - 1];
} else if (dataset[cell - 1] == 0) {
setCellValue(cell, '');
}
}
}
function getDependedRowCells(cell) {
let row = $(`#${cell}`).data('row');
let cells = [];
$(`.r${row}`).each(function() {
cells.push(parseInt(this.id));
});
return cells;
}
function getDependedColumnCells(cell) {
let column = $(`#${cell}`).data('column');
let cells = [];
$(`.c${column}`).each(function() {
cells.push(parseInt(this.id));
});
return cells;
}
function getDependedBlockCells(cell) {
let block = $(`#${cell}`).data('block');
let cells = [];
$(`.b${block}`).each(function() {
cells.push(parseInt(this.id));
});
return cells;
}
function getDependedCells(cell) {
let cells = getDependedBlockCells(cell);
getDependedRowCells(cell).forEach((r) => {
if (cells.indexOf(r) === -1) {
cells.push(r);
}
});
getDependedColumnCells(cell).forEach((c) => {
if (cells.indexOf(c) === -1) {
cells.push(c);
}
});
return cells;
}
function wildGuess() {
for (let cell = 1; cell < 82; cell++) {
if (posibilities[cell].length == 2) {
console.log(`fixing first element for ${cell} as ${posibilities[cell][0]}`);
setCellValue(cell, posibilities[cell][0]);
posibilities[cell] = [posibilities[cell][0]];
break;
}
}
}
function getPossibilities(cell) {
let poss = [1, 2, 3, 4, 5, 6, 7, 8, 9];
getDependedCells(cell).forEach((x) => {
if (getCellValue(x) != '') {
if (poss.indexOf(getCellValue(x)) > -1) {
poss.splice(poss.indexOf(getCellValue(x)), 1);
}
}
});
if (poss.length == 1) {
setCellValue(cell, poss[0]);
}
posibilities[cell] = poss;
return poss;
}
function updatePossibilites() {
for (let cell = 1; cell < 82; cell++) {
if (getCellValue(cell) == '') {
getPossibilities(cell);
}
}
}
$('#solve').on('click', function() {
for (let i = 0; i < 81; i++) {
if (isSolved()) {
console.log('solved...');
break;
} else {
console.log('updating possibilities...');
updatePossibilites();
}
}
});
$('#export').on('click', function() {
let dump = getAllValues();
console.log('Data Dump', dump);
});
$('#prefill').on('click', function() {
fillData();
});
$('#iterate').on('click', function() {
updatePossibilites();
console.log('posibilities', posibilities);
});
$('#wildguess').on('click', function() {
wildGuess();
});