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

get exit string game class

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