View Single Post
 
Old June 3rd, 2008, 04:16 PM
martoufmarty martoufmarty is offline
Registered User
 
Join Date: Jan 2008
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default Letter replace in hangman.

Hi, I'm pretty new to JavaScript. I've been trying to make this Hangman game for the longest time. I have everything done except that I can't actually get the correct letter to reveal itself. I got the first letter to show up, but nothing else.

I can't figure out what I've been doing wrong, and I don't know what else to try. Someone suggested that I create an empty string and add it to the masked word, but I honestly have no idea how to get that to work.

Here's my code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<script language="javascript" type="text/javascript">

var words;
var randy;
var ranword;
var mask;
var wordslong;
var pics;
var guessletter;
var misses;
var i;
var wins;
var loses;
var used;
var lettersguess;
var guessit;
var charguess;
var wordval;
var w;
var h;
var letno;

var alphabit;

var chnum = -1;

w = 0;

h = 0;

misses = 0;

loses = 0;

pics = new Array("hang1.jpg","hang2.jpg","hang3.jpg","hang4.j pg","hang5.jpg","hang6.jpg","hang7.jpg","hangwin.j pg");

words = new Array("bollywood", "chalo", "dekho", "pagal", "rangeela");

alphabit = new Array("a","b","c","d","e","f","g","h","i","j","k", "l","m","n","o","p","q","r","s","t","u","v","w","x ","y","z");

lettersguess = new Array ();

// Function for picking a random word.

// TOUCH AND YOU GET KILLED BY HORRIBLE FIRE.

function pickword()
{

// Set letter guessed to nothing.

        document.hangman.letter.value = "";
        w = 0;
        lettersguess = [];
        chnum = -1;

    // Set image back to first.

        misses = 0;

        document.hangpic.src = pics[misses];

    // Pick random word from 'words' Array.
        randy = Math.floor(Math.random() * words.length);
        ranword = words[randy];

// Create mask for 'ranword'.
        mask = "";
        wordslong = ranword.length;

    for (i = 0; i < wordslong; i++) {
        mask += "*";
        }

    // Display 'ranword' in text box 'word'.
        document.hangman.word.value = mask;

}

// Function for number of wins and loses.

// DON'T TOUCH winlost()!

function winlost()
{
wins = 5 - loses;
                            alert("You've gone through all the words! Out of the five games you played, you lost " + loses + ", and won " + wins +"!");
             alert("Press 'Refresh' then 'New Word' to start the games all over again... Even though you already know what all the answers are. Cheater.");

}

j=0;

// DON'T TOUCH letnomatch()!!!


function letnomatch() {
        // If letter is not in random word, change picture.
             document.hangpic.src = pics[misses+1];
            misses++;


         // Alert upon loss, return everything to zero.
            if (misses == 6) {
                    alert("You lost! The answer was " + ranword);
                         loses++;
                            words.splice(randy,1);
                 lettersguess.length = 0;
                 w = 0;
                        document.hangpic.src = pics[0];
                 document.hangman.word.value = "";
                 document.hangman.letter.value = "";

             // If no words left, alert and inform number of losses.

                        if (words.length == 0) {
             winlost();
                        }

            // If words are left, continue with next game.
                        else {
                            pickword();
                        }
                }
}


// Function for guessing letters.

function guesslet()
{

    guessit = document.hangman.letter.value;

    // Change letter to lowercase.

    guessletter = guessit.toLowerCase();


    letno = alphabit.indexOf(guessletter);

    if (letno == -1) {

        alert("Please type only letters.");

    }

    else {


    // Check if letter has already been guessed.


    used = lettersguess.indexOf(guessletter);

// If already used, alert.

    if (used !== -1) {
        alert("You've already guessed the letter " + guessletter + "! Guess a different letter.");
    }

// If not, add to array.

    if (used == -1) {

         lettersguess[w] = guessletter;

    w++;


// The evil section which all my problems are hiding.

    var stringTest = " ";


    for (j=0; j<wordslong; j++)
        {

        chnum++;

        charguess = ranword.charAt(chnum);

        var indexlet = ranword.indexOf(guessletter)

        if (indexlet == -1) {
            letnomatch();
        }


        if (guessletter == charguess) {
            document.hangman.word.value = mask.replace(mask.charAt(chnum), guessletter);

        }

        if (!(indexlet == -1) && !(guessletter == charguess)) {
            // Do I need a loop here to go through the numbers? I have this in place so I can see what number it says.
alert(chnum);
        }
    break;}

}

}

}

// DON'T TOUCH!!!


function refreshfunc() {

     location.reload(true);

}


</script>

</head>

<body onload="document.hangpic.src = pics[misses];">

<form name="hangman">

<img name="hangpic" src="" width="200" height="300" />

<p>
<input type="text" name="word" /><br />

<input type="text" name="letter" /></p>

<p><input type="button" name="guessbut" onClick="guesslet();" value="Guess A Letter" /> <br />
<input type="button" name="playgame" onClick="pickword();" value="New Word"/></p>
<p>
<input type="button" name="refreshpage" onClick="refreshfunc();" value="Refresh" />
</p>


</form>

</body>
</html>


I know, it's a bit of a mess, and I probably have a couple extra defined variables, and I have left myself little messages of what had to be fixed and what was to be left alone.

Like I said, I'm pretty new to JavaScript, so forgive me.

Any help would be muchly appreciated.