-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnono.js
More file actions
594 lines (437 loc) · 18.7 KB
/
nono.js
File metadata and controls
594 lines (437 loc) · 18.7 KB
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
var myPuzzle;
function play() {
var data = getUrlVars()['p'];
if (data == null) {//if no puzzle data found, pick a pregenerated one at random
var sampler = ["Rmx5IFRvZ2V0aGVyLEEgbWlnaHTEgmR1Y2ssTWF0xIhldyw4LDEwLMSkxKbEosSoxKfEpzHEqcSoxKzEq8SuxLHEr8SlxLTEsMSzxKrEtcSyxK3EtsS5xKTEt8SxxL7FgMS5xKzFhMS7xL3EtMS/xLjFhcS3", "S2Fma2EsQSBCdWcsTWF0dGhldywxMMSTxJXElyzElTHEmcScxJvEmMSfxJzEmsSTxKHEpMSYxJ7Eo8SnxKfEoMSbxKzEqMSjxKDEq8SvxKXEpMStxLXEssSwxJ3Et8SzxKLEtsStxLjEqcSyxKrEu8WDxJc=", "RGlhbCBJdCxNxIdhdHRoZXcsNSw2LDEsMMSWxJjElcSaxJnElMSdxJfElcSfxJ7EosSbxJfEpcSdxKDEmMSf", "RWthbnMsU25ha2UsTWF0dGhldywxMSw2LDDEmMSaxJnElcSZxJ7Em8SgxJ/ElcSjxKHEoMSdxJPEqMScxKnEq8SqxKTEn8StxKjEp8SqxK/EpsSxxKXEtMS0xLI="];
var x = Math.floor(Math.random() * (sampler.length));
data = sampler[x];
}
myPuzzle = decodePuzzle(data);
drawTable(myPuzzle);
}
function create() {
myPuzzle = samplePuzzle(true);
}
function arraysEqual(arr1, arr2) { //Steps through and tests if the arrays match
if (arr1.length !== arr2.length) return false;
for (var i = arr1.length; i--;) {
if (arr1[i] !== arr2[i]) return false;
}
return true;
}
function Puzzle(indata) { //Puzzle object
var puzzle = new Object();
puzzle.title = indata[0];
puzzle.answer = indata[1]; //String to be revealed after the puzzle is solved
puzzle.author = indata[2];
puzzle.width = parseInt(indata[3]);
puzzle.height = parseInt(indata[4]);
puzzle.data = new Array(indata.length - 5); //Actual puzzle data
puzzle.player = new Array(puzzle.data.length); //What the player has marked
var i = 5; //Offset the array index to account for previous entries
for (var x = 0; x < puzzle.data.length; x++) {
puzzle.data[x] = parseInt(indata[i]);
puzzle.player[x] = 0; //Fill in the player array with blanks while stepping through to read the data
i++;
}
puzzle.solved = false;
return puzzle;
}
function getUrlVars() { //Function to return data passed in from URL
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m, key, value) {
vars[key] = value;
});
return vars;
}
function readPuzzle(dec_data) { //Takes decoded data, returns puzzle object if valid
if (dec_data != undefined) {
var tempArr = dec_data.split(',');
if (tempArr.length == parseInt(tempArr[3]) * parseInt(tempArr[4]) + 5) { //Integrity check, see if the data array's length matches the stated height * width
return new Puzzle(tempArr);
}
}
return false;
}
function decodePuzzle(enc_data) { //Decodes data string, returns puzzle object
var temp = lzw_decode(Base64.decode(enc_data));
return readPuzzle(temp);
}
function encodePuzzle(puzzle) { //Takes puzzle object, returns encoded string
var c = puzzle.title + ',' + puzzle.answer + ',' + puzzle.author + ',' + puzzle.width + ',' + puzzle.height + ',' + puzzle.data.toString();
var temp = Base64.encode(lzw_encode(c));
return temp;
}
function samplePuzzle(blank) { //Generate a sample 5x7 puzzle, if true is passed in, returns a blank puzzle instead
var sampledata = ["Test Sample", "Sample Win", "Matty", 5, 7, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0];
if (blank) {
sampledata = ["", "", "", 1, 1, 0];
}
return (new Puzzle(sampledata));
}
function calcColInfo(puzzle) { //Generate the hint numbers for each column of data
var colInfos = new Array(puzzle.width);
var t = 0;
var cur = '';
for (var c = 0; c < puzzle.width; c++) { //For each column
for (var d = c; d < puzzle.width * puzzle.height; d += puzzle.width) { //Step though the data, incrementing by the width of the puzzle
t = 0;
while (puzzle.data[d] != '0' && d < puzzle.width * puzzle.height) { //Step through, but if the data is true add to count
d += puzzle.width;
t++;
}
if (t !== 0) {
cur += t + ' '; //If a block of data was true, add it to the string
}
t = 0; //reset count
}
if (cur === '') { // If no data found for that column, set the string to 0
cur = '0';
}
colInfos[c] = cur.trim(); //Add that column's hint string to the array
cur = '';
}
return colInfos;
}
function calcRowInfo(puzzle) { ///Generate the hint numbers for each row of data
//Similar to the column function, but a simpler process to step through the data
var rowInfos = new Array(puzzle.height);
var t = 0;
var cur = '';
for (var c = 0; c < puzzle.height; c++) { //For each row
for (var e = 0; e < puzzle.width; e++) { //For each item in a row
t = 0;
while (puzzle.data[(c * puzzle.width) + e] != '0' && e < puzzle.width) { //Increase count while stepping through if data found
t++;
e++;
}
if (t !== 0) {
cur += t + " ";
}
t = 0;
}
if (cur === '') {
cur = '0';
}
rowInfos[c] = cur.trim();
cur = '';
}
return rowInfos;
}
function tileClick(cell_num) { //Actions to take when player interacts with puzzle
if (!myPuzzle.solved) { //Do nothing if puzzle is solved.
var cell = document.getElementById('puzz' + cell_num);
if (cell.className == 'empty') { //Check what state the cell is in via it's DOM class
cell.setAttribute('class', 'marked'); //If empty, set it to marked
window.myPuzzle.player[cell_num] = 1; //and set the player data for that cell to true
}
else if (cell.className == 'marked') { //If marked, cycle through to anti
cell.setAttribute('class', 'anti'); //(superficial state to help the player mark cells that are definitely empty)
window.myPuzzle.player[cell_num] = 0; //player data should be false
}
else if (cell.className == 'anti') { //If anti, allow cycle back to empty (unsure state)
cell.setAttribute('class', 'empty');
window.myPuzzle.player[cell_num] = 0;
}
if (winTest()) { //Check if the player has finished
playerWin();
}
}
}
function tileClickCreate(cell_num) { //Action to take in create mode
var cell = document.getElementById('puzz' + cell_num);
//If cell isn't marked, mark it and set the data to true
if (cell.className == 'anti') {
cell.setAttribute('class', 'marked');
window.myPuzzle.data[cell_num] = 1;
}
//And if it is, reset it
else if (cell.className == 'marked') {
cell.setAttribute('class', 'anti');
window.myPuzzle.data[cell_num] = 0;
}
}
function generateLink() { //Generate the link when player is finished in create mode
//Grab the data from the inputs
var widthForm = document.getElementById("width");
myPuzzle.width = parseInt(widthForm.options[widthForm.selectedIndex].value);
var heightForm = document.getElementById("height");
myPuzzle.height = parseInt(heightForm.options[heightForm.selectedIndex].value);
var authorForm = document.getElementById("author");
myPuzzle.author = authorForm.value.replace(",", "");
var titleForm = document.getElementById("title");
myPuzzle.title = titleForm.value.replace(",", "");
var answerForm = document.getElementById("answer");
myPuzzle.answer = answerForm.value.replace(",", "");
var link = "http://cowie.me/nono/?p=" + encodePuzzle(myPuzzle);
var st = document.getElementById('status');
st.setAttribute('class', 'statusgood');
st.innerHTML = "<input type=text value=" + link + ">"; //Put the link in an input box to avoid overflow
}
function winTest() { //Test if the player's data matches the puzzle data
return arraysEqual(window.myPuzzle.data, window.myPuzzle.player);
}
function playerWin() { //Actions to do when the player has solved
myPuzzle.solved = true; //Set solved to true so the puzzle is inactive
var st = document.getElementById('status');
st.setAttribute('class', 'statusgood'); // Set the status area to positive style
st.innerHTML = "A Winner Is You! It's a " + myPuzzle.answer; //Reveal the answer of what the picture is supposed to be
}
function drawTable(puzzle) { // Set up the page and table from the puzzle data
//Display Puzzle information
var temp = document.getElementById('titlefield');
temp.appendChild(document.createTextNode(puzzle.title));
temp = document.getElementById('authorfield');
temp.appendChild(document.createTextNode('by ' + puzzle.author));
var cellcount = 0;
var row = new Array();
var cell = new Array();
var row_num = puzzle.height;
var cell_num = puzzle.width;
//Start the table element
var tab = document.createElement('table');
tab.setAttribute('id', 'puzzletable');
var tbo = document.createElement('tbody');
var colInfos = calcColInfo(puzzle); //Get the column hint data
var rowInfos = calcRowInfo(puzzle); //Get the row hint data
var colInfo = document.createElement('tr');
var topleft = document.createElement('td');
topleft.setAttribute('id', 'topleft');
colInfo.appendChild(topleft); //Skip the top left cell
for (var c = 0; c < cell_num; c++) { //Generate the top row with column hint data
cell[c] = document.createElement('td');
cell[c].setAttribute('class', 'colInfo'); //Set the class for styling
cell[c].setAttribute('id', 'col' + c); //Set unique ID
singleInfo = colInfos[c].split(' '); //Split the column hint data so line breaks can be inserted
for (var s = 0; s < singleInfo.length; s++) { //Step through and insert the breaks
cell[c].appendChild(document.createTextNode(singleInfo[s]));
cell[c].appendChild(document.createElement('br'));
}
colInfo.appendChild(cell[c]); //append the the cell to the row
}
tbo.appendChild(colInfo); //append the row to the table body
var rowInfo;
var cont;
for (c = 0; c < row_num; c++) { //For each row
row[c] = document.createElement('tr'); //Create row
rowInfo = document.createElement('td');
rowInfo.setAttribute('id', 'row' + c);
rowInfo.setAttribute('class', 'rowInfo');
rowInfo.appendChild(document.createTextNode(rowInfos[c]));
row[c].appendChild(rowInfo); //and append the row's hint data
for (var k = 0; k < cell_num; k++) { //Then for each puzzle cell in the row add a cell
cell[k] = document.createElement('td');
cont = document.createTextNode(puzzle.data[cellcount]);
cell[k].setAttribute('id', 'puzz' + cellcount); //set unique id
cell[k].setAttribute('onClick', 'tileClick(' + cellcount + ')'); //Trigger for interaction
cell[k].setAttribute('class', 'empty'); //Set the class to the unsure state
//cell[k].appendChild(cont); //Uncomment to display the array's data for that cell for debugging
row[c].appendChild(cell[k]); //append cell to row
cellcount++;
}
tbo.appendChild(row[c]); //append row to table body
}
var field = document.getElementById('puzzlefield');
tab.appendChild(tbo); //append table body to table
field.appendChild(tab); //append table to page at the puzzlefield div
}
function drawTableCreate() { //Draw the table in create mode
//Get the width and height
var cellcount = 0;
var widthForm = document.getElementById("width");
var width = parseInt(widthForm.options[widthForm.selectedIndex].value);
var heightForm = document.getElementById("height");
var height = parseInt(heightForm.options[heightForm.selectedIndex].value);
//Create a new array based on the w*h and fill it will zeroes to match blank table
myPuzzle.data = new Array(width * height);
for (var x = 0; x < myPuzzle.data.length; x++) {
myPuzzle.data[x] = 0;
}
var row = new Array();
var cell = new Array();
var row_num = height;
var cell_num = width;
var tab = document.createElement('table');
tab.setAttribute('id', 'puzzletable');
var tbo = document.createElement('tbody');
//Similar to drawTable() but no info cells, just the puzzle area..
for (c = 0; c < row_num; c++) {
row[c] = document.createElement('tr');
for (var k = 0; k < cell_num; k++) {
cell[k] = document.createElement('td');
cell[k].setAttribute('id', 'puzz' + cellcount);
cell[k].setAttribute('onClick', 'tileClickCreate(' + cellcount + ')'); //except call tileClickCreate for the action..
cell[k].setAttribute('class', 'anti'); //and start them as anti and not empty since we know they're blank
row[c].appendChild(cell[k]);
cellcount++;
}
tbo.appendChild(row[c]);
}
var field = document.getElementById('puzzlefield');
tab.appendChild(tbo);
field.innerHTML = '';
field.appendChild(tab);
}
function showInstructions() {
var inst = document.getElementById('instructions');
inst.style.display = 'block';
}
function hideInstructions() {
var inst = document.getElementById('instructions');
inst.style.display = 'none';
}
var Base64 = {
// private property
_keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
// public method for encoding
encode: function(input) {
var output = "";
var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
var i = 0;
input = Base64._utf8_encode(input);
while (i < input.length) {
chr1 = input.charCodeAt(i++);
chr2 = input.charCodeAt(i++);
chr3 = input.charCodeAt(i++);
enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;
if (isNaN(chr2)) {
enc3 = enc4 = 64;
}
else if (isNaN(chr3)) {
enc4 = 64;
}
output = output + Base64._keyStr.charAt(enc1) + Base64._keyStr.charAt(enc2) + Base64._keyStr.charAt(enc3) + Base64._keyStr.charAt(enc4);
}
return output;
},
// public method for decoding
decode: function(input) {
var output = "";
var chr1, chr2, chr3;
var enc1, enc2, enc3, enc4;
var i = 0;
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
while (i < input.length) {
enc1 = Base64._keyStr.indexOf(input.charAt(i++));
enc2 = Base64._keyStr.indexOf(input.charAt(i++));
enc3 = Base64._keyStr.indexOf(input.charAt(i++));
enc4 = Base64._keyStr.indexOf(input.charAt(i++));
chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;
output = output + String.fromCharCode(chr1);
if (enc3 != 64) {
output = output + String.fromCharCode(chr2);
}
if (enc4 != 64) {
output = output + String.fromCharCode(chr3);
}
}
output = Base64._utf8_decode(output);
return output;
},
// private method for UTF-8 encoding
_utf8_encode: function(string) {
string = string.replace(/\r\n/g, "\n");
var utftext = "";
for (var n = 0; n < string.length; n++) {
var c = string.charCodeAt(n);
if (c < 128) {
utftext += String.fromCharCode(c);
}
else if ((c > 127) && (c < 2048)) {
utftext += String.fromCharCode((c >> 6) | 192);
utftext += String.fromCharCode((c & 63) | 128);
}
else {
utftext += String.fromCharCode((c >> 12) | 224);
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
utftext += String.fromCharCode((c & 63) | 128);
}
}
return utftext;
},
// private method for UTF-8 decoding
_utf8_decode: function(utftext) {
var string = "";
var i = 0;
var c = c1 = c2 = 0;
while (i < utftext.length) {
c = utftext.charCodeAt(i);
if (c < 128) {
string += String.fromCharCode(c);
i++;
}
else if ((c > 191) && (c < 224)) {
c2 = utftext.charCodeAt(i + 1);
string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
i += 2;
}
else {
c2 = utftext.charCodeAt(i + 1);
c3 = utftext.charCodeAt(i + 2);
string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
i += 3;
}
}
return string;
}
}
// LZW-compress a string
function lzw_encode(s) {
var d = new Date();
var dict = {};
var data = (s + "").split("");
var out = [];
var currChar;
var phrase = data[0];
var code = 256;
for (var i = 1; i < data.length; i++) {
currChar = data[i];
if (dict[phrase + currChar] != null) {
phrase += currChar;
}
else {
out.push(phrase.length > 1 ? dict[phrase] : phrase.charCodeAt(0));
dict[phrase + currChar] = code;
code++;
phrase = currChar;
}
}
out.push(phrase.length > 1 ? dict[phrase] : phrase.charCodeAt(0));
for (var i = 0; i < out.length; i++) {
out[i] = String.fromCharCode(out[i]);
}
var retrunedresult = out.join("");
console.log("Input: " + s.length / 1024 + "kb Output:" + retrunedresult.length / 1024 + "kb Rate: " + (s.length / retrunedresult.length));
console.log((new Date()).getTime() - d.getTime() + ' ms.');
return retrunedresult;
}
// Decompress an LZW-encoded string
function lzw_decode(s) {
var dict = {};
var data = (s + "").split("");
var currChar = data[0];
var oldPhrase = currChar;
var out = [currChar];
var code = 256;
var phrase;
for (var i = 1; i < data.length; i++) {
var currCode = data[i].charCodeAt(0);
if (currCode < 256) {
phrase = data[i];
}
else {
phrase = dict[currCode] ? dict[currCode] : (oldPhrase + currChar);
}
out.push(phrase);
currChar = phrase.charAt(0);
dict[code] = oldPhrase + currChar;
code++;
oldPhrase = phrase;
}
return out.join("");
}