Next page | Contents page |

Exercise H2 - Top level

Rewrite the outline of the Hangman game (given again below here) so that it is completely in the form of JavaScript statements, only needing function bodies to be completed. For now, make each of the function bodies call alert() with the function name as parameter, so you can see it being called.

Declaring some variables that are going to be useful will enable you to write proper JavaScript if statements.

Also write as much as you can of the HTML page, with an input text field for user guesses and 2 buttons (Enter and New game) as discussed earlier.

For selecting a phrase we are going to provide you with a script file (phrases.js) containing encrypted phrases. More details of that in a later exercise.


  function newGame ()
  {
    choose a phrase and display it as dashes;
    clear the diagram and list of failed guesses;
  }
  
  function enterGuess ()
  {
    get the guess from the input field;

    if guess length is 0
      tell user about error; (or maybe just ignore?)
    else if guess length is 1 (guessing a letter)
    {
      for each letter position in the phrase
      {
        if guessed letter matches
          show the letter in the phrase;
      }
  
      if there were no matches
      {
        draw a segment of the diagram;
        show the letter in a list of failures;
      }
    }
    else (length must be > 1, so guessing whole phrase)
    {
      if guess matches the phrase
        game over - user WINS;
      else
        draw a segment of the diagram;
    }	

    if diagram is complete
      game over - user LOSES;
	  
    if phrase is complete
      game over - User WINS;
  }
Next page | Contents page |