Hello!
I am in an intro to web programming class in which our final project is to make a functioning hangman game. I feel like he spent a lot more time on html and css and then just sort of flew right on through javascript. We are supposed to put together pieces of what we have learned to form the game. I got it working so that there is a secret word displayed as *'s upon page load. I made it so that the guesses may only be one character and error checked so that an alert will pop up not letting you guess numbers or symbols. I am having trouble with updating and loading the page address (based upon the added letters to either rite or rong guesses). The hangman images were cycling through when I was not trying to make it mesh up with the length of the wrong guesses string, but now it is not working. Here is my code at this point...
<html>
<head><title>Hangman</title>
<script type="text/javascript">
var seekrit = "elephant"; //Variable for the secret word
var rite = ""; //Variable to store right guesses
var rong =""; //Variable to store wrong guesses
function wordBlendr(wrd1, wrd2) {
var pos;
var ary = new Array();
len2 = wrd2.length; len1 = wrd1.length;
ary.length = len1;
for (i=0; i<len1; i++) { // Initialize ary
ary[i] = "*";
}
for (i=0; i<len2; i++) { // For each char in word2
char = wrd2.charAt(i); // get char
pos = wrd1.indexOf(char); // get its position
while (pos != -1) {
ary[pos] = char;
pos = wrd1.indexOf(char, pos+1); // get new position
}
}
for (i=0; i<len1; i++) { // Print out array contents
document.write(ary[i]);
}
}
function btnClik_onclick() {
var iChars = "!@#$%^&*()+=-[]\\\';,./{} |\":<>?"; //Illegal chars.
var yourGuess = document.form1.Guess.value //Value of the users guess.
var noalpha = /^[0-9]$/; //Illegal numbers
var num = rong.length //Length of rong guesses to determine which hangman image to print.
yourGuess = yourGuess.toLowerCase(); //Convert all to lower case
if (noalpha.test(yourGuess)){ //VALIDATION -- Will not let numbers be guessed.
alert ("Your guess may only be a letter");
return false;
cliks-=1;
}
if (iChars.indexOf(yourGuess.charAt(0)) != -1) { //VALIDATION -- Will not let special characters be guessed.
alert ("Your guess may only be a letter");
return false;
cliks -=1;
}
if (seekrit.indexOf(yourGuess.charAt(0)) != -1) { //If the letter guessed is in the seekrit variable
rite = rite.concat((yourGuess.charAt(0))); //Add guess to rite var.
return true;
document.write(wordBlendr(seekrit,rite)); //Print out wordBlendr to show location of correct guess in word
}
else {
rong = rong.concat((yourGuess.charAt(0))); //Otherwise (letter guess not in seekrit variable, guess added to rong variable.
return false;
}
var cliks = 0;
var strAddr = window.location.href; // Get page address
if (strAddr.indexOf("?") == -1) {
nuAddr = strAddr + "?cliks=1"; // Add "?", cliks
}
else {
pos = strAddr.indexOf("cliks="); // get cliks
cliks = parseInt(strAddr.substring(pos+6));
cliks++; // increment
nuAddr = strAddr.substr(0,pos+6) + cliks; // generate new
}
window.location.href = nuAddr; // GO TO NEW ADDRESS
}
function printIMG(num) {
if (num>10){
num=14;}
document.write("<img src='HIMG/H" + num + ".GIF'><br/>");
Major problem with your code is that document.write() can only be use before the page is rendered. If you call it after the page displays, it wipes out the previous version and starts anew. This will definitely not be what you want after the 1st guess of the Hangman word.
You might want to look into .innerHTML command if that has been presented in your class.
Bookmarks