scg/ch10/GuessableWord

From FANG

Jump to: navigation, search

001 package scg.ch10;
002 
003 import fang2.sprites.StringSprite;
004 
005 /**
006  * A word to be guessed. Displays the number of blanks in the word. The
007  {@code #guess(char)} method replaces blanks with the guessed letter
008  * (where it is correct). {@code #unguessedLetters()} counts the number
009  * of underscores remaining in the word, returning the number of letters
010  * as yet unknown. Note: Guessing the same letter a second time will
011  return true from guess (all checking is done against word); it is
012  * left as an exercise for the reader to modify the guess method so it
013  * returns false if a letter has been previously guessed and is found in
014  * word (hint: indexOf on showWord).
015  */
016 public class GuessableWord
017   extends StringSprite {
018   /** guess in progress: _ or letter, double-spaced */
019   private String showWord;
020 
021   /** the word being guessed */
022   private final String word;
023 
024   /**
025    * Create a new guessable word. Display is a double-spaced (space
026    * between each pair of letters) rendition of the word. Initially all
027    * positions are '_' characters; as guesses are made, correct letters
028    * replace the underscores.
029    *
030    @param  word  the word stored in this guessable word
031    */
032   public GuessableWord(String word{
033     this.word = word;
034     setMonospaced(true);
035     initializeShowWord();
036   }
037 
038   /**
039    * Expose the word. Puts the letters into all of the locations of the
040    * word.
041    */
042   public void expose() {
043     showWord = "";
044     for (int = 0; i != word.length()++i{
045       showWord = showWord + " " + word.charAt(i);
046     }
047     setText(showWord);
048   }
049 
050   /**
051    * Guess the given letter is in the word. Updates the display to
052    * expose any matching positions and return true if the letter is in
053    * the word to be guessed. Leave the display unchanged and return
054    false otherwise.
055    *
056    @param   ch  the letter to guess is in the word
057    *
058    @return  true if ch appears in the word; false otherwise
059    */
060   public boolean guess(char ch{
061     boolean letterFound = contains(ch);
062     if (letterFound{
063       for (int wordNdx = 0, showWordNdx = 1; wordNdx != word.length();
064           ++wordNdx, showWordNdx += 2{
065         if (word.charAt(wordNdx== ch{
066           showWord = showWord.substring(0, showWordNdx+ ch +
067             showWord.substring(showWordNdx + 1);
068         }
069       }
070       setText(showWord);
071     }
072     return letterFound;
073   }
074 
075   /**
076    * Has the word been guessed? Are there no unguessed letters?
077    *
078    @return  true if there are no unguessed letters; false otherwise
079    */
080   public boolean isGuessed() {
081     return (unguessedLetterCount() == 0);
082   }
083 
084   /**
085    * Count the number of underscores ('_') in showWord. This is the
086    * number of letters still to be guessed. The word is guessed when the
087    * number of unguessed letters is 0.
088    *
089    @return  the number of '_' in the shown word; the number of letters
090    *          still to be guessed
091    */
092   public int unguessedLetterCount() {
093     /** how many matches have been found */
094     int underscoreCount = 0;
095 
096     /** where is the most recent underscore found */
097     int justMatchedIndex = showWord.indexOf('_'0);
098     while (!(justMatchedIndex == -1)) { // negative logic is bad!
099       ++underscoreCount;
100       justMatchedIndex = showWord.indexOf('_', justMatchedIndex + 1);
101     }
102     return underscoreCount;
103   }
104 
105   /**
106    * Does the word contain the given letter?
107    *
108    @param   letter  the letter to check for
109    *
110    @return  true if there is one or more instances of letter in word;
111    *          false otherwise
112    */
113   private boolean contains(char letter{
114     return (word.indexOf(letter!= -1);
115   }
116 
117   /**
118    * Initialize the guess to have no correct letters yet (and double
119    * space it). Note that showWord is always twice as long as word and
120    * always begins with a blank space (this is important in the way
121    * guess works). Note that internal spaces are shown as spaces.
122    */
123   private void initializeShowWord() {
124     showWord = "";
125     for (int = 0; i != word.length()++i{
126       if (word.charAt(i!= ' '{
127         showWord += " _";
128       } else {
129         showWord += "  ";
130       }
131     }
132     setText(showWord);
133   }
134 }
135 
136 //Uploaded on Mon Mar 29 21:39:30 EDT 2010


Download/View scg/ch10/GuessableWord.java





Views
Personal tools
Add to 
del.icio.usAdd to 
diggAdd to 
FacebookAdd to 
favoritesAdd to 
GoogleAdd to 
MySpaceAdd to 
PrintAdd to 
SlashdotAdd to 
StumbleUponAdd to 
Twitter

Games
Games