Skip to content

Commit 0439abb

Browse files
committed
Save puzzles with a cookie.
Added sidebar for saving options.
1 parent 8f5c010 commit 0439abb

File tree

5 files changed

+159
-39
lines changed

5 files changed

+159
-39
lines changed

Encoding.js

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ function encode(input) {
3636
}
3737

3838
result += str;
39-
// console.log(result);
40-
//
4139
}
4240
return result;
4341

@@ -51,35 +49,30 @@ function decode(input) {
5149
var num;
5250
var factor;
5351

52+
if (input.length !== 243) {
53+
return;
54+
}
55+
56+
submitGivens();
57+
5458
for (var i = 0; i < 81; i++) {
5559
currentCell = cells[i];
5660

5761
factor = 1;
5862
num = parseInt(input.slice(i * 3, i * 3 + 3), 36);
59-
// num = 35;
60-
61-
62-
63-
// console.log(num);
64-
65-
6663

6764
for (var j = 0; j < 9; j++) {
6865
factor = 2;
6966
currentCell.pencilmarks[j] = !!(num % factor);
7067

71-
// console.log(num + " " + num % factor);
72-
7368
num = (num / factor) | 0;
7469

7570
}
7671

7772
factor = 10;
7873
currentCell.setValue(num % factor);
79-
// console.log(num + " " + currentCell.value + " " + currentCell.candidatesElement.innerHTML);
8074
num = (num / factor) | 0;
8175

82-
// console.log(num);
8376
factor = 2;
8477
currentCell.isGiven = !!(num % factor);
8578
num = (num / factor) | 0;
@@ -94,26 +87,29 @@ function decode(input) {
9487

9588
}
9689
}
97-
// currentCell.value = num % factor;
98-
// num -= factor * ;
99-
100-
// console.log(num);
101-
10290

10391
// Update pencilmarks display.
10492
currentCell.candidatesElement.innerHTML = currentCell.getPencilmarkString();
10593
}
10694

10795
}
10896

109-
// window.onmousewheel = function() {
110-
// var testStr = "01j00x01j6rk01m01m77k0187pc0136rk01300e7pc04q01b5xs01b08m0bk5jc00m04y4ch02u02s01203d55003500q00r7pc02y06g04q03c4qq7pc77k00o55002w4ch01477k00901t6cg00b0225xs09800e08e5xs0124ch04q04q08c0bw6rk08d08901107s6rk05408d4qq0590a308977k07u55001m08908801l";
111-
// var testStr = "5jc5xs01s4qq01007809w4ch77k5500764ch08g04w5jc09u08w09u6cg07603k0746rk4ch07a07g5xs5xs77k4qq0940e96cg09009008w7pc4ch6cg55000i01u02877k5jc2tc00c00c77k00g07408l4qq08139c6rk00k00x7pc0065jc00g0034qq00c7pc01t03o5xs05d6cg01t4ch6cg00g01s5jc05e0cy09c550";
97+
function loadFromCookie() {
98+
var values = document.cookie.replace(/(?:(?:^|.*;\s*)state\s*\=\s*((?:[^;](?!;))*[^;]?).*)|.*/, '$1');
99+
100+
if (values) {
101+
decode(values);
102+
} else {
103+
alert("No current save exists.");
104+
}
105+
}
112106

113-
// console.log(encode());
114-
// decode(testStr);
107+
function saveToCookie() {
108+
var values = document.cookie.replace(/(?:(?:^|.*;\s*)state\s*\=\s*((?:[^;](?!;))*[^;]?).*)|.*/, '$1');
115109

116-
// console.log(cells[0].pencilmarks);
117110

111+
if (values && confirm("A save already exists. Are you sure you want to overwrite it?")) {
112+
document.cookie = 'state=' + encode() + '; expires=Fri, 31 Dec 9999 23:59:59 GMT';
113+
}
118114

119-
// }
115+
}

Puzzle.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,3 +676,6 @@ function Puzzle(givens) {
676676
}
677677
// Initialize cells.
678678
} */
679+
680+
681+
// d.cookie = 'labelWidth=' + w + '; expires=Fri, 31 Dec 9999 23:59:59 GMT"'

Sudoku.css

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ a:active {
3232
position: absolute;
3333
top: 0px;
3434
width: 100%;
35+
min-width: 675px;
3536
}
3637

3738
#top_bar ul {
@@ -69,7 +70,7 @@ a:active {
6970
margin-right: 0px;
7071
margin-bottom: 0px;
7172
margin-left: 8px;
72-
width: 400px;
73+
width: 425px;
7374
}
7475

7576
#begin_solving {
@@ -116,6 +117,27 @@ a:active {
116117
padding: 0px;
117118
}
118119

120+
#side_bar {
121+
width: 450px;
122+
z-index: 2;
123+
position: fixed;
124+
top: 0px;
125+
right: -450px;
126+
height: 100%;
127+
background-color: #AAA;
128+
/* cursor: w-resize; */
129+
overflow: auto;
130+
}
131+
132+
#side_bar_close {
133+
position: absolute;
134+
top: 6px;
135+
right: 6px;
136+
font-weight: bold;
137+
font-family: Arial, sans-serif;
138+
font-size: 10pt;
139+
}
140+
119141
.cell {
120142
border-bottom-style: none;
121143
border-color: black;

Sudoku.js

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,12 @@ function clearCells() {
220220
}
221221

222222
function pastePuzzle() {
223-
var values = window.prompt("Enter a sudoku puzzle. Use \"0\", \".\", \"*\", or \"_\" for empty cells.\nAll other characters are ignored.\nAlternatively, load a previously saved puzzle.");
223+
// var values = window.prompt("Enter a sudoku puzzle. Use \"0\", \".\", \"*\", or \"_\" for empty cells.\nAll other characters are ignored.\nAlternatively, load a previously saved puzzle.");
224+
var values = document.getElementById("paste_puzzle").value;
224225

226+
if (!values) {
227+
return;
228+
}
225229

226230
if (values.length === 81) {
227231
values = values.replace(/[.*_]/g, "0");
@@ -261,6 +265,13 @@ function savePuzzle() {
261265

262266
}
263267

268+
function saveLoadPuzzle() {
269+
document.getElementById('side_bar').style.right='0px';
270+
271+
document.getElementById("copy_puzzle").value = cells.toString().replace(/[^0-9]/g, "");
272+
document.getElementById("export_save").value = encode();
273+
}
274+
264275
function highlight(value) {
265276
if (value === 0) {
266277
for (var i = 0; i < 81; i++) {
@@ -311,6 +322,14 @@ function submitGivens() {
311322
document.getElementById("grid_table").style.position = "static";
312323
document.getElementById("grid_table").style.marginTop = "8px";
313324
document.getElementById("grid_table").style.marginLeft = "8px";
325+
document.getElementById("side_bar").style.right = "-450px";
326+
327+
document.getElementById("import_save_button").title = "This option is disabled when a sudoku puzzle is in progress.";
328+
document.getElementById("import_save_button").disabled = true;
329+
document.getElementById("paste_puzzle_button").title = "This option is disabled when a sudoku puzzle is in progress.";
330+
document.getElementById("paste_puzzle_button").disabled = true;
331+
document.getElementById("load_cookie").title = "This option is disabled when a sudoku puzzle is in progress.";
332+
document.getElementById("load_cookie").disabled = true;
314333

315334
cells.solution = puzzle.toString();
316335
for (var i = 0; i < 81; i++) {
@@ -628,5 +647,37 @@ window.onload = function() {
628647
}
629648

630649
updatePencilmarks();
631-
650+
651+
652+
// var sideBar = document.body.appendChild(document.createElement("div"));
653+
// var sideBarDragPos = 0;
654+
655+
// sideBar.id = "side_bar";
656+
// sideBar.style.width = "450px";
657+
658+
// sideBar.onmousedown = function() {
659+
// sideBarDragPos = window.event.clientX;
660+
// }
661+
662+
// sideBar.onmousemove = function() {
663+
// if (sideBarDragPos) {
664+
// if (sideBarDragPos - window.event.clientX < 0) {
665+
// sideBar.style.right = (sideBarDragPos - window.event.clientX) + "px";
666+
// }
667+
// }
668+
// }
669+
670+
// sideBar.onmouseup = function() {
671+
// sideBar.style.right = -parseInt(sideBar.style.width) + "px";
672+
// sideBarDragPos = 0;
673+
// }
674+
675+
// var currentElement = sideBar.appendChild(document.createElement("div"));
676+
677+
// currentElement.className = "small_header";
678+
// currentElement.textContent = "Paste Puzzle:";
679+
680+
// currentElement = sideBar.appendChild(document.createElement("input"));
681+
// currentElement.type = "text";
682+
// currentElement.id = "paste_puzzle";
632683
};

index.html

Lines changed: 59 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,23 @@
1212
<ul>
1313
<li>To begin, find a sudoku puzzle and input its values below.</li>
1414
<li style="float: right;"><a href="help.html" target="_blank" title="Open the documentation page.">Help/About</a></li>
15-
<li style="float: right;" id="paste_puzzle"><a href="javascript:void(0);" onclick="pastePuzzle();" title="Paste values to place into the puzzle. Note: this overwrites existing values.">Paste puzzle</a></li>
16-
<li style="float: right;" id="save_puzzle"><a href="javascript:void(0);" onclick="savePuzzle();" title="Generate a text string that stores the current clues, values, and pencilmarks.">Save</a></li>
15+
<!-- <li style="float: right;"><a href="javascript:void(0);" onclick="pastePuzzle();" title="Paste values to place into the puzzle. Note: this overwrites existing values.">Paste puzzle</a></li> -->
16+
<!-- <li style="float: right;" id="save_puzzle"><a href="javascript:void(0);" onclick="savePuzzle();" title="Generate a text string that stores the current clues, values, and pencilmarks.">Save</a></li> -->
17+
<li style="float: right;" id="load_puzzle"><a href="javascript:void(0);" onclick="saveLoadPuzzle();" title="View options to save or load a sudoku puzzle.">Save/Load</a></li>
1718
</ul>
1819
</div>
19-
20+
2021
<table id="grid_table">
2122
<tbody></tbody>
2223
</table>
23-
24+
2425
<div id="controls">
2526
<form name="controls_form">
2627
<!-- <div class="small_header" id="solving_status" title="Enter the sudoku puzzle's clues and click SUBMIT CLUES to begin solving.">Entering Clues...</div> -->
2728
<div class="small_header"><a href="help.html" target="_blank" title="Open the documentation page.">Help/About</a></div>
2829
<div class="small_header"><a href="javascript:void(0);" title="Delete all cell values, reset pencil marks, and remove highlighting." onclick="clearCells();">Clear Cells</a></div>
29-
<div class="small_header"><a href="javascript:void(0);" title="Generate a text string that stores the current clues, values, and pencilmarks." onclick="savePuzzle();">Save</a></div>
30+
<!-- <div class="small_header"><a href="javascript:void(0);" title="Generate a text string that stores the current clues, values, and pencilmarks." onclick="savePuzzle();">Save</a></div> -->
31+
<div class="small_header"><a href="javascript:void(0);" title="View options to save or load a sudoku puzzle." onclick="saveLoadPuzzle();">Save/Load</a></div>
3032
<!-- <ul>
3133
<li>
3234
<input type="button" name="clear_cells" value="Clear cells" title="Delete all clues and values, reset pencil marks, and remove highlighting." onclick="clearCells();" />
@@ -36,7 +38,7 @@
3638
<input type="button" name="paste_puzzle" id="paste_puzzle" value="Paste puzzle" title="Paste values to place into the puzzle. Note: this overwrites existing values." onclick="pastePuzzle();" />
3739
</li>
3840
</ul> -->
39-
41+
4042
<div class="small_header">Cell Highlighting:</div>
4143
<ul>
4244
<li>
@@ -52,7 +54,7 @@
5254
<input type="button" name="highlight_none" value="Clear" class="highlight_button" title="Clear all highlighting." onclick="highlight(0);" />
5355
</li>
5456
</ul>
55-
57+
5658
<div class="small_header">Pencil Marks:</div>
5759
<ul>
5860
<li>
@@ -69,7 +71,7 @@
6971
<input type="button" name="reset_pencilmarks" value="Reset" title="Add all pencil marks to empty cells." onclick="clearPencilmarks();" />
7072
</li>
7173
</ul>
72-
74+
7375
<div class="small_header">Validation:</div>
7476
<ul>
7577
<li>
@@ -78,7 +80,7 @@
7880
</li>
7981
<li></li>
8082
</ul>
81-
83+
8284
<div class="small_header">Guessing tools:</div>
8385
<!-- <ul>
8486
<li>
@@ -121,7 +123,7 @@
121123
</tr>
122124
</tbody>
123125
</table>
124-
126+
125127
<div class="small_header">Solution/Hints:</div>
126128
<ul>
127129
<li>
@@ -134,9 +136,55 @@
134136

135137
</form>
136138
</div>
137-
139+
138140
<div id="begin_solving">
139141
<input type="button" value="Begin Solving" id="begin_solving_button" class="large_button" title="Submit the entered numbers as clues and begin solving the sudoku puzzle." onclick="submitGivens();" onmouseover="this.classList.add('large_button_hover'); this.classList.remove('large_button');" onmousedown="this.classList.add('large_button_hover'); this.classList.remove('large_button');" onmouseout="this.classList.add('large_button'); this.classList.remove('large_button_hover');" />
140142
</div>
143+
144+
<div id="side_bar">
145+
<div id="side_bar_close"><a href="javascript:void(0);" onclick="document.getElementById('side_bar').style.right='-450px';">Close</a></div>
146+
147+
<div class="small_header">Paste Puzzle:</div>
148+
<ul>
149+
<li>Enter a sudoku puzzle. Use &quot;0&quot;, &quot;.&quot;, &quot;*&quot;, or &quot;_&quot; for empty cells. All other characters are ignored. This overwrites existing values.</li>
150+
<li>
151+
<input type="text" id="paste_puzzle" style="width: 300px;" title="Paste values to place into the puzzle. Note: this overwrites existing values."/>
152+
<input type="button" id="paste_puzzle_button" value="Paste Puzzle" onclick="pastePuzzle();" title="Paste values to place into the puzzle. Note: this overwrites existing values." />
153+
</li>
154+
</ul>
155+
156+
<div class="small_header">Copy Puzzle:</div>
157+
<ul>
158+
<li>Copy the current values of the sudoku puzzle as an 81 digit text string.</li>
159+
<li>
160+
<input type="text" id="copy_puzzle" style="width: 300px;" title="Copy the current values of the sudoku puzzle." onclick="this.value = cells.toString().replace(/[^0-9]/g, ''); javascript:select();" />
161+
</li>
162+
</ul>
163+
164+
<div class="small_header">Export State:</div>
165+
<ul>
166+
<li>Copy this text string and paste it into a new text file to store the current state of the clues, values, and pencilmarks.</li>
167+
<li>
168+
<input type="text" id="export_save" style="width: 300px;" title="This text string stores the current state of the clues, values, and pencilmarks." onclick="this.value = encode(); javascript:select();" />
169+
</li>
170+
</ul>
171+
172+
<div class="small_header">Import State:</div>
173+
<ul>
174+
<li>Reload a saved sudoku puzzle by pasting the text string below.</li>
175+
<li>
176+
<input type="text" id="import_save" style="width: 300px;" title="Reload an previous save. Note: this overwrites existing values."/>
177+
<input type="button" value="Import Save" id="import_save_button" onclick="decode(document.getElementById('import_save').value);" title="Reload an previous save. Note: this overwrites existing values." />
178+
</li>
179+
</ul>
180+
181+
<div class="small_header">Save/Load Cookie:</div>
182+
<ul>
183+
<li>
184+
<input type="button" value="Save to Cookie" id="save_cookie" onclick="saveToCookie();" title="Save the current state of the clues, values, and pencilmarks to a cookie."/>
185+
<input type="button" value="Load from Cookie" id="load_cookie" onclick="loadFromCookie();" title="Load the sudoku puzzle saved to a cookie if one is present." />
186+
</li>
187+
</ul>
188+
</div>
141189
</body>
142190
</html>

0 commit comments

Comments
 (0)