forked from burkeholland/nodele
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
107 lines (100 loc) · 3.56 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
const prompts = require("prompts");
const chalk = require("chalk");
const wordsJSON = require("./words.json");
const TERMINAL_COLS = process.stdout.columns;
const MAX_TRIES = 6;
const previousGuesses = [];
// global results for showing all results at once
let globalResults = "";
let puzzle = "";
const wordlePrompt = {
type: "text",
name: "word",
format: (value) => {
return value.toUpperCase();
},
message: "Enter a 5 letter word...",
validate: (value) => {
if (value.length != 5) {
return "Word must be 5 letters";
} else if (!/^[a-z]+$/i.test(value)) {
return "Word must only contain letters";
} else if (!wordsJSON.includes(value.toUpperCase())) {
// wordsJSON is now in uppercase, so can directly check via includes
return "Word not found in word list";
} else if (previousGuesses.includes(value.toUpperCase())) {
// same word already entered
return "You have already entered this word";
}
return true;
}
};
async function check(guess) {
// clear previous results
console.clear();
let results = "";
let puzzleNotMatchedLetters = puzzle;
// loop over each letter in the word
for (let i in guess) {
const letter = guess[i];
// check if the letter at the specified index in the guess word exactly
// matches the letter at the specified index in the puzzle
if (letter === puzzle[i]) {
puzzleNotMatchedLetters = puzzleNotMatchedLetters.replace(letter, "");
results += chalk.white.bgGreen.bold(` ${letter} `);
continue;
}
// check if the letter at the specified index in the guess word is at least
// contained in the puzzle at some other position
if (puzzleNotMatchedLetters.includes(letter)) {
puzzleNotMatchedLetters = puzzleNotMatchedLetters.replace(letter, "");
results += chalk.white.bgYellow.bold(` ${letter} `);
continue;
}
// otherwise the letter doesn't exist at all in the puzzle
results += chalk.white.bgGrey.bold(` ${letter} `);
}
globalResults += results.padEnd(results.length + TERMINAL_COLS - 15, " ");
// 15 in above code is 5 letters and 2 spaces in start and end of characters, 3 char for a letter, total 3 *5 =15
// it has to be hardcoded as the chalk's result changes the number of characters
process.stdout.write(globalResults);
}
async function play(tries) {
// the user gets 5 tries to solve the puzzle not including the first guess
if (tries < MAX_TRIES) {
// ask the player for a guess word
const response = await prompts(wordlePrompt);
const guess = response.word;
if (typeof guess === "undefined") {
// this scenario happens when a user presses Ctrl+C and terminates program
// previously it was throwing an error
console.clear();
console.log("You closed the game, Good bye!");
process.exit(0); // 0 for exitting without throwing error
}
// add to already enterd words list
previousGuesses.push(guess);
// if the word matches, they win!
if (guess == puzzle) {
// show board again
check(guess);
console.log("WINNER!");
} else {
check(guess);
// this forces std out to print out the results for the last guess
process.stdout.write("\n");
// repeat the game and increment the number of tries
play(++tries);
}
} else {
console.log(`INCORRECT: The word was ${puzzle}`);
}
}
async function main() {
// get a random word
const randomNumber = Math.floor(Math.random(wordsJSON.length) * wordsJSON.length);
puzzle = wordsJSON[randomNumber].toUpperCase();
// start the game
await play(0);
}
main();