Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.29 KB | None | 0 0
  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 Kölling and David J. Barnes
  15.  * @version 2016.02.29
  16.  */
  17.  
  18. public class Game
  19. {
  20.     private Parser parser;
  21.     private Room currentRoom;
  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.      * Exercise 8.4
  35.      */
  36.     private void createRooms()
  37.     {
  38.         Room start, trap1, trap2, hallway, exit;
  39.      
  40.         // create the rooms
  41.         start = new Room("at the beginning of the maze");
  42.         trap1 = new Room("on a fake floor with a spike pit underneath (trap)");
  43.         trap2 = new Room("walking through a long endless hallway that seemingly goes nowhere (trap)");
  44.         hallway = new Room("sneaking through a hallway that may lead closer to the exit");
  45.         exit = new Room("at the end of the maze");
  46.        
  47.        
  48.         // initialise room exits
  49.         start.setExits(null, trap1, hallway, trap2);
  50.         trap1.setExits(null, null, null, start);
  51.         trap2.setExits(null, start, null, null);
  52.         hallway.setExits(start, exit, null, null);
  53.         exit.setExits(null, null, null, hallway);
  54.  
  55.         currentRoom = start;  // start game outside
  56.     }
  57.  
  58.     /**
  59.      *  Main play routine.  Loops until end of play.
  60.      */
  61.     public void play()
  62.     {            
  63.         printWelcome();
  64.  
  65.         // Enter the main command loop.  Here we repeatedly read commands and
  66.         // execute them until the game is over.
  67.                
  68.         boolean finished = false;
  69.         while (! finished) {
  70.             Command command = parser.getCommand();
  71.             finished = processCommand(command);
  72.         }
  73.         System.out.println("Thank you for playing.  Good bye.");
  74.     }
  75.  
  76.     /**
  77.      * Print out the opening message for the player.
  78.      */
  79.     private void printWelcome()
  80.     {
  81.         System.out.println();
  82.         System.out.println("Welcome to the World of Zuul!");
  83.         System.out.println("World of Zuul is a new, incredibly boring adventure game.");
  84.         System.out.println("Type 'help' if you need help.");
  85.         System.out.println();
  86.         printLocationInfo();
  87.     }
  88.  
  89.     /**
  90.      * Given a command, process (that is: execute) the command.
  91.      * @param command The command to be processed.
  92.      * @return true If the command ends the game, false otherwise.
  93.      */
  94.     private boolean processCommand(Command command)
  95.     {
  96.         boolean wantToQuit = false;
  97.  
  98.         if(command.isUnknown()) {
  99.             System.out.println("I don't know what you mean...");
  100.             return false;
  101.         }
  102.  
  103.         String commandWord = command.getCommandWord();
  104.         if (commandWord.equals("help")) {
  105.             printHelp();
  106.         }
  107.         else if (commandWord.equals("go")) {
  108.             goRoom(command);
  109.         }
  110.         else if (commandWord.equals("quit")) {
  111.             wantToQuit = quit(command);
  112.         }
  113.  
  114.         return wantToQuit;
  115.     }
  116.  
  117.     // implementations of user commands:
  118.  
  119.     /**
  120.      * Print out some help information.
  121.      * Here we print some stupid, cryptic message and a list of the
  122.      * command words.
  123.      */
  124.     private void printHelp()
  125.     {
  126.         System.out.println("You are lost. You are alone. You wander");
  127.         System.out.println("around at the university.");
  128.         System.out.println();
  129.         System.out.println("Your command words are:");
  130.         System.out.println("   go quit help");
  131.     }
  132.  
  133.     /**
  134.      * Try to go in one direction. If there is an exit, enter
  135.      * the new room, otherwise print an error message.
  136.      */
  137.     private void goRoom(Command command)
  138.     {
  139.         if(!command.hasSecondWord()) {
  140.             // if there is no second word, we don't know where to go...
  141.             System.out.println("Go where?");
  142.             return;
  143.         }
  144.  
  145.         String direction = command.getSecondWord();
  146.  
  147.         // Try to leave current room.
  148.         Room nextRoom = null;
  149.         if(direction.equals("north")) {
  150.             nextRoom = currentRoom.northExit;
  151.         }
  152.         if(direction.equals("east")) {
  153.             nextRoom = currentRoom.eastExit;
  154.         }
  155.         if(direction.equals("south")) {
  156.             nextRoom = currentRoom.southExit;
  157.         }
  158.         if(direction.equals("west")) {
  159.             nextRoom = currentRoom.westExit;
  160.         }
  161.  
  162.         if (nextRoom == null) {
  163.             System.out.println("There is no door!");
  164.         }
  165.         else {
  166.             currentRoom = nextRoom;
  167.             printLocationInfo();
  168.         }
  169.     }
  170.    
  171.     /**
  172.      * Exercise 8.5
  173.      * A better method to print the current location information
  174.      */
  175.     private void printLocationInfo()
  176.     {
  177.         System.out.println("You are " + currentRoom.getDescription());
  178.         System.out.println("Exits: ");
  179.         if(currentRoom.northExit != null) {
  180.             System.out.print("north ");
  181.         }
  182.         if(currentRoom.eastExit != null) {
  183.             System.out.print("east ");
  184.         }
  185.         if(currentRoom.southExit != null) {
  186.             System.out.print("south ");
  187.         }
  188.         if(currentRoom.westExit != null) {
  189.             System.out.print("west ");
  190.         }
  191.         System.out.println();
  192.     }
  193.  
  194.     /**
  195.      * "Quit" was entered. Check the rest of the command to see
  196.      * whether we really quit the game.
  197.      * @return true, if this command quits the game, false otherwise.
  198.      */
  199.     private boolean quit(Command command)
  200.     {
  201.         if(command.hasSecondWord()) {
  202.             System.out.println("Quit what?");
  203.             return false;
  204.         }
  205.         else {
  206.             return true;  // signal that we want to quit
  207.         }
  208.     }
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement