-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
58 lines (48 loc) · 2.18 KB
/
game.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
var Word = require('./word.js');
var wordBank = require('./word-bank.js');
const chalk = require('chalk');
function Game() {
// Create a new word using words from the word bank file
var newWord = new Word(wordBank.words[Math.floor(Math.random() * wordBank.words.length)]);
//Call the word.getLetters function to fill the current word's letters array.
newWord.getLetters();
// Create and set new game objects parameters
this.currentWord = newWord;
this.guessesRemaining = 10;
this.pastGuesses = [];
this.status='start';
console.log(chalk.yellow.bold('\nCurrent Word'));
console.log(this.currentWord.wordRender());
console.log(" Guesses remaining: " + this.guessesRemaining+'\n');
}
// Check if player's guess is correct and update word display
Game.prototype.isGuessCorrect = function (playerGuess) {
// For Testing, display the current word
//console.log(this.currentWord);
//Checks if player's guessed letter is in current word and updates letter's display
var isGuessCorrect = this.currentWord.checkLetter(playerGuess);
if (isGuessCorrect === 0) {
// If guess isn't correct (letter not in current word) then lower number of guesses
console.log(chalk.redBright("\n WRONG!!!"));
this.guessesRemaining--;
} else {
// If guess is correct (letter is in current word at least once) inform the user they were correct
console.log(chalk.greenBright("\n CORRECT!!!"));
}
// Checks if player has guessed the whole word
this.currentWord.findWord();
if ((this.guessesRemaining > 0) && (this.currentWord.found === false)) {
console.log(this.currentWord.wordRender());
console.log(" Guesses remaining: " + this.guessesRemaining);
console.log("-------------------");
this.status='continue';
}
else if (this.guessesRemaining === 0) {
console.log("\n GAME OVER. \n The correct word was: \n " +chalk.redBright(this.currentWord.wordValue)+"\n");
this.status='gameOver';
} else {
console.log("\n YOU WON!\n The correct word was: \n "+chalk.greenBright(this.currentWord.wordRender())+"\n");
this.status='win';
}
};
module.exports = Game;