scg/ch14/gamestuff/Location

From FANG

Jump to: navigation, search

001 package scg.ch14.gamestuff;
002 
003 import java.util.ArrayList;
004 import java.util.Scanner;
005 
006 import scg.ch14.core.Game;
007 
008 /**
009  * A GameObject representing a location in the adventure game.
010  *
011  <p>A Location has two specific fields: lights, a boolean indicating
012  * whether or not the lights are on and darkDescription, how the
013  * location looks if there is no light. If there is a darkDescription
014  * and the lights are off, {@link #getDescription()} returns the dark
015  * description. Thus regular description handling in the {@link Game}
016  * can be used with lights without modification.
017  */
018 public class Location
019   extends GameObject {
020   /** value of UUID to mean no such location exists */
021   public static final String NO_SUCH_LOCATION = "NO_SUCH_LOCATION";
022 
023   /** UUIDs of locations in given directions */
024   private String east;
025 
026   /** UUIDs of key objects for going a given direction */
027   private String eKey;
028   private String nKey;
029   private String north;
030   private String sKey;
031 
032   private String south;
033 
034   /** a list of visiting critters */
035   private final ArrayList<Critter> visitors;
036 
037   private String west;
038 
039   /** does player win by getting here? */
040   private boolean winningLocation;
041   private String wKey;
042 
043   /**
044    * Construct a new, empty Location.
045    */
046   public Location() {
047     visitors = new ArrayList<Critter>();
048     north = NO_SUCH_LOCATION;
049     south = NO_SUCH_LOCATION;
050     east = NO_SUCH_LOCATION;
051     west = NO_SUCH_LOCATION;
052 
053     eKey = null;
054     nKey = null;
055     sKey = null;
056     wKey = null;
057 
058     winningLocation = false;
059   }
060 
061   /**
062    * Read one location from the given scanner
063    *
064    @param   gameObjectScanner  scanner open for input
065    *
066    @return  the new Location or null if there is a problem
067    */
068   public static Location readObjectFromFile(Scanner gameObjectScanner{
069     String classID = gameObjectScanner.next();
070     if (!classID.equalsIgnoreCase("Location")) {
071       return null;// not the expected type of object; punt
072     }
073 
074     String endClassID = "/" + classID;
075 
076     Location lo = new Location();
077     GameObject.processAttributes(gameObjectScanner, endClassID, lo);
078     return lo;
079   }
080 
081   /**
082    * add the given critter to the object
083    */
084   public void add(Critter critter{
085     if (visitors.indexOf(critter0{
086       visitors.add(critter);
087       critter.setLocation(this);
088     }
089   }
090 
091   /**
092    * Get the critter by name
093    *
094    @param   name  name to find
095    *
096    @return  reference to critter if it is found; null otherwise
097    */
098   public Critter critterByName(String name{
099     Critter retval = null;
100     int cNdx = critterIndexByName(name);
101     if (cNdx >= 0{
102       retval = visitors.get(cNdx);
103     }
104     return retval;
105   }
106 
107   /**
108    * Get the critter by identifier
109    *
110    @param   uuid  uuid to find
111    *
112    @return  reference to critter if it is found; null otherwise
113    */
114   public Critter critterByUUID(String uuid{
115     Critter retval = null;
116     int cNdx = critterIndex(uuid);
117     if (cNdx >= 0{
118       retval = visitors.get(cNdx);
119     }
120     return retval;
121   }
122 
123   /**
124    @return  the east
125    */
126   public String getEast() {
127     return east;
128   }
129 
130   public String getEKey() {
131     return eKey;
132   }
133 
134   public String getNKey() {
135     return nKey;
136   }
137 
138   /**
139    @return  the north
140    */
141   public String getNorth() {
142     return north;
143   }
144 
145   public String getSKey() {
146     return sKey;
147   }
148 
149   /**
150    @return  the south
151    */
152   public String getSouth() {
153     return south;
154   }
155 
156   /**
157    * Get the list of visitors in the location
158    */
159   public ArrayList<Critter> getVisitors() {
160     return visitors;
161   }
162 
163   /**
164    @return  the west
165    */
166   public String getWest() {
167     return west;
168   }
169 
170   public String getWKey() {
171     return wKey;
172   }
173 
174   /**
175    * Does the critter exist in the visitors?
176    *
177    @param   uuid  the uuid to check for
178    *
179    @return  true if it is in the visitors, false otherwise
180    */
181   public boolean hasCritterUUID(String uuid{
182     return critterIndex(uuid>= 0;
183   }
184 
185   /**
186    @return  the winningLocation
187    */
188   public boolean isWinningLocation() {
189     return winningLocation;
190   }
191 
192   /**
193    * Remove critter from the visitors by uuid (if it is here)
194    *
195    @param   uuid  the uuid of the object to get
196    *
197    @return  reference to removed object if one was found; null
198    *          otherwise
199    */
200   public Critter removeCritterUUID(String uuid{
201     Critter retval = null;
202     int ndx = critterIndex(uuid);
203     if (ndx >= 0{
204       retval = visitors.remove(ndx);
205     }
206     return retval;
207   }
208 
209   /**
210    @param  east  the east to set
211    */
212   public void setEast(String east{
213     this.east = east;
214   }
215 
216   public void setEKey(String eKey{
217     this.eKey = eKey;
218   }
219 
220   public void setNKey(String nKey{
221     this.nKey = nKey;
222   }
223 
224   /**
225    @param  north  the north to set
226    */
227   public void setNorth(String north{
228     this.north = north;
229   }
230 
231   public void setSKey(String sKey{
232     this.sKey = sKey;
233   }
234 
235   /**
236    @param  south  the south to set
237    */
238   public void setSouth(String south{
239     this.south = south;
240   }
241 
242   /**
243    @param  west  the west to set
244    */
245   public void setWest(String west{
246     this.west = west;
247   }
248 
249   /**
250    @param  winningLocation  the winningLocation to set
251    */
252   public void setWinningLocation(boolean winningLocation{
253     this.winningLocation = winningLocation;
254   }
255 
256   public void setWKey(String wKey{
257     this.wKey = wKey;
258   }
259 
260   /**
261    * Find the index of the given critter
262    *
263    @param   critterUUID  critters uuid
264    *
265    @return  index of match or -1
266    */
267   private int critterIndex(String critterUUID{
268     for (int = 0; i != visitors.size()++i{
269       if (visitors.get(i).getUUID().equalsIgnoreCase(critterUUID)) {
270         return i;
271       }
272     }
273     return -1;
274   }
275 
276   /**
277    * Find the index of the given critter
278    *
279    @param   critterName  critters uuid
280    *
281    @return  index of match or -1
282    */
283   private int critterIndexByName(String critterName{
284     for (int = 0; i != visitors.size()++i{
285       if (visitors.get(i).getName().equalsIgnoreCase(critterName)) {
286         return i;
287       }
288     }
289     return -1;
290   }
291 
292   /**
293    * Handle, if possible, the given name/value pair
294    *
295    @param   attribute  name of the attribute
296    @param   value      the value of the attribute
297    *
298    @return  true if the attribute was handled at this level; false
299    *          otherwise.
300    */
301   @Override
302   protected boolean handleAttributeValuePair(String attribute,
303     String value{
304     if (super.handleAttributeValuePair(attribute, value)) {
305       return true;
306     }
307     
308     value = value.toLowerCase()// all of them need it
309     if (attribute.equalsIgnoreCase("nKey")) {
310       setNKey(value);
311       return true;
312     } else if (attribute.equalsIgnoreCase("eKey")) {
313       setEKey(value);
314       return true;
315     } else if (attribute.equalsIgnoreCase("nKey")) {
316       setNKey(value);
317       return true;
318     } else if (attribute.equalsIgnoreCase("sKey")) {
319       setSKey(value);
320       return true;
321     } else if (attribute.equalsIgnoreCase("wKey")) {
322       setWKey(value);
323       return true;
324     } else if (attribute.equalsIgnoreCase("east")) {
325       setEast(value);
326       return true;
327     } else if (attribute.equalsIgnoreCase("north")) {
328       setNorth(value);
329       return true;
330     } else if (attribute.equalsIgnoreCase("south")) {
331       setSouth(value);
332       return true;
333     } else if (attribute.equalsIgnoreCase("west")) {
334       setWest(value);
335       return true;
336     } else if (attribute.equalsIgnoreCase("winningLocation")) {
337       setWinningLocation(Boolean.parseBoolean(value));
338       return true;
339     }
340     return false;
341   }
342 
343   /**
344    * Internal toString helper method.
345    *
346    @return  A string representation of the fields.
347    */
348   @Override
349   protected String toStringGuts() {
350     return super.toStringGuts() + "\n" + "\n" + "north = " + north +
351       "\n" + "south = " + south + "\n" + "east = " + east + "\n" +
352       "west = " + west;
353   }
354 }
355 
356 //Uploaded on Mon Mar 29 21:40:52 EDT 2010


Download/View scg/ch14/gamestuff/Location.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