-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhangedMan.js
316 lines (268 loc) · 7.86 KB
/
hangedMan.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/**
* Hangman game written in JavaScript.
*
* The program generates a random word,
* the player guesses letters,
* if the word contains a letter its occurences are revealed,
* o/w the player gets a strike.
* The game continues until the player guesses the word or 10 strikes are reached.
*
* @link https://github.com/saharlaor/hangman
* @file This file contains a full hangman game implementation.
* @author SaharLaOr.
* @since 1.0.0
**/
if (require.main === module) {
(function () {
'use strict';
// External packages
const figlet = require('figlet');
const randomWords = require('random-words');
const prompts = require('prompt-sync')();
const sleep = require('system-sleep');
// Project consts, lets
let isWon = false;
const word = randomWords();
const FONTS = ["Banner", "Big", "Blocks", "Cricket", "Doom", "Rectangles", "Speed"];
const openingScreenFont = FONTS[Math.floor(Math.random() * FONTS.length)];
const hangmanIllustrations = [`
=========`, `
|
=========`, `
|
|
=========`, `
|
|
|
=========`, `
|
|
|
|
=========`, `
|
|
|
|
|
=========`, `
|
|
|
|
|
|
=========`, `
--+
|
|
|
|
|
|
=========`, `
+---+
|
|
|
|
|
|
=========`, `
+---+
| |
|
|
|
|
|
=========`, `
+---+
| |
O |
/|\\ |
/ \\ |
|
|
=========`];
const freemanIllustrations = [`
(_)
/|\\
|
/ \\
`, `
(_)
\\|/
|
/ \\
`];
/**
* Main function of the game, includes opening screen and the actual game.
*
* Print title screen in baloon letters.
*
* @since 1.0.0
**/
function hangMan() {
// Print opening screen
figlet("Hang Man", {
font: openingScreenFont,
horizontalLayout: 'default',
verticalLayout: 'default',
width: 80,
whitespaceBreak: true
}, (err, data) => {
if (err) {
// Handling ctrl+c
process.on('SIGINT', function () {
console.clear();
console.log("Goodbye");
process.exit();
});
console.log("Something went wrong with figlet");
console.dir(err);
return;
} else { console.log(data)};
// Play the game
isWon = startGame();
gameOver(isWon);
});
}
/**
* Handle the end of a game.
*
* Print a relevant message for the end of the game.
*
* @since 1.0.0
*
* @param {boolean} isWon Did the player win the game.
**/
function gameOver(isWon) {
if (isWon) {
for (let i = 0; i < 15; i++) {
console.log(`Congratulations!
You found the word ${word} and saved the man`);
console.log(freemanIllustrations[i % 2]);
sleep(300);
console.clear();
}
console.log(`You won!
The word was ${word}`);
} else {
console.log(`Game over!
You lost
The word was ${word}`);
console.log(hangmanIllustrations[10]);
}
}
/**
* Go through a player's turn.
*
* Let the player guess a letter, check it against the word.
*
* @since 1.0.0
*
* @param {string} word The word.
* @param {Array} guessed An array of guessed letters.
*
* @return {Object} The guess and whether it's in the word
**/
function playerTurn(word, guessed) {
let isValidInput;
let guess;
// Ask player for a letter with validation
do {
isValidInput = true;
guess = prompts("Guess a letter: ").toLowerCase();
if (guess.length === 0) {
isValidInput = false;
console.log("You entered nothing, try entering a letter");
} else if (guess.length != 1 && guess.length != word.length) {
isValidInput = false;
console.log("You entered invalid amount of characters");
} else if (!/[a-z]+/.test(guess)) {
isValidInput = false;
console.log("Your guess consisted of invalid characters");
} else if (guessed.includes(guess)) {
isValidInput = false;
console.log(`You already guessed ${guess}`);
}
} while (!isValidInput);
// Return the guessed letter and whether it exists
return {
isInWord: word.indexOf(guess) != -1,
guess: guess
}
}
/**
* Update the hidden word with all the matches of the player guess.
*
* In case of a single letter guess, iterate both word and hiddenWord.
* If the letter in the index matches the guess, insert to hiddenWord.
* In case of whole word guess, check if the guess is the word.
*
* @since 1.0.0
*
* @param {string} word The word.
* @param {string} hiddenWord The obscured word.
* @param {string} guess The player guess.
*
* @return {string} Updated hiddenWord with guess.
**/
function updateHiddenWord(word, hiddenWord, guess) {
if (guess.length === 1) {
// In case the player guessed a single letter
for (let i = 0; i < word.length; i++) {
if (word[i] === guess) {
hiddenWord =
hiddenWord.substring(0, i)
+ guess
+ hiddenWord.substring(i + 1);
}
}
} else if (word == guess) {
// In case the player guessed the whole word
hiddenWord = word;
}
return hiddenWord;
}
/**
* Hanged man game.
*
* Generate a random word, print it opaquely (using '*').
* Let the player guess letters until he either wins or
* accumulate 10 wrong guesses.
*
* @since 1.0.0
*
* @return {boolean} Whether the player won the game
**/
function startGame() {
let hiddenWord = word.replace(/./g, '*');
let guessed = Array();
let misses = 0;
// Loop until game over
while (misses < 10 && !isWon) {
console.log("\nThe word is:\n" + hiddenWord + "\n");
// Give the player a turn
let guessObj = playerTurn(word, guessed);
guessed.push(guessObj.guess);
if (guessObj.isInWord) {
hiddenWord = updateHiddenWord(word, hiddenWord, guessObj.guess);
} else { misses += 1; }
// Print game status
console.clear();
console.log(`You have missed a total of ${misses} times`);
console.log("All your guesses: " + guessed.join(", "));
console.log(hangmanIllustrations[misses]);
// Update whether the player has won
isWon = (hiddenWord === word);
}
console.clear();
return isWon;
}
// Start Game
hangMan();
})();
}