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 57

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