|
001 packagepackage is used to name the directory or folder a class is in scg.ch14.gamestuff;
002
003 importimport means to make the classes and/or packages available in this program java.util.ArrayList;
004 importimport means to make the classes and/or packages available in this program java.util.Scanner;
005
006 importimport means to make the classes and/or packages available in this program scg.ch14.util.ReadAndWrite;
007
008 /**
009 * The base classclass is a group of fields and methods used for making objects in the game object hierarchy. GameObject is not
010 * designed to be instantiated directly. Instead it provides the common
011 * base forfor is a looping structure for repeatedly executing a block of code {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, {open braces start code blocks and must be matched with a close brace@link Item}close braces end code blocks and must match an earlier open brace, and {open braces start code blocks and must be matched with a close brace@link Critter}close braces end code blocks and must match an earlier open brace.
012 *
013 * <p>A {open braces start code blocks and must be matched with a close brace@link GameObject}close braces end code blocks and must match an earlier open brace has three fields:
014 *
015 * <ul>
016 * <li>uuid</li> - Universally Unique IDentifier; the id forfor is a looping structure for repeatedly executing a block of code the object
017 * which must be globally (with in the game) unique. Not typically
018 * displayed to user.
019 * <li>name</li> - name of the game object; typically displayed forfor is a looping structure for repeatedly executing a block of code the
020 * player.
021 * <li>description</li> - describes the object; shown to user when they
022 * encounter the object or explicitly look at it.
023 * </ul>
024 */
025 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 GameObject {open braces start code blocks and must be matched with a close brace
026 /**
027 * Advance through a record in an input file. The scanner has just
028 * scanned past the classclass is a group of fields and methods used for making objects identifier. The endClassID is the tag
029 * marking the end of the record and the go is the game object to read
030 * into.
031 *
032 * @paramnull gameObjectScanner scanner inside a classclass is a group of fields and methods used for making objects record
033 * @paramnull endClassID the string marking the end of the
034 * record
035 * @paramnull go object of the given {open braces start code blocks and must be matched with a close brace@link GameObject}close braces end code blocks and must match an earlier open brace
036 * extending classclass is a group of fields and methods used for making objects
037 *
038 * @returnnull
039 */
040 protectedprotected is used to restrict access to the current class and subclasses only staticstatic means that an instance is not required for access (class level access) GameObject processAttributes(
041 Scanner gameObjectScanner, String endClassID, GameObject go) {open braces start code blocks and must be matched with a close brace
042 String attribute =this assignment operator makes the left side equal to the right side ReadAndWrite.readAttribute(gameObjectScanner);
043 whilewhile is a looping structure for executing code repeatedly (!this is the not operator, which changes true to false and false to trueattribute.equalsIgnoreCase(endClassID)) {open braces start code blocks and must be matched with a close brace
044 /* ignore */ ReadAndWrite.readMatch(gameObjectScanner, "=");
045 String value =this assignment operator makes the left side equal to the right side ReadAndWrite.readValue(gameObjectScanner);
046 go.handleAttributeValuePair(attribute, value);
047
048 attribute =this assignment operator makes the left side equal to the right side ReadAndWrite.readAttribute(gameObjectScanner);
049 }close braces end code blocks and must match an earlier open brace
050 returnreturn means to provide the result of the method and/or cease execution of the method immediately go;
051 }close braces end code blocks and must match an earlier open brace
052
053 /** the description of the object */
054 privateprivate is used to restrict access to the current class only String description;
055
056 /** Items held by thisthis means the current object (the implicit parameter) Critter; built after load */
057 privateprivate is used to restrict access to the current class only finalfinal means not changeable (often used for constants) ArrayList<Item> inventory;
058
059 /** the name; need not be unique */
060 privateprivate is used to restrict access to the current class only String name;
061
062 /** universally unique identifier - the object's id */
063 privateprivate is used to restrict access to the current class only String uuid;
064
065 /**
066 * Protected constructor; {open braces start code blocks and must be matched with a close brace@link GameObject}close braces end code blocks and must match an earlier open brace cannot be constructed
067 * "naked", only subclasses of thisthis means the current object (the implicit parameter) classclass is a group of fields and methods used for making objects can be constructed.
068 */
069 protectedprotected is used to restrict access to the current class and subclasses only GameObject() {open braces start code blocks and must be matched with a close brace
070 uuid =this assignment operator makes the left side equal to the right side nullnull is the value used to refer to a non-existant object;
071 name =this assignment operator makes the left side equal to the right side nullnull is the value used to refer to a non-existant object;
072 description =this assignment operator makes the left side equal to the right side nullnull is the value used to refer to a non-existant object;
073 inventory =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor ArrayList<Item>();
074 }close braces end code blocks and must match an earlier open brace
075
076 /**
077 * add the given item to the object
078 */
079 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value add(Item item) {open braces start code blocks and must be matched with a close brace
080 ifif executes the next statement only if the condition in parenthesis evaluates to true (inventory.indexOf(item) < 0) {open braces start code blocks and must be matched with a close brace
081 inventory.add(item);
082 item.setOwner(thisthis means the current object (the implicit parameter));
083 }close braces end code blocks and must match an earlier open brace
084 }close braces end code blocks and must match an earlier open brace
085
086 /**
087 * Get the object's description
088 *
089 * @returnnull the description
090 */
091 publicpublic is used to indicate unrestricted access (any other class can have access) String getDescription() {open braces start code blocks and must be matched with a close brace
092 returnreturn means to provide the result of the method and/or cease execution of the method immediately description;
093 }close braces end code blocks and must match an earlier open brace
094
095 /**
096 * Get the full description: The description plus any "ownedDescription" bits contributed by any of the items held.
097 * @returnnull the full description.
098 */
099 publicpublic is used to indicate unrestricted access (any other class can have access) String getFullDescription() {open braces start code blocks and must be matched with a close brace
100 String fullDescription =this assignment operator makes the left side equal to the right side getDescription();
101 String separator =this assignment operator makes the left side equal to the right side " ";
102 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 inventory.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
103 String ownedDescription =this assignment operator makes the left side equal to the right side inventory.get(i).getOwnedDescription();
104 ifif executes the next statement only if the condition in parenthesis evaluates to true (ownedDescription !=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
105 fullDescription +=this increases the variable on the left by the value on the right separator +adds two numbers together or concatenates Strings together ownedDescription;
106 }close braces end code blocks and must match an earlier open brace
107 }close braces end code blocks and must match an earlier open brace
108 returnreturn means to provide the result of the method and/or cease execution of the method immediately fullDescription;
109 }close braces end code blocks and must match an earlier open brace
110
111 /**
112 * The list of items in thisthis means the current object (the implicit parameter) game object.
113 *
114 * @returnnull the inventory
115 */
116 publicpublic is used to indicate unrestricted access (any other class can have access) ArrayList<Item> getInventory() {open braces start code blocks and must be matched with a close brace
117 returnreturn means to provide the result of the method and/or cease execution of the method immediately inventory;
118 }close braces end code blocks and must match an earlier open brace
119
120 /**
121 * @returnnull the name
122 */
123 publicpublic is used to indicate unrestricted access (any other class can have access) String getName() {open braces start code blocks and must be matched with a close brace
124 returnreturn means to provide the result of the method and/or cease execution of the method immediately name;
125 }close braces end code blocks and must match an earlier open brace
126
127 /**
128 * Get the Universally Unique Identifier
129 *
130 * @returnnull the uuid
131 */
132 publicpublic is used to indicate unrestricted access (any other class can have access) String getUUID() {open braces start code blocks and must be matched with a close brace
133 returnreturn means to provide the result of the method and/or cease execution of the method immediately uuid;
134 }close braces end code blocks and must match an earlier open brace
135
136 /**
137 * Does thisthis means the current object (the implicit parameter) object have the given item in its inventory?
138 *
139 * @paramnull name name of item to find
140 *
141 * @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 the item is in inventory, falsefalse is a value for the boolean type and means not true otherwise
142 */
143 publicpublic is used to indicate unrestricted access (any other class can have access) booleanboolean is a type that is either true or false hasItemName(String name) {open braces start code blocks and must be matched with a close brace
144 returnreturn means to provide the result of the method and/or cease execution of the method immediately itemIndexByName(name) >=this evaluates to true if the left side is not less than the right side 0;
145 }close braces end code blocks and must match an earlier open brace
146
147 /**
148 * Does the item exist in the inventory?
149 *
150 * @paramnull uuid the uuid to check forfor is a looping structure for repeatedly executing a block of code
151 *
152 * @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 it is in the inventory, falsefalse is a value for the boolean type and means not true otherwise
153 */
154 publicpublic is used to indicate unrestricted access (any other class can have access) booleanboolean is a type that is either true or false hasItemUUID(String uuid) {open braces start code blocks and must be matched with a close brace
155 returnreturn means to provide the result of the method and/or cease execution of the method immediately itemIndexByUUID(uuid) >=this evaluates to true if the left side is not less than the right side 0;
156 }close braces end code blocks and must match an earlier open brace
157
158 /**
159 * Find item by name.
160 *
161 * @paramnull itemName name of item to find
162 *
163 * @returnnull the item 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 there is no such item
164 */
165 publicpublic is used to indicate unrestricted access (any other class can have access) Item itemByName(String itemName) {open braces start code blocks and must be matched with a close brace
166 Item retval =this assignment operator makes the left side equal to the right side nullnull is the value used to refer to a non-existant object;
167 intint is the type for whole numbers and it is short for integer ndx =this assignment operator makes the left side equal to the right side itemIndexByName(itemName);
168 ifif executes the next statement only if the condition in parenthesis evaluates to true (ndx >=this evaluates to true if the left side is not less than the right side 0) {open braces start code blocks and must be matched with a close brace
169 retval =this assignment operator makes the left side equal to the right side inventory.get(ndx);
170 }close braces end code blocks and must match an earlier open brace
171 returnreturn means to provide the result of the method and/or cease execution of the method immediately retval;
172 }close braces end code blocks and must match an earlier open brace
173
174 /**
175 * Get the item by UUID
176 *
177 * @paramnull itemUUID uuid to find
178 *
179 * @returnnull reference to item ifif executes the next statement only if the condition in parenthesis evaluates to true it is found; nullnull is the value used to refer to a non-existant object otherwise
180 */
181 publicpublic is used to indicate unrestricted access (any other class can have access) Item itemByUUID(String itemUUID) {open braces start code blocks and must be matched with a close brace
182 Item retval =this assignment operator makes the left side equal to the right side nullnull is the value used to refer to a non-existant object;
183 intint is the type for whole numbers and it is short for integer ndx =this assignment operator makes the left side equal to the right side itemIndexByUUID(itemUUID);
184 ifif executes the next statement only if the condition in parenthesis evaluates to true (ndx >=this evaluates to true if the left side is not less than the right side 0) {open braces start code blocks and must be matched with a close brace
185 retval =this assignment operator makes the left side equal to the right side inventory.get(ndx);
186 }close braces end code blocks and must match an earlier open brace
187 returnreturn means to provide the result of the method and/or cease execution of the method immediately retval;
188 }close braces end code blocks and must match an earlier open brace
189
190 /**
191 * Remove the named item from the inventory of thisthis means the current object (the implicit parameter) object.
192 *
193 * @paramnull itemName name of object to remove
194 *
195 * @returnnull the item removed 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 none was removed
196 */
197 publicpublic is used to indicate unrestricted access (any other class can have access) Item removeItemName(String itemName) {open braces start code blocks and must be matched with a close brace
198 Item retval =this assignment operator makes the left side equal to the right side nullnull is the value used to refer to a non-existant object;
199 intint is the type for whole numbers and it is short for integer ndx =this assignment operator makes the left side equal to the right side itemIndexByName(itemName);
200 ifif executes the next statement only if the condition in parenthesis evaluates to true (ndx >=this evaluates to true if the left side is not less than the right side 0) {open braces start code blocks and must be matched with a close brace
201 retval =this assignment operator makes the left side equal to the right side inventory.remove(ndx);
202 }close braces end code blocks and must match an earlier open brace
203 returnreturn means to provide the result of the method and/or cease execution of the method immediately retval;
204 }close braces end code blocks and must match an earlier open brace
205
206 /**
207 * Remove item from the inventory by uuid (ifif executes the next statement only if the condition in parenthesis evaluates to true it is here)
208 *
209 * @paramnull itemUUID the uuid of the object to get
210 *
211 * @returnnull reference to removed object ifif executes the next statement only if the condition in parenthesis evaluates to true one was found; nullnull is the value used to refer to a non-existant object
212 * otherwise
213 */
214 publicpublic is used to indicate unrestricted access (any other class can have access) Item removeItemUUID(String itemUUID) {open braces start code blocks and must be matched with a close brace
215 Item retval =this assignment operator makes the left side equal to the right side nullnull is the value used to refer to a non-existant object;
216 intint is the type for whole numbers and it is short for integer ndx =this assignment operator makes the left side equal to the right side itemIndexByUUID(itemUUID);
217 ifif executes the next statement only if the condition in parenthesis evaluates to true (ndx >=this evaluates to true if the left side is not less than the right side 0) {open braces start code blocks and must be matched with a close brace
218 retval =this assignment operator makes the left side equal to the right side inventory.remove(ndx);
219 }close braces end code blocks and must match an earlier open brace
220 returnreturn means to provide the result of the method and/or cease execution of the method immediately retval;
221 }close braces end code blocks and must match an earlier open brace
222
223 /**
224 * Set the description of the
225 *
226 * @paramnull description the description to set
227 */
228 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value setDescription(String description) {open braces start code blocks and must be matched with a close brace
229 thisthis means the current object (the implicit parameter).description =this assignment operator makes the left side equal to the right side description;
230 }close braces end code blocks and must match an earlier open brace
231
232 /**
233 * Set the object's name
234 *
235 * @paramnull name the name to set
236 */
237 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value setName(String name) {open braces start code blocks and must be matched with a close brace
238 thisthis means the current object (the implicit parameter).name =this assignment operator makes the left side equal to the right side name;
239 }close braces end code blocks and must match an earlier open brace
240
241 /**
242 * Get a string representation of thisthis means the current object (the implicit parameter) object. Note thisthis means the current object (the implicit parameter) is NOT the
243 * same as the save format.
244 */
245 @Override
246 publicpublic is used to indicate unrestricted access (any other class can have access) String toString() {open braces start code blocks and must be matched with a close brace
247 returnreturn means to provide the result of the method and/or cease execution of the method immediately getClass().getName() +adds two numbers together or concatenates Strings together " [\n" +adds two numbers together or concatenates Strings together toStringGuts() +adds two numbers together or concatenates Strings together "\n]";
248 }close braces end code blocks and must match an earlier open brace
249
250 /**
251 * Find the index of the given item name (forfor is a looping structure for repeatedly executing a block of code picking up items)
252 *
253 * @paramnull itemName name to find
254 *
255 * @returnnull index of the matching item (or -1)
256 */
257 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer itemIndexByName(String itemName) {open braces start code blocks and must be matched with a close brace
258 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 inventory.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
259 ifif executes the next statement only if the condition in parenthesis evaluates to true (inventory.get(i).getName().equalsIgnoreCase(itemName)) {open braces start code blocks and must be matched with a close brace
260 returnreturn means to provide the result of the method and/or cease execution of the method immediately i;
261 }close braces end code blocks and must match an earlier open brace
262 }close braces end code blocks and must match an earlier open brace
263 returnreturn means to provide the result of the method and/or cease execution of the method immediately -1;
264 }close braces end code blocks and must match an earlier open brace
265
266 /**
267 * Find the index of the given item UUID (forfor is a looping structure for repeatedly executing a block of code picking up items)
268 *
269 * @paramnull itemUUID
270 *
271 * @returnnull index of match or -1
272 */
273 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer itemIndexByUUID(String itemUUID) {open braces start code blocks and must be matched with a close brace
274 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 inventory.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
275 Item item =this assignment operator makes the left side equal to the right side inventory.get(i);
276 ifif executes the next statement only if the condition in parenthesis evaluates to true (item.getUUID().equalsIgnoreCase(itemUUID)) {open braces start code blocks and must be matched with a close brace
277 returnreturn means to provide the result of the method and/or cease execution of the method immediately i;
278 }close braces end code blocks and must match an earlier open brace
279 }close braces end code blocks and must match an earlier open brace
280 returnreturn means to provide the result of the method and/or cease execution of the method immediately -1;
281 }close braces end code blocks and must match an earlier open brace
282
283 /**
284 * Set the Universally Unique IDentifier; privateprivate is used to restrict access to the current class only because the UUID is
285 * not something that should be changed.
286 *
287 * @paramnull uuid the newnew is used to create objects by calling the constructor uuid value
288 */
289 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value setUUID(String uuid) {open braces start code blocks and must be matched with a close brace
290 thisthis means the current object (the implicit parameter).uuid =this assignment operator makes the left side equal to the right side uuid;
291 }close braces end code blocks and must match an earlier open brace
292
293 /**
294 * Handle, ifif executes the next statement only if the condition in parenthesis evaluates to true possible, the given name/value pair
295 *
296 * @paramnull attribute name of the attribute
297 * @paramnull value the value of the attribute
298 *
299 * @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 the attribute was handled at thisthis means the current object (the implicit parameter) level; falsefalse is a value for the boolean type and means not true
300 * otherwise.
301 */
302 protectedprotected is used to restrict access to the current class and subclasses only booleanboolean is a type that is either true or false handleAttributeValuePair(String attribute,
303 String value) {open braces start code blocks and must be matched with a close brace
304 ifif executes the next statement only if the condition in parenthesis evaluates to true (attribute.equalsIgnoreCase("uuid")) {open braces start code blocks and must be matched with a close brace
305 setUUID(value.toLowerCase());
306 returnreturn means to provide the result of the method and/or cease execution of the method immediately truetrue is the boolean value that is the opposite of false;
307 }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 (attribute.equalsIgnoreCase("name")) {open braces start code blocks and must be matched with a close brace
308 setName(value);
309 returnreturn means to provide the result of the method and/or cease execution of the method immediately truetrue is the boolean value that is the opposite of false;
310 }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 (attribute.equalsIgnoreCase("description")) {open braces start code blocks and must be matched with a close brace
311 setDescription(value);
312 returnreturn means to provide the result of the method and/or cease execution of the method immediately truetrue is the boolean value that is the opposite of false;
313 }close braces end code blocks and must match an earlier open brace
314 returnreturn means to provide the result of the method and/or cease execution of the method immediately falsefalse is a value for the boolean type and means not true;
315 }close braces end code blocks and must match an earlier open brace
316
317 /**
318 * Internal toString helper method.
319 *
320 * @returnnull A string representation of the fields.
321 */
322 protectedprotected is used to restrict access to the current class and subclasses only String toStringGuts() {open braces start code blocks and must be matched with a close brace
323 String invList =this assignment operator makes the left side equal to the right side "inventory = [";
324 String separator =this assignment operator makes the left side equal to the right side "";
325 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 inventory.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
326 invList +=this increases the variable on the left by the value on the right separator +adds two numbers together or concatenates Strings together inventory.get(i).toString();
327 separator =this assignment operator makes the left side equal to the right side ", ";
328 }close braces end code blocks and must match an earlier open brace
329 invList +=this increases the variable on the left by the value on the right "]";
330 returnreturn means to provide the result of the method and/or cease execution of the method immediately "uuid = " +adds two numbers together or concatenates Strings together uuid +adds two numbers together or concatenates Strings together "\n" +adds two numbers together or concatenates Strings together "name = " +adds two numbers together or concatenates Strings together name +adds two numbers together or concatenates Strings together "\n" +adds two numbers together or concatenates Strings together
331 "description = " +adds two numbers together or concatenates Strings together description +adds two numbers together or concatenates Strings together "\n" +adds two numbers together or concatenates Strings together invList;
332 }close braces end code blocks and must match an earlier open brace
333 }close braces end code blocks and must match an earlier open brace
334
335 //Uploaded on Mon Mar 29 21:40:09 EDT 2010
|