becker.xtras.hangman
Class SampleHangman

java.lang.Object
  extended by becker.xtras.hangman.SampleHangman
All Implemented Interfaces:
IHangman

public class SampleHangman
extends Object
implements IHangman

A sample implementation of IHangman used to demonstrate the operation of the game of Hangman.

Please see the package description for a more in-depth discussion of using this class.

Author:
Byron Weber Becker

Field Summary
static char FIRST_LETTER
           
static char LAST_LETTER
           
static int MAX_WRONG_GUESSES
           
static int NUM_LETTERS
           
 
Constructor Summary
SampleHangman()
           
 
Method Summary
 void addView(IView aView)
          Add a view (graphical user interface) to the game.
 void forfeit()
          The user has given up and forfeited the game.
 String getGuessedPhrase()
          Return the phrase that has been guessed so far.
 boolean lost()
          Has the player lost the game (had more than MAX_WRONG_GUESSES or has forfeit)?
static void main(String[] args)
           
 void newGame()
          Begin a new game with a randomly chosen phrase for the player to guess.
 void newGame(String thePhrase)
          Begin a new game with the player trying to guess the given phrase.
 int numWrongGuesses()
          How many wrong guesses has the player used?
 void processGuess(char theLetter)
          Process a letter the player has guessed.
 boolean wasGuessed(char aLetter)
          Has the given letter been guessed before?
 boolean won()
          Has the player won the game by correctly guessing all the letters in the phrase?
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FIRST_LETTER

public static final char FIRST_LETTER
See Also:
Constant Field Values

LAST_LETTER

public static final char LAST_LETTER
See Also:
Constant Field Values

NUM_LETTERS

public static final int NUM_LETTERS
See Also:
Constant Field Values

MAX_WRONG_GUESSES

public static final int MAX_WRONG_GUESSES
See Also:
Constant Field Values
Constructor Detail

SampleHangman

public SampleHangman()
Method Detail

newGame

public void newGame()
Description copied from interface: IHangman
Begin a new game with a randomly chosen phrase for the player to guess.

Specified by:
newGame in interface IHangman

newGame

public void newGame(String thePhrase)
Description copied from interface: IHangman
Begin a new game with the player trying to guess the given phrase.

Specified by:
newGame in interface IHangman
Parameters:
thePhrase - the phrase to guess

numWrongGuesses

public int numWrongGuesses()
Description copied from interface: IHangman
How many wrong guesses has the player used?

Specified by:
numWrongGuesses in interface IHangman
Returns:
the number of wrong guesses made

lost

public boolean lost()
Description copied from interface: IHangman
Has the player lost the game (had more than MAX_WRONG_GUESSES or has forfeit)?

Specified by:
lost in interface IHangman
Returns:
true if the player has lost; false otherwise

won

public boolean won()
Description copied from interface: IHangman
Has the player won the game by correctly guessing all the letters in the phrase?

Specified by:
won in interface IHangman
Returns:
true if the player has won; false otherwise

processGuess

public void processGuess(char theLetter)
Description copied from interface: IHangman
Process a letter the player has guessed.

This method is called by the user interface when the user clicks on a letter to guess it. Assuming wasGuessed(theLetter), won(), and lost() all return false before this method is called and numWrongGuesses returns n, then after this method is called the following should be true:

Specified by:
processGuess in interface IHangman
Parameters:
theLetter - the letter guessed

wasGuessed

public boolean wasGuessed(char aLetter)
Description copied from interface: IHangman
Has the given letter been guessed before?

Specified by:
wasGuessed in interface IHangman
Returns:
true if the letter was already guessed; false otherwise

forfeit

public void forfeit()
Description copied from interface: IHangman
The user has given up and forfeited the game.

Specified by:
forfeit in interface IHangman

getGuessedPhrase

public String getGuessedPhrase()
Description copied from interface: IHangman
Return the phrase that has been guessed so far. If the player has already lost the game, return the phrase they were trying to guess.

Examples:

PhraseLetters guessedReturn
WAIT A MINUTEnone. . . .   .   . . . . . .
WAIT A MINUTEA I. A I .   A   . I . . . .
WAIT A MINUTElost gameW A I T   A   M I N U T E

Specified by:
getGuessedPhrase in interface IHangman
Returns:
the guessed phrase

addView

public void addView(IView aView)
Description copied from interface: IHangman
Add a view (graphical user interface) to the game. The view's updateView method must be called each time the game's state changes.

Specified by:
addView in interface IHangman

main

public static void main(String[] args)