我想做它,这样用户就可以猜出任何一个字母表,而不必按顺序排列。单词来自csv文件
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 numofwrong = 0;
var lettersremain;
var totallife = 9;
var lifelost = 0;
var answerArray=[];
var guess;
console.log("\n" + "-= Welcome to HangMan =-")
var namein = readline.question("\n" + "Please Enter Your Name :")
//Grab a random word from the array of words
var game = new WordCollection();
var wordrandom = game.pool[Math.floor(Math.random() * game.pool.length)];
var lastchosenword = wordrandom.word;
//console.log(wordrandom);
//Change letters into lowercase
var outputchosenword = wordrandom.word.toLowerCase();
console.log(outputchosenword)
// Convert the word to " _ "
var outputunderlineword = new Array(outputchosenword.length);
var checkoutputunderline = new Array();
for (i = 0; i < outputunderlineword.length; i++) {
outputunderlineword[i] = "_";
} console.log(outputunderlineword.join(" "))
//Display the Alphabets
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'];
console.log(Alphabet.join(' '))
var correctans=[];
for (var i = 0; i < outputunderlineword.length; i++) {
Enter = readline.question(namein + "'s guess (Enter 9 for lifelines or 0 to pass): ")
if (Enter == outputchosenword[i] || Enter.match(/[^a-zA-Z]/g)) {
console.log("Good Job! " + Enter + " is one of the letters")
correctans.push(Enter)
console.log(correctans)
}
else if (Enter !== outputchosenword[i] ) {
console.log("Sorry. " + Enter + " is not a part of the word.")
}
}-= Welcome to HangMan =-
Please Enter Your Name :hi
bat
_ _ _
A B C D E F G H I J K L M
N O P Q R S T U V W X Y Z
hi's guess (Enter 9 for lifelines or 0 to pass): b
Good Job! b is one of the letters
[ 'b' ]
hi's guess (Enter 9 for lifelines or 0 to pass): t
Sorry. t is not a part of the word.
hi's guess (Enter 9 for lifelines or 0 to pass): a
Sorry. a is not a part of the word.当字母表按顺序排列时,
-= Welcome to HangMan =-
Please Enter Your Name :hi
bat
_ _ _
A B C D E F G H I J K L M
N O P Q R S T U V W X Y Z
hi's guess (Enter 9 for lifelines or 0 to pass): b
Good Job! b is one of the letters
[ 'b' ]
hi's guess (Enter 9 for lifelines or 0 to pass): a
Good Job! a is one of the letters
[ 'b', 'a' ]
hi's guess (Enter 9 for lifelines or 0 to pass): t
Good Job! t is one of the letters
[ 'b', 'a', 't' ]var correctans=[];
while (outputchosenword[i] !== Enter || (lifelost = 9)) {
Enter = readline.question(namein + "'s guess (Enter 9 for lifelines or 0 to pass): ")
if ((outputchosenword.includes(Enter) || Enter.match(/[^a-zA-Z]/g))) {
console.log("Good Job! " + Enter + " is one of the letters")
correctans.push(Enter)
console.log(correctans)
} else {
console.log("Sorry. " + Enter + " is not a part of the word.")
}
} while (outputchosenword[i] !== Enter || (lifelost = 9)) {
^
ReferenceError: Enter is not defined在尝试了这些建议之后,
while (lifelost <= totallife && !isEntireWordFound) {
function guessLetter(wordguess, enter){
if(wordguess.includes(enter)){
var regex = new RegExp(enter,"g");
return wordguess.replace(regex,"");
}
return wordguess;
}
wordguess = guessLetter(wordguess,enter);
console.log("after guessing 'h' word = ",wordguess);
var enter = readline.question(namein + "'s guess (Enter 9 for lifelines or 0 to pass): ")
if (outputchosenword.includes(enter)) {
console.log("Good Job! " + enter + " is one of the letters");
correctans.push(enter);
console.log(correctans);
}
else {
console.log("Sorry. " + enter + " is not a part of the word.");
lifelost++;
}
} 发布于 2020-08-05 13:41:50
您对用户输入字母的调用是在单词的字母循环中进行的,您需要将其分开。伪码
while user has not correctly guessed word or run out of lives
get user guess
if users guess matches any letter in the word
do something
else
do something else如果不能(轻松)测试这段代码,您可能需要如下所示
var correctans=[];
var lifelost = 0
var totalLife = 9;
var isEntireWordFound = false; // set this to true somewhere!!
while (lifelost <= totallife && !isEntireWordFound) {
var enter = readline.question(namein + "'s guess (Enter 9 for lifelines or 0 to pass): ")
if (outputchosenword.includes(enter)) {
console.log("Good Job! " + enter + " is one of the letters");
correctans.push(enter);
console.log(correctans);
} else {
console.log("Sorry. " + enter + " is not a part of the word.");
lifelost++;
}
} 这当然是遗漏了你看到他们正确地猜出了整个单词的部分,以及你关于“通过”或“生命线”的部分--但希望能让你朝着正确的方向前进。注意这段代码是如何与上面的伪代码完全匹配的!!
让我们看看您如何确定用户是否已经猜到了这个词。不像第一次看上去那么容易,因为重复的字母--也就是说,你不能仅仅数数猜字母的数量等于单词的长度!
一个简单的方法是从单词中删除所有出现的猜测字母,当这个字母的长度变为零时,整个单词就被猜到了。
var word = "hello";
function guessLetter(word, chr){
if(word.includes(chr)){
var regex = new RegExp(chr,"g");
return word.replace(regex,"");
}
return word;
}
word = guessLetter(word,"h");
console.log("after guessing 'h' word = ",word);
word = guessLetter(word,"l");
console.log("after guessing 'l' word = ",word);
word = guessLetter(word,"o");
console.log("after guessing 'o' word = ",word);
word = guessLetter(word,"e");
console.log("after guessing 'e' word = ",word);
https://stackoverflow.com/questions/63266407
复制相似问题