Published
Edited
Jan 26, 2021
Insert cell
md`# Codenames`
Insert cell
Coder("example.com").encode(800)
Insert cell
Coder = function(seed) {
return {
alphabet: shuffle(words(), seed),
decode: function(input) {
return this.alphabet[0];
},
encode: function(input) {
return encode(this.alphabet, input);
}
};
}
Insert cell
encode = function(alphabet, input) {
var encoded = "",
n = alphabet.length;
input += n;

do {
encoded = alphabet[input % n] + " " + encoded;
input = parseInt(input / n, 10);
} while (input);
return encoded.trim();
}
Insert cell
shuffle = function(alphabet, salt) {
var integer, j, temp, i, v, p;

for (i = alphabet.length - 1, v = 0, p = 0; i > 0; i--, v++) {
v %= salt.length;
p += integer = salt[v].charCodeAt(0);
j = (integer + v + p) % i;

temp = alphabet[j];
alphabet = alphabet.slice(0, j).concat(alphabet[i], alphabet.slice(j + 1));
alphabet = alphabet.slice(0, i).concat(temp, alphabet.slice(i + 1));
}

return alphabet;
}
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more