-
Notifications
You must be signed in to change notification settings - Fork 0
/
hangman.js
69 lines (59 loc) · 1.55 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
let dictionary = ['cica']; // 'kiscica', 'nagycica', 'közepescica', 'kicsitkicsicica', 'kicsitnagycica', 'macska'];
let word = [];
let guessedWord = [];
let life = 10;
var readline = require('readline-sync');
const generateWord = () => {
word = dictionary[Math.floor(Math.random() * dictionary.length)];
for (let i = 0; i < word.length; i++) {
guessedWord.push('_');
}
life = Math.floor(word.length / 2);
};
const printGame = () => {
for (let i = 0; i < guessedWord.length; i++) {
process.stdout.write(guessedWord[i] + ' ');
}
console.log('\nLife points', life);
};
const isSolved = () => {
for (let i = 0; i < guessedWord.length; i++) {
if (guessedWord[i] === '_') return false;
}
return true;
};
const makeGuess = (a) => {
let found = false;
for (let i = 0; i < word.length; i++) {
if (word.charAt(i) === a) {
guessedWord[i] = word.charAt(i);
found = true;
}
}
return found;
};
const main = () => {
generateWord();
console.log('Üdvözöllek az akasztófa játékban, kérlek a billentyűk lenyomásával próbáld meg kitalálni a szót. Ha elfogy az életed, a kiscicák fognak veled végezni.');
printGame();
while (life > 0) {
if (!makeGuess(readline.keyIn())) {
life--;
}
printGame();
if (isSolved()) {
console.log('nyertél');
break;
}
}
if (life === 0) {
console.log('Sajnos vesztettél');
}
};
/* let a = 'c';
word = generateWord(word, dictionary);
printGame(guessedWord, life);
isSolved(guessedWord);
makeGuess(a);
console.log(word); */
main();