PDA

View Full Version : Hang man - problem customising


Diane
05-15-2003, 01:45 PM
Please can anyone help me with this? I am trying to replace the default word list in this Hangman game by Mike Mcgrath to make it more child-friendly. The script is from Javascript.com (http://javascriptkit.com/script/script2/hangman.shtml) (script source in full) and the problem I have is in this string of code which defines the wordlist:

var words = new Array("","acrimonious","allegiance","ameliorate","annihilate","watertight"); (Edited to remove long list of words)

Now, when I delete and/or replace the words in the string with shorter, simpler ones, the puzzle stops working. Can anyone tell me if 1) there needs to be a minimum number of words in the list and 2) do the words need to be a minimum number of letters in length? Is this where the problem lies, or do I need to look elsewhere in the script?

I've tried lots of variations, but have got to the point where I need some expert help. Thank you very much for your time - I really appreciate it.

Danne
05-15-2003, 03:27 PM
I think it will work if you replace this line:
index=Math.round(Math.random()*10000) % 100;

in function new_word(form) with this line:
index=Math.ceil(Math.random()*(words.length-1));

Then you can change the words array as u like. Don't remove the first item("") though.

Diane
05-15-2003, 04:02 PM
Many thanks - that seems to have cured the problem! I would never have solved this on my own :)

Kind regards,
Diane

Diane
02-25-2005, 04:15 PM
Current problem solved - thanks anyway.

Diane
04-15-2005, 07:09 PM
I have the same problem again as my original post, but the script has since been updated as given below, to comply with W3C/xhtml demands for name to be replaced by id. I have tried the solution given previously, but it no longer solves the problem. The script is working partially, ie a new random word from the array is given when "Go" is clicked, and there are some clues given. However, unless a used letter is entered (the pop-up alert works), there is no response from the game - the letter isn't entered or counted as a "life" if incorrect. I have played around with the line index=Math.round(Math.random()*10000)%words.length; without success, but Javascript is not something I know very much about. The empty "" at the start of the array for the random words has been removed from the updated script by the author.

Again, any help is very much appreciated! Thank you for you looking!

var alpha=new Array();
var alpha_index=0;

var bravo=new Array();
var bravo_index=0;

var running=0;
var failnum=0;
var advising=0;

function pick()
{
var choice="";
var blank=0;

for (i=0; i<words[index].length; i++)
{
t=0;
for(j=0; j<=alpha_index; j++)
if(words[index].charAt(i)==alpha[j] || words[index].charAt(i)==alpha[j].toLowerCase()) t=1;

if (t) choice+=words[index].charAt(i)+" ";
else
{
choice+="_ ";
blank=1;
}
}

document.getElementById("word").value=choice;

if (!blank)
{
document.getElementById("tried").value=" === You Win! ===";
document.getElementById("score").value++;
running=0;
}
}


function new_word()
{
if(!running)
{
running=1;
failnum=0;
document.getElementById("lives").value=failnum;
document.getElementById("tried").value="";
document.getElementById("word").value="";
index=Math.round(Math.random()*10000)%words.length;
alpha[0]=words[index].charAt(0);
alpha[1]=words[index].charAt(words[index].length-1);
alpha_index=1;
bravo[0]=words[index].charAt(0);
bravo[1]=words[index].charAt(words[index].length-1);
bravo_index=1;
pick();
}
else advise("A word is already in play!");
}

function seek(letter)
{
if (!running) advise(".....Click GO to start !");
else
{
t=0;
for (i=0; i<=bravo_index; i++)
{
if (bravo[i]==letter || bravo[i]==letter.toLowerCase()) t=1;
}

if (!t)
{
document.getElementById("f").tried.value+=letter+" "
bravo_index++;
bravo[bravo_index]=letter;

for(i=0;i<words[index].length;i++)
if(words[index].charAt(i)==letter || words[index].charAt(i)==letter.toLowerCase()) t=1;

if(t)
{
alpha_index++;
alpha[alpha_index]=letter;
}
else failnum++;

document.getElementById("f").lives.value=failnum;
if (failnum==6)
{
document.getElementById("f").tried.value="You lose - Try again!";
document.getElementById("f").word.value=words[index];
document.getElementById("f").score.value--;
running=0;
}
else pick();
}
else advise("Letter "+letter+" is already used!");
}
}

function advise(msg)
{
if (!advising)
{
advising=-1;
savetext=document.getElementById("tried").value;
document.getElementById("tried").value=msg;
window.setTimeout("document.getElementById('tried').value=savetext; advising=0;",1000);
}
}

var words = new Array("heart","lungs","kidneys","brain","skin","bones","skull","spine","stomach","blood","joints","skeleton","liver","muscles");