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 with main

By: ace on Sep 22nd, 2010  |  syntax: None  |  size: 18.73 KB  |  hits: 12  |  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. import java.util.Random;
  3. import java.util.Collection;
  4. import java.util.Set;
  5.  
  6. /**
  7.  *  This class is the main class of the "World of Zuul" application.
  8.  *  "World of Zuul" is a very simple, text based adventure game.  Users
  9.  *  can walk around some scenery. That's all. It should really be extended
  10.  *  to make it more interesting!
  11.  *
  12.  *  To play this game, create an instance of this class and call the "play"
  13.  *  method.
  14.  *
  15.  *  This main class creates and initialises all the others: it creates all
  16.  *  rooms, creates the parser and starts the game.  It also evaluates and
  17.  *  executes the commands that the parser returns.
  18.  *
  19.  * @author  Michael Kolling and David J. Barnes
  20.  * @version 1.0 (February 2002)
  21.  */
  22.  
  23.  
  24. public class Game
  25. {
  26.  
  27.     private Parser parser;
  28.    
  29.     private String lastDirection;
  30.     private HashMap<String,Room> rooms;
  31.     private Player player;
  32.     private int numberOfMoves;
  33.     private Room lastRoom;
  34.     private Random tPort;
  35.     private HashMap<String,Character> characters;  
  36.     /**
  37.      * Create the game and initialise its internal map.
  38.      */
  39.    
  40.     public static void main(String[] args)
  41. {
  42.     Game game = new Game();
  43.    
  44. }
  45.    
  46.    
  47.    
  48.     public Game()
  49.     {
  50.        
  51.         parser = new Parser();
  52.         rooms = new HashMap<String, Room>();
  53.         characters = new HashMap<String, Character>();
  54.         player = new Player();
  55.         createRooms();
  56.         numberOfMoves = 0;
  57.        tPort = new Random();
  58.        
  59.        
  60.        
  61.     }
  62.    
  63.    
  64.  
  65.     /**
  66.      * Create all the rooms and link their exits together.
  67.      */
  68.     private void createRooms()
  69.     {
  70.         Room outside, galleria, theatre, pub, store, office, closet;
  71.      
  72.         // create the rooms
  73.         outside = new Room("outside the main entrance of the mall");
  74.         galleria = new Room("in the mall galleria");
  75.         theatre = new Room("in a movie theatre");
  76.         pub = new Room("in the pub");
  77.         store = new Room("in a store");
  78.         office = new Room("in the mall office");
  79.         closet = new Room ("in a closet");
  80.         //initialise roommap
  81.         rooms.put("outside", outside);
  82.         rooms.put("theatre", theatre);
  83.         rooms.put("pub", pub);
  84.         rooms.put("office", office);
  85.         rooms.put("galleria", galleria);
  86.         rooms.put ("store", store);
  87.         rooms.put("closet", closet);
  88.        
  89.         // initialise room exits
  90.         outside.setExit("east", pub);
  91.         outside.setExit("south", galleria);
  92.         outside.setExit ("west", theatre);
  93.         theatre.setExit ("east", outside);
  94.         theatre.setExit ("south", office);
  95.         pub.setExit("west", outside);
  96.         pub.setExit("south", store);
  97.         office.setExit("north", theatre);
  98.         office.setExit("east", galleria);
  99.         galleria.setExit("north", outside);
  100.         galleria.setExit("east", store);
  101.         galleria.setExit("west", office);
  102.         galleria.setExit("south", closet);
  103.         store.setExit ("north", pub);
  104.         store.setExit("west", galleria);
  105.         closet.setExit("north", galleria);
  106.        
  107.         createItems();
  108.         player.setCurrentRoom(outside);  // start game outside
  109.         lastDirection = null;
  110.         createCharacters();
  111.        
  112.  
  113.        
  114.     }
  115.    
  116.     private void createItems()
  117.     {
  118.        Item tickingBomb, popcornMachine, beerTap, beer, key;
  119.        
  120.         // create the items
  121.         tickingBomb = new Item(1, "a ticking bomb", "bomb");
  122.         popcornMachine = new Item(4, "a popcorn machine", "popcornmachine");
  123.         key = new Item(0, "a key", "key");
  124.         beerTap  = new Item (3, "a beer tap", "beertap");
  125.         rooms.get("pub").setItems(tickingBomb);
  126.         rooms.get("pub").setItems(popcornMachine);
  127.         rooms.get("pub").setItems(beerTap);
  128.         rooms.get("office").setItems(key);
  129.      }
  130.      
  131.      
  132.      /* create charcters and populater hashmap characters*/
  133.      
  134.       private void createCharacters()
  135.     {
  136.        Character littleBoy, bartender, shopper, distraughtMother, partyGuy;
  137.        
  138.         // create the items
  139.         littleBoy = new Character("littleBoy", "tommy");
  140.         bartender = new Character("bartender", "michael");
  141.         shopper = new Character("shopper", "julie");
  142.         distraughtMother = new Character("distraughtmother", "emily");
  143.         partyGuy = new MobileCharacter("partyGuy", "bob", "west");
  144.              
  145.         littleBoy.setCurrentRoom(rooms.get("store"));
  146.         bartender.setCurrentRoom(rooms.get("pub"));
  147.         shopper.setCurrentRoom(rooms.get("store"));
  148.         distraughtMother.setCurrentRoom(rooms.get("theatre"));
  149.         partyGuy.setCurrentRoom(rooms.get("pub"));
  150.        
  151.         characters.put("littleboy", littleBoy);
  152.         characters.put("bartender", bartender);
  153.         characters.put("shopper", shopper);
  154.         characters.put("distraughtmother", distraughtMother);
  155.         characters.put("partyGuy", partyGuy);
  156.        
  157.        
  158.      }
  159.      
  160.  
  161. private void printLocationInfo()
  162. {
  163. System.out.println ("You are " + player.getCurrentRoom().getLongDescription() + characterInfo());
  164.  
  165. }
  166.    
  167.    
  168.    
  169.     /**
  170.      *  Main play routine.  Loops until end of play.
  171.      */
  172.     public void play()
  173.     {            
  174.         printWelcome();
  175.        
  176.         // Enter the main command loop.  Here we repeatedly read commands and
  177.         // execute them until the game is over.
  178.                
  179.         boolean finished = false;
  180.         while (! finished) {
  181.             Command command = parser.getCommand();
  182.             finished = processCommand(command);
  183.         }
  184.         System.out.println("Thank you for playing.  Good bye.");
  185.     }
  186.  
  187.     /**
  188.      * Print out the opening message for the player.
  189.      */
  190.     private void printWelcome()
  191.     {
  192.         System.out.println();
  193.         System.out.println("Welcome to the World of Zuul!");
  194.         System.out.println("World of Zuul is a new, incredibly boring adventure game.");
  195.         System.out.println("Type 'help' if you need help.");
  196.         System.out.println();
  197.         printLocationInfo();
  198.        
  199.     }
  200.  
  201.     /**
  202.      * Given a command, process (that is: execute) the command.
  203.      * If this command ends the game, true is returned, otherwise false is
  204.      * returned.
  205.      */
  206.     private boolean processCommand(Command command)
  207.     {
  208.         boolean wantToQuit = false;
  209.        
  210.         if(numberOfMoves == 20)
  211.             {
  212.                 System.out.println("the bomb has detonated destoying the mall! You have failed.");
  213.                 return true;
  214.             }
  215.        
  216.            
  217.         if(command.isUnknown()) {
  218.             System.out.println("I don't know what you mean...");
  219.             return false;
  220.         }
  221.  
  222.         String commandWord = command.getCommandWord();
  223.         if (commandWord.equals("help"))
  224.             printHelp();
  225.         else if (commandWord.equals("go"))
  226.             goRoom(command);
  227.             else if (commandWord.equals("look"))
  228.             look();
  229.         else if (commandWord.equals("quit"))
  230.             wantToQuit = quit(command);
  231.         else if (commandWord.equals("defuse"))
  232.             wantToQuit = defuse(command);
  233.         else if(commandWord.equals ("back"))
  234.             back();
  235.         else if (commandWord.equals ("take"))
  236.             take(command);
  237.         else if (commandWord.equals("drop"))
  238.             drop (command);
  239.         else if (commandWord.equals("inventory"))
  240.             inventory();
  241.        else if (commandWord.equals("pour"))
  242.           pour(command);
  243.        else if (commandWord.equals("drink"))
  244.           drink(command);
  245.       else if (commandWord.equals("rescue"))
  246.         rescue(command);
  247.              return wantToQuit;
  248.  
  249.        
  250.     }
  251.  
  252.     // implementations of user commands:
  253.  
  254.     /**
  255.      * Print out some help information.
  256.      * Here we print some stupid, cryptic message and a list of the
  257.      * command words.
  258.      */
  259.     private void printHelp()
  260.     {
  261.         System.out.println("You are lost. You are alone. ");
  262.         System.out.println("You wander around at the mall.");
  263.         System.out.println();
  264.         System.out.println("Your command words are:");
  265.         System.out.println ("" + parser.showCommands());
  266.  
  267.     }
  268.  
  269.     /**
  270.      * Try to go to one direction. If there is an exit, enter
  271.      * the new room, otherwise print an error message.
  272.      */
  273.     private void goRoom(Command command)
  274.     {
  275.         if(!command.hasSecondWord()) {
  276.             // if there is no second word, we don't know where to go...
  277.             System.out.println("Go where?");
  278.             return;
  279.         }
  280.  
  281.         String direction = command.getSecondWord();
  282.        
  283.         // Try to leave current room.
  284.        
  285.         Room nextRoom = player.getCurrentRoom().getExit(direction);
  286.        
  287.        
  288.    
  289.         if (nextRoom == null)
  290.            {
  291.                System.out.println("There is no door!");
  292.             }
  293.       else if ((nextRoom == rooms.get("office")) && (player.getCurrentRoom() == rooms.get("galleria")))
  294.             {
  295.                 System.out.println ("That door is locked");
  296.                 printLocationInfo();
  297.             }
  298.            
  299.        else   if (nextRoom == rooms.get("closet"))
  300.             {
  301.                 tPort();
  302.                 printLocationInfo();
  303.                 incrementMoves();
  304.                 moveCharacter();
  305.             }
  306.            
  307.             else
  308.             {
  309.             incrementMoves();
  310.             lastDirection = direction;
  311.             player.setLastRoom(player.getCurrentRoom());
  312.             player.setCurrentRoom(nextRoom);
  313.             System.out.println (" end of line");
  314.             printLocationInfo();
  315.             moveCharacter();
  316.            
  317.         }
  318.    
  319.     }
  320.    
  321.     private boolean defuse(Command command)
  322.         {
  323.            if(!command.hasSecondWord())
  324.             {
  325.                 System.out.println("defuse what?");
  326.                 return false;
  327.                
  328.             }
  329.            
  330.             if (player.checkInventory().equals ("a ticking bomb"))
  331.                 {
  332.                     System.out.println ("you have defused the bomb");
  333.                     System.out.println ("You have saved the mall! You are commended for bravery.");
  334.                     return true;
  335.                 }
  336.         else
  337.             {
  338.                 System.out.println("You cannot defuse that!");
  339.                return false;
  340.             }
  341.            
  342.         }
  343.  
  344.     private void look()
  345. {
  346.     System.out.println ("" + player.getCurrentRoom().getLongDescription());
  347. }
  348.  
  349.    
  350.     /**
  351.      * "Quit" was entered. Check the rest of the command to see
  352.      * whether we really quit the game. Return true, if this command
  353.      * quits the game, false otherwise.
  354.      */
  355.     private boolean quit(Command command)
  356.     {
  357.         if(command.hasSecondWord()) {
  358.             System.out.println("Quit what?");
  359.             return false;
  360.         }
  361.         else
  362.             return true;  // signal that we want to quit
  363.     }
  364.    
  365.     private void back()
  366.        {
  367.            String reverseLastDirection = "";
  368.            System.out.println("" + lastDirection);
  369.            
  370.            if(lastDirection.equals ("north"))
  371.            
  372.            {
  373.            reverseLastDirection = "south";
  374.            
  375.         }
  376.            if(lastDirection.equals("east"))
  377.            {
  378.                reverseLastDirection = "west";
  379.              
  380.             }
  381.             if (lastDirection.equals("south"))
  382.             {
  383.                 reverseLastDirection = "north";
  384.                
  385.             }
  386.             if (lastDirection.equals("west"))
  387.             {
  388.                 reverseLastDirection = "east";
  389.                
  390.             }
  391.             System.out.println("" + reverseLastDirection);
  392.             Command backCommand = new Command ("go", reverseLastDirection);
  393.             goRoom(backCommand);
  394.            }
  395.  
  396.  
  397.        private void take(Command command)
  398.     {
  399.      
  400.         if(!command.hasSecondWord()) {
  401.             // if there is no second word, we don't know what to pick up...
  402.             System.out.println("Take what?");
  403.             return;
  404.         }
  405.  
  406.         String itemName = command.getSecondWord();
  407.            
  408.              // take item
  409.           String result = player.takeItem(itemName);
  410.           System.out.println(result);
  411.            
  412.          
  413.          
  414.         }
  415.        
  416.         private void drop(Command command)
  417.         {
  418.             if(!command.hasSecondWord())
  419.                 {
  420.                     System.out.println ("Drop what?");
  421.                    
  422.                 }
  423.                 String itemName = command.getSecondWord();
  424.                 player.dropItem(itemName);
  425.                 System.out.println ("you have dropped " + itemName);
  426.                
  427.             }
  428.        
  429.            
  430.        private void inventory()
  431.        {
  432.            
  433.            System.out.println(player.checkInventory());
  434.         }
  435.        
  436.       private void pour(Command command)
  437.         {
  438.             if(!command.hasSecondWord())
  439.                 {
  440.                     System.out.println ("Pour what?");
  441.                    
  442.                 }
  443.                 else
  444.                 {
  445.                 if (command.getSecondWord().equals("beer") &&
  446.                 player.getCurrentRoom().itemTest("beertap") == true)
  447.                 {
  448.                     System.out.println ("you pull the beer tap");
  449.                     Item beer = new Item (1, "a beer", "beer");
  450.                     player.getCurrentRoom().setItems(beer);
  451.                 }
  452.             else
  453.             {
  454.                 System.out.println("You cannot pour that.");
  455.             }
  456.         }
  457.     }
  458.                
  459.       private void drink(Command command)
  460.       {
  461.           if(!command.hasSecondWord())
  462.             {
  463.                 System.out.println("Drink what?");
  464.             }
  465.            
  466.             else
  467.             {
  468.                 if (command.getSecondWord().equals("beer") &&
  469.                 player.getCurrentRoom().itemTest("beer") == true)
  470.                     {
  471.                         System.out.println("you drink a beer.");
  472.                         player.increaseStrength(1);
  473.                     }
  474.                 }
  475.             }
  476.        
  477.       private void incrementMoves()
  478.       {
  479.           numberOfMoves = numberOfMoves +1;
  480.         }
  481.      
  482.       private void tPort()
  483.         {
  484.             int i = tPort.nextInt(6);
  485.             System.out.println ("you have been teleported.");
  486.             System.out.println ("" + i);
  487.             if(i == 0)
  488.                 {
  489.                     System.out.println("you have been teleported to the outside.");
  490.                     player.setCurrentRoom(rooms.get("outside"));
  491.                 }
  492.             else if(i == 1)
  493.                 {
  494.                    System.out.println("you have been teleported to the pub.");
  495.                    player.setCurrentRoom(rooms.get("pub"));
  496.                 }
  497.            else if(i == 2)
  498.                 {
  499.                     System.out.println("you have been teleported to the theatre");
  500.                     player.setCurrentRoom(rooms.get("theatre"));
  501.                 }
  502.           else  if(i == 3)
  503.                 {
  504.                     System.out.println("you have been teleported to the office.");
  505.                     player.setCurrentRoom(rooms.get("office"));
  506.                 }
  507.             else if(i == 4)
  508.                 {
  509.                     System.out.println("you have been teleported to the store.");
  510.                     player.setCurrentRoom(rooms.get("store"));
  511.                 }
  512.           else  if(i == 5)
  513.                 {
  514.                     System.out.println("you have been teleported to the galleria.");
  515.                     player.setCurrentRoom(rooms.get("galleria"));
  516.                 }
  517.              
  518.             else
  519.                 {
  520.                     System.out.println("you have reached the else statement.");
  521.                     printLocationInfo();
  522.                     player.setCurrentRoom(rooms.get("closet"));
  523.              
  524.                 }
  525.             }
  526.      
  527.        private void rescue(Command command)
  528.        {
  529.              
  530.             if(!command.hasSecondWord())
  531.             {
  532.             // if there is no second word, we don't know where to go...
  533.                 System.out.println("Rescue who?");
  534.                 return;
  535.             }
  536.  
  537.             else
  538.             {
  539.        
  540.                 String characterID = command.getSecondWord();
  541.        
  542.                 if(characters.get(characterID).getCurrentRoom() == null)
  543.                 {
  544.                 System.out.println("They are not here!");
  545.                 }
  546.      
  547.        
  548.                 else
  549.                 {
  550.                     if(characters.get(characterID).getCurrentRoom() == player.getCurrentRoom())
  551.                     {
  552.                         characters.get(characterID).setCurrentRoom(rooms.get("outside"));
  553.                         System.out.println (("you have saved ") + characterID + (" !"));
  554.                     }
  555.        
  556.                 else
  557.                 {
  558.                     System.out.println("They are not here!");
  559.                 }
  560.             }
  561. }
  562.        
  563. }      
  564.        
  565.       //returns the values - character objects - from the hashmap of characters
  566.      
  567.       private Collection <Character> characterCollection()
  568.         {
  569.            Collection <Character> characterValues = characters.values();
  570.            return characterValues;
  571.             //return characters.values();
  572.         }
  573.        
  574.        private String characterInfo()
  575.        {
  576.            
  577.            String s = ("There is ");
  578.            for (Character character : characterCollection())
  579.            {
  580.              
  581.                
  582.                if (character.getCurrentRoom() == player.getCurrentRoom())
  583.                {
  584.                   s = s + character.getCharacterDescription() + ("\n");
  585.                 }
  586.             }  
  587.               return s;
  588.             }
  589.              
  590.      private void moveCharacter ()
  591.      {
  592.         // MobileCharacter mobileCharacter, Room room, String direction
  593.        MobileCharacter bob = (MobileCharacter) characters.get("partyGuy");
  594.        Room room = bob.getCurrentRoom();
  595.        String direction = bob.getDirection();
  596.        Room nextRoom = bob.getCurrentRoom().getExit(direction);
  597.        Set<String> exitSet = room.exitSet();
  598.         for(String s : exitSet){
  599.        
  600.        if(s.equals(direction))
  601.         {
  602.             bob.setCurrentRoom(nextRoom);
  603.             System.out.println("bob is in " + bob.getCurrentRoom().getDescription());
  604.            
  605.         }
  606.        else
  607.        {
  608.            bob.changeDirection();
  609.            System.out.println("bob's direction is " + bob.getDirection());
  610.         }
  611.    
  612.     }
  613. }
  614. }