scg/ch14/core/Game

From FANG

Jump to: navigation, search

001 package scg.ch14.core;
002 
003 import java.io.File;
004 import java.io.FileNotFoundException;
005 import java.io.FileReader;
006 import java.util.ArrayList;
007 import java.util.Scanner;
008 
009 import scg.ch14.dictionary.Dictionary;
010 
011 import scg.ch14.gamestuff.Critter;
012 import scg.ch14.gamestuff.GameObject;
013 import scg.ch14.gamestuff.Item;
014 import scg.ch14.gamestuff.Location;
015 
016 import scg.ch14.io.Keyboard;
017 import scg.ch14.io.SansCommentFilterReader;
018 
019 import scg.ch14.util.ReadAndWrite;
020 
021 /**
022  * Text adventure game phase all items. Reads the map, cast, and props.
023  * Permits moving around the map, seeing the description of each
024  * location, items and all of that.
025  */
026 public class Game {
027   public static final String CONFIGURATION_FILE_NAME = "gameconfig.txt";
028 
029   /** the default number of columns for output */
030   public static final int DEFAULT_SCREEN_WIDTH = 72;
031 
032   /** the UUID of the player in the critter file */
033   public static final String PLAYER_UUID = "player";
034 
035   /** Reference to the game; for getCurrentGame() */
036   private static Game theGame;
037 
038   /** command alias dictionary */
039   private Dictionary aliases;
040 
041   /** name of dictionary file */
042   private String aliasesFName;
043 
044   /** all of the critters in the universe */
045   private ArrayList<Critter> cast;
046 
047   /** the name of the cast file */
048   private String castFName;
049 
050   /** game over flag */
051   private boolean gameOver;
052 
053   /** the map of the universe */
054   private ArrayList<Location> map;
055 
056   /** map file name */
057   private String mapFName;
058 
059   /** delayed message string */
060   private String message;
061 
062   /** the player's critter */
063   private Critter player;
064 
065   /** all of the items in the universe */
066   private ArrayList<Item> props;
067 
068   /** props file name */
069   private String propsFName;
070 
071   /** ready flag; set false if configuration problems occur */
072   private boolean ready;
073 
074   /** did the user quit the game? They neither win nor lose */
075   private boolean userQuit;
076 
077   /** width of the screen for output; a variable so it can be changed */
078   private int screenWidth;
079 
080   /** show the description this time around? */
081   private boolean showDescription;
082 
083   /**
084    * Construct a new game (with just a map). The name of the Location
085    * file and the dictionary are hard coded here. They probably belong
086    * in a configuration file.
087    */
088   public Game() {
089     screenWidth = DEFAULT_SCREEN_WIDTH;
090     ready = false;
091     message = "";
092     if (processConfigurationFile(CONFIGURATION_FILE_NAME)) {
093       this.aliases = new Dictionary(openFileForInput(aliasesFName));
094       this.map = readMapFile(mapFName);
095       this.cast = readCastFile(castFName);
096       this.props = readPropsFile(propsFName);
097 
098       distributeCritters();
099       distributeLoot();
100 
101       if ((map != null&& (cast != null&& (props != null)) {
102         player = critterByUUID(PLAYER_UUID);
103         cast.remove(player);// we are not a critter in the game
104         this.ready = true;
105       }
106     }
107   }
108 
109   /**
110    * Get a static reference to the current game. Since there is only one
111    {@link Game} the {@link GameObject}s in it can find it through this
112    * method just like in FANG. They need it for calling message.
113    *
114    @return  the current game.
115    */
116   public static Game getCurrentGame() {
117     return theGame;
118   }
119 
120   /**
121    * Main program for the game . Create a game object; if all goes well,
122    * call play.
123    *
124    @param  args  command-line arguments to the program; ignored by
125    *               this program
126    */
127   public static void main(String[] args{
128     theGame = new Game();
129     if (theGame.isReady()) {
130       theGame.play();
131     } else {
132       System.err.println("Trouble initalizing game.");
133     }
134   }
135 
136   /**
137    * Add a new message to the message lines.
138    *
139    @param  msg
140    */
141   public void addMessage(String msg{
142     if (message.length() != 0{
143       message += "\n";
144     }
145     message += msg;
146     showDescription = false;
147   }
148 
149   /**
150    * Is the game ready (did reading work?)
151    *
152    @return  true if ready; false otherwise
153    */
154   public boolean isReady() {
155     return ready;
156   }
157 
158   /**
159    * The main game loop. Play until done.
160    */
161   public void play() {
162     userQuit = false;
163     gameOver = false;
164     showDescription = true;
165     while (!gameOver{
166       showMessage();
167       if (showDescription{
168         // show state
169         showGameState();
170       }
171 
172       showDescription = true;
173       
174       // get user input
175       System.out.print("Game> ");
176       String commandLine = Keyboard.nextLine();
177       // update state
178       this.processCommand(commandLine);
179     }
180     // show any pending messages from the last turn
181     showMessage();
182 
183     if (!userQuit{// user quit?
184       if (player.getHealth() <= 0{// or lose?
185         System.out.println("You lost the game. Next time avoid dying.");
186       } else {// or win?
187         showGameState();// show last location
188         System.out.println("You win!");
189       }
190     }
191   }
192 
193   /**
194    * Show the delayed message, if any, and clear the message
195    */
196   public void showMessage() {
197     if (message.length() != 0{
198       System.out.println(message);
199       message = "";
200     }
201   }
202 
203   /**
204    * Find a location given its UUID.
205    *
206    @param   uuid  the UUID of the location to find in the list of all
207    *                Critters.
208    *
209    @return  the matching location or null
210    */
211   private Critter critterByUUID(String uuid{
212     for (int = 0; i != cast.size()++i{
213       if (cast.get(i).getUUID().equalsIgnoreCase(uuid)) {
214         return cast.get(i);
215       }
216     }
217     return null;
218   }
219 
220   /**
221    * After loading {@link #cast} and {@link #map}, connect up the
222    * critters with their locations.
223    */
224   private void distributeCritters() {
225     for (int = 0; i != cast.size()++i{
226       Critter critter = cast.get(i);
227       Location location = locationByUUID(critter.getLocationID());
228       location.add(critter);
229     }
230   }
231 
232   /**
233    * After loading {@link #props}{@link #cast}, and {@link #map},
234    * connect up the prop elements with their owners. Owners have the
235    * prop added.
236    */
237   private void distributeLoot() {
238     for (int = 0; i != props.size()++i{
239       Item item = props.get(i);
240       GameObject owner = gameObjectByUUID(item.getOwnerID());
241       owner.add(item);
242     }
243   }
244 
245   /**
246    * Drop an item. The remainder of the commandLine is available to pull
247    * out the names.
248    *
249    @param   commandLine  the remaining line
250    *
251    @return  true if at least one item is dropped
252    */
253   private boolean doDrop(Scanner commandLine{
254     Location curr = locationByUUID(player.getLocationID());
255     boolean foundOne = false;
256     String dropped = "";
257     String separator = "";
258     while (commandLine.hasNext()) {
259       Item theItem = itemByCommandLine(player, commandLine);
260       if (theItem != null{
261         give(player, curr, theItem);
262         dropped += separator + theItem.getName();
263         foundOne = true;
264       }
265     }
266     if (foundOne{
267       addMessage("You dropped " + dropped + "");
268     }
269     return foundOne;
270   }
271 
272   /**
273    * Move player East if possible.
274    *
275    @return  true if player moved; false otherwise
276    */
277   private boolean doEast() {
278     Location here = player.getLocation();
279     boolean canMove = (here.getEast() != Location.NO_SUCH_LOCATION&&
280       unlocked(here.getEKey(), player, here);
281     if (canMove{
282       moveTo(locationByUUID(here.getEast()));
283     }
284     return canMove;
285   }
286 
287   /**
288    * Give an item from the player to some other critter. The command can
289    * have one of two forms:
290    *
291    <p>give <critter> <item>
292    *
293    <p>give <item> <critter>
294    *
295    <p>(the word "give" has already been processed so this method is
296    * called). Must take words off commandLine, building up a name. Check
297    * that name against both the list of critters in the current location
298    * (who we could give to) and the player's inventory (what we could be
299    * giving)
300    *
301    <p>Assuming a match is found, the rest of the line has to be the
302    * other name. Try to find it.
303    *
304    <p>Both a critter and an item are found, then give(player, critter,
305    * item).
306    *
307    <p>The tradesFor list in the critter is also checked to see if an
308    * exchange takes place. If so, give(critter, player, tradeItem).
309    *
310    @param   commandLine  the two names
311    *
312    @return  true if something is given, false otherwise
313    */
314   private boolean doGive(Scanner commandLine{
315     String firstName = commandLine.next();
316     Location curr = locationByUUID(player.getLocationID());
317     Item theItem = player.itemByName(firstName);
318     Critter theCritter = curr.critterByName(firstName);
319 
320     while (commandLine.hasNext() && (theItem == null&&
321         (theCritter == null)) {
322       firstName += " " + commandLine.next();
323       theItem = player.itemByName(firstName);
324       theCritter = curr.critterByName(firstName);
325     }
326 
327     if (theItem != null{
328       String critterName = restOfLine(commandLine);
329       theCritter = curr.critterByName(critterName);
330     } else if (theCritter != null{
331       String itemName = restOfLine(commandLine);
332       theItem = player.itemByName(itemName);
333     }
334 
335     if ((theCritter != null&& (theItem != null)) {
336       String trade = theCritter.willTradeFor(theItem.getUUID());
337       addMessage("You give the " + theItem.getName() + " to the " +
338         theCritter.getName());
339       give(player, theCritter, theItem);
340       if (trade != null{
341         Item tradeItem = theCritter.itemByUUID(trade);
342         if (tradeItem != null{
343           give(theCritter, player, tradeItem);
344           addMessage("The " + theCritter.getName() + " gives you a " +
345             tradeItem.getName());
346         }
347       }
348 
349       if (theCritter.getHealth() <= 0{
350         addMessage(theCritter.getName() + " has left.");
351         curr.removeCritterUUID(theCritter.getUUID());
352       }
353 
354       return true;
355     }
356 
357     return false;
358   }
359 
360   /**
361    * Move player North if possible.
362    *
363    @return  true if player moved; false otherwise
364    */
365   private boolean doNorth() {
366     Location here = player.getLocation();
367     boolean canMove = (here.getNorth() != Location.NO_SUCH_LOCATION&&
368       unlocked(here.getNKey(), player, here);
369     if (canMove{
370       moveTo(locationByUUID(here.getNorth()));
371     }
372     return canMove;
373   }
374 
375   /**
376    * Pickup the item(s) listed in the commandLine
377    *
378    @param   commandLine  scanner of rest of the line
379    *
380    @return  true if at least one item is picked up
381    */
382   private boolean doPickup(Scanner commandLine{
383     Location curr = locationByUUID(player.getLocationID());
384     boolean gotOne = false;
385     String pickedup = "";
386     String separator = "";
387     while (commandLine.hasNext()) {
388       Item theItem = itemByCommandLine(curr, commandLine);
389       if (theItem != null{
390         give(curr, player, theItem);
391         pickedup += separator + theItem.getName();
392         gotOne = true;
393       }
394     }
395     if (gotOne{
396       addMessage("You picked up " + pickedup + ".");
397     }
398     return gotOne;
399   }
400 
401   /**
402    * Move player South if possible.
403    *
404    @return  true if player moved; false otherwise
405    */
406   private boolean doSouth() {
407     Location here = player.getLocation();
408     boolean canMove = (here.getSouth() != Location.NO_SUCH_LOCATION&&
409       unlocked(here.getSKey(), player, here);
410     if (canMove{
411       moveTo(locationByUUID(here.getSouth()));
412     }
413     return canMove;
414   }
415 
416   /**
417    * Move player West if possible.
418    *
419    @return  true if player moved; false otherwise
420    */
421   private boolean doWest() {
422     Location here = player.getLocation();
423     boolean canMove = (here.getWest() != Location.NO_SUCH_LOCATION&&
424       unlocked(here.getWKey(), player, here);
425     if (canMove{
426       moveTo(locationByUUID(here.getWest()));
427     }
428     return canMove;
429   }
430 
431   /**
432    * Given the uuid, find the object matching.
433    *
434    @param   uuid  the uuid to find
435    *
436    @return  null if no object is found to match, matching {@link
437    *          GameObject} otherwise.
438    */
439   private GameObject gameObjectByUUID(String uuid{
440     if (locationByUUID(uuid!= null{
441       return locationByUUID(uuid);
442     }
443     if (critterByUUID(uuid!= null{
444       return critterByUUID(uuid);
445     }
446     if (itemByUUID(uuid!= null{
447       return itemByUUID(uuid);
448     }
449     return null;
450   }
451 
452   /**
453    * Actually transfer the item from giver to getter.
454    *
455    @param  giver   the GameObject that has item
456    @param  getter  the GameObject to end up with item
457    @param  item    the item to transfer
458    */
459   private void give(GameObject giver, GameObject getter, Item item{
460     giver.removeItemUUID(item.getUUID());
461     getter.add(item);
462   }
463 
464   /**
465    * Try to match the next set of words in the commandLine with the name
466    * of an item in owner's inventory
467    *
468    @param   owner        the owner who must own the element we seek
469    @param   lineProcessor  the commandLine (we pull words off of it)
470    *
471    @return  the first matching item (matching some prefix of the words
472    *          in commandLine) if there is one; null if no match is found
473    */
474   private Item itemByCommandLine(GameObject owner,
475     Scanner lineProcessor{
476     String name = "";
477     String separator = "";
478     while (lineProcessor.hasNext()) {
479       name += separator + lineProcessor.next();
480       separator = " ";
481       Item theItem = owner.itemByName(name);
482       if (theItem != null{
483         return theItem;
484       }
485     }
486     return null;
487   }
488 
489   /**
490    * Find a location given its UUID.
491    *
492    @param   uuid  the UUID of the location to find in the list of all
493    *                Items.
494    *
495    @return  the matching location or null
496    */
497   private Item itemByUUID(String uuid{
498     for (int = 0; i != props.size()++i{
499       if (props.get(i).getUUID().equalsIgnoreCase(uuid)) {
500         return props.get(i);
501       }
502     }
503     return null;
504   }
505 
506   /**
507    * Find a location given its UUID.
508    *
509    @param   uuid  the UUID of the location to find in the list of all
510    *                Locations.
511    *
512    @return  the matching location or null
513    */
514   private Location locationByUUID(String uuid{
515     for (int = 0; i != map.size()++i{
516       if (map.get(i).getUUID().equalsIgnoreCase(uuid)) {
517         return map.get(i);
518       }
519     }
520     return null;
521   }
522 
523   /**
524    * Handle looking. See if the name matches an item in the player's
525    * inventory, an item in the location, or a critter in the location.
526    * Return true if a match was found and described.
527    *
528    @param   name  the name of the thing to look at.
529    *
530    @return  true if a description was printed; false otherwise
531    */
532   private boolean look(String name{
533     if (name.length() == 0{
534       return false;
535     }
536     Item ours = player.itemByName(name);
537     if (ours != null{
538       showStringWrapped(ours.getFullDescription());
539       return true;
540     }
541     Location curr = locationByUUID(player.getLocationID());
542     Item theirs = curr.itemByName(name);
543     if (theirs != null{
544       showStringWrapped(theirs.getFullDescription());
545       return true;
546     }
547     Critter them = curr.critterByName(name);
548     if (them != null{
549       showStringWrapped(them.getFullDescription());
550       return true;
551     }
552     return false;
553   }
554 
555   /**
556    * Actually move the player to the given location. This is were the
557    * player and location are updated (and winning is checked).
558    *
559    @param  toWhere  the Location to which the player should move.
560    */
561   private void moveTo(Location toWhere{
562     player.setLocation(toWhere);
563     toWhere.add(player);
564     gameOver = toWhere.isWinningLocation();
565   }
566 
567   /**
568    * Open the named file for input, removing any comments from the file
569    * as it is read.
570    *
571    @param   fname  path name (relative or absolute) to file to open
572    *
573    @return  {@link Scanner} attached to the given file or null if
574    *          there was a problem opening the named file
575    */
576   private Scanner openFileForInput(String fname{
577     Scanner opened = null;
578     try {
579       opened = new Scanner(new SansCommentFilterReader(
580             new FileReader(new File(fname))));
581     } catch (FileNotFoundException e{
582       // do nothing (null will be returned which is what we want)
583     }
584     return opened;
585   }
586 
587   /**
588    * Given a command, as entered by the user, process it. Translate the
589    * command through the alias dictionary and then look it up. This is
590    * just a dispatch routine; the real work is done in the handler
591    * commands.
592    *
593    @param  commandLine  the word entered by the user.
594    */
595   private void processCommand(String commandLine{
596     if (commandLine.length() == 0{
597       return;
598     }
599     // Scan across the command line as entered by the user
600     Scanner lineProcessor = new Scanner(commandLine);
601     String command = lineProcessor.next();
602 
603     String normalizedCommand = aliases.get(command);
604     if (normalizedCommand == null{
605       normalizedCommand = command;
606     }
607     if (normalizedCommand.equalsIgnoreCase("north")) {
608       doNorth();
609     } else if (normalizedCommand.equalsIgnoreCase("south")) {
610       doSouth();
611     } else if (normalizedCommand.equalsIgnoreCase("east")) {
612       doEast();
613     } else if (normalizedCommand.equalsIgnoreCase("west")) {
614       doWest();
615     } else if (normalizedCommand.equalsIgnoreCase("inventory")) {
616       showInventory("You have "true, player.getInventory());
617     } else if (normalizedCommand.equalsIgnoreCase("look")) {
618       String name = restOfLine(lineProcessor);
619       if (look(name)) {
620         showDescription = false;
621       }
622     } else if (normalizedCommand.equalsIgnoreCase("pickup")) {
623       if (!doPickup(lineProcessor)) {
624         addMessage("Unable to find anything to pickup.");
625       }
626     } else if (normalizedCommand.equalsIgnoreCase("drop")) {
627       if (!doDrop(lineProcessor)) {
628         addMessage("Unable to find anything to drop.");
629       }
630     } else if (normalizedCommand.equalsIgnoreCase("give")) {
631       if (!doGive(lineProcessor)) {
632         addMessage("Unable to give that to them.");
633       }
634     } else if (normalizedCommand.equalsIgnoreCase("exit")) {
635       gameOver = true;
636       userQuit = true;
637     } else {
638       unknownCommand(commandLine, normalizedCommand);
639     }
640     if (player.getHealth() <= 0{
641       gameOver = true;
642     }
643   }
644 
645   /**
646    Process the named configuration file. The file is a collection of
647    * attribute value pairs which should specify the "alias", "cast",
648    * "map", and "props" file names. If all four files are specified,
649    * method returns true (with fields set).
650    *
651    @param   configurationFName  name of file to process
652    *
653    @return  true if all four file names were set to non-null values;
654    *          false otherwise
655    */
656   private boolean processConfigurationFile(String configurationFName{
657     aliasesFName = null;
658     castFName = null;
659     mapFName = null;
660     propsFName = null;
661 
662     Scanner config = openFileForInput(configurationFName);
663     String attribute = ReadAndWrite.readAttribute(config);
664     while (config.hasNext()) {
665       /* ignore */ ReadAndWrite.readMatch(config, "=");
666       String value = ReadAndWrite.readValue(config);
667       if (attribute.equalsIgnoreCase("alias")) {
668         aliasesFName = value;
669       } else if (attribute.equalsIgnoreCase("cast")) {
670         castFName = value;
671       } else if (attribute.equalsIgnoreCase("map")) {
672         mapFName = value;
673       } else if (attribute.equalsIgnoreCase("props")) {
674         propsFName = value;
675       }
676       if (config.hasNext()) {
677         attribute = ReadAndWrite.readAttribute(config);
678       }
679     }
680 
681     return (aliasesFName != null&& (castFName != null&&
682       (mapFName != null&& (propsFName != null);
683   }
684 
685   /**
686    * Read the map file. Open the named file and read it as critters into
687    * the array list returned by the method.
688    *
689    @param   fname  name of critter file
690    *
691    @return  ArrayList<Critter> containing all of the read critters.
692    */
693   private ArrayList<Critter> readCastFile(String fname{
694     ArrayList<Critter> returnCast = null;
695     Scanner castFile = openFileForInput(fname);
696     if (castFile != null{
697       returnCast = new ArrayList<Critter>();
698       while (castFile.hasNext()) {
699         Critter critter = Critter.readObjectFromFile(castFile);
700         if (critter != null{
701           returnCast.add(critter);
702         }
703       }
704     }
705     return returnCast;
706   }
707 
708   /**
709    * Read the map file. Open the named file and read it as locations
710    * into the array list returned by the method.
711    *
712    @param   fname  name of location file
713    *
714    @return  ArrayList<Location> conatining all of the read locations.
715    */
716   private ArrayList<Location> readMapFile(String fname{
717     ArrayList<Location> returnMap = null;
718     Scanner mapFile = openFileForInput(fname);
719     if (mapFile != null{
720       returnMap = new ArrayList<Location>();
721       while (mapFile.hasNext()) {
722         Location location = Location.readObjectFromFile(mapFile);
723         if (location != null{
724           returnMap.add(location);
725         }
726       }
727     }
728     return returnMap;
729   }
730 
731   /**
732    * Read the props file. Open the named file and read it as items into
733    * the array list returned by the method.
734    *
735    @param   fname  name of item file
736    *
737    @return  ArrayList<Item> containing all of the read items.
738    */
739   private ArrayList<Item> readPropsFile(String fname{
740     ArrayList<Item> returnProps = null;
741     Scanner propsFile = openFileForInput(fname);
742     if (propsFile != null{
743       returnProps = new ArrayList<Item>();
744       while (propsFile.hasNext()) {
745         Item item = Item.readObjectFromFile(propsFile);
746         if (item != null{
747           returnProps.add(item);
748         }
749       }
750     }
751     return returnProps;
752   }
753 
754   /**
755    * Get the rest of the line from the scanner (assumed to be on a
756    * single line): trim and compress spaces.
757    *
758    @param   lineProcessor  the scanner to clean out
759    *
760    @return  the rest of the line.
761    */
762   private String restOfLine(Scanner lineProcessor{
763     return lineProcessor.nextLine().trim().replaceAll("\\s+"" ");
764   }
765 
766   /**
767    * Display the current state in a game-centric way. Show the
768    * description of the current place and list the stuff and critters in
769    * it.
770    */
771   private void showGameState() {
772     // Get the current location.
773     Location curr = locationByUUID(player.getLocationID());
774     showStringWrapped(curr.getFullDescription());
775 
776     ArrayList<Item> inventory = curr.getInventory();
777     if (!inventory.isEmpty()) {// only if there is stuff
778       showInventory("You see ", inventory);
779     }
780 
781     // list all the visitors
782     ArrayList<Critter> critters = curr.getVisitors();
783     for (int = 0; c != critters.size()++c{
784       // EXACT REFERENCE MATCH is just what we want here (!= rather
785       // than !equals)
786       if (critters.get(c!= player{// don't list self
787         System.out.println("You see a " + critters.get(c).getName());
788       }
789     }
790   }
791 
792   /**
793    * Print the prefix followed by the list of all of the items.
794    *
795    @param  prefix     The string to go before the list, "You see",
796    *                    "You have", etc.
797    @param  inventory  The inventory list
798    */
799   private void showInventory(String prefix, ArrayList<Item> inventory{
800     showInventory(prefix, false, inventory);
801   }
802 
803   /**
804    * Print the prefix followed by the list of all of the items. Only
805    * items with no "owned description" are shown unless showAll is true.
806    * The reason: items with owned descriptions change the description of
807    * their owner.
808    *
809    @param  prefix     The string to go before the list, "You see",
810    *                    "You have", etc.
811    @param  showAll    force owned description items to show up, too
812    @param  inventory  The inventory list
813    */
814   private void showInventory(String prefix, boolean showAll,
815     ArrayList<Item> inventory{
816     if (!inventory.isEmpty()) {
817       boolean somethingToShow = false;
818       String inventoryString = prefix;
819       String separator = "";
820 
821       for (int = 0; i != inventory.size()++i{
822         Item item = inventory.get(i);
823         if (showAll || (item.getOwnedDescription() == null)) {
824           inventoryString += separator + inventory.get(i).getName();
825           separator = ", ";
826           somethingToShow = true;
827         }
828       }
829 
830       inventoryString += ".";
831       if (somethingToShow{
832         showStringWrapped(inventoryString);
833       }
834     }
835   }
836 
837   /**
838    * Wrap the given string so that it doesn't go past the edge of the
839    * screen. Uses {@link util.ReadAndWrite#wrap(Stringint)} to do the
840    * wrapping; then processes the list here.
841    *
842    @param  displayString  the string to show
843    */
844   private void showStringWrapped(String displayString{
845     ArrayList<String> wrappedDisplay = ReadAndWrite.wrap(displayString,
846         screenWidth);
847     for (int = 0; i != wrappedDisplay.size()++i{
848       System.out.println(wrappedDisplay.get(i));
849     }
850   }
851 
852   /**
853    * Tell the user that their command was not understood. The parameters
854    * are the pre and post translation versions of the command. That way,
855    if one wants, one can show the internal command alias that was
856    * found.
857    *
858    @param  playerProvided  just what the player typed
859    @param  translated      the internal alias
860    */
861   private void unknownCommand(String playerProvided,
862     String translated{
863     System.out.println("Unable to understand comamnd " +
864       playerProvided);
865   }
866 
867   /**
868    * Is the direction unlocked? That is, if key is required for critter
869    * to move from here, check if it is okay.
870    *
871    <p>If key is null, all is well (no key needed)
872    *
873    <p>If key is a UUID, unlocked if critter has key as an item or the
874    * location has the key as a critter.
875    *
876    <p>If key begins '!' then it is a NOT key (can't have the item or
877    * critter).
878    *
879    @param   key      the uuid to unlock
880    @param   wantsToMove  the critter who wants to move
881    @param   here     the location to move from
882    *
883    @return  true if the way is clear; false otherwise
884    */
885   private boolean unlocked(String key, Critter wantsToMove, Location here{
886     boolean passable = true;
887     if (key != null{
888       if (key.charAt(0== '!'{// It is a "not" key
889         key = key.substring(1);// get rid of !
890         passable = (!wantsToMove.hasItemUUID(key&&
891             (!here.hasCritterUUID(key)));
892       } else {
893         passable = (wantsToMove.hasItemUUID(key||
894             (here.hasCritterUUID(key)));
895       }
896     }
897     if (!passable{
898       addMessage("Your way appears to be blocked.");
899     }
900     return passable;
901   }
902 }
903 
904 //Uploaded on Mon Mar 29 21:40:59 EDT 2010


Download/View scg/ch14/core/Game.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