Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.83 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 = currentRoom.getExit(direction);
  149.  
  150.         if (nextRoom == null) {
  151.             System.out.println("There is no door!");
  152.         }
  153.         else {
  154.             currentRoom = nextRoom;
  155.             printLocationInfo();
  156.         }
  157.     }
  158.    
  159.     /**
  160.      * Exercise 8.7
  161.      * A better method to print the current location information
  162.      */
  163.     private void printLocationInfo()
  164.     {
  165.         System.out.println("You are " + currentRoom.getDescription());
  166.         System.out.println("Exits: ");
  167.         if(currentRoom.getExit("north") != null) {
  168.             System.out.print("north ");
  169.         }
  170.         if(currentRoom.getExit("east") != null) {
  171.             System.out.print("east ");
  172.         }
  173.         if(currentRoom.getExit("south") != null) {
  174.             System.out.print("south ");
  175.         }
  176.         if(currentRoom.getExit("west") != null) {
  177.             System.out.print("west ");
  178.         }
  179.         System.out.println();
  180.     }
  181.  
  182.     /**
  183.      * "Quit" was entered. Check the rest of the command to see
  184.      * whether we really quit the game.
  185.      * @return true, if this command quits the game, false otherwise.
  186.      */
  187.     private boolean quit(Command command)
  188.     {
  189.         if(command.hasSecondWord()) {
  190.             System.out.println("Quit what?");
  191.             return false;
  192.         }
  193.         else {
  194.             return true;  // signal that we want to quit
  195.         }
  196.     }
  197. }
  198.  
  199. /**
  200.  * Class Room - a room in an adventure game.
  201.  *
  202.  * This class is part of the "World of Zuul" application.
  203.  * "World of Zuul" is a very simple, text based adventure game.  
  204.  *
  205.  * A "Room" represents one location in the scenery of the game.  It is
  206.  * connected to other rooms via exits.  The exits are labelled north,
  207.  * east, south, west.  For each direction, the room stores a reference
  208.  * to the neighboring room, or null if there is no exit in that direction.
  209.  *
  210.  * @author  Michael Kölling and David J. Barnes
  211.  * @version 2016.02.29
  212.  */
  213. public class Room
  214. {
  215.     private String description;
  216.     private Room northExit;
  217.     private Room southExit;
  218.     private Room eastExit;
  219.     private Room westExit;
  220.  
  221.     /**
  222.      * Create a room described "description". Initially, it has
  223.      * no exits. "description" is something like "a kitchen" or
  224.      * "an open court yard".
  225.      * @param description The room's description.
  226.      */
  227.     public Room(String description)
  228.     {
  229.         this.description = description;
  230.     }
  231.  
  232.     /**
  233.      * Define the exits of this room.  Every direction either leads
  234.      * to another room or is null (no exit there).
  235.      * @param north The north exit.
  236.      * @param east The east east.
  237.      * @param south The south exit.
  238.      * @param west The west exit.
  239.      */
  240.     public void setExits(Room north, Room east, Room south, Room west)
  241.     {
  242.         if(north != null) {
  243.             northExit = north;
  244.         }
  245.         if(east != null) {
  246.             eastExit = east;
  247.         }
  248.         if(south != null) {
  249.             southExit = south;
  250.         }
  251.         if(west != null) {
  252.             westExit = west;
  253.         }
  254.     }
  255.  
  256.     /**
  257.      * @return The description of the room.
  258.      */
  259.     public String getDescription()
  260.     {
  261.         return description;
  262.     }
  263.    
  264.     /**
  265.      * Exercise 8.6
  266.      */
  267.     public Room getExit(String direction)
  268.     {
  269.         if(direction.equals("north")) {
  270.             return northExit;
  271.         }
  272.         if(direction.equals("east")) {
  273.             return eastExit;
  274.         }
  275.         if(direction.equals("south")) {
  276.             return southExit;
  277.         }
  278.         if(direction.equals("west")) {
  279.             return westExit;
  280.         }
  281.         return null;
  282.     }
  283.    
  284.     /**
  285.      * Exercise 8.7
  286.      * Return a description of the room's exits,
  287.      * for example, "Exits: north west".
  288.      * @return A description of the available exits.
  289.      */
  290.     public String getExitString()
  291.     {
  292.         String exitString = "Exits: ";
  293.         if(northExit != null) {
  294.             exitString += "north";
  295.         }
  296.         if(eastExit != null) {
  297.             exitString += "east";
  298.         }
  299.         if(southExit != null) {
  300.             exitString += "south";
  301.         }
  302.         if(westExit != null) {
  303.             exitString += "west";
  304.         }
  305.         return exitString;
  306.     }
  307. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement