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

bombsquad game2

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