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

bomb squad 1

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