Watch Encyclopedia / website · watchencyclopedia.org / exp / enigma.js
| 1 | WE.mount('enigma', function (S) { |
| 2 | var C = S.C, ctx = S.ctx, A = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', ROT = [['EKMFLGDQVZNTOWYHXUSPAIBRCJ', 'Q'], ['AJDKSIRUXBLHWTMCQGZNPYFVOE', 'E'], ['BDFHJLCPRTXVZNYEIWGAKMUSQO', 'V']], REF = 'YRUHQSLDPXNGOKMIEBFZCWVJAT'; |
| 3 | var F = ROT.map(function (r) { return r[0].split('').map(function (c) { return A.indexOf(c); }); }), B = F.map(function (f) { var b = []; f.forEach(function (v, i) { b[v] = i; }); return b; }), RF = REF.split('').map(function (c) { return A.indexOf(c); }), NT = ROT.map(function (r) { return A.indexOf(r[1]); }); |
| 4 | function L(v) { return A[v]; } |
| 5 | var s1 = S.slider('Left wheel', 0, 25, 0, 1, L), s2 = S.slider('Middle wheel', 0, 25, 0, 1, L), s3 = S.slider('Right wheel', 0, 25, 0, 1, L); |
| 6 | var inp = S.input('Type a message', 'HELLO', null, 40); |
| 7 | S.button('Feed the output back in', function () { var o = run(inp.v).out; inp.el.value = o; inp.v = o; }); |
| 8 | function run(msg) { var p = [s1.v, s2.v, s3.v], out = '', last = null; msg.toUpperCase().split('').forEach(function (ch) { var c = A.indexOf(ch); if (c < 0) { if (ch === ' ') out += ' '; return; } |
| 9 | if (p[1] === NT[1]) { p[0] = (p[0] + 1) % 26; p[1] = (p[1] + 1) % 26; } else if (p[2] === NT[2]) p[1] = (p[1] + 1) % 26; p[2] = (p[2] + 1) % 26; |
| 10 | var tr = [c], x = c; for (var i = 2; i >= 0; i--) { x = (F[i][(x + p[i]) % 26] - p[i] + 26) % 26; tr.push(x); } x = RF[x]; tr.push(x); for (i = 0; i < 3; i++) { x = (B[i][(x + p[i]) % 26] - p[i] + 26) % 26; tr.push(x); } out += A[x]; last = { tr: tr, p: p.slice() }; }); |
| 11 | return { out: out, last: last, p: p }; } |
| 12 | S._enigma = run; |
| 13 | S.loop(function () { |
| 14 | var w = S.W, h = S.H, sm = S.small(), R = run(inp.v), top = sm ? 96 : 56, rh = (h - top - 10) / 26, x0 = sm ? 26 : w * .06, x1 = sm ? w - 12 : w * .6, colw = (x1 - x0) / 4.4, fs = Math.max(8, Math.min(12, rh * .8)); |
| 15 | S.clear(); |
| 16 | function Y(i) { return top + (i + .5) * rh; } |
| 17 | var names = ['Mirror', 'Left wheel', 'Middle wheel', 'Right wheel'], xs = [0, 1, 2, 3].map(function (k) { return x0 + colw * (.2 + k * 1.1); }); |
| 18 | xs.forEach(function (x, k) { S.rr(x, top - 4, colw * .8, rh * 26 + 8, 8, '#161a21', C.grid); S.txt(sm ? names[k].split(' ')[0] : names[k], x + colw * .4, top - 30, C.mut, 11, 'center'); if (k > 0) { S.rr(x + colw * .4 - 13, top - 22, 26, 17, 5, C.bg, C.dim); S.txt(A[R.p[k - 1]], x + colw * .4, top - 13, C.acc, 11.5, 'center', 700); } }); |
| 19 | var kx = x1 - colw * .05; for (var i = 0; i < 26; i++) S.txt(A[i], kx + 8, Y(i), C.dim, fs, 'center'); |
| 20 | if (R.last) { var t = R.last.tr; // t: [in, afterR, afterM, afterL, afterRef, backL, backM, backR] |
| 21 | function seg(pts, col) { ctx.beginPath(); pts.forEach(function (q) { ctx.lineTo(q[0], q[1]); }); ctx.strokeStyle = col; ctx.lineWidth = 2.5; ctx.lineJoin = 'round'; ctx.stroke(); } |
| 22 | var e = colw * .8, f = [[kx, Y(t[0])], [xs[3] + e, Y(t[0])], [xs[3], Y(t[1])], [xs[2] + e, Y(t[1])], [xs[2], Y(t[2])], [xs[1] + e, Y(t[2])], [xs[1], Y(t[3])], [xs[0] + e, Y(t[3])], [xs[0] + e * .3, Y(t[3])]]; |
| 23 | var b = [[xs[0] + e * .3, Y(t[4])], [xs[0] + e, Y(t[4])], [xs[1], Y(t[4])], [xs[1] + e, Y(t[5])], [xs[2], Y(t[5])], [xs[2] + e, Y(t[6])], [xs[3], Y(t[6])], [xs[3] + e, Y(t[7])], [kx, Y(t[7])]]; |
| 24 | seg(f, C.acc); seg([[xs[0] + e * .3, Y(t[3])], [xs[0] + e * .3, Y(t[4])]], C.ink); seg(b, C.blue); |
| 25 | S.dot(kx + 8, Y(t[0]), fs * .85, C.acc); S.txt(A[t[0]], kx + 8, Y(t[0]) + .5, '#1a1206', fs, 'center', 700); S.dot(kx + 8, Y(t[7]), fs * .85, C.blue); S.txt(A[t[7]], kx + 8, Y(t[7]) + .5, '#0b1520', fs, 'center', 700); } |
| 26 | var tx = sm ? 12 : w * .66, ty = sm ? 14 : h * .22, big = sm ? 15 : 24, shown = inp.v.toUpperCase().replace(/[^A-Z ]/g, ''); |
| 27 | if (sm) { S.txt('In: ' + shown.slice(-26), tx, ty, C.acc, big, 'left', 700); S.txt('Out: ' + R.out.slice(-26), tx, ty + 22, C.blue, big, 'left', 700); } |
| 28 | else { S.txt('You type', tx, ty, C.mut, 12.5); S.txt(shown.slice(-22) || ' ', tx, ty + 30, C.acc, big, 'left', 700); S.txt('The machine lights up', tx, ty + 80, C.mut, 12.5); S.txt(R.out.slice(-22) || ' ', tx, ty + 110, C.blue, big, 'left', 700); S.txt('Wheels now at ' + R.p.map(L).join(' '), tx, ty + 160, C.mut, 13); } |
| 29 | S.say('Each key press sends a current through three wired wheels, off a mirror, and back by a different route to light another letter. Then the right wheel <b>clicks forward</b>, so the wiring changes for every letter. The same letter typed twice comes out different. To read a message, set the same starting wheels and type the scrambled text back in.'); |
| 30 | }); |
| 31 | }); |