Creating Twitter Hangman

With the libraries in place to create the game, it’s time to dig in to the actual code for the game. The main idea is to post a tweet with the blanks for a game of Hangman and respond to tweets from users guessing the missing letters. The application responds to any user that sends tweets at it with the number of times that letter appears and tweets the updated board state.

The complete code for the game appears in Listing 22-5. Replace any code you have in app.js with the code in Listing 22-5. You need to replace the accountName and Twitter configuration variables as you did earlier.

Listing 22-5: Twitter Hangman

var twitter = require("ntwitter"), fs = require('fs'), words = fs.readFileSync('words.txt').toString().split("\n"); var client = new twitter({ consumer_key: "YOUR_CONSUMER_KEY", consumer_secret: "YOUR_CONSUMER_SECRET", access_token_key: "YOUR_ACCESS_TOKEN_KEY", access_token_secret: "YOUR_ACCESS_TOKEN_SECRET" }); var accountName = "hangmanword"; function randomWord() { var word; do { word = words[Math.floor(Math.random()*words.length)]; } while(!word.match(/^\w+$/) || word.length < 5) return word; } var Hangman = function(accountName,client) { var self = this; this.gameNumber = 0; var hangman = "__O-[-<"; this.newWord = function() { this.word = randomWord(); this.currentWord = this.word.split(""); this.currentGuesses = []; this.guesses = []; this.lettersRemaining = this.currentWord.length; this.guessesRemaining = 5; this.gameNumber++; this.sendGameUpdate(); ...

Get Professional HTML5 Mobile Game Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.