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