From FANG
|
001 packagepackage is used to name the directory or folder a class is in scg.ch14.core;
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.io.FileReader;
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 scg.ch14.dictionary.Dictionary;
010
011 importimport means to make the classes and/or packages available in this program scg.ch14.gamestuff.Location;
012
013 importimport means to make the classes and/or packages available in this program scg.ch14.io.Keyboard;
014 importimport means to make the classes and/or packages available in this program scg.ch14.io.SansCommentFilterReader;
015
016 /**
017 * Text adventure game phase with just the map. Reads the map and
018 * permits moving around the map, seeing the "description" of each
019 * location. At thisthis means the current object (the implicit parameter) point the description is the string representation
020 * of the {open braces start code blocks and must be matched with a close brace@link Location}close braces end code blocks and must match an earlier open brace objects.
021 */
022 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 GameWithMapOnly {open braces start code blocks and must be matched with a close brace
023 /**
024 * Main program forfor is a looping structure for repeatedly executing a block of code the game with just a map. Create a game object (it
025 * takes no parameters as the file names are hard wired) and then call
026 * play. The "Game Begins" and "Game Ends" lines are there forfor is a looping structure for repeatedly executing a block of code
027 * debugging; make sure things actually happen.
028 *
029 * @paramthis is the Javadoc tag for documenting the purpose of parameters args command-line arguments to the program; ignored by
030 * thisthis means the current object (the implicit parameter) program
031 */
032 publicpublic is used to indicate unrestricted access (any other class can have access) staticstatic means that an instance is not required for access (class level access) voidvoid means the method does not return a value mainThe main method is the place where applications begin executing.(String[brackets are typically used to declare, initialize and index (indicate which element of) arrays]brackets are typically used to declare, initialize and index (indicate which element of) arrays args) {open braces start code blocks and must be matched with a close brace
033 GameWithMapOnly gmo =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor GameWithMapOnly();
034 System.out.println("Game Begins");
035 gmo.play();
036 System.out.println("Game Over");
037 }close braces end code blocks and must match an earlier open brace
038
039 /** command alias dictionary */
040 privateprivate is used to restrict access to the current class only finalfinal means not changeable (often used for constants) Dictionary aliases;
041
042 /** game over flag */
043 privateprivate is used to restrict access to the current class only booleanboolean is a type that is either true or false gameOver;
044
045 /** the map of the world */
046 privateprivate is used to restrict access to the current class only finalfinal means not changeable (often used for constants) ArrayList<Location> map;
047
048 /** the uuid where the player "is" */
049 privateprivate is used to restrict access to the current class only String player;
050
051 /**
052 * Construct a newnew is used to create objects by calling the constructor game (with just a map). The name of the Location
053 * file and the dictionary are hard coded here. They probably belong
054 * in a configuration file.
055 */
056 publicpublic is used to indicate unrestricted access (any other class can have access) GameWithMapOnly() {open braces start code blocks and must be matched with a close brace
057 aliases =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor Dictionary(openFileForInput("school.aliases"));
058 map =this assignment operator makes the left side equal to the right side readMapFile("school.map");
059 ifif executes the next statement only if the condition in parenthesis evaluates to true (map !=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
060 player =this assignment operator makes the left side equal to the right side map.get(0).getUUID();
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
063
064 /**
065 * The mainThe main method is the place where applications begin executing. game loop. Play until done.
066 */
067 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value play() {open braces start code blocks and must be matched with a close brace
068 gameOver =this assignment operator makes the left side equal to the right side falsefalse is a value for the boolean type and means not true;
069 whilewhile is a looping structure for executing code repeatedly (!this is the not operator, which changes true to false and false to truegameOver) {open braces start code blocks and must be matched with a close brace
070 // show state
071 System.out.println(locationByUUID(player));
072 // get user input
073 String command =this assignment operator makes the left side equal to the right side Keyboard.next();
074 // update state
075 processCommand(command);
076 }close braces end code blocks and must match an earlier open brace
077 }close braces end code blocks and must match an earlier open brace
078
079 /**
080 * Move player East ifif executes the next statement only if the condition in parenthesis evaluates to true possible.
081 *
082 * @returnnull truetrue is the boolean value that is the opposite of false ifif executes the next statement only if the condition in parenthesis evaluates to true player moved; falsefalse is a value for the boolean type and means not true otherwise
083 */
084 privateprivate is used to restrict access to the current class only booleanboolean is a type that is either true or false doEast() {open braces start code blocks and must be matched with a close brace
085 Location here =this assignment operator makes the left side equal to the right side locationByUUID(player);
086 booleanboolean is a type that is either true or false canMove =this assignment operator makes the left side equal to the right side (here.getEast() !=this is the not equals operator which evaluates to true if both sides are different Location.NO_SUCH_LOCATION);
087 ifif executes the next statement only if the condition in parenthesis evaluates to true (canMove) {open braces start code blocks and must be matched with a close brace
088 player =this assignment operator makes the left side equal to the right side here.getEast();
089 }close braces end code blocks and must match an earlier open brace
090 returnreturn means to provide the result of the method and/or cease execution of the method immediately canMove;
091 }close braces end code blocks and must match an earlier open brace
092
093 /**
094 * Move player North ifif executes the next statement only if the condition in parenthesis evaluates to true possible.
095 *
096 * @returnnull truetrue is the boolean value that is the opposite of false ifif executes the next statement only if the condition in parenthesis evaluates to true player moved; falsefalse is a value for the boolean type and means not true otherwise
097 */
098 privateprivate is used to restrict access to the current class only booleanboolean is a type that is either true or false doNorth() {open braces start code blocks and must be matched with a close brace
099 Location here =this assignment operator makes the left side equal to the right side locationByUUID(player);
100 booleanboolean is a type that is either true or false canMove =this assignment operator makes the left side equal to the right side (here.getNorth() !=this is the not equals operator which evaluates to true if both sides are different Location.NO_SUCH_LOCATION);
101 ifif executes the next statement only if the condition in parenthesis evaluates to true (canMove) {open braces start code blocks and must be matched with a close brace
102 player =this assignment operator makes the left side equal to the right side here.getNorth();
103 }close braces end code blocks and must match an earlier open brace
104 returnreturn means to provide the result of the method and/or cease execution of the method immediately canMove;
105 }close braces end code blocks and must match an earlier open brace
106
107 /**
108 * Move player South ifif executes the next statement only if the condition in parenthesis evaluates to true possible.
109 *
110 * @returnnull truetrue is the boolean value that is the opposite of false ifif executes the next statement only if the condition in parenthesis evaluates to true player moved; falsefalse is a value for the boolean type and means not true otherwise
111 */
112 privateprivate is used to restrict access to the current class only booleanboolean is a type that is either true or false doSouth() {open braces start code blocks and must be matched with a close brace
113 Location here =this assignment operator makes the left side equal to the right side locationByUUID(player);
114 booleanboolean is a type that is either true or false canMove =this assignment operator makes the left side equal to the right side (here.getSouth() !=this is the not equals operator which evaluates to true if both sides are different Location.NO_SUCH_LOCATION);
115 ifif executes the next statement only if the condition in parenthesis evaluates to true (canMove) {open braces start code blocks and must be matched with a close brace
116 player =this assignment operator makes the left side equal to the right side here.getSouth();
117 }close braces end code blocks and must match an earlier open brace
118 returnreturn means to provide the result of the method and/or cease execution of the method immediately canMove;
119 }close braces end code blocks and must match an earlier open brace
120
121 /**
122 * Move player West ifif executes the next statement only if the condition in parenthesis evaluates to true possible.
123 *
124 * @returnnull truetrue is the boolean value that is the opposite of false ifif executes the next statement only if the condition in parenthesis evaluates to true player moved; falsefalse is a value for the boolean type and means not true otherwise
125 */
126 privateprivate is used to restrict access to the current class only booleanboolean is a type that is either true or false doWest() {open braces start code blocks and must be matched with a close brace
127 Location here =this assignment operator makes the left side equal to the right side locationByUUID(player);
128 booleanboolean is a type that is either true or false canMove =this assignment operator makes the left side equal to the right side (here.getWest() !=this is the not equals operator which evaluates to true if both sides are different Location.NO_SUCH_LOCATION);
129 ifif executes the next statement only if the condition in parenthesis evaluates to true (canMove) {open braces start code blocks and must be matched with a close brace
130 player =this assignment operator makes the left side equal to the right side here.getWest();
131 }close braces end code blocks and must match an earlier open brace
132 returnreturn means to provide the result of the method and/or cease execution of the method immediately canMove;
133 }close braces end code blocks and must match an earlier open brace
134
135 /**
136 * Find the location that goes with the given UUID. Uses sequential
137 * search of the {open braces start code blocks and must be matched with a close brace@link #map}close braces end code blocks and must match an earlier open brace.
138 *
139 * @paramthis is the Javadoc tag for documenting the purpose of parameters uuid the id to search forfor is a looping structure for repeatedly executing a block of code
140 *
141 * @returnnull nullnull is the value used to refer to a non-existant object ifif executes the next statement only if the condition in parenthesis evaluates to true not found; reference to the given {open braces start code blocks and must be matched with a close brace@link Location}close braces end code blocks and must match an earlier open brace
142 * otherwise.
143 */
144 privateprivate is used to restrict access to the current class only Location locationByUUID(String uuid) {open braces start code blocks and must be matched with a close brace
145 forfor is a looping structure for repeatedly executing a block of code (intint is the type for whole numbers and it is short for integer i =this assignment operator makes the left side equal to the right side 0; i !=this is the not equals operator which evaluates to true if both sides are different map.size(); ++this is the increment operator, which increases the variable by 1i) {open braces start code blocks and must be matched with a close brace
146 ifif executes the next statement only if the condition in parenthesis evaluates to true (map.get(i).getUUID().equalsIgnoreCase(uuid)) {open braces start code blocks and must be matched with a close brace
147 returnreturn means to provide the result of the method and/or cease execution of the method immediately map.get(i);
148 }close braces end code blocks and must match an earlier open brace
149 }close braces end code blocks and must match an earlier open brace
150 returnreturn means to provide the result of the method and/or cease execution of the method immediately nullnull is the value used to refer to a non-existant object;
151 }close braces end code blocks and must match an earlier open brace
152
153 /**
154 * Open the named file forfor is a looping structure for repeatedly executing a block of code input, removing any comments from the file
155 * as it is read.
156 *
157 * @paramthis is the Javadoc tag for documenting the purpose of parameters fname path name (relative or absolute) to file to open
158 *
159 * @returnnull a {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 attached to the given file or nullnull is the value used to refer to a non-existant object ifif executes the next statement only if the condition in parenthesis evaluates to true
160 * there was a problem opening the named file
161 */
162 privateprivate is used to restrict access to the current class only Scanner openFileForInput(String fname) {open braces start code blocks and must be matched with a close brace
163 Scanner opened =this assignment operator makes the left side equal to the right side nullnull is the value used to refer to a non-existant object;
164 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
165 opened =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor Scanner(newnew is used to create objects by calling the constructor SansCommentFilterReader(
166 newnew is used to create objects by calling the constructor FileReader(newnew is used to create objects by calling the constructor File(fname))));
167 }close braces end code blocks and must match an earlier open brace catchcatch means to handle an exception that may occur (FileNotFoundException e) {open braces start code blocks and must be matched with a close brace
168 // do nothing (null will be returned which is what we want)
169 }close braces end code blocks and must match an earlier open brace
170 returnreturn means to provide the result of the method and/or cease execution of the method immediately opened;
171 }close braces end code blocks and must match an earlier open brace
172
173 /**
174 * Given a command, as entered by the user, process it. Translate the
175 * command through the alias dictionary and then look it up. This is
176 * just a dispatch routine; the real work is done in the handler
177 * commands.
178 *
179 * @paramthis is the Javadoc tag for documenting the purpose of parameters command the word entered by the user.
180 */
181 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value processCommand(String command) {open braces start code blocks and must be matched with a close brace
182 String normalizedCommand =this assignment operator makes the left side equal to the right side aliases.get(command);
183 ifif executes the next statement only if the condition in parenthesis evaluates to true (normalizedCommand.equalsIgnoreCase("north")) {open braces start code blocks and must be matched with a close brace
184 doNorth();
185 }close braces end code blocks and must match an earlier open brace elseelse is what happens when the if condition is false ifif executes the next statement only if the condition in parenthesis evaluates to true (normalizedCommand.equalsIgnoreCase("south")) {open braces start code blocks and must be matched with a close brace
186 doSouth();
187 }close braces end code blocks and must match an earlier open brace elseelse is what happens when the if condition is false ifif executes the next statement only if the condition in parenthesis evaluates to true (normalizedCommand.equalsIgnoreCase("east")) {open braces start code blocks and must be matched with a close brace
188 doEast();
189 }close braces end code blocks and must match an earlier open brace elseelse is what happens when the if condition is false ifif executes the next statement only if the condition in parenthesis evaluates to true (normalizedCommand.equalsIgnoreCase("west")) {open braces start code blocks and must be matched with a close brace
190 doWest();
191 }close braces end code blocks and must match an earlier open brace elseelse is what happens when the if condition is false ifif executes the next statement only if the condition in parenthesis evaluates to true (normalizedCommand.equalsIgnoreCase("exit")) {open braces start code blocks and must be matched with a close brace
192 gameOver =this assignment operator makes the left side equal to the right side truetrue is the boolean value that is the opposite of false;
193 }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
194 unknownCommand(command, normalizedCommand);
195 }close braces end code blocks and must match an earlier open brace
196 }close braces end code blocks and must match an earlier open brace
197
198 /**
199 * Read the map file. Open the named file and read it as locations
200 * into the array list returned by the method.
201 *
202 * @paramthis is the Javadoc tag for documenting the purpose of parameters fname name of location file
203 *
204 * @returnnull ArrayList<Location> conatining all of the read locations.
205 */
206 privateprivate is used to restrict access to the current class only ArrayList<Location> readMapFile(String fname) {open braces start code blocks and must be matched with a close brace
207 ArrayList<Location> returnMap =this assignment operator makes the left side equal to the right side nullnull is the value used to refer to a non-existant object;
208 Scanner mapFile =this assignment operator makes the left side equal to the right side openFileForInput(fname);
209 ifif executes the next statement only if the condition in parenthesis evaluates to true (mapFile !=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
210 returnMap =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor ArrayList<Location>();
211 whilewhile is a looping structure for executing code repeatedly (mapFile.hasNext()) {open braces start code blocks and must be matched with a close brace
212 Location location =this assignment operator makes the left side equal to the right side Location.readObjectFromFile(mapFile);
213 ifif executes the next statement only if the condition in parenthesis evaluates to true (location !=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
214 returnMap.add(location);
215 }close braces end code blocks and must match an earlier open brace
216 }close braces end code blocks and must match an earlier open brace
217 }close braces end code blocks and must match an earlier open brace
218 returnreturn means to provide the result of the method and/or cease execution of the method immediately returnMap;
219 }close braces end code blocks and must match an earlier open brace
220
221 /**
222 * Tell the user that their command was not understood. The parameters
223 * are the pre and post translation versions of the command. That way,
224 * ifif executes the next statement only if the condition in parenthesis evaluates to true one wants, one can show the internal command alias that was
225 * found.
226 *
227 * @paramthis is the Javadoc tag for documenting the purpose of parameters playerProvided just what the player typed
228 * @paramthis is the Javadoc tag for documenting the purpose of parameters translated the internal alias
229 */
230 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value unknownCommand(String playerProvided,
231 String translated) {open braces start code blocks and must be matched with a close brace
232 System.out.println("Unable to understand comamnd " +adds two numbers together or concatenates Strings together
233 playerProvided);
234 }close braces end code blocks and must match an earlier open brace
235 }close braces end code blocks and must match an earlier open brace
236
237 //Uploaded on Mon Mar 29 21:39:00 EDT 2010
|
Download/View scg/ch14/core/GameWithMapOnly.java