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

backgameclass3

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