Advertisement
mbah_bejo

Game

Nov 15th, 2020
802
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.54 KB | None | 0 0
  1. /**
  2.  *  Ini main Class "World of Zuul"
  3.  *  "World of Zuul" sangat simpel, game adventure berbasis text.  User
  4.  *  bisa berjalan di sekitar pemandangan. Itu saja. Ini benar-benar harus
  5.  *  diperpanjang agar lebih menarik!
  6.  *
  7.  *  Untuk bermain, buat instance dari class ini dan panggil method "play"
  8.  *
  9.  *  Main class ini membuat dan menginisialisasi all the others: Class ini
  10.  *  membuat semua rooms, membuat parser dan memulai game.
  11.  *  Ini juga mengevaluasi dan menjalankan perintah yang dikembalikan
  12.  *  dari parser.
  13.  *
  14.  * @author  thomasdwi.a
  15.  * @version 20201115
  16.  */
  17.  
  18. public class Game
  19. {
  20.     private Parser parser;
  21.     private Room currentRoom;
  22.        
  23.     /**
  24.      * Membuat game dan menginisialisasi map internal.
  25.      */
  26.     public Game()
  27.     {
  28.         createRooms();
  29.         parser = new Parser();
  30.     }
  31.  
  32.     /**
  33.      * Membuat semua room dan menghubungkan pintu keluar mereka.
  34.      */
  35.     private void createRooms()
  36.     {
  37.         Room outside, theater, pub, lab, office;
  38.      
  39.         // Membuat semua room
  40.         outside = new Room("outside the main entrance of the university");
  41.         theater = new Room("in a lecture theater");
  42.         pub = new Room("in the campus pub");
  43.         lab = new Room("in a computing lab");
  44.         office = new Room("in the computing admin office");
  45.        
  46.         // inisialisasi jalan keluar dari room
  47.         outside.setExits(null, theater, lab, pub);
  48.         theater.setExits(null, null, null, outside);
  49.         pub.setExits(null, outside, null, null);
  50.         lab.setExits(outside, office, null, null);
  51.         office.setExits(null, null, null, lab);
  52.  
  53.         currentRoom = outside;  // memulai game dari luar(outside)
  54.     }
  55.  
  56.     /**
  57.      *  Main play routine. Berulang hingga selesai permainan.
  58.      */
  59.     public void play()
  60.     {            
  61.         printWelcome();
  62.  
  63.         // masuk ke main command loop.  Disini kita  berulang kali membaca perintah dan
  64.         // menjalankannya sampai permainan selesai.
  65.                
  66.         boolean finished = false;
  67.         while (! finished) {
  68.             Command command = parser.getCommand();
  69.             finished = processCommand(command);
  70.         }
  71.         System.out.println("Thank you for playing.  Good bye.");
  72.     }
  73.  
  74.     /**
  75.      * Mencetak pesan selamat datang untuk pemain.
  76.      */
  77.     private void printWelcome()
  78.     {
  79.         System.out.println();
  80.         System.out.println("Welcome to the World of Zuul!");
  81.         System.out.println("World of Zuul is a new, incredibly boring adventure game.");
  82.         System.out.println("Type 'help' if you need help.");
  83.         System.out.println();
  84.         System.out.println("You are " + currentRoom.getDescription());
  85.         System.out.print("Exits: ");
  86.         if(currentRoom.northExit != null) {
  87.             System.out.print("north ");
  88.         }
  89.         if(currentRoom.eastExit != null) {
  90.             System.out.print("east ");
  91.         }
  92.         if(currentRoom.southExit != null) {
  93.             System.out.print("south ");
  94.         }
  95.         if(currentRoom.westExit != null) {
  96.             System.out.print("west ");
  97.         }
  98.         System.out.println();
  99.     }
  100.  
  101.     /**
  102.      * Diberikan perintah, proses (yaitu: menjalankan) perintah.
  103.      * @param command sebagai Perintah yang akan diproses.
  104.      * @return true Jika perintah menyelesaikan game, false jika tidak.
  105.      */
  106.     private boolean processCommand(Command command)
  107.     {
  108.         boolean wantToQuit = false;
  109.  
  110.         if(command.isUnknown()) {
  111.             System.out.println("I don't know what you mean...");
  112.             return false;
  113.         }
  114.  
  115.         String commandWord = command.getCommandWord();
  116.         if (commandWord.equals("help")) {
  117.             printHelp();
  118.         }
  119.         else if (commandWord.equals("go")) {
  120.             goRoom(command);
  121.         }
  122.         else if (commandWord.equals("quit")) {
  123.             wantToQuit = quit(command);
  124.         }
  125.  
  126.         return wantToQuit;
  127.     }
  128.  
  129.     // Implementasi dari perintah pemain:
  130.  
  131.     /**
  132.      * mencetak informasi help.
  133.      * Di sini kami mencetak beberapa pesan bodoh dan tersembunyi serta
  134.      * daftar kata command
  135.      */
  136.     private void printHelp()
  137.     {
  138.         System.out.println("You are lost. You are alone. You wander");
  139.         System.out.println("around at the university.");
  140.         System.out.println();
  141.         System.out.println("Your command words are:");
  142.         System.out.println("   go quit help");
  143.     }
  144.  
  145.     /**
  146.      * Cobalah untuk pergi ke satu arah. Jika ada jalan keluar,
  147.      * masuk ke room baru, jika tidak cetak pesan error.
  148.      */
  149.     private void goRoom(Command command)
  150.     {
  151.         if(!command.hasSecondWord()) {
  152.             // jika tidak ada second word, kita tidak kemana-mana...
  153.             System.out.println("Go where?");
  154.             return;
  155.         }
  156.  
  157.         String direction = command.getSecondWord();
  158.  
  159.         // Coba meninggalkan room sekarang.
  160.         Room nextRoom = null;
  161.         if(direction.equals("north")) {
  162.             nextRoom = currentRoom.northExit;
  163.         }
  164.         if(direction.equals("east")) {
  165.             nextRoom = currentRoom.eastExit;
  166.         }
  167.         if(direction.equals("south")) {
  168.             nextRoom = currentRoom.southExit;
  169.         }
  170.         if(direction.equals("west")) {
  171.             nextRoom = currentRoom.westExit;
  172.         }
  173.  
  174.         if (nextRoom == null) {
  175.             System.out.println("There is no door!");
  176.         }
  177.         else {
  178.             currentRoom = nextRoom;
  179.             System.out.println("You are " + currentRoom.getDescription());
  180.             System.out.print("Exits: ");
  181.             if(currentRoom.northExit != null) {
  182.                 System.out.print("north ");
  183.             }
  184.             if(currentRoom.eastExit != null) {
  185.                 System.out.print("east ");
  186.             }
  187.             if(currentRoom.southExit != null) {
  188.                 System.out.print("south ");
  189.             }
  190.             if(currentRoom.westExit != null) {
  191.                 System.out.print("west ");
  192.             }
  193.             System.out.println();
  194.         }
  195.     }
  196.  
  197.     /**
  198.      * "Quit" dimasukkan. Cek sisa command untuk melihat
  199.      * apakah kita benar-benar keluar dari permainan.
  200.      * @return true, jika perintah ini untuk keluar dari game, false jika tidak.
  201.      */
  202.     private boolean quit(Command command)
  203.     {
  204.         if(command.hasSecondWord()) {
  205.             System.out.println("Quit what?");
  206.             return false;
  207.         }
  208.         else {
  209.             return true;  // sinyal bahwa kita mau quit
  210.         }
  211.     }
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement