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

insane game class1

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