-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
155 lines (129 loc) · 3.87 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!usr/bin/env node
import fs from 'fs';
import chalk from 'chalk';
import figlet from 'figlet';
import gradient from 'gradient-string';
import chalkAnimation from 'chalk-animation';
import inquirer from 'inquirer';
import { createSpinner } from 'nanospinner';
let secretWord;
let PlayerName;
let tries = 6;
// Function to read words from a file
function readWordsFromFile(filePath) {
try {
const data = fs.readFileSync(filePath, 'utf8');
return data.split('\n').map((word) => word.trim()).filter(Boolean);
} catch (error) {
console.error(`Error reading file: ${error.message}`);
process.exit(1);
}
}
// Function to generate a random word for the game
function generateSecretWord() {
const words = readWordsFromFile('words.txt');
const randomIndex = Math.floor(Math.random() * words.length);
return words[randomIndex];
}
const sleep = (ms = 2000) => new Promise((r) => setTimeout(r, ms));
// Display a welcome message and introduce the basic rules.
async function welcome() {
console.clear();
// Print Wordle CLI text synchronously
const Header = "Wordle CLI ";
const headerText = await new Promise((resolve) => {
figlet(Header, (err, data) => {
resolve(data);
});
});
console.log("\n\n");
console.log(gradient.pastel.multiline(headerText));
// Display game rules
console.log(`${chalk.bgBlackBright.bold('How to play')}
-> Enter a word to guess.
-> You have 6 guesses.
`);
secretWord = generateSecretWord();
}
// Get the name of the player
async function askName() {
const ans = await inquirer.prompt({
name: 'Player_Name',
type: 'input',
message: 'What is your name?',
default() {
return 'Player';
},
});
return ans.Player_Name;
}
// Function to generate clues for the given guess
function generateClues(word, secret) {
const clues = [];
const words = readWordsFromFile('words.txt');
if(words.includes(word))
{
for (let i = 0; i < secret.length; i++) {
if (word[i] === secret[i]) {
clues.push(chalk.green(word[i])); // Correct letter in the correct position
} else if (secret.includes(word[i])) {
clues.push(chalk.yellow(word[i])); // Correct letter in the wrong position
} else {
clues.push(chalk.gray('_')); // Incorrect letter
}
}
return clues.join(' ');
}
else
{
console.log("\nThe word is not in the word List\nTry Again\n");
}
}
// Handle the player's guess
async function handleGuess(word) {
const spinner = createSpinner('Checking the word...').start();
await sleep();
const clues = generateClues(word, secretWord);
if (word === secretWord) {
spinner.success('');
console.clear();
const msg = `C o n g r a t s \t\t\t${PlayerName}`;
figlet(msg, (err, data)=>{
console.log(gradient.pastel.multiline(data));
});
console.log("\n\nYou Guessed the correct word\nThank you for playing!\n\n\n\n");
process.exit(0);
} else {
spinner.error({ text: `Incorrect guess. Clues: ${clues}\n\n` });
tries--;
if (tries === 0) {
console.log(chalk.red(`Sorry, you're out of attempts. The correct word was ${secretWord}.`));
process.exit(1);
}
}
}
// Ask the player to make a guess
async function makeGuess() {
const word = await inquirer.prompt({
name: 'Get_word',
type: 'input',
message: `\n\nAttempt-${6 - tries + 1}: Guess the word:`,
validate(input) {
if (!input || input.trim() === '') {
return 'Please enter a word.';
}
return true;
},
});
return handleGuess(word.Get_word.toLowerCase());
}
// Main game loop
async function playWordle() {
// PlayerName = await askName();
while (tries > 0) {
await makeGuess();
}
}
// Start the game
await welcome();
playWordle();