简体   繁体   English

代码没有执行,可能有语法问题,或者我可能完全错了

[英]Code just isn't performing, might have a syntax issue or i could be completely wrong

So I have a simple project of - Create a Hangman game. 因此,我有一个简单的项目-创建一个Hangman游戏。 I had the incredibly basic game of hangman with a single word, no array being used, coded and working. 我使用一个单词就完成了hangman令人难以置信的基本游戏,没有使用,编码和工作数组。 Upon adding in additive guesses and an array that randomized words, it no longer works. 在添加附加猜测和随机单词数组之后,它不再起作用。

From what I can tell, It no longer chooses a word (the dashes that take place of the letters only shows one dash now, before when there was no array and I had a preset word, it showed dashes for each letter), so therefor all guesses are wrong. 据我所知,它不再选择一个单词(代替字母的破折号现在只显示一个破折号,在没有数组并且我有一个预设的单词之前,每个字母都显示破折号),因此所有的猜测都是错误的。 Secondly, even though all these guesses are wrong, none are being counted. 其次,尽管所有这些猜测都是错误的,但没有一个被计算在内。

JSfiddle - http://jsfiddle.net/7jo8w1zw/ JSfiddle- http://jsfiddle.net/7jo8w1zw/

HTML - HTML-

<body>


<form id="form" name="form" method="post" action="">
<input type="button" id="but" value="Start"/>
<div id="hangman-jquery">
    <div id="word"></div>
    <div id="alpha"></div>
</div>
</form>

<div id="win">
</div>

<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="hangman.js"></script>
</body>

jquery - jQuery-

function hangman(word) {
    var trys = 0
    var alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $.each(alpha.split(''), function(i, val) {
        $('#alpha').append($('<span class="guess">' + val + '</span>'));
    });
    $.each(word.split(''), function(i, val) {
        $('#word').append($('<span class="letter" letter="' + val + '">-</span>'));
    });
    $('.guess').click(function() {
        var count = $('#word [letter=' + $(this).text() + ']').each(function() {
            $(this).text($(this).attr('letter'));
        }).length;
        $(this).removeClass('guess').css('color', (count > 0 ? 'green' : 'red')).unbind('click');

        if (guess > 0) {
        $('#win').text("Correct Guess");
        } else if (guess < 0) {
        $(this).html(++trys);
        $('#win').text("You have tried to guess the word and failed " + trys + " times");
        }
        if (trys == 6) {
        alert("You have guessed six times, you lose");
        trys = 0;
        $("#win").text("");
        }
    });
}

$(document).ready(function() {
    $('#but').click(function() {
        var options = new Array("Dog", "Cat", "Bat", "Horse", "Tiger", "Lion", "Bear", "Liger", "Doom", "Spider", "Trees", "Laptop");
        var random = 1 + Math.floor(Math.random() * 12);
        hangman('options'[random]);
    });
});

/*Createa web page with the game Hangman, in which the user guesses letters in a hidden word. 
Create an array with at least a dozen words and pick one at random each time the game is started. 
Display a dash for each missing letter. Allow the user to guess letters continuously 
(up to 6 guesses) until all the letters in the word are correctly guessed. 
As the user enters each guess, display the word again, filling in the guess if it was correct. 
For example, if the hidden word is “ computer”, first display --------. 
After the user guesses “ p”, the display becomes ---p----. Make sure that when a user makes a 
correct guess, all the matching letters are filled in. For example, if the word is “ banana”, then
when the user guesses “ a”, all three “ a” characters are filled in. (25 points)
*/

I threw in my instructions at the end in case anyone cared to read it over. 最后,我附上说明,以防有人介意阅读。 Otherwise im not getting any errors as far as i can tell (im not great at understanding firebug just yet), its just not doing what i hoped it to. 否则,就我所知,即时消息我没有得到任何错误(我还不是很了解Firebug),只是没有按照我希望的那样做。

Thank you ahead of time. 提前谢谢你。 Your help as usual is invaluable! 像往常一样,您的帮助非常宝贵!

you wrote hangman('options'[random]);

options is a var so it shouldn't be between quotes. options是一个var,因此不应在引号之间。 what it is doing now is taking a random character from the string 'option' 现在要做的是从字符串'option'中获取随机字符

also guess is not defined inside the hangman function. 还猜测未在hangman函数中定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM