私は現在ハングマンのゲームをjavascriptで作成していますが、以前に選択した単語を新しい単語に置き換えて置き換えません。現在、私は自分のコードを設定して、プレイヤーが単語を正しく推測すると、メッセージがポップアップし、ユーザーに再度プレイしたいかどうかを尋ねるように設定しています。彼らがYを押すと、単語と配列をランダムに選択する関数を呼び出し、プッシュメソッドを使用して選択した単語の長さが同じ空白行を空の配列にします。しかし、Yを押した後にこの関数を呼び出すと、前の単語は消えず、キー押下も登録されません。前の選択された単語をhtmlで消して、新しい単語で置き換えてください。JavaScriptのHangmanのゲームで
var hangmanObject = {
randomWords: ['rock','paper','modular synthesizer', 'led zeppelin'],
chosenWord: "",
numWins: 0,
numLives: 10,
empty: [],
incorrect: [],
splitArray: []
}
function startFunction()
{
document.getElementById("numLives").innerHTML = hangmanObject.numLives;
displayChosenWord();
}
function displayChosenWord()
{
hangmanObject.chosenWord = hangmanObject.randomWords[Math.floor(Math.random()*hangmanObject.randomWords.length)]
hangmanObject.splitArray = hangmanObject.chosenWord.split("");
for (x = 0; x < hangmanObject.chosenWord.length; x++)
{
if (hangmanObject.chosenWord.charAt(x) === " ")
hangmanObject.empty.push(" ");
else
hangmanObject.empty.push(" _ ");
}
document.getElementById("blanks").innerHTML = hangmanObject.empty.join("");
}
document.onkeyup = function(event)
{
var userGuess = String.fromCharCode(event.keyCode).toLowerCase();
for (x = 0; x < hangmanObject.chosenWord.length; x++)
{
if (hangmanObject.chosenWord.charAt(x) === userGuess)
{
hangmanObject.empty[x] = userGuess;
document.getElementById("blanks").innerHTML = hangmanObject.empty.join("");
}
}
if (hangmanObject.splitArray.indexOf(userGuess) === -1) //checking to see if wrong letter chosen
{
hangmanObject.numLives--;
document.getElementById("numLives").innerHTML = hangmanObject.numLives;
hangmanObject.incorrect.push(userGuess);
document.getElementById("LettersGuessed").innerHTML = hangmanObject.incorrect;
}
console.log(hangmanObject.empty);
if (hangmanObject.empty.indexOf(" _ ") === -1)
{
hangmanObject.numWins++;
// console.log("i won");
document.getElementById("wins").innerHTML = hangmanObject.numWins;
document.getElementById("Play").innerHTML = "Play Again? Y/N";
document.onkeyup = function(event)
{
// Determines which exact key was selected. Make it lowercase
var Choice = String.fromCharCode(event.keyCode).toLowerCase();
if (Choice === 'y')
{
hangmanObject.numLives = 10;
displayChosenWord();
}
}
}
if (hangmanObject.numLives <= 0)
{
document.getElementById("lose").innerHTML = "You Lose";
}
}
ああそうです。だから私の主な問題は、私は空の配列をリセットしていなかったことと、空の配列に間違った配列が再びありませんでした。私はこれを試していただきありがとうございます。 – henhen
どうすればif文がすぐ上のif文内にネストされているか(if(userGuess == 'y')if文があるのですか?if(hangmanObject.gameState === 'finished'){ document。 getElementById( "Play")innerHTML = "Play Again?Y/N"; } //そのif文の中にif(userGuess == 'y')をネストしようとしましたが、gameStateを "playing"に設定しましたが、プログラムは次の単語に対して押されたキーを登録しません。 。それはなぜそれをするのですか? – henhen
'onkeyup'コールバックの実行ごとに一度だけ' userGuess'値を評価することができます。つまり、キーアップイベントが発生するたびに、if-elseツリー全体が評価されます。 詳細を見ると、 'gameState === 'playing''ブランチの中で' gameState ===' finished''が 'true'と評価される唯一の方法は、**最後に押されたキーが終了したために**変更されました。そのため、「Play again(Y/N)」メッセージが設定され、「finished」ブランチの次のkeyupイベントまで「y」または「n」キーが評価されません。それが明確になることを願っています。 –