bagusistighfar86

Untitled

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