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

gameclassitems2

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