HTML and CSS Reference
In-Depth Information
var hangman = new Hangman(accountName,client);
The main game is contained in the Hangman object, which keeps track of the state of the current game. To
start a new game, the newWord method is called, which grabs a random word and then initializes the guesses
and posts the game to Twitter. Next, the sendTweet method simply calls updateStatus on the client to
send a tweet, capturing the tweet sent or any errors to the console.
The sendGameUpdate method outputs a tweet of the form:
Game 1: __O-[- Word: _ _ _ m _ _ _
This shows the current game number, how far along the man is from being hanged (in a poor ASCII render-
ing), and the letters that have been found.
The next three methods are used to respond to the person who was trying to play with a message directed at
him letting him know how his last guess did.
The main meat of the game is in the handleGuess method, which takes in a tweet, checks to make sure
it's a tweet directed at the account name, and then grabs the last alphabetic character of the tweet and uses that
as the guess character.
This allows users to tweet messages such as the following:
@hangmangame is there an a?
@hangmangame how about a b?
@hangmangame c
All the preceding examples should work. Allowing a little creativity in messaging is necessary because Twit-
ter won't let you post the same tweet in succession.
After it extracts the guess, the game checks if the guess has already been tried. If so, it lets the player know. If
it's not a repeat guess, the game then checks for the number of occurrences and the number of letters remaining
and then sends a tweet to the user based on whether the user correctly guessed the word.
If the player guessed the word or has run out of guesses, the game lets him know and then sends out a new
word. Figure 22-3 shows a sample game.
Search WWH ::

Custom Search