wordle-shell
Version:
A Wordle for the command line
121 lines (120 loc) • 4.36 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const chalk_1 = __importDefault(require("chalk"));
const inquirer_1 = __importDefault(require("inquirer"));
const Wordle_1 = __importStar(require("./Wordle"));
const WordPicker_1 = __importDefault(require("./WordPicker"));
class Game {
constructor(lang) {
this.max_tries = 6;
this.current_try = 1;
this.guesses = Array();
const wordPicker = new WordPicker_1.default(lang);
this.wordle = new Wordle_1.default(wordPicker);
}
run() {
this.prompt();
}
prompt(msg) {
console.clear();
console.log("Current try number: ", this.current_try);
this.promptGuesses();
if (msg) {
console.log(msg);
}
inquirer_1.default.prompt([
{
type: "input",
name: "guess",
message: "What is your guess?",
validate: (input, _) => {
return String(input).length === Wordle_1.default.get_word_size();
},
}
]).then((anwser) => {
const guess = anwser.guess.toUpperCase();
try {
const states = this.wordle.checkWord(guess);
this.guesses.push({ guess, states });
if (Wordle_1.default.done(states)) {
this.promptSuccess();
}
else {
if (this.current_try < this.max_tries) {
this.current_try++;
this.prompt();
}
else {
this.promptFailure();
}
}
}
catch (error) {
this.prompt(error.message);
}
});
}
promptGuesses() {
for (let prev_guess of this.guesses) {
this.promptGuess(prev_guess.guess, prev_guess.states);
}
}
promptSuccess() {
console.clear();
console.log(chalk_1.default.bgBlack.greenBright("Congratulations"));
console.log(chalk_1.default.bgBlack.greenBright("You Won!"));
}
promptFailure() {
console.clear();
console.log("You Failed");
console.log("The Word was:", chalk_1.default.bgWhite.black(this.wordle.getWord()));
}
pickColor(state) {
switch (state) {
case Wordle_1.CharState.Correct:
return chalk_1.default.bgGreen.white;
case Wordle_1.CharState.Misplaced:
return chalk_1.default.bgYellow.white;
case Wordle_1.CharState.Wrong:
return chalk_1.default.bgRed.white;
default:
return chalk_1.default.bgBlue.white;
}
}
promptGuess(guess, states) {
var _a;
let builder = "";
for (let index = 0; index < guess.length; index++) {
const color = this.pickColor((_a = states[index]) !== null && _a !== void 0 ? _a : Wordle_1.CharState.Unknow);
builder += color(guess[index]);
}
console.log(builder);
}
}
exports.default = Game;