trizehn

Game

Nov 18th, 2020
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.90 KB | None | 0 0
  1. /**
  2.  * Write a description of class Game here.
  3.  *
  4.  * @author Daffa Tristan Firdaus
  5.  * @version 18 November 2020
  6.  */
  7.  
  8. public class Game
  9. {
  10.     private Parser parser;
  11.     private Room currentRoom;
  12.        
  13.     /**
  14.      * Create the game and initialise its internal map
  15.      */
  16.     public Game()
  17.     {
  18.         createRooms();
  19.         parser = new Parser();
  20.     }
  21.  
  22.     /**
  23.      * Create all the rooms and link their exits together.
  24.      */
  25.     private void createRooms()
  26.     {
  27.         Room outside, theater, pub, lab, office;
  28.      
  29.         // Create the rooms
  30.         outside = new Room("outside the main entrance of the university");
  31.         theater = new Room("in a lecture theater");
  32.         pub = new Room("in the campus pub");
  33.         lab = new Room("in a computing lab");
  34.         office = new Room("in the computing admin office");
  35.        
  36.         // initialise room exits
  37.         outside.setExits(null, theater, lab, pub);
  38.         theater.setExits(null, null, null, outside);
  39.         pub.setExits(null, outside, null, null);
  40.         lab.setExits(outside, office, null, null);
  41.         office.setExits(null, null, null, lab);
  42.  
  43.         currentRoom = outside;  // start game outside
  44.     }
  45.  
  46.     /**
  47.      *  Main play routine. Berulang hingga selesai permainan.
  48.      */
  49.     public void play()
  50.     {            
  51.         printWelcome();
  52.  
  53.         // enter the main command loop. Here we repeatedlu read commands and
  54.         // execute the until the game is over.
  55.                
  56.         boolean finished = false;
  57.         while (! finished) {
  58.             Command command = parser.getCommand();
  59.             finished = processCommand(command);
  60.         }
  61.         System.out.println("Thank you for playing.  Good bye.");
  62.     }
  63.  
  64.     /**
  65.      * Print out the opening message for the player.
  66.      */
  67.     private void printWelcome()
  68.     {
  69.         System.out.println();
  70.         System.out.println("Welcome to the World of Zuul!");
  71.         System.out.println("World of Zuul is a new, incredibly boring adventure game.");
  72.         System.out.println("Type 'help' if you need help.");
  73.         System.out.println();
  74.         System.out.println("You are " + currentRoom.getDescription());
  75.         System.out.print("Exits: ");
  76.         if(currentRoom.northExit != null) {
  77.             System.out.print("north ");
  78.         }
  79.         if(currentRoom.eastExit != null) {
  80.             System.out.print("east ");
  81.         }
  82.         if(currentRoom.southExit != null) {
  83.             System.out.print("south ");
  84.         }
  85.         if(currentRoom.westExit != null) {
  86.             System.out.print("west ");
  87.         }
  88.         System.out.println();
  89.     }
  90.  
  91.     /**
  92.      * Given a command, process (that is : execute) the command.
  93.      * If this command ends the game, true is returned, otherwise false is
  94.      * returned.
  95.      * @param command
  96.      * @return bool
  97.      */
  98.     private boolean processCommand(Command command)
  99.     {
  100.         boolean wantToQuit = false;
  101.  
  102.         if(command.isUnknown()) {
  103.             System.out.println("I don't understand...");
  104.             return false;
  105.         }
  106.  
  107.         String commandWord = command.getCommandWord();
  108.         if (commandWord.equals("help"))
  109.             printHelp();
  110.         else if (commandWord.equals("go"))
  111.             goRoom(command);
  112.        
  113.         else if (commandWord.equals("quit"))
  114.             wantToQuit = quit(command);
  115.        
  116.         return wantToQuit;
  117.     }
  118.  
  119.     // Implementasi of user commands:
  120.  
  121.     /**
  122.      * Print out some help information.
  123.      * Here we print some stupid, cryptic message and ad list of the
  124.      * command words
  125.      */
  126.     private void printHelp()
  127.     {
  128.         System.out.println("You are lost. You are alone. You wander");
  129.         System.out.println("around at the university.");
  130.         System.out.println();
  131.         System.out.println("Your command words are:");
  132.         System.out.println("   go quit help");
  133.     }
  134.  
  135.     /**
  136.      * Try to go to one direction. If there is an exit, enter
  137.      * the new room, otherwise print an error message.
  138.      */
  139.     private void goRoom(Command command)
  140.     {
  141.         if(!command.hasSecondWord()) {
  142.             // if there is no second word, we don't know where to go ...
  143.             System.out.println("Go where?");
  144.             return;
  145.         }
  146.  
  147.         String direction = command.getSecondWord();
  148.  
  149.         // Try to leave current room
  150.         Room nextRoom = null;
  151.         if(direction.equals("north"))
  152.             nextRoom = currentRoom.northExit;
  153.         if(direction.equals("east"))
  154.             nextRoom = currentRoom.eastExit;
  155.         if(direction.equals("south"))
  156.             nextRoom = currentRoom.southExit;
  157.         if(direction.equals("west"))
  158.             nextRoom = currentRoom.westExit;
  159.  
  160.         if (nextRoom == null) {
  161.             System.out.println("There is no door!");
  162.         }
  163.         else {
  164.             currentRoom = nextRoom;
  165.             System.out.println("You are " + currentRoom.getDescription());
  166.             System.out.print("Exits: ");
  167.             if(currentRoom.northExit != null)
  168.                 System.out.print("north ");
  169.             if(currentRoom.eastExit != null)
  170.                 System.out.print("east ");
  171.             if(currentRoom.southExit != null)
  172.                 System.out.print("south ");
  173.             if(currentRoom.westExit != null)
  174.                 System.out.print("west ");
  175.             System.out.println();
  176.         }
  177.     }
  178.  
  179.     /**
  180.      * "Quit" was entered. Check the rest of the command to see
  181.      * whether we really quit the game. Return true, if this command
  182.      * quits the game, false otherwise.
  183.      *
  184.      * @return bool
  185.      */
  186.     private boolean quit(Command command)
  187.     {
  188.         if(command.hasSecondWord()) {
  189.             System.out.println("Quit what?");
  190.             return false;
  191.         }
  192.         else {
  193.             return true;  // signal that we want to quit
  194.         }
  195.     }
  196. }
Advertisement
Add Comment
Please, Sign In to add comment