简体   繁体   中英

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. I had the incredibly basic game of hangman with a single word, no array being used, coded and working. 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/

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 -

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.

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. what it is doing now is taking a random character from the string 'option'

also guess is not defined inside the hangman function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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