dbrown
Class Hangman

java.lang.Object
  extended by dbrown.Hangman

public class Hangman
extends java.lang.Object

The main Hangman game class. It provides the attributes and methods for playing the game.

Version:
2011-11-11
Author:
David Brown (from Byron Weber Becker)

Nested Class Summary
static class Hangman.GameStateFlags
          The possible states of the game.
 
Field Summary
static char FIRST_LETTER
          The end points of the valid phrase letters.
static java.lang.String FORFEIT
           
private  Hangman.GameStateFlags gameState
          Contains the current state of the game.
static java.lang.String GUESS
           
private  boolean[] guessedLetters
          Letters of the alphabet may be guessed or not yet guessed and their states are contained in this array.
private  java.lang.StringBuffer guessedPhrase
          The phrase guessed so far is stored in a mutable structure.
static char LAST_LETTER
           
static java.lang.String LOSE
           
static int MAX_WRONG_GUESSES
          The maximum number of allowed wrong guesses.
static java.lang.String NEW_GAME
           
static int NUM_LETTERS
          The number of letters in the alphabet.
static java.util.regex.Pattern pattern
          Regular expressions are used to convert the phrase to a string of asterisks.
private  java.beans.PropertyChangeSupport pcs
          Allows views to listen to generic or named changes in the hangman game.
private  java.lang.String phrase
          The phrase to be guessed.
private  PhraseList phrases
          The list of phrases to be guessed.
static java.lang.String WIN
           
private  int wrongGuesses
          The number of wrong guesses made so far.
 
Constructor Summary
Hangman(java.lang.String filename)
           
 
Method Summary
 void addPropertyChangeListener(java.beans.PropertyChangeListener listener)
          Allows for generic listeners.
 void addPropertyChangeListener(java.lang.String propertyName, java.beans.PropertyChangeListener listener)
          Allows for named listeners.
 void forfeit()
          Forfeits the game and exposes the phrase to be guessed.
 java.lang.String getGuessedPhrase()
           
 int getWrongGuesses()
           
 void guess(char letter)
          Accepts a letter guessed by the player and checks to see whether it has already been guessed and whether is is part of the phrase to be guessed.
private  void initGuessedLetters(boolean state)
          Initializes the list of guessed letters to not guessed so far.
 boolean isPlaying()
           
 boolean lost()
           
 void newGame()
          Initializes a new game by selecting a phrase from the file of phrases in the phraseList object.
 void newGame(java.lang.String newPhrase)
          Initializes a new game with a given phrase.
 boolean wasGuessed(char letter)
          Determines whether a character guessed by the player has already been guessed or not.
 boolean won()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FORFEIT

public static final java.lang.String FORFEIT
See Also:
Constant Field Values

NEW_GAME

public static final java.lang.String NEW_GAME
See Also:
Constant Field Values

WIN

public static final java.lang.String WIN
See Also:
Constant Field Values

LOSE

public static final java.lang.String LOSE
See Also:
Constant Field Values

GUESS

public static final java.lang.String GUESS
See Also:
Constant Field Values

FIRST_LETTER

public static final char FIRST_LETTER
The end points of the valid phrase letters.

See Also:
Constant Field Values

LAST_LETTER

public static final char LAST_LETTER
See Also:
Constant Field Values

MAX_WRONG_GUESSES

public static final int MAX_WRONG_GUESSES
The maximum number of allowed wrong guesses. This must match the number of body parts in a gallows view.

See Also:
Constant Field Values

NUM_LETTERS

public static final int NUM_LETTERS
The number of letters in the alphabet.

See Also:
Constant Field Values

pattern

public static final java.util.regex.Pattern pattern
Regular expressions are used to convert the phrase to a string of asterisks. This pattern excludes white space characters from the characters to be converted.


gameState

private Hangman.GameStateFlags gameState
Contains the current state of the game.


guessedLetters

private final boolean[] guessedLetters
Letters of the alphabet may be guessed or not yet guessed and their states are contained in this array.


guessedPhrase

private java.lang.StringBuffer guessedPhrase
The phrase guessed so far is stored in a mutable structure.


phrase

private java.lang.String phrase
The phrase to be guessed.


phrases

private PhraseList phrases
The list of phrases to be guessed.


wrongGuesses

private int wrongGuesses
The number of wrong guesses made so far.


pcs

private final java.beans.PropertyChangeSupport pcs
Allows views to listen to generic or named changes in the hangman game.

Constructor Detail

Hangman

public Hangman(java.lang.String filename)
        throws java.io.FileNotFoundException
Parameters:
filename - - the name of the file to open that contains the phrases to be guessed
Throws:
java.io.FileNotFoundException
Method Detail

addPropertyChangeListener

public void addPropertyChangeListener(java.beans.PropertyChangeListener listener)
Allows for generic listeners. Listeners will update no matter what change has taken place in the Hangman model.

Parameters:
listener -

addPropertyChangeListener

public void addPropertyChangeListener(java.lang.String propertyName,
                                      java.beans.PropertyChangeListener listener)
Allows for named listeners. Listeners will update only when a named property of the Hangman model is updated.

Parameters:
propertyName -
listener -

forfeit

public void forfeit()
Forfeits the game and exposes the phrase to be guessed.


getGuessedPhrase

public java.lang.String getGuessedPhrase()
Returns:
the phrase as guessed so far

getWrongGuesses

public int getWrongGuesses()
Returns:
the number of wrong guesses so far

guess

public void guess(char letter)
           throws java.lang.Exception
Accepts a letter guessed by the player and checks to see whether it has already been guessed and whether is is part of the phrase to be guessed. It further determines whether the game has been won, lost, or should continue.

Parameters:
letter - - the letter guessed by the player
Throws:
java.lang.Exception - - when the letter has already been guessed

isPlaying

public boolean isPlaying()
Returns:
whether the game is currently being played

lost

public boolean lost()
Returns:
whether the player has lost the game

newGame

public void newGame()
Initializes a new game by selecting a phrase from the file of phrases in the phraseList object.


newGame

public void newGame(java.lang.String newPhrase)
Initializes a new game with a given phrase.

Parameters:
newPhrase - the phrase for the player to guess.

wasGuessed

public boolean wasGuessed(char letter)
                   throws java.lang.IllegalArgumentException
Determines whether a character guessed by the player has already been guessed or not.

Parameters:
letter - the letter the player has guessed as being part of the current phrase
Returns:
whether the letter has already been guessed or not
Throws:
java.lang.IllegalArgumentException - when the character passed as a parameter is not part of the alphabet

won

public boolean won()
Returns:
whether the player has won the game

initGuessedLetters

private void initGuessedLetters(boolean state)
Initializes the list of guessed letters to not guessed so far.