|
001 packagepackage is used to name the directory or folder a class is in scg.ch10;
002
003 importimport means to make the classes and/or packages available in this program java.io.File;
004 importimport means to make the classes and/or packages available in this program java.io.FileNotFoundException;
005 importimport means to make the classes and/or packages available in this program java.net.URL;
006 importimport means to make the classes and/or packages available in this program java.util.ArrayList;
007 importimport means to make the classes and/or packages available in this program java.util.Scanner;
008
009 importimport means to make the classes and/or packages available in this program fang2.core.Game;
010 importimport means to make the classes and/or packages available in this program fang2.sprites.StringSprite;
011 importimport means to make the classes and/or packages available in this program fang2.util.InitializeApplication;
012
013 /**
014 * The Hangman game: During {open braces start code blocks and must be matched with a close brace@code #setup()}close braces end code blocks and must match an earlier open brace a word is generated as are
015 * all the appropriate sprites (score, alphabet selector, hangman, show
016 * word). Game runs with player selecting the letters and the hangman
017 * and show word updating (show word updates ifif executes the next statement only if the condition in parenthesis evaluates to true the letter _is_ in the
018 * word; the hangman is updated ifif executes the next statement only if the condition in parenthesis evaluates to true the letter _is not_ in the word).
019 * Level ends when the player is hung _or_ the word is guessed.
020 */
021 publicpublic is used to indicate unrestricted access (any other class can have access) classclass is a group of fields and methods used for making objects Hangman
022 extendsextends means to customize or extend the functionality of a class Game {open braces start code blocks and must be matched with a close brace
023 /** the alphabet selector on the screen */
024 AlphabetSelector alphabet;
025
026 /** represents the hangman (gallows and body) */
027 HangmanSprite hangman;
028
029 /** sprite representing the score */
030 ScoreSprite scoreSprite;
031
032 /** the list of possible phrases to choose forfor is a looping structure for repeatedly executing a block of code the player to guess */
033 ArrayList<String> phrases =this assignment operator makes the left side equal to the right side nullnull is the value used to refer to a non-existant object;
034
035 /** sprite representing the show word */
036 GuessableWord gamePhrase;
037
038 /**
039 * Advance one frame: ifif executes the next statement only if the condition in parenthesis evaluates to true game continues, check forfor is a looping structure for repeatedly executing a block of code selected letter; ifif executes the next statement only if the condition in parenthesis evaluates to true
040 * letter is not nullnull is the value used to refer to a non-existant object character, guess letter in the word to be
041 * guessed; ifif executes the next statement only if the condition in parenthesis evaluates to true guess is not good, add a body part to the hangman
042 * sprite and ifif executes the next statement only if the condition in parenthesis evaluates to true hangman is dead expose word and end game; ifif executes the next statement only if the condition in parenthesis evaluates to true guess
043 * was good, check ifif executes the next statement only if the condition in parenthesis evaluates to true word is guessed and ifif executes the next statement only if the condition in parenthesis evaluates to true it is, game over. If game
044 * does not continuecontinue terminates the current iteration of the loop and continues to the next iteration, keep checking forfor is a looping structure for repeatedly executing a block of code a press of the spacebar. When
045 * it is pressed, restart the game.
046 */
047 @Override
048 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value advance(doubledouble is the type for numbers that can contain decimal fractions dT) {open braces start code blocks and must be matched with a close brace
049 ifif executes the next statement only if the condition in parenthesis evaluates to true (isGameOver()) {open braces start code blocks and must be matched with a close brace
050 ifif executes the next statement only if the condition in parenthesis evaluates to true (getKeyPressed() ==this is the comparison operator which evaluates to true if both sides are the same ' ') {open braces start code blocks and must be matched with a close brace
051 setGameOver(falsefalse is a value for the boolean type and means not true);
052 startOver();
053 }close braces end code blocks and must match an earlier open brace
054 }close braces end code blocks and must match an earlier open brace elseelse is what happens when the if condition is false {open braces start code blocks and must be matched with a close brace
055 charchar is the type for a single letter or symbol and it is short for character ch =this assignment operator makes the left side equal to the right side alphabet.selectedChar();
056 ifif executes the next statement only if the condition in parenthesis evaluates to true (ch !=this is the not equals operator which evaluates to true if both sides are different '\0') {open braces start code blocks and must be matched with a close brace
057 ifif executes the next statement only if the condition in parenthesis evaluates to true (gamePhrase.guess(ch)) {open braces start code blocks and must be matched with a close brace
058 ifif executes the next statement only if the condition in parenthesis evaluates to true (gamePhrase.isGuessed()) {open braces start code blocks and must be matched with a close brace
059 scoreSprite.win();
060 doneWithGame("Congratulations");
061 }close braces end code blocks and must match an earlier open brace
062 }close braces end code blocks and must match an earlier open brace elseelse is what happens when the if condition is false {open braces start code blocks and must be matched with a close brace
063 hangman.incrementState();
064 ifif executes the next statement only if the condition in parenthesis evaluates to true (hangman.isDead()) {open braces start code blocks and must be matched with a close brace
065 gamePhrase.expose();
066 gamePhrase.setColor(getColor("red"));
067 scoreSprite.lose();
068 doneWithGame("You lose!");
069 }close braces end code blocks and must match an earlier open brace
070 }close braces end code blocks and must match an earlier open brace
071 }close braces end code blocks and must match an earlier open brace
072 }close braces end code blocks and must match an earlier open brace
073 }close braces end code blocks and must match an earlier open brace
074
075 /**
076 * Setup the sprites: the alphabet selector, the hangman, the score,
077 * and the word.
078 */
079 @Override
080 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value setup() {open braces start code blocks and must be matched with a close brace
081 setupAlphabet();
082 setupHangman();
083 setupScore();
084 setupGamePhrase();
085 }close braces end code blocks and must match an earlier open brace
086
087 /**
088 * Done with the game. Display an end of game message on the screen
089 * and append instructions on how to restart the game.
090 *
091 * @paramthis is the Javadoc tag for documenting the purpose of parameters endOfGameMessage the end of game message to display forfor is a looping structure for repeatedly executing a block of code
092 * the player
093 */
094 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value doneWithGame(String endOfGameMessage) {open braces start code blocks and must be matched with a close brace
095 StringSprite restartMessage =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor StringSprite(endOfGameMessage +adds two numbers together or concatenates Strings together
096 "\nPress <space> to play again.");
097 restartMessage.setScale(0.9);
098 restartMessage.setLocation(0.5, 0.75);
099 addSprite(restartMessage);
100 alphabet.hide();
101 setGameOver(truetrue is the boolean value that is the opposite of false);
102 }close braces end code blocks and must match an earlier open brace
103
104 /**
105 * Get the name of the word file. If there are named arguments, it is
106 * the argument named "words". (This is the casecase is used with switch for multiple alternatives (like if/else if...) forfor is a looping structure for repeatedly executing a block of code applets, forfor is a looping structure for repeatedly executing a block of code
107 * example.) If there is no named argument words, get the first
108 * unnamed argument. If there are no unnamed arguments, punt (returnreturn means to provide the result of the method and/or cease execution of the method immediately
109 * defaultdefault is what is executed when no cases are matched name).
110 *
111 * @returnnull name of the word file (defaultdefault is what is executed when no cases are matched or otherwise)
112 */
113 privateprivate is used to restrict access to the current class only String getFname() {open braces start code blocks and must be matched with a close brace
114 // try to use the named parameter from the applet/application
115 String name =this assignment operator makes the left side equal to the right side getParameter("words");
116
117 ifif executes the next statement only if the condition in parenthesis evaluates to true (name !=this is the not equals operator which evaluates to true if both sides are different nullnull is the value used to refer to a non-existant object) {open braces start code blocks and must be matched with a close brace
118 returnreturn means to provide the result of the method and/or cease execution of the method immediately name;
119 }close braces end code blocks and must match an earlier open brace
120 // try to use the unnamed parameter from the application
121 ArrayList<String> args =this assignment operator makes the left side equal to the right side InitializeApplication.getUnnamedArgs();
122 ifif executes the next statement only if the condition in parenthesis evaluates to true (args.size() > 0) {open braces start code blocks and must be matched with a close brace
123 returnreturn means to provide the result of the method and/or cease execution of the method immediately args.get(0);
124 }close braces end code blocks and must match an earlier open brace elseelse is what happens when the if condition is false {open braces start code blocks and must be matched with a close brace
125 // give up - we don't have a file name
126 returnreturn means to provide the result of the method and/or cease execution of the method immediately "phrases.txt";
127 }close braces end code blocks and must match an earlier open brace
128 }close braces end code blocks and must match an earlier open brace
129
130 /**
131 * Null the {open braces start code blocks and must be matched with a close brace@link #phrases}close braces end code blocks and must match an earlier open brace field. If fname is non-nullnull is the value used to refer to a non-existant object, trytry is for executing a code block that may experience exceptions (errors) opening
132 * it as a {open braces start code blocks and must be matched with a close brace@link File}close braces end code blocks and must match an earlier open brace. If that throwsthrows means that the method does not handle the given types of exceptions that may occur an exception (there is a
133 * problem) attempt to use the name as an {open braces start code blocks and must be matched with a close brace@link URL}close braces end code blocks and must match an earlier open brace name to get a
134 * {open braces start code blocks and must be matched with a close brace@link Game}close braces end code blocks and must match an earlier open brace resource (used forfor is a looping structure for repeatedly executing a block of code on-line applets).
135 *
136 * <p>If either approach properly opens the {open braces start code blocks and must be matched with a close brace@link Scanner}close braces end code blocks and must match an earlier open brace read the
137 * file line by line. Each line goes into {open braces start code blocks and must be matched with a close brace@link #phrases}close braces end code blocks and must match an earlier open brace.
138 *
139 * <p>If neither approach works, the method prints an appropriate
140 * error message about not being able to open the named file/URL and
141 * leaves phrases ==this is the comparison operator which evaluates to true if both sides are the same nullnull is the value used to refer to a non-existant object.
142 *
143 * @paramthis is the Javadoc tag for documenting the purpose of parameters fname the file to read; nullnull is the value used to refer to a non-existant object means no file name specified
144 */
145 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value listLoadFromFile(String fname) {open braces start code blocks and must be matched with a close brace
146 phrases =this assignment operator makes the left side equal to the right side nullnull is the value used to refer to a non-existant object;
147 ifif executes the next statement only if the condition in parenthesis evaluates to true (fname !=this is the not equals operator which evaluates to true if both sides are different nullnull is the value used to refer to a non-existant object) {open braces start code blocks and must be matched with a close brace
148 Scanner scanner =this assignment operator makes the left side equal to the right side nullnull is the value used to refer to a non-existant object;
149 trytry is for executing a code block that may experience exceptions (errors) {open braces start code blocks and must be matched with a close brace
150 File file =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor File(fname);
151 ifif executes the next statement only if the condition in parenthesis evaluates to true (file.exists() &this performs a bit-wise and (not the same as boolean and which is &&)&this performs a bit-wise and (not the same as boolean and which is &&) file.canRead()) {open braces start code blocks and must be matched with a close brace
152 scanner =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor Scanner(file);
153 }close braces end code blocks and must match an earlier open brace
154 }close braces end code blocks and must match an earlier open brace catchcatch means to handle an exception that may occur (Exception e) {open braces start code blocks and must be matched with a close brace
155 URL url =this assignment operator makes the left side equal to the right side Game.getGameResource(fname);
156 trytry is for executing a code block that may experience exceptions (errors) {open braces start code blocks and must be matched with a close brace
157 scanner =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor Scanner(url.openStream());
158 }close braces end code blocks and must match an earlier open brace catchcatch means to handle an exception that may occur (Exception e1) {open braces start code blocks and must be matched with a close brace
159 System.out.println(
160 "There was an error reading the word file.");
161 System.out.println(
162 " Make sure to include the path to a word file.");
163 System.out.println(
164 " Make sure file exists and is readable.");
165 }close braces end code blocks and must match an earlier open brace
166 }close braces end code blocks and must match an earlier open brace
167
168 ifif executes the next statement only if the condition in parenthesis evaluates to true (scanner !=this is the not equals operator which evaluates to true if both sides are different nullnull is the value used to refer to a non-existant object) {open braces start code blocks and must be matched with a close brace
169 ArrayList<String> localStrings =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor ArrayList<String>();
170 String line;
171 whilewhile is a looping structure for executing code repeatedly (scanner.hasNextLine()) {open braces start code blocks and must be matched with a close brace
172 line =this assignment operator makes the left side equal to the right side scanner.nextLine();
173 ifif executes the next statement only if the condition in parenthesis evaluates to true (line.length() > 0) {open braces start code blocks and must be matched with a close brace
174 localStrings.add(line.toLowerCase());
175 }close braces end code blocks and must match an earlier open brace
176 }close braces end code blocks and must match an earlier open brace
177 phrases =this assignment operator makes the left side equal to the right side localStrings;
178 scanner.close();
179 }close braces end code blocks and must match an earlier open brace
180 }close braces end code blocks and must match an earlier open brace
181
182 ifif executes the next statement only if the condition in parenthesis evaluates to true (phrases ==this is the comparison operator which evaluates to true if both sides are the same nullnull is the value used to refer to a non-existant object) {open braces start code blocks and must be matched with a close brace
183 System.out.println("There was an error reading the word file.");
184 System.out.println(
185 " Make sure to include the path to a word file.");
186 System.out.println(" Make sure file exists and is readable.");
187 System.exit(1);
188 }close braces end code blocks and must match an earlier open brace
189 }close braces end code blocks and must match an earlier open brace
190
191 /**
192 * Setup the alphabet selector sprite. Almost as wide as the screen,
193 * centered in bottom half of the screen.
194 */
195 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value setupAlphabet() {open braces start code blocks and must be matched with a close brace
196 alphabet =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor AlphabetSelector();
197 alphabet.setScale(0.85);
198 alphabet.setLocation(0.5, 0.75);
199 addSprite(alphabet);
200 }close braces end code blocks and must match an earlier open brace
201
202 /**
203 * Setup the hangman sprite. Centered in top half of the screen.
204 */
205 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value setupHangman() {open braces start code blocks and must be matched with a close brace
206 hangman =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor HangmanSprite();
207 hangman.setLocation(0.5, 0.25);
208 hangman.setScale(0.5);
209 hangman.setColor(getColor("misty rose"));
210 addSprite(hangman);
211 }close braces end code blocks and must match an earlier open brace
212
213 /**
214 * Setup the score sprite in the upper left corner of the screen.
215 */
216 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value setupScore() {open braces start code blocks and must be matched with a close brace
217 scoreSprite =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor ScoreSprite();
218 scoreSprite.leftJustify();
219 scoreSprite.topJustify();
220 scoreSprite.setLocation(0.0, 0.0);
221 scoreSprite.setScale(0.1);
222 addSprite(scoreSprite);
223 }close braces end code blocks and must match an earlier open brace
224
225 /**
226 * Setup the guessable word in the center of the screen. Calls {open braces start code blocks and must be matched with a close brace@link
227 * #pickPhrase()}close braces end code blocks and must match an earlier open brace to get the value to play.
228 */
229 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value setupGamePhrase() {open braces start code blocks and must be matched with a close brace
230 String pickedPhrase =this assignment operator makes the left side equal to the right side pickPhrase();
231 gamePhrase =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor GuessableWord(pickedPhrase);
232 gamePhrase.bottomJustify();
233 gamePhrase.setWidth(0.7);
234 gamePhrase.setLocation(0.5, 0.6);
235 addSprite(gamePhrase);
236 }close braces end code blocks and must match an earlier open brace
237
238 /**
239 * Load a selection of phrase and returnreturn means to provide the result of the method and/or cease execution of the method immediately one, the word to be guessed.
240 *
241 * @returnnull the phrase to be guessed
242 */
243 privateprivate is used to restrict access to the current class only String pickPhrase() {open braces start code blocks and must be matched with a close brace
244 ifif executes the next statement only if the condition in parenthesis evaluates to true ((phrases ==this is the comparison operator which evaluates to true if both sides are the same nullnull is the value used to refer to a non-existant object) ||this is boolean or, meaning if either or both are true then the result is true phrases.isEmpty()) {open braces start code blocks and must be matched with a close brace
245 listLoadFromFile(getFname());
246 }close braces end code blocks and must match an earlier open brace
247 ifif executes the next statement only if the condition in parenthesis evaluates to true (phrases ==this is the comparison operator which evaluates to true if both sides are the same nullnull is the value used to refer to a non-existant object) {open braces start code blocks and must be matched with a close brace
248 returnreturn means to provide the result of the method and/or cease execution of the method immediately "cats and dogs";//
249 }close braces end code blocks and must match an earlier open brace
250 intint is the type for whole numbers and it is short for integer phraseNdx =this assignment operator makes the left side equal to the right side randomInt(0, phrases.size());
251 returnreturn means to provide the result of the method and/or cease execution of the method immediately phrases.remove(phraseNdx);
252 }close braces end code blocks and must match an earlier open brace
253 }close braces end code blocks and must match an earlier open brace
254
255 //Uploaded on Mon Mar 29 21:40:46 EDT 2010
|