-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
152 lines (115 loc) · 4.62 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
143
144
145
146
147
148
149
150
151
152
//// Constructor initialize
var Word = require("./word.js");
var inquirer = require("inquirer");
var colors = require("colors");
// letters entry
var lettersArray = "abcdefghijklmnopqrstuvwxyz";
// List of words to choose from
var webDevWords = ["html", "css", "javascript", "jquery", "github", "ajax",
"mysql", "nodejs", "api", "axios", "push", "pull", "commit" ];
// Pick Random index from web developer words array
var randomIndex = Math.floor(Math.random() * webDevWords.length);
var randomWord = webDevWords[randomIndex];
// Pass random word through Word constructor
computerWord = new Word(randomWord);
var requireNewWord = false;
// Array for guessed letters
var incorrectLetters = [];
var correctLetters = [];
// Guesses left
var guessesLeft = 10;
function startGame() {
// Generates new word for Word constructor if true
if (requireNewWord) {
// Selects random web developer words array
var randomIndex = Math.floor(Math.random() * webDevWords.length);
var randomWord = webDevWords[randomIndex];
// Passes random word through the Word constructor
computerWord = new Word(randomWord);
requireNewWord = false;
}
// TestS if a letter guessed is correct
var wordComplete = [];
computerWord.objArray.forEach(completeCheck);
// letters remaining to be guessed
if (wordComplete.includes(false)) {
inquirer
.prompt([
{
type: "input",
message: "You get 10 letter guesses to find common web development terms".cyan,
name: "userinput"
}
])
.then(function (input) {
if (!lettersArray.includes(input.userinput) || input.userinput.length > 1) {
console.log("\nPlease try again!\n".yellow);
startGame();
} else {
if (incorrectLetters.includes(input.userinput) || correctLetters.includes(input.userinput) || input.userinput === "") {
console.log("\nAlready Guessed or Nothing Entered\n".red);
startGame();
} else {
// Checks if guess is correct
var wordCheckArray = [];
computerWord.userGuess(input.userinput);
// Checks if guess is correct
computerWord.objArray.forEach(wordCheck);
if (wordCheckArray.join('') === wordComplete.join('')) {
console.log("\nIncorrect\n".red);
incorrectLetters.push(input.userinput);
guessesLeft--;
} else {
console.log("\nCorrect!\n".green);
correctLetters.push(input.userinput);
}
computerWord.log();
// Print guesses left
console.log("Guesses Left: " + guessesLeft + "\n".yellow);
// Print letters guessed already
console.log("Letters Guessed: " + incorrectLetters.join(" ") + "\n");
// Guesses left
if (guessesLeft > 0) {
// Call function
startGame();
} else {
console.log("Sorry, you lose!\n".red);
restartGame();
}
function wordCheck(key) {
wordCheckArray.push(key.guessed);
}
}
}
})
} else {
console.log("YOU GOT IT!\n".green);
restartGame();
}
function completeCheck(key) {
wordComplete.push(key.guessed);
}
}
function restartGame() {
inquirer
.prompt([
{
type: "list",
message: "Would you like to:",
choices: ["Play Again", "Exit"],
name: "restart"
}
])
.then(function (input) {
if (input.restart === "Play Again") {
requireNewWord = true;
incorrectLetters = [];
correctLetters = [];
guessesLeft = 10;
knowledge();
} else {
return
}
})
}
startGame();