Skip to content

Instantly share code, notes, and snippets.

@kirkegaard
Last active April 5, 2024 21:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kirkegaard/51c530df548a89a02586d02fa4168c3e to your computer and use it in GitHub Desktop.
Save kirkegaard/51c530df548a89a02586d02fa4168c3e to your computer and use it in GitHub Desktop.
Name this decent modder. A guessing game in node
const readline = require('node:readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const HIDDEN_CHAR = "_";
const words = [
"HDR",
"miketf1",
"sammy sam",
"orangeglo",
"b23n",
"DMG",
"razole",
"leggomyfroggo",
"imod systems",
"hozy",
"makho",
"zipplet",
"aidan",
"jayro",
"The Damned",
"jack",
"CRDRC"
];
const randomIndex = Math.floor(Math.random() * words.length);
const selectedWord = words[randomIndex];
let currentAnswer = new Array(selectedWord.length).fill(HIDDEN_CHAR);
const guess = (input) => input.toLowerCase() === selectedWord.toLowerCase();
const getHint = () => {
const notGuessedCharacters = [];
for (let i = 0; i <= currentAnswer.length; i++) {
if (currentAnswer[i] === HIDDEN_CHAR) {
notGuessedCharacters.push(i);
}
}
const hintRandom = Math.floor(Math.random() * notGuessedCharacters.length);
const selectedHint = notGuessedCharacters[hintRandom];
currentAnswer[selectedHint] = selectedWord[selectedHint];
};
const main = () => {
if (currentAnswer.join("") === selectedWord) {
console.log(`Sorry, the answer was: ${selectedWord}`);
rl.close();
return;
}
const q = `Name this decent modder: ${currentAnswer.join("")}`;
rl.question(`${q}\nAnswer: `, (input) => {
if (guess(input)) {
console.log('Hurray');
rl.close();
return;
}
console.log('Try again');
getHint();
main();
});
};
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment