|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object becker.xtras.hangman.SampleHangman
public class SampleHangman
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.
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 |
---|
public static final char FIRST_LETTER
public static final char LAST_LETTER
public static final int NUM_LETTERS
public static final int MAX_WRONG_GUESSES
Constructor Detail |
---|
public SampleHangman()
Method Detail |
---|
public void newGame()
IHangman
newGame
in interface IHangman
public void newGame(String thePhrase)
IHangman
newGame
in interface IHangman
thePhrase
- the phrase to guesspublic int numWrongGuesses()
IHangman
numWrongGuesses
in interface IHangman
public boolean lost()
IHangman
lost
in interface IHangman
public boolean won()
IHangman
won
in interface IHangman
public void processGuess(char theLetter)
IHangman
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:
wasGuessed(theLetter)
returns true
.theLetter
, then getGuessedPhrase
will have theLetter
included in the string it returns
and numWrongGuesses()
will return n.theLetter
,
numWrongGuesses()
will return n+1.
processGuess
in interface IHangman
theLetter
- the letter guessedpublic boolean wasGuessed(char aLetter)
IHangman
wasGuessed
in interface IHangman
public void forfeit()
IHangman
forfeit
in interface IHangman
public String getGuessedPhrase()
IHangman
Examples:
Phrase Letters guessed Return WAIT A MINUTE none . . . . . . . . . . . WAIT A MINUTE A I . A I . A . I . . . . WAIT A MINUTE lost game W A I T A M I N U T E
getGuessedPhrase
in interface IHangman
public void addView(IView aView)
IHangman
updateView
method must be called each time the game's state changes.
addView
in interface IHangman
public static void main(String[] args)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |