Dwinanda

World of Zuul (GAME)

Nov 23rd, 2020 (edited)
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.75 KB | None | 0 0
  1. /**
  2.  *  Merupakan Main Class dari World of zuul
  3.  *  versi sederhana untuk sebuah world of zuul
  4.  *  pemain bisa bergerak dengan banyak kondisi sehingga menyenangkan
  5.  *
  6.  *  untuk memainkan nya dibutuhkan instansi dari class ini yaitu dengan
  7.  *  "call" method
  8.  *
  9.  *  class ini menginisialisasikan class yang lain
  10.  *  semua rooms,membuat class parsers dan mulai game.  
  11.  *  class ini mengevaluasi dan mengeksekusi perintah dari class parser
  12.  *
  13.  * @author Dwinanda Bagoes Ansori
  14.  * @version 24 November 2020
  15.  */
  16. public class Game
  17. {
  18.     private Parser parser;
  19.     private Room currentRoom;
  20.    
  21.     // membuat game dan petanya
  22.     public Game()
  23.     {
  24.         createRooms();
  25.         parser = new Parser();
  26.     }
  27.    
  28.     //membuat semua ruangan dan menhubungkan ruangan-ruangan tersebut.
  29.     private void createRooms()
  30.     {
  31.         Room outside, theater, pub, lab, office;
  32.        
  33.         outside = new Room ("outside the main entrance of the university");
  34.         theater = new Room ("in a lecture theater");
  35.         pub = new Room ("in the campus pub");
  36.         lab = new Room ("in a computing lab");
  37.         office = new Room ("in the computing admin office");
  38.        
  39.         // inisialisasi arah keluar masing masing room
  40.         outside.setExits(null, theater, lab, pub);
  41.         theater.setExits(null, null, null, outside);
  42.         pub.setExits(null, outside, null, null);
  43.         lab.setExits(outside, office, null, null);
  44.         office.setExits(null, null, null, lab);
  45.        
  46.         currentRoom = outside;  //mulai game nya dari outside
  47.     }
  48.    
  49.     /**
  50.      *  dilakukan gamenya sampai selesai
  51.      */
  52.     public void play()
  53.     {
  54.         printWelcome();
  55.        
  56.         // masuk ke loop perintah
  57.         // eksekusi sampai game selesai
  58.         boolean finished = false;
  59.         while (!finished)
  60.         {
  61.             Command command = parser.getCommand();
  62.             finished = processCommand(command);
  63.         }
  64.         System.out.println("Thank you for playing. Good Bye");
  65.     }
  66.    
  67.     /**
  68.      * Pesan awalan.
  69.      */
  70.     private void printWelcome()
  71.     {
  72.         System.out.println();
  73.         System.out.println("Welcome to Adventure");
  74.         System.out.println("Adventure is a new, incredibly boring adventure game");
  75.         System.out.println("Type 'help' if you need help.");
  76.         System.out.println();
  77.         System.out.println("You are " + currentRoom.getDescription());
  78.         System.out.println("Exits: ");
  79.         if (currentRoom.northExit != null)
  80.             System.out.println("north ");
  81.         if (currentRoom.eastExit != null)
  82.             System.out.println("east ");
  83.         if (currentRoom.southExit != null)
  84.             System.out.println("south ");
  85.         if (currentRoom.westExit != null)
  86.             System.out.println("west ");
  87.     }
  88.    
  89.     /**
  90.      * perintah akan dieksekusi
  91.      * @param command akan diproses.
  92.      * @return true jika menyeleseikan game nya.
  93.      */
  94.     private boolean processCommand(Command command)
  95.     {
  96.         boolean wantToQuit = false;
  97.        
  98.         if (command.isUnknown())
  99.         {
  100.             System.out.println("I don't know what you mean...");
  101.             return false;
  102.         }
  103.        
  104.         String commandWord = command.getCommandWord();
  105.         if (commandWord.equals("help"))
  106.             printHelp();
  107.         else if (commandWord.equals("go"))
  108.             goRoom(command);
  109.         else if (commandWord.equals("quit"))
  110.             wantToQuit = quit(command);
  111.         return wantToQuit;
  112.     }
  113.    
  114.     private void printHelp()
  115.     {
  116.         System.out.println("You are lost. You are alone. You wander");
  117.         System.out.println("around at the university");
  118.         System.out.println();
  119.         System.out.println("Your command words are:");
  120.         System.out.println("    go quit help");
  121.     }
  122.    
  123.     /**
  124.      * lakukan pergi ke suatu arah jika ada keluar maka pindah tempat
  125.      * jika tidak jadi error
  126.      */
  127.     private void goRoom(Command command)
  128.     {
  129.         if(!command.hasSecondWord())
  130.         {
  131.             // jika tidak ada kata kedua, sehingga jadi sesat
  132.             System.out.println("Go where?");
  133.             return;
  134.         }
  135.        
  136.         String direction = command.getSecondWord();
  137.        
  138.         // pindah pindah tempat
  139.         Room nextRoom = null;
  140.         if (direction.equals("north"))
  141.             nextRoom = currentRoom.northExit;
  142.         if (direction.equals("east"))
  143.             nextRoom = currentRoom.eastExit;
  144.         if (direction.equals("south"))
  145.             nextRoom = currentRoom.southExit;
  146.         if (direction.equals("west"))
  147.             nextRoom = currentRoom.westExit;
  148.            
  149.         if (nextRoom == null)
  150.             System.out.println("There is no door!");
  151.         else
  152.         {
  153.             currentRoom = nextRoom;
  154.             System.out.println("You are " + currentRoom.getDescription());
  155.             System.out.println("Exits: ");
  156.             if (currentRoom.northExit != null)
  157.                 System.out.println("north ");
  158.             if (currentRoom.eastExit != null)
  159.                 System.out.println("east ");
  160.             if (currentRoom.southExit != null)
  161.                 System.out.println("south ");
  162.             if (currentRoom.westExit != null)
  163.                 System.out.println("west ");
  164.         }
  165.     }
  166.    
  167.     /**
  168.      * apabila ada user enter 'quit' maka check semua command untuk melihat apakah kita bisa
  169.      * keluar dari game tersebut
  170.      * @return true jika bisa keluar dan false jika tidak
  171.      */
  172.     private boolean quit(Command command)
  173.     {
  174.         if (command.hasSecondWord())
  175.         {
  176.             System.out.println("Quit where?");
  177.             return false;
  178.         }
  179.         else
  180.             return true;
  181.     }
  182. }
  183.  
Add Comment
Please, Sign In to add comment