Skip to content

Commit 1ed6c48

Browse files
committed
Now save color of cells.
Backward compatibility with previous save encoding. Arrow keys now skip over clues during navigation.
1 parent ffa861a commit 1ed6c48

File tree

5 files changed

+196
-180
lines changed

5 files changed

+196
-180
lines changed

Encoding.js

Lines changed: 70 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,7 @@ function encode(input) {
44
var num;
55
var factor;
66
var str;
7-
8-
// cells.getColorIndex("red");
9-
// cells.getColorValue(4);
10-
// cells.blankCellColor
11-
// currentCell.element.classList
12-
7+
var encodingList = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];
138

149
for (var i = 0; i < 81; i++) {
1510
currentCell = cells[i];
@@ -29,17 +24,24 @@ function encode(input) {
2924
num += factor * (currentCell.isGiven ? 1 : 0);
3025
factor *= 2;
3126

32-
str = num.toString(36);
27+
num += factor * currentCell.getColor();
28+
factor *= 5;
29+
30+
str = encodingList[num / 3844 | 0];
31+
str += encodingList[(num / 62 | 0) % 62];
32+
str += encodingList[num % 62];
3333

3434
while (str.length < 3) {
3535
str = "0" + str;
3636
}
3737

3838
result += str;
3939
}
40+
41+
result += cells.blankCellColor;
42+
4043
return result;
4144

42-
4345
}
4446

4547
function decode(input) {
@@ -48,16 +50,31 @@ function decode(input) {
4850
var str;
4951
var num;
5052
var factor;
53+
var values = "";
54+
var decodingList = {"0":0, "1":1, "2":2, "3":3, "4":4, "5":5, "6":6, "7":7, "8":8, "9":9, "A":10, "B":11, "C":12, "D":13, "E":14, "F":15, "G":16, "H":17, "I":18, "J":19, "K":20, "L":21, "M":22, "N":23, "O":24, "P":25, "Q":26, "R":27, "S":28, "T":29, "U":30, "V":31, "W":32, "X":33, "Y":34, "Z":35, "a":36, "b":37, "c":38, "d":39, "e":40, "f":41, "g":42, "h":43, "i":44, "j":45, "k":46, "l":47, "m":48, "n":49, "o":50, "p":51, "q":52, "r":53, "s":54, "t":55, "u":56, "v":57, "w":58, "x":59, "y":60, "z":61};
55+
var colorsEncoded = true;
56+
57+
input = input.replace(/[^0-9A-Za-z]/g, "")
5158

52-
if (input.length !== 243) {
59+
if (input.length !== 243 && input.length !== 244) {
60+
alert("Invalid input.");
5361
return;
5462
}
5563

64+
colorsEncoded = input.length === 244;
65+
5666
for (var i = 0; i < 81; i++) {
5767
currentCell = cells[i];
5868

5969
factor = 1;
60-
num = parseInt(input.slice(i * 3, i * 3 + 3), 36);
70+
71+
if (colorsEncoded) {
72+
num = decodingList[input.charAt(i * 3)] * 3844;
73+
num += decodingList[input.charAt(i * 3 + 1)] * 62;
74+
num += decodingList[input.charAt(i * 3 + 2)];
75+
} else {
76+
num = parseInt(input.slice(i * 3, i * 3 + 3), 36);
77+
}
6178

6279
for (var j = 0; j < 9; j++) {
6380
factor = 2;
@@ -69,28 +86,68 @@ function decode(input) {
6986

7087
factor = 10;
7188
currentCell.setValue(num % factor);
89+
values += num % factor + "";
7290
num = (num / factor) | 0;
7391

7492
factor = 2;
7593
currentCell.isGiven = !!(num % factor);
7694
num = (num / factor) | 0;
7795

96+
factor = 5;
97+
currentCell.setColor(num % factor);
98+
num = (num / factor) | 0;
99+
78100
if (currentCell.isGiven) {
79101
currentCell.element.innerHTML = currentCell.value;
102+
currentCell.candidatesElement.innerHTML = currentCell.value;
80103
} else {
81104
if (currentCell.value) {
82105
currentCell.element.firstChild.value = currentCell.value;
106+
currentCell.candidatesElement.innerHTML = currentCell.value;
83107
} else {
108+
// Update pencilmarks display.
109+
currentCell.candidatesElement.innerHTML = currentCell.getPencilmarkString();
84110
currentCell.element.firstChild.value = "";
85111

86112
}
87113
}
88114

89-
// Update pencilmarks display.
90-
currentCell.candidatesElement.innerHTML = currentCell.getPencilmarkString();
115+
}
116+
117+
cells.blankCellColor = parseInt(input.charAt(243));
118+
119+
// Highlight selected color link.
120+
document.getElementById("mark_blank_cells_0").style.backgroundColor = "";
121+
document.getElementById("mark_blank_cells_" + cells.blankCellColor).style.backgroundColor = "#FF8";
122+
123+
124+
var puzzle = new Puzzle(values);
125+
126+
if (!puzzle.hasSolution()) {
127+
alert("There is no solution for the inputted values.");
128+
return;
91129
}
92130

93-
submitGivens();
131+
document.getElementById("top_bar").style.display = "none";
132+
document.getElementById("begin_solving").style.display = "none";
133+
document.getElementById("controls").style.display = "block";
134+
document.getElementById("grid_table").style.position = "static";
135+
document.getElementById("grid_table").style.marginTop = "8px";
136+
document.getElementById("grid_table").style.marginLeft = "8px";
137+
document.getElementById("side_bar").style.display = "none";
138+
139+
document.getElementById("import_save_button").title = "This option is disabled when a sudoku puzzle is in progress.";
140+
document.getElementById("import_save_button").disabled = true;
141+
document.getElementById("paste_puzzle_button").title = "This option is disabled when a sudoku puzzle is in progress.";
142+
document.getElementById("paste_puzzle_button").disabled = true;
143+
document.getElementById("load_cookie").title = "This option is disabled when a sudoku puzzle is in progress.";
144+
document.getElementById("load_cookie").disabled = true;
145+
146+
cells.solution = puzzle.toString();
147+
for (var i = 0; i < 81; i++) {
148+
cells[i].solution = parseInt(cells.solution.charAt(i));
149+
}
150+
94151
showDuplicates();
95152
}
96153

Puzzle.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -573,8 +573,6 @@ function Puzzle(givens) {
573573
};
574574

575575

576-
// Returns the solution that occurs first in lexicographical order to the puzzle if firstSolution is true.
577-
// Otherwise returns the solution that occurs last in lexicographical order.
578576
var solve = function solve(givens) {
579577
var stack = [];
580578
var values;
@@ -599,7 +597,6 @@ function Puzzle(givens) {
599597
}
600598
}
601599

602-
console.log("Returned givens.");
603600
return givens;
604601
};
605602

Sudoku.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ a:active {
116116
}
117117

118118
#side_bar {
119-
width: 450px;
119+
width: 460px;
120120
z-index: 2;
121121
position: fixed;
122122
top: 0px;

0 commit comments

Comments
 (0)