Advertisement
Guest User

Untitled

a guest
Nov 17th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.10 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.      * Create all the rooms and link their exits together.
  33.      */
  34.     private void createRooms()
  35.     {
  36.         Room outside, theater, pub, lab, office, stairsFloor1, corridor1, stairsFloor2, library, canteen, gym, classroom, examHall;
  37.         Item textbooks, studentID;
  38.         // create the rooms
  39.         outside = new Room("outside the main entrance of the university");
  40.         theater = new Room("in a lecture theater");
  41.         pub = new Room("in the campus pub");
  42.         lab = new Room("in a computing lab");
  43.         office = new Room("in the computing admin office, you can pick up your student ID from here");
  44.         stairsFloor1 = new Room("by some stairs that lead up to floor 1");
  45.         corridor1 = new Room("in the corridor");
  46.         classroom = new Room("in a classroom");
  47.         library = new Room("in the library, there may be some useful textbooks");
  48.         canteen = new Room("in the canteen");
  49.         gym = new Room("in the gym");
  50.         stairsFloor2 = new Room("by some stairs that lead up to the exam hall");
  51.         examHall = new Room("in the exam hall");
  52.        
  53.         // initialise room exits
  54.         outside.setExit("east", theater);
  55.         outside.setExit("south", lab);
  56.         outside.setExit("west", pub);
  57.  
  58.         theater.setExit("west", outside);
  59.  
  60.         pub.setExit("east", outside);
  61.         pub.setExit("south", stairsFloor1);
  62.  
  63.         lab.setExit("north", outside);
  64.         lab.setExit("east", office);
  65.  
  66.         office.setExit("west", lab);
  67.        
  68.         stairsFloor1.setExit("up", corridor1);
  69.         stairsFloor1.setExit("north", pub);
  70.         corridor1.setExit("down" , stairsFloor1);
  71.         corridor1.setExit("east" , classroom);
  72.        
  73.         classroom.setExit("north", library);
  74.         classroom.setExit("east", canteen);
  75.         classroom.setExit("west", corridor1);
  76.        
  77.         canteen.setExit("north", gym);
  78.         canteen.setExit("west", classroom);
  79.        
  80.         gym.setExit("south", canteen);
  81.         gym.setExit("west", library);
  82.        
  83.         library.setExit("east", gym);
  84.         library.setExit("south", classroom);
  85.         library.setExit("west", stairsFloor2);
  86.        
  87.         stairsFloor2.setExit("up", examHall);
  88.         stairsFloor2.setExit("east", library);
  89.        
  90.         examHall.setExit("down", stairsFloor2);
  91.        
  92.        
  93.         textbooks = new Item("There are some textbooks");
  94.         studentID = new Item("This is your studentID");
  95.        
  96.         textbooks.setItem(lab, textbooks);
  97.         studentID.setItem(office, studentID);
  98.        
  99.         currentRoom = outside;  // start game outside
  100.     }
  101.    
  102.     /**
  103.      *  Main play routine.  Loops until end of play.
  104.      */
  105.     public void play()
  106.     {            
  107.         printWelcome();
  108.  
  109.         // Enter the main command loop.  Here we repeatedly read commands and
  110.         // execute them until the game is over.
  111.                
  112.         boolean finished = false;
  113.         while (! finished) {
  114.             Command command = parser.getCommand();
  115.             finished = processCommand(command);
  116.         }
  117.         System.out.println("Thank you for playing.  Good bye.");
  118.     }
  119.  
  120.     /**
  121.      * Print out the opening message for the player.
  122.      */
  123.     private void printWelcome()
  124.     {
  125.         System.out.println();
  126.         System.out.println("Welcome to the World of Zuul!");
  127.         System.out.println("World of Zuul is a new, incredibly boring adventure game.");
  128.         System.out.println("Type 'help' if you need help.");
  129.         System.out.println();
  130.         System.out.println(currentRoom.getLongDescription());
  131.     }
  132.  
  133.     /**
  134.      * Given a command, process (that is: execute) the command.
  135.      * @param command The command to be processed.
  136.      * @return true If the command ends the game, false otherwise.
  137.      */
  138.     private boolean processCommand(Command command)
  139.     {
  140.         boolean wantToQuit = false;
  141.  
  142.         if(command.isUnknown()) {
  143.             System.out.println("I don't know what you mean...");
  144.             return false;
  145.         }
  146.  
  147.         String commandWord = command.getCommandWord();
  148.         if (commandWord.equals("help")) {
  149.             printHelp();
  150.         }
  151.         else if (commandWord.equals("go")) {
  152.             goRoom(command);
  153.         }
  154.         else if (commandWord.equals("quit")) {
  155.             wantToQuit = quit(command);
  156.         }
  157.         //else if (commandWord.equals("carry")){
  158.            
  159.         // else command not recognised.
  160.         return wantToQuit;
  161.     }
  162.  
  163.     // implementations of user commands:
  164.  
  165.     /**
  166.      * Print out some help information.
  167.      * Here we print some stupid, cryptic message and a list of the
  168.      * command words.
  169.      */
  170.     private void printHelp()
  171.     {
  172.         System.out.println("You are lost. You are alone. You wander");
  173.         System.out.println("around at the university.");
  174.         System.out.println();
  175.         System.out.println("Your command words are:");
  176.         parser.showCommands();
  177.     }
  178.  
  179.     /**
  180.      * Try to in to one direction. If there is an exit, enter the new
  181.      * room, otherwise print an error message.
  182.      */
  183.     private void goRoom(Command command)
  184.     {
  185.         if(!command.hasSecondWord()) {
  186.             // if there is no second word, we don't know where to go...
  187.             System.out.println("Go where?");
  188.             return;
  189.         }
  190.  
  191.         String direction = command.getSecondWord();
  192.  
  193.         // Try to leave current room.
  194.         Room nextRoom = currentRoom.getExit(direction);
  195.  
  196.         if (nextRoom == null) {
  197.             System.out.println("There is no door!");
  198.         }
  199.         else {
  200.             currentRoom = nextRoom;
  201.             System.out.println(currentRoom.getLongDescription());
  202.            
  203.            
  204.         }
  205.     }
  206.     /**
  207.      * "Quit" was entered. Check the rest of the command to see
  208.      * whether we really quit the game.
  209.      * @return true, if this command quits the game, false otherwise.
  210.      */
  211.     private boolean quit(Command command)
  212.     {
  213.         if(command.hasSecondWord()) {
  214.             System.out.println("Quit what?");
  215.             return false;
  216.         }
  217.         else {
  218.             return true;  // signal that we want to quit
  219.         }
  220.     }
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement