-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
104 lines (84 loc) · 2.95 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
var Word = require('./word.js')
var inquirer = require('inquirer')
var isLetter = require('is-letter');
var states = ["ALASKA", "ARIZONA", "ARKANSAS", "CALIFORNIA", "COLORADO", "CONNECTICUT", "DELAWARE", "FLORIDA", "GEORGIA", "HAWAII", "IDAHO", "ILLINOIS"]
var guessesRemaining = 10;
var guessedLetters = [];
var word = new Word(states[Math.floor(Math.random() * states.length)]);
function startGame() {
console.log('\nHangman!\n')
if (guessedLetters.length > 0) {
guessedLetters = []
}
inquirer.prompt([
{
name: 'play',
type: 'confirm',
message: 'Ready to play?'
}
]).then(function (answer) {
if (answer.play) {
console.log('You get 10 guesses to guess the right State.')
console.log('Good Luck!')
newGame(word)
} else {
console.log('Byebye')
}
})
}
var newGame = function () {
inquirer.prompt([
{
name: 'letter',
message: 'Guess a letter!',
validate: function (value) {
if (isLetter(value)) {
return true
} else {
return false
}
}
}
]).then(function (answer) {
var letterPicked = (answer.letter).toUpperCase();
var guessedAlready = false
for (var i = 0; i < guessedLetters.length; i++) {
if (letterPicked === guessedLetters[i]) {
guessedAlready = true
}
}
if (guessedAlready === false) {
guessedLetters.push(letterPicked)
var found = word.checkIfLetterFound(letterPicked)
if (found === 0) {
console.log("Nope you're wrong!\n")
guessesRemaining--
console.log('Guesses reamaining: ' + guessesRemaining)
console.log(word.wordRender())
console.log('Letters guessed: ' + guessedLetters + "\n")
} else {
console.log('Yes! You are correct!!\n')
if (word.checkWord() === true) {
console.log("\n" + word.wordRender() +"\n")
console.log('YOU WIN!')
newGame()
} else {
console.log("\n" + 'Guesses remaining: ' + guessesRemaining)
console.log(word.wordRender())
console.log('Letters guessed: ' + guessedLetters+"\n")
}
}
if (guessesRemaining > 0 && word.wordFound === false) {
newGame();
} else if (guessesRemaining === 0) {
console.log('GAME OVER YOU LOSE')
console.log('The word you were trying to guess was: ' + word.word)
startGame();
}
} else {
console.log('You"ve guessed that letter already, try again.')
newGame();
}
})
}
startGame();