Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Sep 2005
    Location
    Chicago, IL
    Posts
    160
    Thanks
    0
    Thanked 0 Times in 0 Posts

    js game won't work :(

    OK, so I have this code in a .js file and it works fine the way it is (as shown below), but as soon as I change the words it won't work anymore. I checked the javascript console on Netscape and it said that [index] on line 51 (which is: alpha[0]=words[index].charAt(0) and 79 (which i'm not sure) has no properties. But yet if I put in the words that are there below it works fine, I'm really confused and I must be missing something small and stupid cause it's not making any sense right now. You can see a ruogh version of the game (it's a hangman game ) at: http://savepitbulls.8m.com/newwin/pithangman.html Please help me.

    Here's the working code:
    PHP Code:
    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=0i<words[index].lengthi++)
      {
        
    t=0;
        for(
    j=0j<=alpha_indexj++) 
        if(
    words[index].charAt(i)==alpha[j] || words[index].charAt(i)==alpha[j].toLowerCase()) t=1;
        
        if (
    tchoice+=words[index].charAt(i)+" ";
        else 
        {
          
    choice+="_ ";
          
    blank=1;
        }
      }   
        
      
    document.f.word.value=choice;
        
      if (!
    blank)
      {
        
    document.f.tried.value="   === You Win! ===";
        
    document.f.score.value++;
        
    running=0;
      }



    function 
    new_word(form)
    {
      if(!
    running)
      {
        
    running=1;
        
    failnum=0;
        
    form.lives.value=failnum;
        
    form.tried.value="";
        
    form.word.value="";
        
    index=Math.round(Math.random()*10000) % 100;
        
    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 (!
    runningadvise(".....Click GO to start !");
      else
      {
        
    t=0;
        for (
    i=0i<=bravo_indexi++)
        {
          if (
    bravo[i]==letter || bravo[i]==letter.toLowerCase()) t=1;
        }

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

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

    var 
    words = new Array("","acrimonious","allegiance","ameliorate","annihilate","antiseptic","articulate","authoritative","benefactor","boisterous","breakthrough","carcinogenic","censorious","chivalrous","collarbone","commendable","compendium","comprehensive","conclusive","conscientious","considerate","deferential","denouement","determinate","diffidence","disruption","earthenware","elliptical","entanglement","escutcheon","extinguish","extradition","fastidious","flamboyant","forethought","forthright","gregarious","handmaiden","honeysuckle","hypocritical","illustrious","infallible","lumberjack","mischievous","mollycoddle","nimbleness","nonplussed","obliterate","obsequious","obstreperous","opalescent","ostensible","pandemonium","paraphernalia","pawnbroker","pedestrian","peremptory","perfunctory","pernicious","perpetrate","personable","pickpocket","poltergeist","precipitous","predicament","preposterous","presumptuous","prevaricate","propensity","provisional","pugnacious","ramshackle","rattlesnake","reciprocate","recrimination","redoubtable","relinquish","remonstrate","repository","reprehensible","resolution","resplendent","restitution","retaliation","retribution","saccharine","salubrious","skulduggery","skyscraper","soothsayer","tearjerker","transcribe","turpentine","unassuming","underscore","undertaker","underwrite","unobtrusive","vernacular","waterfront","watertight"); 
    Here's the words i'm changing it too, just incase you wanted to know:
    PHP Code:
    var words = new Array("","breed","brindle","bsl","game","harness","kennel","pitbull","puppies","rednose","springpole","terrier"); 
    It's a pit bull hangman games, using pit bull words. The problem is it doesn't matter what words you put in, if you change the words it gives you an error and won't work.
    I'm only telling you how I would do it, not how its supposed to be done. ;)

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,511
    Thanks
    3
    Thanked 503 Times in 490 Posts
    index=Math.round(Math.random()*10000) % 100;

    exceeds words.length

  • #3
    Regular Coder
    Join Date
    Sep 2005
    Location
    Chicago, IL
    Posts
    160
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Alright, one last question. I understand what the Math.random and everything does, but what does the % 100; do at the end of that line? Wouldn't it be better to just use Math.floor(Math.random()*words.length) ? P.S.: There's a 101 words in the first array, incase you wanted to know.
    I'm only telling you how I would do it, not how its supposed to be done. ;)

  • #4
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,511
    Thanks
    3
    Thanked 503 Times in 490 Posts
    % = Modulus arithmetic

    i.e. the digits after the decimal point after division

    using the array length is safer than a fixed value

  • #5
    Regular Coder
    Join Date
    Sep 2005
    Location
    Chicago, IL
    Posts
    160
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Awesome, thanks for your help. It's always the little stuff you never realize that throws everything off. Thanks alot!
    I'm only telling you how I would do it, not how its supposed to be done. ;)

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by vwphillips
    i.e. the digits after the decimal point after division
    That's an innacurate description - modulus does not give the digits after the decimal point in a division, it gives the remainder of a division, so 9 % 4 = 1
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •