Code works with 4 letter words, but not random word length

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tpgames
    Contributor
    • Jan 2007
    • 783

    Code works with 4 letter words, but not random word length

    I do not understand why this code does not work? It will show ????? for the word length, but will does not actually access the individual letters within the word list. I enter the vowels as guesses, and NONE of the vowels are said to be in the word. I'm trying to put the working code in a separate JS file, instead of having the silly thing on the same HTML page. This might allow me to incorporate user choosing which game to play within the same HTML page, if my other support issue gets resolved.
    Thanks!

    ps. The code appears to work if I have the JS on the same page as the HTML.

    Erroroneous JS code
    Code:
    //characters can not be longer than 16 letters because browser will not show it.
    var can_play = true;
    var words = new Array("waschbecken","toilette","seife","badewanne","wasserhahn","handtuch","dusche","waschlappen","toilettenpapier","wasser","zahnpasta","zahnbürste");
    
    var to_guess = "";
    var display_word = "";
    var used_letters = "";
    var wrong_guesses = 0;
    
    
    function selectLetter(l)
    {
    if (can_play == false)
    {
    return;
    }
    
    if (used_letters.indexOf(l) != -1)
    {
    return;
    }
    	
    used_letters += l;
    document.game.usedLetters.value = used_letters;
    	
    if (to_guess.indexOf(l) != -1)
    {
     // correct letter guess
    pos = 0;
    temp_mask = display_word;
    
    
    while (to_guess.indexOf(l, pos) != -1)
    {
    pos = to_guess.indexOf(l, pos);			
    end = pos + 1;
    
    start_text = temp_mask.substring(0, pos);
    end_text = temp_mask.substring(end, temp_mask.length);
    
    temp_mask = start_text + l + end_text;
    pos = end;
    }
    
    display_word = temp_mask;
    document.game.displayWord.value = display_word;
    		
    if (display_word.indexOf("?") == -1)
    {
    // won
    alert("You've saved the Book from Evaporation! Congratulations, Mazel Tov and all that good stuff!");
    can_play = false;
    }
    }
    else
    {
    // incorrect letter guess
    
    // 12 guesses
    wrong_guesses += 1;
    eval("document.hm.src=\"/gaming/2/word/evapbook/12/" + "hm" + wrong_guesses + ".gif\"");
    		
    if (wrong_guesses == 12)
    {
    // lost
    alert("Oh no! The Book is evaporating! Please try again!");
    can_play = false;
    }
    }
    }
    
    function reset()
    {
    selectWord();
    document.game.usedLetters.value = "";
    used_letters = "";
    wrong_guesses = 0;
    document.hm.src="/gaming/2/word/evapbook/12/hmstart.gif";
      var links = document.getElementsByTagName("a");   
          for (i = 0; i < links.length; i++) {
              links[i].style.color = "#0000ff";
          }
    }
    
    function selectWord()
    {
    can_play = true;
    random_number = Math.round(Math.random() * (words.length - 1));
    to_guess = words[random_number];
    //document.game.theWord.value = to_guess;
    	
    // display masked word
    masked_word = createMask(to_guess);
    document.game.displayWord.value = masked_word;
    display_word = masked_word;
    }
    
    function createMask(m)
    {
    mask = "";
    word_length = m.length;
    
    
    for (i = 0; i < word_length; i ++)
    {
    mask += "?";
    }
    return mask;
    }
    The HTML code:

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html lang="utf-8">
    <head>
    <title> Scrambled Words </title>
    
    <META http-equiv="expires" content="Thur, 1 March 2007 13:00:00 GMT">
    <meta http-equiv="Content-Type" content="text/html charset=utf-8">
    <link type="text/css" rel="stylesheet" 
    href="http://www.globalwritersclub.com/gaming/2/word/scrambled/css/scrambled.css"> 
    
    <script language="Javascript" src="http://www.globalwritersclub.com/gaming/2/word/evapbook/eb/de/js/1a.js"></script>
    
    <!-- script originally by Chris Fortey @ http://www.c-g-f.net -->
    
    
    </HEAD>
    
    <body onLoad="reset(); return true;">
    
    <table border="0" cellpadding="0" cellspacing="0" width="100%" Summary="NAV TABLE">
     <tr>
    <td class="tcp">
    
    <br>
    The Book Rescue
    <br>
    Plug in your guesses below!<br>
    You have 12 guesses.
    <br>&nbsp;<br>
    
    <!--THE SCRIPT THAT HE USES-->
    
    
    <img src="/gaming/2/word/evapbook/12/hmstart.gif" height="230" width="266" name="hm" alt="Image used to display stages of errors."> 
    
    <form name="game">
    
    <p><center>
    Display Word: <input type="text" name="displayWord"><br>
    
    Used Letters: <input type="text" name="usedLetters"> </center></p>
    </form>
    
    
    
    <p><center>
    
    <a href="javascript:selectLetter('A');" onclick="this.style.color='#ff0000'">A</a> |
    <a href="javascript:selectLetter('Ä');" onclick="this.style.color='#ff0000'">Ä</a> |
    <a href="javascript:selectLetter('B');" onclick="this.style.color='#ff0000'">B</a> | 
    <a href="javascript:selectLetter('C');" onclick="this.style.color='#ff0000'">C</a> | 
    <a href="javascript:selectLetter('D');" onclick="this.style.color='#ff0000'">D</a> | 
    <a href="javascript:selectLetter('E');" onclick="this.style.color='#ff0000'">E</a> | 
    <a href="javascript:selectLetter('F');" onclick="this.style.color='#ff0000'">F</a> | 
    <a href="javascript:selectLetter('G');" onclick="this.style.color='#ff0000'">G</a> | 
    <a href="javascript:selectLetter('H');" onclick="this.style.color='#ff0000'">H</a> | 
    
    <a href="javascript:selectLetter('I');" onclick="this.style.color='#ff0000'">I</a> | 
    <a href="javascript:selectLetter('J');" onclick="this.style.color='#ff0000'">J</a> | 
    <a href="javascript:selectLetter('K');" onclick="this.style.color='#ff0000'">K</a> | 
    <a href="javascript:selectLetter('L');" onclick="this.style.color='#ff0000'">L</a>
    <a href="javascript:selectLetter('M');" onclick="this.style.color='#ff0000'">M</a>
    <a href="javascript:selectLetter('N');" onclick="this.style.color='#ff0000'">N</a> | <br />
    <a href="javascript:selectLetter('O');" onclick="this.style.color='#ff0000'">O</a> | 
    <a href="javascript:selectLetter('Ö');" onclick="this.style.color='#ff0000'">Ö</a> | 
    
    <a href="javascript:selectLetter('P');" onclick="this.style.color='#ff0000'">P</a> | 
    <a href="javascript:selectLetter('Q');" onclick="this.style.color='#ff0000'">Q</a> | 
    <a href="javascript:selectLetter('R');" onclick="this.style.color='#ff0000'">R</a> | 
    <a href="javascript:selectLetter('S');" onclick="this.style.color='#ff0000'">S</a> | 
    <a href="javascript:selectLetter('T');" onclick="this.style.color='#ff0000'">T</a> | 
    <a href="javascript:selectLetter('U');" onclick="this.style.color='#ff0000'">U</a> | 
    <a href="javascript:selectLetter('Ü');" onclick="this.style.color='#ff0000'">Ü</a> |
    
    <a href="javascript:selectLetter('V');" onclick="this.style.color='#ff0000'">V</a> | 
    <a href="javascript:selectLetter('W');" onclick="this.style.color='#ff0000'">W</a> | 
    <a href="javascript:selectLetter('X');" onclick="this.style.color='#ff0000'">X</a> | 
    <a href="javascript:selectLetter('Y');" onclick="this.style.color='#ff0000'">Y</a> | 
    <a href="javascript:selectLetter('Z');" onclick="this.style.color='#ff0000'">Z</a> </center> </p>
    
    
    
    <p> 
    <a href="javascript:reset()">Start game / Reset game</a></p>
    
    </table>
    
    </body>
    </html>
  • tpgames
    Contributor
    • Jan 2007
    • 783

    #2
    Oops! Forgot to change title. Code does NOT work period, when I put JS in own file.

    Comment

    • tpgames
      Contributor
      • Jan 2007
      • 783

      #3
      Well, code does NOT work even within the same HTML file. I recopied the original, into a new HTML file - EXACTLY! Tested it. Works. Then, I changed the wordlist to be 5 letters long "abcde", "abcde" etc. The code refused to work. I made absolutely certain that the wordlists was made exactly like the 4 letter word list. I double checked to be sure it was "abcde", "abcde") and NOT "abcde, "abcde') or some other error.

      ORIGINAL Code (with wordlist edited down considerably)
      Code:
      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
      <html>
      <head>
      <title> Evaporating Book </title>
      <META http-equiv="expires" content="Thur, 1 March 2007 13:00:00 GMT">
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
      <link rel="stylesheet" href="../../../game.css" type="text/css"> 
      <link rel="shortcut icon" href="/gwc/img/gwcicon.gif">
      
      <script>
      //characters can not be longer than 16 letters because browser will not show it.
      var can_play = true;
      var words = new Array("AAHS", "AALS", "ABAC", "ABAS", "ABBA", "ABBE", "ABBS", "ABED", "ABET", "ABID", "ABLE", "ABLY", "ABOS", "ABRI", "ABUT", "ABYE", "ABYS", "ZYGA", "ZYME");
      
      
      var to_guess = "";
      var display_word = "";
      var used_letters = "";
      var wrong_guesses = 0;
      
      
      function selectLetter(l)
      {
      if (can_play == false)
      {
      return;
      }
      
      if (used_letters.indexOf(l) != -1)
      {
      return;
      }
      	
      used_letters += l;
      document.game.usedLetters.value = used_letters;
      	
      if (to_guess.indexOf(l) != -1)
      {
       // correct letter guess
      pos = 0;
      temp_mask = display_word;
      
      
      while (to_guess.indexOf(l, pos) != -1)
      {
      pos = to_guess.indexOf(l, pos);			
      end = pos + 1;
      
      start_text = temp_mask.substring(0, pos);
      end_text = temp_mask.substring(end, temp_mask.length);
      
      temp_mask = start_text + l + end_text;
      pos = end;
      }
      
      display_word = temp_mask;
      document.game.displayWord.value = display_word;
      		
      if (display_word.indexOf("?") == -1)
      {
      // won
      alert("You've saved the Book from Evaporation! Congratulations, Mazel Tov and all that good stuff!");
      can_play = false;
      }
      }
      else
      {
      // incorrect letter guess
      
      // 10 guesses
      wrong_guesses += 1;
      eval("document.hm.src=\"hm" + wrong_guesses + ".gif\"");
      		
      if (wrong_guesses == 10)
      {
      // lost
      alert("Oh no! The Book is evaporating! Please try again!");
      can_play = false;
      }
      }
      }
      
      function reset()
      {
      selectWord();
      document.game.usedLetters.value = "";
      used_letters = "";
      wrong_guesses = 0;
      document.hm.src="hmstart.gif";  
      var links = document.getElementsByTagName("a");   
            for (i = 0; i < links.length; i++) {
                links[i].style.color = "#0000ff";
            } 
      }
      
      
      
      function selectWord()
      {
      can_play = true;
      random_number = Math.round(Math.random() * (words.length - 1));
      to_guess = words[random_number];
      //document.game.theWord.value = to_guess;
      	
      // display masked word
      masked_word = createMask(to_guess);
      document.game.displayWord.value = masked_word;
      display_word = masked_word;
      }
      
      function createMask(m)
      {
      mask = "";
      word_length = m.length;
      
      
      for (i = 0; i < word_length; i ++)
      {
      mask += "?";
      }
      return mask;
      }
      //-- by Chris Fortey @ http://www.c-g-f.net
      </script>
      
      
      
      </HEAD>
      
      <body onLoad="reset(); return true;">
      
      <p>
      <center><br>
       Evaporating Book
      <br>
      
      Plug in your guesses below! <br>
      You have 10 guesses.
      <br>&nbsp;<br></center></p>
      
      <!--THE SCRIPT THAT HE USES-->
      
      <p> <center> 
      
      <img src="hmstart.gif" height="230" width="266" name="hm" alt="Useless Countdown Image in Java Script that won't give alt tags for the other images."> </center> </p>
      
      <form name="game">
      
      <p><center>
      Display Word: <input type="text" name="displayWord"><br>
      
      Used Letters: <input type="text" name="usedLetters"> </center></p>
      </form>
      
      
      
      <p><center>
      
      <a href="javascript:selectLetter('A');" onclick="this.style.color='#ff0000'">A</a> | 
      <a href="javascript:selectLetter('B');" onclick="this.style.color='#ff0000'">B</a> | 
      <a href="javascript:selectLetter('C');" onclick="this.style.color='#ff0000'">C</a> | 
      <a href="javascript:selectLetter('D');" onclick="this.style.color='#ff0000'">D</a> | 
      <a href="javascript:selectLetter('E');" onclick="this.style.color='#ff0000'">E</a> | 
      
      <a href="javascript:selectLetter('F');" onclick="this.style.color='#ff0000'">F</a> | 
      <a href="javascript:selectLetter('G');" onclick="this.style.color='#ff0000'">G</a> | 
      <a href="javascript:selectLetter('H');" onclick="this.style.color='#ff0000'">H</a> | 
      
      <a href="javascript:selectLetter('I');" onclick="this.style.color='#ff0000'">I</a> | 
      <a href="javascript:selectLetter('J');" onclick="this.style.color='#ff0000'">J</a> | 
      <a href="javascript:selectLetter('K');" onclick="this.style.color='#ff0000'">K</a> | 
      
      <a href="javascript:selectLetter('L');" onclick="this.style.color='#ff0000'">L</a>
      <a href="javascript:selectLetter('M');" onclick="this.style.color='#ff0000'">M</a><br />
      <a href="javascript:selectLetter('N');" onclick="this.style.color='#ff0000'">N</a> | 
      <a href="javascript:selectLetter('O');" onclick="this.style.color='#ff0000'">O</a> | 
      
      <a href="javascript:selectLetter('P');" onclick="this.style.color='#ff0000'">P</a> | 
      <a href="javascript:selectLetter('Q');" onclick="this.style.color='#ff0000'">Q</a> | 
      <a href="javascript:selectLetter('R');" onclick="this.style.color='#ff0000'">R</a> | 
      
      <a href="javascript:selectLetter('S');" onclick="this.style.color='#ff0000'">S</a> | 
      <a href="javascript:selectLetter('T');" onclick="this.style.color='#ff0000'">T</a> | 
      <a href="javascript:selectLetter('U');" onclick="this.style.color='#ff0000'">U</a> | 
      
      <a href="javascript:selectLetter('V');" onclick="this.style.color='#ff0000'">V</a> | 
      <a href="javascript:selectLetter('W');" onclick="this.style.color='#ff0000'">W</a> | 
      <a href="javascript:selectLetter('X');" onclick="this.style.color='#ff0000'">X</a> | 
      
      <a href="javascript:selectLetter('Y');" onclick="this.style.color='#ff0000'">Y</a> | 
      <a href="javascript:selectLetter('Z');" onclick="this.style.color='#ff0000'">Z</a> </center> </p>
      
      
      
      <p> 
      <center> 
      
      <a href="javascript:reset()">Start game / Reset game</a>
      
      </center></p>
      </script>
      
      
      </body>
      </html>

      MY NEW wordlist
      Code:
      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
      <html lang="utf-8">
      <head>
      <title> Evaporating Book Words </title>
      
      <META http-equiv="expires" content="Thur, 1 March 2007 13:00:00 GMT">
      <meta http-equiv="Content-Type" content="text/html charset=utf-8">
      <link type="text/css" rel="stylesheet" 
      href="http://www.globalwritersclub.com/gaming/2/word/scrambled/css/scrambled.css"> 
      
      <script>
      //characters can not be longer than 16 letters because browser will not show it.
      var can_play = true;
      var words = new Array("abcde", "abcde", "abcde", "abcde", "abcde", "abcde", "abcde");
      
      var to_guess = "";
      var display_word = "";
      var used_letters = "";
      var wrong_guesses = 0;
      
      function selectLetter(l)
      {
      if (can_play == false)
      {
      return;
      }
      
      if (used_letters.indexOf(l) != -1)
      {
      return;
      }
      	
      used_letters += l;
      document.game.usedLetters.value = used_letters;
      	
      if (to_guess.indexOf(l) != -1)
      {
       // correct letter guess
      pos = 0;
      temp_mask = display_word;
      
      
      while (to_guess.indexOf(l, pos) != -1)
      {
      pos = to_guess.indexOf(l, pos);			
      end = pos + 1;
      
      start_text = temp_mask.substring(0, pos);
      end_text = temp_mask.substring(end, temp_mask.length);
      
      temp_mask = start_text + l + end_text;
      pos = end;
      }
      
      display_word = temp_mask;
      document.game.displayWord.value = display_word;
      		
      if (display_word.indexOf("?") == -1)
      {
      // won
      alert("You've saved the Book from Evaporation! Congratulations, Mazel Tov and all that good stuff!");
      can_play = false;
      }
      }
      else
      {
      // incorrect letter guess
      
      // 10 guesses
      wrong_guesses += 1;
      eval("document.hm.src=\"hm" + wrong_guesses + ".gif\"");
      		
      if (wrong_guesses == 26)
      {
      // lost
      alert("Oh no! The Book is evaporating! Please try again!");
      can_play = false;
      }
      }
      }
      
      function reset()
      {
      selectWord();
      document.game.usedLetters.value = "";
      used_letters = "";
      wrong_guesses = 0;
      document.hm.src="hmstart.gif";  
      var links = document.getElementsByTagName("a");   
            for (i = 0; i < links.length; i++) {
                links[i].style.color = "#0000ff";
            } 
      }
      
      
      
      function selectWord()
      {
      can_play = true;
      random_number = Math.round(Math.random() * (words.length - 1));
      to_guess = words[random_number];
      //document.game.theWord.value = to_guess;
      	
      // display masked word
      masked_word = createMask(to_guess);
      document.game.displayWord.value = masked_word;
      display_word = masked_word;
      }
      
      function createMask(m)
      {
      mask = "";
      word_length = m.length;
      
      
      for (i = 0; i < word_length; i ++)
      {
      mask += "?";
      }
      return mask;
      }
      //-- by Chris Fortey @ http://www.c-g-f.net
      </script>
      
      
      
      </HEAD>
      
      <body onLoad="reset(); return true;">
      
      <p>
      <center><br>
       Evaporating Book
      <br>
      
      Plug in your guesses below! <br>
      You have 10 guesses.
      <br>&nbsp;<br></center></p>
      
      <!--THE SCRIPT THAT HE USES-->
      
      <p> <center> 
      
      <img src="hmstart.gif" height="230" width="266" name="hm" alt="Useless Countdown Image in Java Script that won't give alt tags for the other images."> </center> </p>
      
      <form name="game">
      
      <p><center>
      Display Word: <input type="text" name="displayWord"><br>
      
      Used Letters: <input type="text" name="usedLetters"> </center></p>
      </form>
      
      
      
      <p><center>
      
      <a href="javascript:selectLetter('A');" onclick="this.style.color='#ff0000'">A</a> | 
      
      <a href="javascript:selectLetter('B');" onclick="this.style.color='#ff0000'">B</a> | 
      <a href="javascript:selectLetter('C');" onclick="this.style.color='#ff0000'">C</a> | 
      <a href="javascript:selectLetter('D');" onclick="this.style.color='#ff0000'">D</a> | 
      <a href="javascript:selectLetter('E');" onclick="this.style.color='#ff0000'">E</a> | 
      
      <a href="javascript:selectLetter('F');" onclick="this.style.color='#ff0000'">F</a> | 
      <a href="javascript:selectLetter('G');" onclick="this.style.color='#ff0000'">G</a> | 
      
      <a href="javascript:selectLetter('H');" onclick="this.style.color='#ff0000'">H</a> | 
      
      <a href="javascript:selectLetter('I');" onclick="this.style.color='#ff0000'">I</a> | 
      <a href="javascript:selectLetter('J');" onclick="this.style.color='#ff0000'">J</a> | 
      <a href="javascript:selectLetter('K');" onclick="this.style.color='#ff0000'">K</a> | 
      
      <a href="javascript:selectLetter('L');" onclick="this.style.color='#ff0000'">L</a>
      <a href="javascript:selectLetter('M');" onclick="this.style.color='#ff0000'">M</a><br />
      <a href="javascript:selectLetter('N');" onclick="this.style.color='#ff0000'">N</a> | 
      
      <a href="javascript:selectLetter('O');" onclick="this.style.color='#ff0000'">O</a> | 
      
      <a href="javascript:selectLetter('P');" onclick="this.style.color='#ff0000'">P</a> | 
      <a href="javascript:selectLetter('Q');" onclick="this.style.color='#ff0000'">Q</a> | 
      <a href="javascript:selectLetter('R');" onclick="this.style.color='#ff0000'">R</a> | 
      
      <a href="javascript:selectLetter('S');" onclick="this.style.color='#ff0000'">S</a> | 
      <a href="javascript:selectLetter('T');" onclick="this.style.color='#ff0000'">T</a> | 
      
      <a href="javascript:selectLetter('U');" onclick="this.style.color='#ff0000'">U</a> | 
      
      <a href="javascript:selectLetter('V');" onclick="this.style.color='#ff0000'">V</a> | 
      <a href="javascript:selectLetter('W');" onclick="this.style.color='#ff0000'">W</a> | 
      <a href="javascript:selectLetter('X');" onclick="this.style.color='#ff0000'">X</a> | 
      
      <a href="javascript:selectLetter('Y');" onclick="this.style.color='#ff0000'">Y</a> | 
      <a href="javascript:selectLetter('Z');" onclick="this.style.color='#ff0000'">Z</a> </center> </p>
      
      
      
      <p> 
      <center> 
      
      <a href="javascript:reset()">Start game / Reset game</a>
      
      </center></p>
      </script>
      
      
      </body>
      </html>
      Link to ORIGINAL game

      Link to NEW GAME

      Thanks! I quit trying to figure this out myself and will patiently wait for help. Signed Stumped!

      Comment

      • tpgames
        Contributor
        • Jan 2007
        • 783

        #4
        SOLVED!!! Okay, so I KNOW that the code used CAPS letters unless you change one bit of the code in the HTML file to SMALL case letters. Once, I got the word lists letter to be the same size as the HTML file letters, the code once more worked. LOL

        thus the best answer is my own. Get a better editor! lol

        Comment

        Working...