View Single Post
 
Old June 15th, 2008, 07:33 PM
martoufmarty martoufmarty is offline
Registered User
 
Join Date: Jan 2008
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The code helped me a bit. I'm almost there. I have the strings going to loop through and added together, but I don't know why it's not showing the letters like it's supposed to.

<!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 alphabit = "abcdefghijklmnopqrstuvwxyz";
    var foundLets;
    var guessletter;
    var lettersguess = new Array ();
    var loses = 0;
    var mask;
    var misses = 0;
    var num;
    var pics = new Array("hang1.jpg","hang2.jpg","hang3.jpg","hang4.j pg","hang5.jpg","hang6.jpg","hang7.jpg","hangwin.j pg");
    var randy;
    var ranword;
    var used;
    var w = 0;
    var wins;
    var words = new Array("bollywood", "chalo", "dekho", "pagal", "rangeela");

// Function for picking a random word.

function pickword()
{

  // Set letter guessed to nothing.

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

    // Set image back to first.

        misses = 0;
        num = -1;

        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 = "";

        for (i = 0; i < ranword.length; 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.");

}

// Function for guessing letters.

function guesslet()
{

    // Change letter to lowercase.

    guessletter = (document.hangman.letter.value).toLowerCase();

    if (alphabit.indexOf(guessletter) == -1 || !(guessletter.length == 1)) {
        alert("Please type only letters, and if you are typing letters, only type one at a time.");
    }

    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++;
            num = -1;
            foundLets = "";

            for (j=0; j< ranword.length; j++)
                {
                num++;

                if (ranword.charAt(num) == guessletter) {

                    foundLets += ranword.charAt(num);

                         if ((document.hangman.word.value).indexOf('*') == -1) {

                                document.hangpic.src = pics[7];
                                alert("You won! Congrats! The word was " + ranword + ". Click 'OK' when you're ready to play again.");
                                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();
                                    }
                        }
                }

                else {
                    foundLets += mask.charAt(num);
                // 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();
                                }
                    }
                }
            document.hangman.word.value = foundLets;
            break;
            }
        }

    }

}

// Refresh game to start over again, if user wants.


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>
<p>&nbsp;
</p>

</form>

</body>
</html>