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