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 9th, 2010  |  syntax: None  |  size: 6.46 KB  |  hits: 10  |  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.    
  24.        
  25.     /**
  26.      * Create the game and initialise its internal map.
  27.      */
  28.     public Game()
  29.     {
  30.         createOrgans();
  31.         parser = new Parser();
  32.         energy = 3;
  33.     }
  34.  
  35.     /**
  36.      * Create all the rooms and link their exits together.
  37.      */
  38.     private void createOrgans()
  39.     {
  40.         Organ circulatorySystem, respitorySystem, upperGI, lowerGI, sinuses;
  41.      
  42.         // create the rooms
  43.         circulatorySystem = new Organ("in the circulatory system");
  44.         respitorySystem = new Organ("int the respitory system");
  45.         upperGI = new Organ("in the upper GI");
  46.         lowerGI = new Organ("in the lower GI");
  47.         sinuses = new Organ("in the sinuses");
  48.         // set organ exits
  49.         circulatorySystem.setExit("anterior", respitorySystem);
  50.         circulatorySystem.setExit ("posterior", upperGI);
  51.         respitorySystem.setExit ("anterior", sinuses);
  52.         respitorySystem.setExit ("posterior", circulatorySystem );
  53.         upperGI.setExit ("anterior", circulatorySystem );
  54.         upperGI.setExit ("posterior", lowerGI );
  55.         lowerGI.setExit ("anterior", upperGI);
  56.         sinuses.setExit ("posterior", respitorySystem);
  57.         currentOrgan = circulatorySystem;  // start game outside
  58.     }
  59.  
  60.     /**
  61.      *  Main play routine.  Loops until end of play.
  62.      */
  63.     public void play()
  64.     {            
  65.         printWelcome();
  66.  
  67.         // Enter the main command loop.  Here we repeatedly read commands and
  68.         // execute them until the game is over.
  69.                
  70.         boolean finished = false;
  71.         while (! finished) {
  72.             Command command = parser.getCommand();
  73.             finished = processCommand(command);
  74.         }
  75.         System.out.println("Thank you for playing.  Good bye.");
  76.     }
  77.  
  78.     /**
  79.      * Print out the opening message for the player.
  80.      */
  81.     private void printWelcome()
  82.     {
  83.         System.out.println();
  84.         System.out.println("Welcome to the World of Zuul!");
  85.         System.out.println("World of Zuul is a new, incredibly boring adventure game.");
  86.         System.out.println("Type 'help' if you need help.");
  87.         System.out.println();
  88.         printLocationInfo();
  89.     }
  90.  
  91.     /**
  92.      * Given a command, process (that is: execute) the command.
  93.      * If this command ends the game, true is returned, otherwise false is
  94.      * returned.
  95.      */
  96.     private boolean processCommand(Command command)
  97.     {
  98.         boolean wantToQuit = false;
  99.  
  100.         if(command.isUnknown()) {
  101.             System.out.println("I don't know what you mean...");
  102.             return false;
  103.         }
  104.  
  105.         String commandWord = command.getCommandWord();
  106.         if (commandWord.equals("help"))
  107.             printHelp();
  108.         else if (commandWord.equals("go"))
  109.             goOrgan(command);
  110.          else if (commandWord.equals("look"))
  111.             look();
  112.           else if (commandWord.equals("eat"))
  113.             eat();
  114.           else if (commandWord.equals ("energy"))
  115.             energyLevel();
  116.             else if (commandWord.equals("quit"))
  117.          
  118.             wantToQuit = quit(command);
  119.  
  120.         return wantToQuit;
  121.     }
  122.  
  123.     // a command to look around the room
  124.     private void look()
  125.         {
  126.             System.out.println(currentOrgan.getLongDescription());
  127.         }
  128.     // implementations of user commands:
  129.  
  130.     /**
  131.      * Print out some help information.
  132.      * Here we print some stupid, cryptic message and a list of the
  133.      * command words.
  134.      */
  135.     private void printHelp()
  136.     {
  137.         System.out.println("You are lost. You are alone. You wander");
  138.         System.out.println("around at the university.");
  139.         System.out.println();
  140.         System.out.println("Your command words are:");
  141.         System.out.println("   go quit help");
  142.     }
  143.  
  144.     // eat glucose to power up
  145.    
  146.     private void eat()
  147.         {
  148.             if (currentOrgan.glucoseLevel > 0)
  149.                 {
  150.                     energy += currentOrgan.glucoseLevel;
  151.                     System.out.println ("you have eaten " + currentOrgan.glucoseLevel);
  152.                 }
  153.             else
  154.                 {
  155.                     System.out.println("there is nothing to eat.");
  156.                 }
  157.         }
  158.        
  159.         private void energyLevel()
  160.             {
  161.                 System.out.println ("energy level " + energy);
  162.             }
  163.     /**
  164.      * Try to go to one direction. If there is an exit, enter
  165.      * the new room, otherwise print an error message.
  166.      */
  167.     private void goOrgan(Command command)
  168.     {
  169.         if(!command.hasSecondWord())
  170.         {
  171.             // if there is no second word, we don't know where to go...
  172.             System.out.println("Go where?");
  173.             return;
  174.         }
  175.  
  176.         String direction = command.getSecondWord();
  177.  
  178.         // Try to leave current room.
  179.        
  180.         Organ nextOrgan = currentOrgan.getExit (direction);
  181.          
  182.         if (nextOrgan == null)
  183.             System.out.println("There is no door!");
  184.         else
  185.         {
  186.             currentOrgan = nextOrgan;
  187.              printLocationInfo();
  188.            
  189.         }
  190.     }
  191.  
  192.     /**
  193.      * "Quit" was entered. Check the rest of the command to see
  194.      * whether we really quit the game. Return true, if this command
  195.      * quits the game, false otherwise.
  196.      */
  197.     private boolean quit(Command command)
  198.     {
  199.         if(command.hasSecondWord()) {
  200.             System.out.println("Quit what?");
  201.             return false;
  202.         }
  203.         else
  204.             return true;  // signal that we want to quit
  205.     }
  206.    
  207.     private void printLocationInfo()
  208.     {
  209.         System.out.println (currentOrgan.getLongDescription());
  210.        
  211.     }
  212.    
  213. }