Hi I am trying to create a hangman game purely out of javascript, I am currently having an error where the hangman does not end even after the user has tried to answer the correct alphabet for a single word 9 times.
The goal of the game is to allow the user to guess up to 10 words , with the condition that they do not get any of the words wrong. E.g 1 word 7 attempts to enter the correct alphabet, if all 7 attempts are up then the game ends. If the user gets one word correct it moves on to the next word. The words are imported from a csv file. Thanks for helping out !
var readline = require("readline-sync");
class Word {
constructor(word, definition){
this.word = word
this.definition = definition
}
}
class WordCollection {
constructor(pool, words){
this.pool = [];
this.readFile();
}
readFile() {
const filePath = 'C:/FoP';
const filename = filePath + '/input.csv';
const fs = require('fs');
try {
const text =fs.readFileSync(filename, "UTF-8");
const textByLine = text.split('\r\n');
var wordline;
for (var i = 0 ; (i < textByLine.length) ; i++) {
wordline = textByLine[i].split(',');
this.pool.push(new Word(wordline[0],wordline[1]));
}
} catch(err) {;
console.log('file ' + " " + filename + " " + 'not found. Program terminated');
process.exit();
}
}
}
console.clear();
var hangmanPics = [
`
_____
| |
|
|
|
|
_|_
| |_____
| |
|_________|`,
`
_____
| |
| o
|
|
|
_|_
| |_____
| |
|_________|`,
`
_____
| |
| o
| |
|
|
_|_
| |_____
| |
|_________|`,
`
_____
| |
| o
| /|
|
|
_|_
| |_____
| |
|_________|`,
`
_____
| |
| o
| /|\\
|
|
_|_
| |_____
| |
|_________|`,
`
_____
| |
| o
| /|\\
| |
|
_|_
| |_____
| |
|_________|`,
`
_____
| |
| o
| /|\\
| |
| /
_|_
| |_____
| |
|_________|`,
`
_____
| |
| o
| /|\\
| |
| / \\
_|_
| |_____
| |
|_________|`]
console.log("\n" + "-= Welcome to HangMan =-")
var namein = readline.question("\n" + "Please Enter Your Name :")
var game = new WordCollection();
var wordrandom = game.pool[Math.floor(Math.random() * game.pool.length)];
var outputchosenword = wordrandom.word.toLowerCase();
console.log(outputchosenword)
var Alphabet = ['\nA', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
'I', 'J', 'K', 'L', 'M','\nN', 'O', 'P', 'Q', 'R', 'S',
'T', 'U', 'V', 'W', 'X', 'Y', 'Z\n'];
console.log(Alphabet.join(' '))
var attempts = 0;
var life = 9;
//Make array for answer
var answer = [];
for (var i = 0; i < outputchosenword.length; i++) {
answer[i] = "_";
}
var remainingLetters = outputchosenword.length;
//The game loop
while (remainingLetters > 0 ) {
//Showing the letters in form of "_"
console.log("This is the word to guess:\n" + answer.join(" "));
//Prompt player to guess
var guess = readline.question(namein + "'s guess (Enter 9 for lifelines or 0 to pass): ")
if (guess === null) {
//Leave the game
break;
} else if (guess.length !== 1) {
console.log("Please enter one single letter.");
} else {
//Update match with guess
var goodGuess = false;
for (var j = 0; j < outputchosenword.length; j++) {
if (outputchosenword[j] === guess) {
var no = 0;
console.log("Good Job ! You have found " + guess)
goodGuess = true;
// This is to check if the user has entered the same alphabet if so alert the user
if (answer[j] !== "_") {
console.log("Letter already be guessed");
break;
} else {
answer[j] = guess;
remainingLetters--;
}
}
}
}
if(!goodGuess){
console.log("Sorry, " + guess + " is not a part of the word ");
attempts++
console.log(hangmanPics[attempts]);
--life
}
}
//End of playing loop
//Show answer and congratulate the player
console.log(answer.join(" "));
console.log("Good work! The right answer is " + outputchosenword);