-
Notifications
You must be signed in to change notification settings - Fork 0
/
hangman.js
81 lines (59 loc) · 1.99 KB
/
hangman.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
'use strict'; // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode
var readline = require('readline');
function newline() {
console.log('\n');
}
const welcomeMessage = `
=== Welcome to Hangman ===
To start playing just follow the prompts.
To exit the game before the end press Control + c
Let's get started!`;
console.log(welcomeMessage);
const word = 'misty';
const wordArray = word.split('');
const maskedWord = wordArray.map(x => '_');
// array to collect guesses
const guesses = [];
// collect correct guesses
// create a loop of making guess
// collecting guess
// checking if guess is correct
// if is add to correct guesses
// unmask that letter in the maskedWord
var terminal = readline.createInterface({
input: process.stdin,
output: process.stdout
});
console.log('The word: ', maskedWord.join(' '));
terminal.setPrompt('Guess a letter: ');
terminal.prompt();
terminal.on('line', function(guess) {
// this is the game loop or guess loop
console.log('The word: ', maskedWord.join(' '));
console.log(word.includes(guess));
if (guess.length !== 1) {
console.log('You can only enter one letter, try again');
terminal.prompt();
} else if (word.includes(guess)) {
// submit one letter guesses
// - guess is either in the word - unmasked
// if guess is in letter
console.log('That is correct');
// tell user that guess is in letter
// save guess in guesses
// correct_guesses = check what letters are in word to get correct guesss
// incorrect_guesses = is guesses minus correct_guesses
// - not in the word - sorry that was wrong
// - already guessed
// for ^^ this need to store the guesses, and have game loop
console.log(guess);
// store guesses
console.log('guesses', guesses);
console.log('word', word);
console.log('masked word', maskedWord);
terminal.prompt();
} else {
console.log('Sorry, that is not in the word, guess again.');
terminal.prompt();
}
});