Skip to content

Instantly share code, notes, and snippets.

@bpevs
Last active May 8, 2022 21:20
Show Gist options
  • Save bpevs/ac3c700f7d7800113c0d5d5251b0bdd8 to your computer and use it in GitHub Desktop.
Save bpevs/ac3c700f7d7800113c0d5d5251b0bdd8 to your computer and use it in GitHub Desktop.
Deno implementation of Wordle for cmd
// Deno version of Wordle
// Loosely inspired by https://gist.github.com/huytd/6a1a6a7b34a0d0abcac00b47e3d01513
//
// Run this script: deno run --allow-read wordle.ts
// Install this script: deno install --name=wordle --allow-read https://gist.githubusercontent.com/ivebencrazy/ac3c700f7d7800113c0d5d5251b0bdd8/raw/3cbf169aa7b5e59f6da033b53775085fe5d863ee/wordle.ts
import {
bgGreen,
bgWhite,
bgYellow,
black,
} from "https://deno.land/[email protected]/fmt/colors.ts";
const maxGuess = 6;
const words = (new TextDecoder("utf-8"))
.decode(await Deno.readFile("/usr/share/dict/words"))
.split("\n")
.filter((word) => word.length === 5)
.map((word) => word.toUpperCase());
const answer = words[Math.floor(Math.random() * words.length)];
let end = false;
let guessCount = 0;
while (end !== true) {
guessCount++;
if (guessCount > maxGuess) {
console.log(`You Lose! The word is ${answer}`);
end = true;
} else {
const promptQuestion = `Enter your guess (${guessCount} / 6):`;
const userAnswer = (prompt(promptQuestion) || "").toUpperCase();
if (words.find((word) => word === userAnswer) == null) {
console.log(`${userAnswer} is not a valid word! Guess again!`);
guessCount--;
} else {
if (userAnswer === answer) {
console.log(`You Win!`);
end = true;
}
let output = "";
answer.split("").forEach((answerLetter, index) => {
const userAnswerLetter = userAnswer[index];
if (answerLetter === userAnswerLetter) {
output += bgGreen(` ${userAnswerLetter} `);
} else if (answer.indexOf(userAnswerLetter) !== -1) {
output += bgYellow(` ${userAnswerLetter} `);
} else {
output += bgWhite(` ${userAnswerLetter} `);
}
});
console.log(black(output));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment