-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
281 lines (249 loc) · 8.98 KB
/
sketch.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
let cells = [];
let stage = 1;
let backgroundColor = 0;
let cellColor = 190;
let valueColor = 64;
let fontSize = 12;
let historyTemp = [];
let cellHistory = [];
let input;
let inputButton;
function setup() {
let smallSquareSide = 25;
let medBorder = 1;
let largeBorder = 5;
let smallSquaresPerMedSquareX = 3;
let smallSquaresPerMedSquareY = 3;
let medSquaresPerLargeSquareX = 3;
let medSquaresPerLargeSquareY = 3;
let largeSquaresX = 3;
let largeSquaresY = 3;
let totalX = calculateTotalSize(smallSquareSide, medBorder, largeBorder, largeSquaresX, smallSquaresPerMedSquareX, medSquaresPerLargeSquareX);
let totalY = calculateTotalSize(smallSquareSide, medBorder, largeBorder, largeSquaresY, smallSquaresPerMedSquareY, medSquaresPerLargeSquareY);
createCanvas(totalX, totalY);
background(backgroundColor);
fontSize = smallSquareSide * 2;
let maxX = 0;
let maxY = 0;
noStroke();
for (let l = 0; l < largeSquaresX; l++) {
for (let m = 0; m < largeSquaresY; m++) {
for (let i = 0; i < medSquaresPerLargeSquareX; i++) {
for (let j = 0; j < medSquaresPerLargeSquareY; j++) {
let w = smallSquaresPerMedSquareX * smallSquareSide;
let h = smallSquaresPerMedSquareY * smallSquareSide;
let x = largeBorder + (i * w) + (i * medBorder);
let y = largeBorder + (j * h) + (j * medBorder);
fill(cellColor);
rect((l * maxX) + x, (m * maxY) + y, w, h);
let nx = (l * maxX) + x;
let ny = (m * maxY) + y;
// sub grids
let subGrids = [];
// grid
let grid = new Grid(nx, ny, w, h);
// cell
let row = i + (medSquaresPerLargeSquareX * l);
let col = j + (medSquaresPerLargeSquareY * m);
let cell = new Cell(row, col, grid, subGrids);
cells.push(cell);
for (let n = 0; n < smallSquaresPerMedSquareX; n++) {
for (let o = 0; o < smallSquaresPerMedSquareY; o++) {
let sx = nx + (n * smallSquareSide);
let sy = ny + (o * smallSquareSide);
// fill(32);
// rect(sx, sy, smallSquareSide, smallSquareSide);
let sg = new Grid(sx, sy, smallSquareSide, smallSquareSide);
let value = findBox(n * 3, o * 3) + 1;
let button = cellButton(value, sx, sy, smallSquareSide, smallSquareSide);
button.mousePressed(() => setCellValue(cell, value));
sg.button = button;
sg.show = true;
sg.value = value;
subGrids.push(sg);
}
}
if (i === medSquaresPerLargeSquareX - 1) maxX = x + w;
if (j === medSquaresPerLargeSquareY - 1) maxY = y + h;
}
}
}
}
cells = cells.sort((a, b) => parseInt(a.id) - parseInt(b.id));
input = createInput();
input.position(10, totalY + 10);
inputButton = createButton("SUBMIT");
inputButton.position(input.x + input.width, input.y);
inputButton.mousePressed(submitValues);
}
function draw() {
let rerun = true;
findSingleValues(); // do always
let completedCells = cells.filter(c => c.value).length;
if (findHiddenValues()) { // continue if no changes made
if (reduceLocations(completedCells)) { // continue if no changes made
if (reduceGroups(completedCells)) { // continue if no changes made
rerun = false; // do not rerun if no changes made
}
}
}
displayGrid();
if (!rerun) {
cellHistory.push(historyTemp);
historyTemp = [];
noLoop();
console.log(completedCells === 81 ? "finished!" : "stuck!");
}
}
function submitValues() {
input.hide();
inputButton.hide();
let submittedFromInput = true;
let inputIndex = 0;
while (submittedFromInput) {
let v = input.value()[inputIndex];
let vInt = parseInt(v);
let cell = cells[inputIndex];
if (vInt && vInt > 0 && vInt < 10) setCellValue(cell, vInt);
inputIndex++;
if (input.value().length === inputIndex) submittedFromInput = false;
}
}
function cellButton(value, x, y, h, w) {
let button = createButton(value);
button.position(x, y);
button.size(h, w);
button.style("background-color", "transparent");
button.style("border", "none");
return button;
}
function setCellValue(cellPressed, value) {
historyTemp.push(cellPressed.id);
//console.log("Setting cell", cellPressed.id, "to value", value);
cellPressed.setValue(value);
for (const cell of cells) {
if (cell.id === cellPressed.id) continue;
if (!cellInRowColBox(cellPressed, cell)) continue;
cell.removePossibleValue(value);
}
loop();
}
function findSingleValues() {
let changeMade = false;
for (const cell of cells) {
let value = cell.getSingleValue();
if (!value) continue;
setCellValue(cell, value);
changeMade = true;
break;
}
return !changeMade;
}
function findHiddenValues() {
let changeMade = false;
for (const cell of cells) {
let value = cell.getHiddenValue(cells);
if (!value) continue;
setCellValue(cell, value);
changeMade = true;
break;
}
return !changeMade;
}
function reduceLocations(completedCells) {
if (completedCells < 20) return true;
let changeMade = false;
let filterItems = [["row", "box"], ["col", "box"], ["box", "row"], ["box", "col"]];
for (const filterItem of filterItems) {
let filter = filterItem[0];
let subFilter = filterItem[1];
for (let filterValue = 0; filterValue < 9; filterValue++) {
let filteredCells = filterCells(cells, filter, filterValue);
for (let v = 1; v < 10; v++) {
let trackedContainer = -1;
for (const cell of filteredCells) {
if (cell.value && cell.value === v) break;
if (cell.value) continue;
if (!binaryValueContains(cell.binaryPossibilities, v)) continue;
let subFilterValue = subFilter === "row" ? cell.row : subFilter === "col" ? cell.col : cell.box;
if (trackedContainer === -1) trackedContainer = subFilterValue;
if (trackedContainer === subFilterValue) continue;
trackedContainer = -1;
break;
}
if (trackedContainer === -1) continue;
let subFilteredCells = filterCells(cells, subFilter, trackedContainer);
for (const cell of subFilteredCells) {
let filterValueSub = filter === "row" ? cell.row : filter === "col" ? cell.col : cell.box;
if (filterValueSub === filterValue) continue;
if (cell.value) continue;
if (!binaryValueContains(cell.binaryPossibilities, v)) continue;
cell.removePossibleValue(v);
changeMade = true;
}
if (changeMade) break;
}
if (changeMade) break;
}
if (changeMade) break;
}
return !changeMade;
}
function reduceGroups(completedCells) {
if (completedCells < 20) return true;
let changeMade = false;
let filters = ["row", "col", "box"];
for (const filter of filters) {
for (let filterValue = 0; filterValue < 9; filterValue++) {
let filteredCells = filterCells(cells, filter, filterValue);
for (const cell of filteredCells) {
if (cell.value) continue;
let possibleValues = possibleValuesFromBinary(cell.binaryPossibilities);
if (possibleValues > 4) continue; // performance limit
let matchedCellIds = [];
for (const cellToCompare of filteredCells) {
if (cellToCompare.value) continue;
if (!binaryValueContainsOtherBinary(cell.binaryPossibilities, cellToCompare.binaryPossibilities)) continue;
matchedCellIds.push(cellToCompare.id);
if (matchedCellIds.length > possibleValues.length) break;
}
if (matchedCellIds.length !== possibleValues.length) continue;
let cellsToChange = filteredCells.filter(c => matchedCellIds.indexOf(c.id) < 0);
for (const cellToChange of cellsToChange) {
if (cellToChange.value) continue;
for (const v of possibleValues) {
if (!binaryValueContains(cellToChange.binaryPossibilities, v)) continue;
cellToChange.removePossibleValue(v);
changeMade = true;
}
}
if (changeMade) break;
}
if (changeMade) break;
}
if (changeMade) break;
}
return !changeMade;
}
function displayGrid() {
noStroke();
fill(valueColor);
textSize(fontSize);
textAlign(CENTER, CENTER);
for (const cell of cells) {
if (cell.value) {
let grid = cell.grid;
text(grid.value, grid.x + (grid.width / 2), grid.y + (grid.height / 2));
}
for (const subGrid of cell.subGrids) {
if (!subGrid.show) subGrid.button.hide();
}
}
}
function calculateTotalSize(smallSquareSide, medBorder, largeBorder, largeSquares, smallSquaresPerMedSquare, medSquaresPerLargeSquare) {
let medBorderCount = (medSquaresPerLargeSquare - 1) * largeSquares;
let largeBorderCount = largeSquares + 1;
let smallSquareCount = smallSquaresPerMedSquare * medSquaresPerLargeSquare * largeSquares;
let total = (medBorderCount * medBorder) + (largeBorderCount * largeBorder) + (smallSquareCount * smallSquareSide);
return total;
}