Skip to content

Instantly share code, notes, and snippets.

@L0laapk3
Last active July 16, 2020 17:27
Show Gist options
  • Save L0laapk3/4f7e95e7f89c9f1870e38e985dcfcacb to your computer and use it in GitHub Desktop.
Save L0laapk3/4f7e95e7f89c9f1870e38e985dcfcacb to your computer and use it in GitHub Desktop.
hangman script for nadekobot :]
// to be ran at https://gitlab.com/Kwoth/nadekobot/-/raw/global/src/NadekoBot/data/hangman.json
excludedLetters = "nt";
wordBlank = "_a___";
a = JSON.parse(document.getElementsByTagName("body")[0].innerText);
b = a.Animals.concat(a.Countries).concat(a.Movies).concat(a.Things);
alfabet = "abcdefghijklmnopqrstuvwxyz0123456789";
excludedLetters = excludedLetters.split("");
wordBlank = wordBlank.split("");
for (let w of b) {
w.Word = w.Word.toLowerCase();
w.letters = [...new Set(w.Word.split(""))].sort().join("");
}
guessedLetters = [...new Set(excludedLetters.concat(wordBlank))].filter(x => alfabet.indexOf(x) >= 0).join("");
b = b.filter(w =>
w.Word.length == wordBlank.length &&
wordBlank.every((l, i) => (l == "_" && w.Word[i] != " " && guessedLetters.indexOf(w.Word[i]) == -1) || l == w.Word[i]) &&
excludedLetters.every(l => w.letters.indexOf(l) < 0)
);
frequency = new Array(alfabet.length).fill(0).map(_ => ({}));
for (let w of b)
for (let i = 0; i < alfabet.length; i++) {
const key = wordBlank.map((o, j) => w.Word[j] == alfabet[i] ? alfabet[i] : o).join("");
if (!frequency[i][key])
frequency[i][key] = 1;
else
frequency[i][key]++;
}
bestGuess = frequency.map((l, i) => [Math.max(...Object.values(l)), alfabet[i]]).sort((a, b) => a[0] - b[0])[0][1];
if (b.length == 1)
console.log(b[0].Word);
else if (b.length < 10)
console.log(b.length + " remaining, best pick: " + bestGuess + " (" + b.map(w => w.Word).join(", ") + ")");
else
console.log(b.length + " remaining, best pick: " + bestGuess);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment