becker.xtras.jotto
Class Hint

java.lang.Object
  extended by becker.xtras.jotto.Hint
All Implemented Interfaces:
IWordPredicate
Direct Known Subclasses:
SampleHintConsistentWithGuesses, SampleHintContainsLetter, SampleHintWithAllLetters, SampleHintWithoutLetter, SampleHintWithSomeLetters

public abstract class Hint
extends Object
implements IWordPredicate

A Hint object is used to specify one kind of hint to a Jotto player. Hint must be extended to provide a method named isOK(Word w) and another named getHintWords(int maxDesired, IHintData hintData). The getHintWords method is called to generate the words for the hint. It likely gets the list of known words from hintData and passes itself to the IWordList.getWords(IWordPredicate p) method. Passing itself to getWords works because Hint implements IWordPredicate via the isOK method. This method is used to determine whether or not a given Word is appropriate to include in the answer.

For example, the following subclass of Hint could be used to show the user words that contain a specified letter.

 public class HintContainsLetter extends Hint
 {  private char letter;

    public HintContainsLetter()
    {  super("containing the letter", Hint.LETTER);
    }
 
    public Word[] getHintWords(int maxDesired, IHintData hintData)
    {  this.letter = hintData.getLetter();
       return hintData.getKnownWords().getWords(maxDesired, this);
    }

    public boolean isOK(Word w)
    {  return w.getWord().indexOf(this.letter) >= 0;
    }
 }
 

Note that Hint implements IWordPredicate, implying that all subclasses will also implement that interface whether or not they include the phrase "implements IWordPredicate".

Hint objects are added to the program using the JottoModel.addHint(Hint) method in JottoModel. This method adds the hint to a list of hints which will be displayed in the user interface. When the user requests a hint, the program:

  1. obtains the corresponding Hint object from the list.
  2. constructs an object implementing IHintData from which the hint can retrieve the data it needs.
  3. calls getHintWords, passing the IHintData object as a parameter.
  4. displays words returned by getHintWords to the user.

Author:
Byron Weber Becker

Field Summary
static int DIFFICULTY
          The hint requires a word difficulty level to determine if a word should be included.
static int GUESSES
          The hint requires an array the user's previous guesses to determine if a word should be included.
static int LETTER
          The hint requires a single letter to determine if a word should be included.
static int LETTERS
          The hint requires an array of letters to determine if a word should be included.
static int NONE
          The hint requires no extra information to determine if a word should be included.
 
Constructor Summary
Hint(String aDescription, int argType)
          Construct a hint object.
 
Method Summary
 int getArgumentType()
          Get the argument type for this hint, as given to the constructor.
 String getDescription()
          Get the description for this hint as given to the constructor.
abstract  Word[] getHintWords(int maxDesired, IHintData hintData)
          Get the words to display as the hint.
abstract  boolean isOK(Word w)
          Override this method to specify whether or not the Word w is an acceptable answer for this hint.
static void main(String[] args)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LETTER

public static final int LETTER
The hint requires a single letter to determine if a word should be included.

See Also:
Constant Field Values

LETTERS

public static final int LETTERS
The hint requires an array of letters to determine if a word should be included.

See Also:
Constant Field Values

GUESSES

public static final int GUESSES
The hint requires an array the user's previous guesses to determine if a word should be included.

See Also:
Constant Field Values

NONE

public static final int NONE
The hint requires no extra information to determine if a word should be included.

See Also:
Constant Field Values

DIFFICULTY

public static final int DIFFICULTY
The hint requires a word difficulty level to determine if a word should be included.

See Also:
Constant Field Values
Constructor Detail

Hint

public Hint(String aDescription,
            int argType)
Construct a hint object.

Parameters:
aDescription - The description of the hint that will be displayed in the user interface. This string is returned by getDescription(). The user interface will append it to the end of "A word " or "All words ".
argType - The type of argument required by this hint. Must be one of {LETTER, LETTERS, GUESSES, DIFFICULTY, NONE}. The value is returned by getArgumentType() and determines which version of setArgument is called.
Method Detail

getDescription

public final String getDescription()
Get the description for this hint as given to the constructor.

Returns:
the description passed to the constructor

getArgumentType

public final int getArgumentType()
Get the argument type for this hint, as given to the constructor.

Returns:
the argument type passed to the constructor

isOK

public abstract boolean isOK(Word w)
Override this method to specify whether or not the Word w is an acceptable answer for this hint.

Specified by:
isOK in interface IWordPredicate
Parameters:
w - The word to test.
Returns:
true if the word should be included in the hint's answer; false otherwise.

getHintWords

public abstract Word[] getHintWords(int maxDesired,
                                    IHintData hintData)
Get the words to display as the hint.

Returns:
the words in the list of known words that satisfies the isOK method.

main

public static void main(String[] args)