gpt4 book ai didi

javascript - 刽子手游戏中的增量字母

转载 作者:行者123 更新时间:2023-11-30 06:13:20 26 4
gpt4 key购买 nike

如果用户猜错了,我需要一次添加字母 H、A、N、G、M、A、N

我尝试过使用循环,但不确定该怎么做

function playgame(yourword) {

//yourword array
/*********************/
let word = yourword.toUpperCase();
let letters = word.split("");
var answerArray = [letters[0]];
for (var i = 1; i < letters.length; i++) {
answerArray[i] = "_";
}
let answer = answerArray.join();

//hangman array
/*********************/
let hgm = ["H", "A", "N", "G", "M", "A", "N"];
let hgArray = [hgm[0]];
for (var i = 1; i < hgm.length; i++) {
hgArray[i] = "_";
}
//loop
let remainingLetters = word.length;
let remainingLetter = hgm.length;
while (remainingLetters > 0 & remainingLetter > 0) {

//show the array
console.log(answerArray.join(" ").toUpperCase());
console.log(hgArray.join(",").toUpperCase());

let guess = prompt("guess a letter").toUpperCase();
//add a letter if guessed correctly
for (var i = 0; i < letters.length; i++) {
if (word[i] === guess) {
answerArray[i] = guess;
remainingLetters--;
}
//add a letter to the hangman string if guessed incorrecty
//MY PROBLEM IS HERE

for (var h = 0; h < hgm.length; h++) {
if (word[h] !== guess) {
remainingLetter--;
}

//TO HERE

}

}
console.log(answerArray.join(" ").toUpperCase());
console.log(hgArray.join(",").toUpperCase());
if (remainingLetters === 0) {
console.log("you win");
} else if (remainingLetters !== 0) {
console.log("you lose");
}
}
}

我曾尝试使用循环来递增下一个字母,但我不确定该怎么做。

请帮忙

最佳答案

您不需要循环,因为您只想在当前位置插入一个字母。

function playgame(yourword) {

//yourword array
/*********************/
let word = yourword.toUpperCase();
let letters = word.split("");
var answerArray = [letters[0]];
for (var i = 1; i < letters.length; i++) {
answerArray[i] = "_";
}
let answer = answerArray.join();

//hangman array
/*********************/
let hgm = ["H", "A", "N", "G", "M", "A", "N"];
let hgArray = [hgm[0]];
for (var i = 1; i < hgm.length; i++) {
hgArray[i] = "_";
}
//loop
let remainingLetters = word.length - 1;
let remainingLetter = hgm.length;
let currentHangmanPosition = 1;
while (remainingLetters > 0 & remainingLetter > 0) {

//show the array
console.log(answerArray.join(" ").toUpperCase());
console.log(hgArray.join(",").toUpperCase());

let guess = prompt("guess a letter").toUpperCase();
//add a letter if guessed correctly

if (letters[word.length - remainingLetters] == guess) {
answerArray[word.length - remainingLetters] = guess;
remainingLetters--;
}else{
hgArray[currentHangmanPosition] = hgm[currentHangmanPosition];
currentHangmanPosition++;
}


console.log(answerArray.join(" ").toUpperCase());
console.log(hgArray.join(",").toUpperCase());
if (remainingLetters === 0) {
console.log("you win");
} else if (currentHangmanPosition == hgm.length) {
console.log("you lose");
}
}
}

关于javascript - 刽子手游戏中的增量字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57640439/

26 4 0
文章推荐: java - 使用泛型时复合模式的可扩展性
文章推荐: java - 方法覆盖和继承
文章推荐: java - 如何从包含键 String 和 list 的 hashmap 中仅获取一个值