-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·105 lines (98 loc) · 3.39 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
#!/usr/bin/env node
const prompts = require("prompts");
const chalk = require("chalk");
const wordsJSON = require("./words.json");
const terminal_cols = process.stdout.columns;
const MAX_TRIES = 6;
const previous_gusses = [];
// global results for showing all results at once
let glob_results = "";
let puzzle = "";
const wordlePrompt = {
type: "text",
name: "word",
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 (previous_gusses.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 = "";
// loop over each letter in the word
for (let i in guess) {
// check if the letter at the specified index in the guess word exactly
// matches the letter at the specified index in the puzzle
if (guess[i] === puzzle[i]) {
results += chalk.white.bgGreen.bold(` ${guess[i]} `);
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 (puzzle.includes(guess[i])) {
results += chalk.white.bgYellow.bold(` ${guess[i]} `);
continue;
}
// otherwise the letter doesn't exist at all in the puzzle
results += chalk.white.bgGrey.bold(` ${guess[i]} `);
}
glob_results += 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(glob_results);
}
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);
if (!response.word) {
console.log("You closed the game, Good bye!");
process.exit(0);
}
const guess = response.word.toUpperCase(); // optional chaining
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);
}
// add to already enterd words list
previous_gusses.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
randomNumber = Math.floor(Math.random(wordsJSON.length) * wordsJSON.length);
puzzle = wordsJSON[randomNumber].toUpperCase();
// start the game
await play(0);
}
main();