RSS Hangman
From RobsWiki
RSS Hangman is my result for the Space Cadets Week 2 task:
| Table of contents |
What Is RSS Hangman ?
RSS Hangman is like the pencil and paper game ... except that instead of guessing a single word on its own, you are given an article title from an RSS feed (eg. BBC News website, The Onion.com, Ananova) and one of the words is replaced with blanks. Select letters from the drop down box. If they are in the word, the appropriate blanks will be filled in. If not, hangman comes one stage closer to a painful death. Up to 10 mistakes are allowed. You can specify custom RSS feeds from which to make hangman games.
Click Here To Play RSS Hangman! (http://www.robertknight.me.uk/soton/cadets/TextApplet.html)
How Does It Work?
When the applet starts a FetcherThread object is created. This is responsible for periodically polling a given RSS feed to get new updates. In order to extract information from an RSS feed, bearing in mind Java's security mechanisms, an RSSItemFetcher object is created which interacts with a server-side PHP script. The short PHP script downloads the RSS feed from a given URL, parses it using a SAX-based parser, and returns a list of the item titles found in the feed. The main class has a timer event which fires at short intervals, and when this timer event fires, the FetcherThread is queried to see if a new list is available. The actual RSS feed itself is queried once every three minutes.
The display area of the game is contained in the HangmanDisplay class, which extends the swing container class. The setText method of HangmanDisplay is used to set the text for the game. The word which must be guessed is prepended with an asterisk - eg. "Welcome to *Space Cadets". To try a letter, other objects call the HangmanDisplay's tryLetter method. tryLetter advances the state of the hangman if the letter is wrong, or sets an internal success variable if there are no more letters to guess. It also adds the letter to a list of letters which have already been tried.
A timer is set up to fire every 50ms, and this triggers a repaint of the display. When the display paints itself, it looks at the letters which have been tried, and prints the characters or draws a blank as necessary.
The game UI is created by the main class in its setupUI method. Each component which needs to be interactive adds the main class as an action listener. The actionPerformed method then communicates with the HangmanDisplay and FetcherThread objects depending on component the user interacted with.
What needs improving?
The item which is chosen for each game is picked at random. This means that it is possible to have the same game multiple times in the same session. From a programming point of view, the code should really be separated out into one .java file per class for neatness.
Where is the code?
Here - TextApplet.java (http://www.robertknight.me.uk/soton/cadets/TextApplet.java)

