我正在用java脚本编写一个hangman程序,如果玩家失败了,我需要帮助来显示这个词

I am writing a hangman program in java script and need help getting the word to display if the player loosed

本文关键字:失败 玩家 如果 显示 帮助 hangman java 脚本 一个 程序      更新时间:2023-11-11

这是javascript代码:

我需要帮助,让字显示后,球员失去。

var can_play = true;
//this is the array of words
var words = new Array("VALIDATE", "DESIGN", "INPUT", "ARRAY", "OBJECT", "DOCUMENTATION", "JQUERY", "CALCULATE", "ABSOLUTE", "DREAMWEAVER", "BROWSER", "HTML", "CONCATINATION");
var display_word = "";
var used_letters = "";
var wrong_guesses = 0;
//this will allow the letters to be entered in only 1 time
function selectLetter(l) {
    if (can_play == false) {
        return;
    }
    if (used_letters.indexOf(l) != -1) {
        return;
    }
    used_letters += l;
    document.game.usedLetters.value = used_letters;
    if (to_guess.indexOf(l) != -1) {
        // this will display the correct letter guesses
        pos = 0;
        temp_mask = display_word;

        while (to_guess.indexOf(l, pos) != -1) {
            pos = to_guess.indexOf(l, pos);
            end = pos + 1;
            start_text = temp_mask.substring(0, pos);
            end_text = temp_mask.substring(end, temp_mask.length);
            temp_mask = start_text + l + end_text;
            pos = end;
        }
        display_word = temp_mask;
        document.game.displayWord.value = display_word;
        if (display_word.indexOf("*") == -1) {
            // this will display a message if you win
            $('#win').html("Well done, you won!");
            can_play = false;
        }
    } else {
        // this will display the incorrect letter guesses
        wrong_guesses += 1;
        $('#wrong_guesses').html(wrong_guesses);
        if (wrong_guesses == 6) {
            // this will display a message if you loose
            $('#win').html("Sorry, you have lost!");
            can_play = false;
        }
    }
}
//this will reset the game to play again
function reset() {
    selectWord();
    document.game.usedLetters.value = "";
    guessed_letters = "";
    wrong_guesses = 0;
    $('#win').html("");
    $('#wrong_guesses').html("");
}
//this will have the computer select a word from my array
function selectWord() {
    can_play = true;
    random_number = Math.round(Math.random() * (words.length - 1));
    to_guess = words[random_number];

    // this will display mask 
    masked_word = createMask(to_guess);
    document.game.displayWord.value = masked_word;
    display_word = masked_word;
}
function createMask(m) {
    mask = "";
    word_length = m.length;

    for (i = 0; i < word_length; i++) {
        mask += "*";
    }
    return mask;
}
$('#win').html("Sorry, you have lost, the word was " + to_guess + "!");

你在这里指定了待猜单词:

to_guess = words[random_number];

您将从将代码发布到代码审查中学到很多东西。