Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

game with createItems and room hashmap

By: ace on Jul 28th, 2010  |  syntax: None  |  size: 6.87 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import java.util.HashMap;
  2.  
  3. /**
  4.  *  This class is the main class of the "World of Zuul" application.
  5.  *  "World of Zuul" is a very simple, text based adventure game.  Users
  6.  *  can walk around some scenery. That's all. It should really be extended
  7.  *  to make it more interesting!
  8.  *
  9.  *  To play this game, create an instance of this class and call the "play"
  10.  *  method.
  11.  *
  12.  *  This main class creates and initialises all the others: it creates all
  13.  *  rooms, creates the parser and starts the game.  It also evaluates and
  14.  *  executes the commands that the parser returns.
  15.  *
  16.  * @author  Michael Kolling and David J. Barnes
  17.  * @version 1.0 (February 2002)
  18.  */
  19.  
  20. public class Game
  21. {
  22.     private Parser parser;
  23.     private Room currentRoom;
  24.     private HashMap<String, Room> rooms;
  25.        
  26.     /**
  27.      * Create the game and initialise its internal map.
  28.      */
  29.     public Game()
  30.     {
  31.         rooms = new HashMap<String, Room>();
  32.         createRooms();
  33.         createItems();
  34.         parser = new Parser();
  35.     }
  36.  
  37.     /**
  38.      * Create all the rooms and link their exits together.
  39.      */
  40.     private void createRooms()
  41.     {
  42.         Room outside, galleria, theatre, pub, store, office;
  43.      
  44.         // create the rooms
  45.         outside = new Room("outside the main entrance of the mall");
  46.         galleria = new Room("in the mall galleria");
  47.         theatre = new Room("in a movie theatre");
  48.         pub = new Room("in the pub");
  49.         store = new Room("in a store");
  50.         office = new Room("in the mall office");
  51.        
  52.         // initialise room exits
  53.         outside.setExit("east", pub);
  54.         outside.setExit("south", galleria);
  55.         outside.setExit ("west", theatre);
  56.         theatre.setExit ("east", outside);
  57.         theatre.setExit ("south", office);
  58.         pub.setExit("west", outside);
  59.         pub.setExit("south", store);
  60.         office.setExit("north", theatre);
  61.         office.setExit("east", galleria);
  62.         galleria.setExit("north", outside);
  63.         galleria.setExit("east", store);
  64.         galleria.setExit("west", office);
  65.         store.setExit ("north", pub);
  66.         store.setExit("west", galleria);
  67.        
  68.         rooms.put("outside", outside);
  69.         rooms.put("galleria", galleria);
  70.         rooms.put("theatre", theatre);
  71.         rooms.put("pub", pub);
  72.         rooms.put("office", office);
  73.         rooms.put("store", store);
  74.  
  75.         currentRoom = outside;  // start game outside
  76.     }
  77.    
  78.     private void createItems()
  79.     {
  80.        Item tickingBomb, popcornMachine, beerTap;
  81.        
  82.         // create the items
  83.         tickingBomb = new Item(1, "a ticking bomb");
  84.         popcornMachine = new Item(2, "a popcorn machine");
  85.         beerTap  = new Item (3, "a beer tap");
  86.        
  87.         // initialize item locations
  88.        
  89.         rooms.get("pub").setItems(tickingBomb);
  90.         rooms.get("pub").setItems(popcornMachine);
  91.         rooms.get("pub").setItems(beerTap);
  92.     }
  93.  
  94. private void printLocationInfo()
  95. {
  96. System.out.println("You are " + currentRoom.getLongDescription());
  97. System.out.print("Exits: ");
  98. System.out.println(currentRoom.getExitString());
  99. }
  100.    
  101.    
  102.    
  103.     /**
  104.      *  Main play routine.  Loops until end of play.
  105.      */
  106.     public void play()
  107.     {            
  108.         printWelcome();
  109.  
  110.         // Enter the main command loop.  Here we repeatedly read commands and
  111.         // execute them until the game is over.
  112.                
  113.         boolean finished = false;
  114.         while (! finished) {
  115.             Command command = parser.getCommand();
  116.             finished = processCommand(command);
  117.         }
  118.         System.out.println("Thank you for playing.  Good bye.");
  119.     }
  120.  
  121.     /**
  122.      * Print out the opening message for the player.
  123.      */
  124.     private void printWelcome()
  125.     {
  126.         System.out.println();
  127.         System.out.println("Welcome to the World of Zuul!");
  128.         System.out.println("World of Zuul is a new, incredibly boring adventure game.");
  129.         System.out.println("Type 'help' if you need help.");
  130.         System.out.println();
  131.         printLocationInfo();
  132.        
  133.     }
  134.  
  135.     /**
  136.      * Given a command, process (that is: execute) the command.
  137.      * If this command ends the game, true is returned, otherwise false is
  138.      * returned.
  139.      */
  140.     private boolean processCommand(Command command)
  141.     {
  142.         boolean wantToQuit = false;
  143.  
  144.         if(command.isUnknown()) {
  145.             System.out.println("I don't know what you mean...");
  146.             return false;
  147.         }
  148.  
  149.         String commandWord = command.getCommandWord();
  150.         if (commandWord.equals("help"))
  151.             printHelp();
  152.         else if (commandWord.equals("go"))
  153.             goRoom(command);
  154.             else if (commandWord.equals("look"))
  155.             look();
  156.         else if (commandWord.equals("quit"))
  157.             wantToQuit = quit(command);
  158.         else if (commandWord.equals("defuse"))
  159.             defuse();
  160.  
  161.         return wantToQuit;
  162.     }
  163.  
  164.     // implementations of user commands:
  165.  
  166.     /**
  167.      * Print out some help information.
  168.      * Here we print some stupid, cryptic message and a list of the
  169.      * command words.
  170.      */
  171.     private void printHelp()
  172.     {
  173.         System.out.println("You are lost. You are alone. You wander");
  174.         System.out.println("around at the university.");
  175.         System.out.println();
  176.         System.out.println("Your command words are:");
  177.         System.out.println ("" + parser.showCommands());
  178.  
  179.     }
  180.  
  181.     /**
  182.      * Try to go to one direction. If there is an exit, enter
  183.      * the new room, otherwise print an error message.
  184.      */
  185.     private void goRoom(Command command)
  186.     {
  187.         if(!command.hasSecondWord()) {
  188.             // if there is no second word, we don't know where to go...
  189.             System.out.println("Go where?");
  190.             return;
  191.         }
  192.  
  193.         String direction = command.getSecondWord();
  194.        
  195.         // Try to leave current room.
  196.         Room nextRoom = currentRoom.getExit(direction);
  197.        
  198.    
  199.         if (nextRoom == null)
  200.             System.out.println("There is no door!");
  201.         else {
  202.             currentRoom = nextRoom;
  203.             printLocationInfo();
  204.            
  205.         }
  206.     }
  207.    
  208.     private void defuse()
  209.         {
  210.          System.out.println ("you have defused the bomb");
  211.         }
  212.  
  213.     private void look()
  214. {
  215. System.out.println(currentRoom.getLongDescription());
  216. }
  217.  
  218.    
  219.     /**
  220.      * "Quit" was entered. Check the rest of the command to see
  221.      * whether we really quit the game. Return true, if this command
  222.      * quits the game, false otherwise.
  223.      */
  224.     private boolean quit(Command command)
  225.     {
  226.         if(command.hasSecondWord()) {
  227.             System.out.println("Quit what?");
  228.             return false;
  229.         }
  230.         else
  231.             return true;  // signal that we want to quit
  232.     }
  233. }