Last active
April 5, 2024 21:41
-
-
Save kirkegaard/51c530df548a89a02586d02fa4168c3e to your computer and use it in GitHub Desktop.
Name this decent modder. A guessing game in node
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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