-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
142 lines (128 loc) · 3.55 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
#!/usr/bin/env node
'use strict';
import chalk from 'chalk';
import chalkAnimation from 'chalk-animation';
import figlet from 'figlet';
import gradient from 'gradient-string';
import inquirer from 'inquirer';
import { createSpinner } from 'nanospinner';
import * as emoji from 'node-emoji';
import { MAX_ATTEMPTS, WORD_LIST } from './config.js';
const words = WORD_LIST;
const sleep = (ms = 2000) => new Promise((res, rej) => setTimeout(res, ms));
let word,
points = MAX_ATTEMPTS,
wordWhiteSpaces;
function addIntro() {
return new Promise((res, rej) => {
let introduction = 'Word Guessing Game!';
figlet(introduction, (error, data) => {
console.log(gradient.pastel.multiline(data));
});
});
}
async function welcome() {
let welcomeText = chalkAnimation.rainbow('Hello and Welcome !').start();
await sleep(3000);
welcomeText.stop();
console.log(`
${emoji.get('smirk')} you will be presented with a ${chalk.blue('hidden word')}
that you must ${chalk.cyan('guess')} one letter at a time
Each incorrect guess will deduct points from your score,
so ${chalk.magenta('be careful')}!
Good luck! ${emoji.get('v')}
`);
}
async function askToStartTheGame() {
const answers = await inquirer.prompt({
name: 'askToStart',
type: 'input',
message: 'start the game ? \n ',
default() {
return 'yes/no';
},
});
await handleQuestion(answers.askToStart == 'yes');
}
async function handleQuestion(isCorrect) {
const spinner = createSpinner('loading ...').start();
await sleep();
if (isCorrect) {
spinner.success({ text: 'your word is ready !' });
} else {
spinner.error({ text: 'ok maybe another time' });
process.exit(1);
}
}
function chooseAWord() {
word = words.at(Math.random() * 20);
wordWhiteSpaces = '_ ,'.repeat(word.length);
console.log(`
${wordWhiteSpaces}
`);
}
async function askForLetter() {
const answers = await inquirer.prompt({
name: 'askForLetter',
type: 'input',
message: 'Enter a letter from a to z ? \n ',
});
let PositionNumbers = await handleLetterInsertion(answers.askForLetter);
--points;
return { PositionNumbers, name: answers.askForLetter };
}
async function handleLetterInsertion(letter) {
const spinner = createSpinner('loading ...').start();
let indices = [];
await sleep(1000);
if (word.includes(letter)) {
spinner.success({ text: 'nice job !' });
for (var i = 0; i < word.length; i++) {
if (word[i] === letter) indices.push(i);
}
} else {
spinner.error({ text: 'oh try again' });
}
return indices;
}
function updateWord(letter) {
if (letter.PositionNumbers.length === 0) {
return;
} else {
let WhiteSpacesArray = wordWhiteSpaces.split(' ,');
for (let i = 0; i < letter.PositionNumbers.length; i++) {
WhiteSpacesArray[letter.PositionNumbers[i]] = letter.name;
}
let updatedWord = WhiteSpacesArray.join(' ,');
wordWhiteSpaces = updatedWord;
console.log(wordWhiteSpaces);
}
}
function looser() {
console.clear();
console.log(`sorry you loose the word was : ${word}`);
process.exit(1);
}
async function winner() {
console.clear();
await sleep(250);
chalkAnimation.pulse('you win !').start();
await sleep(3000);
console.log(`
the word was : ${chalk.blue(word)}`);
process.exit(1);
}
addIntro();
await sleep(1000);
await welcome();
await askToStartTheGame();
chooseAWord();
for (let i = 1; i <= points; i++) {
console.log('points : ', points);
let letter = await askForLetter();
updateWord(letter);
if (!wordWhiteSpaces.includes('_')) {
await winner();
}
}
looser();