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

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