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

gameclass 3

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