PBO - Tugas 6 - Aplikasi World of Zuul

 

TUGAS 6 - PBO - World of Zuul







Nama : Kevin Ashil Faadilah
NRP : 05111740000178
Kelas : PBO - B

Tugas kali ini menduplikasi gim bertajuk World of Zuul yang merupakan implementasi dari desain class (cohesion dan coupling) dan disadur dari link berikut milik Bapak Fajar Baskoro. Gim ini adalah gim yang sangat sederhana bertemakan adventure di mana pengguna dapat berkeliling banyak tempat, dalam hal ini universitas.

Ada 5 class yang digunakan dalam pembuatan gim ini:
  1. Class Command.java, sebagai class tempat penyimpanan perintah-perintah yang diinputkan pengguna.
  2. Class CommandWords.java, sebagai pendefinisian perintah valid dalam gim (go, help, quit) dan disimpan dalam array object string.
  3. Class Game.java, sebagai class utama untuk menjalankan class-class lain, seperti parsing, membuat room, dan juga sebagai tampilan utama gim.
  4. Class Parse.java, sebagai class untuk menerjemahkan perintah yang diinputkan pengguna dan mengubahnya menjadi perintah "Adventure".
  5. Class Room.java, sebagai class yang menampilkan lokasi ruangan di dalam gim.

Source Code

1. Command.java
DATA HOSTED WITH ♥ BY PASTEBIN.COM - DOWNLOAD RAW - SEE ORIGINAL
  1. /**
  2.  * Class ini menyimpan informasi perintah yang akan diinginkan user.
  3.  * Perintah berisi dua string : "Command Word" dan "Second Word".
  4.  *
  5.  * @author Kevin Ashil
  6.  * @version 24 November 2020
  7.  */
  8.  
  9. public class Command
  10. {
  11.     private String commandWord;
  12.     private String secondWord;
  13.    
  14.     public Command(String firstWord, String secondWord)
  15.     {
  16.         commandWord = firstWord;
  17.         this.secondWord = secondWord;
  18.     }
  19.    
  20.     public String getCommandWord()
  21.     {
  22.         return commandWord;
  23.     }
  24.    
  25.     public String getSecondWord()
  26.     {
  27.         return secondWord;
  28.     }
  29.    
  30.     public boolean isUnknown()
  31.     {
  32.         return (commandWord == null);
  33.     }
  34.    
  35.     public boolean hasSecondWord()
  36.     {
  37.         return (secondWord != null);
  38.     }
  39. }

2. CommandWords.java
DATA HOSTED WITH ♥ BY PASTEBIN.COM - DOWNLOAD RAW - SEE ORIGINAL
  1. /**
  2.  * Class ini menyimpan daftar perintah yang digunakan dalam game.
  3.  *
  4.  * @author Kevin Ashil
  5.  * @version 24 November 2020
  6.  */
  7.  
  8. public class CommandWords
  9. {
  10.     private static final String validCommands[] = {
  11.         "go""quit""help""look"
  12.     };
  13.    
  14.     public CommandWords()
  15.     {
  16.     }
  17.    
  18.     public boolean isCommand(String aString)
  19.     {
  20.         for (int i = 0; i < validCommands.length; i++)
  21.         {
  22.             if (validCommands[i].equals(aString))
  23.             {
  24.                 return true;
  25.             }
  26.         }
  27.        
  28.         return false;
  29.     }
  30. }

3. Game.java
DATA HOSTED WITH ♥ BY PASTEBIN.COM - DOWNLOAD RAW - SEE ORIGINAL
  1. /**
  2.  * Class utama "World of Zuul".
  3.  * Class ini menginisialisasi semua class, termasuk membuat room, parsing dan
  4.  * memulai game, dan eksekusi perintah yang telah di-parsing.
  5.  *
  6.  * @author Kevin Ashil
  7.  * @version 24 November 2020
  8.  */
  9.  
  10. public class Game
  11. {
  12.     private Parser parser;
  13.     private Room currentRoom;
  14.    
  15.     public Game()
  16.     {
  17.         createRooms();
  18.         parser = new Parser();
  19.     }
  20.    
  21.     private void createRooms()
  22.     {
  23.         Room outside, theatre, pub, lab, office;
  24.        
  25.         outside = new Room("outside the main entrance of the university");
  26.         theatre = new Room("in a lecture theatre");
  27.         pub = new Room("in the campus pub");
  28.         lab = new Room("in a computing lab");
  29.         office = new Room("in the computing admin office");
  30.        
  31.         outside.setExits(null, theatre, lab, pub);
  32.         theatre.setExits(nullnullnull, outside);
  33.         pub.setExits(null, outside, nullnull);
  34.         lab.setExits(outside, office, nullnull);
  35.         office.setExits(nullnullnull, lab);
  36.        
  37.         currentRoom = outside;
  38.     }
  39.    
  40.     public void play()
  41.     {
  42.         printWelcome();
  43.        
  44.         boolean finished = false;
  45.         while (! finished)
  46.         {
  47.             Command command = parser.getCommand();
  48.             finished = processCommand(command);
  49.         }
  50.        
  51.         System.out.println("Thank you for playing. Good bye.");
  52.     }
  53.    
  54.     private void printWelcome()
  55.     {
  56.         System.out.println();
  57.         System.out.println("Welcome to Adventure!");
  58.         System.out.println("Adventure is a new, incredibly boring adventure game.");
  59.         System.out.println("Type 'help' if you need help.");
  60.         System.out.println();
  61.         System.out.println("You are " + currentRoom.getDescription());
  62.         System.out.print("Exits: ");
  63.        
  64.         if (currentRoom.northExit != null)
  65.         {
  66.             System.out.print("north ");
  67.         }
  68.        
  69.         if (currentRoom.eastExit != null)
  70.         {
  71.             System.out.print("east ");
  72.         }
  73.        
  74.         if (currentRoom.southExit != null)
  75.         {
  76.             System.out.print("south ");
  77.         }
  78.        
  79.         if (currentRoom.westExit != null)
  80.         {
  81.             System.out.print("west ");
  82.         }
  83.        
  84.         System.out.println();
  85.         System.out.println();
  86.     }
  87.    
  88.     private boolean processCommand(Command command)
  89.     {
  90.         boolean wantToQuit = false;
  91.        
  92.         if (command.isUnknown())
  93.         {
  94.             System.out.println("I don't know what you mean...");
  95.             System.out.println();
  96.             return false;
  97.         }
  98.        
  99.         String commandWord = command.getCommandWord();
  100.         if (commandWord.equals("help"))
  101.         {
  102.             printHelp();
  103.         }
  104.        
  105.         else if (commandWord.equals("go"))
  106.         {
  107.             goRoom(command);
  108.         }
  109.        
  110.         else if (commandWord.equals("quit"))
  111.         {
  112.             wantToQuit = quit(command);
  113.         }
  114.        
  115.         return wantToQuit;
  116.     }
  117.    
  118.     private void printHelp()
  119.     {
  120.         System.out.println("You are lost. You are alone. You are wander");
  121.         System.out.println("around at the university.");
  122.         System.out.println();
  123.         System.out.println("Your command words are:");
  124.         System.out.println("    go quit help");
  125.         System.out.println();
  126.     }
  127.    
  128.     private void goRoom(Command command)
  129.     {
  130.         if (! command.hasSecondWord())
  131.         {
  132.             System.out.println("Go where?");
  133.             System.out.println();
  134.             return;
  135.         }
  136.        
  137.         String direction = command.getSecondWord();
  138.        
  139.         Room nextRoom = null;
  140.         if (direction.equals("north"))
  141.         {
  142.             nextRoom = currentRoom.northExit;
  143.         }
  144.        
  145.         if (direction.equals("east"))
  146.         {
  147.             nextRoom = currentRoom.eastExit;
  148.         }
  149.        
  150.         if (direction.equals("west"))
  151.         {
  152.             nextRoom = currentRoom.westExit;
  153.         }
  154.        
  155.         if (direction.equals("south"))
  156.         {
  157.             nextRoom = currentRoom.southExit;
  158.         }
  159.        
  160.         if (nextRoom == null)
  161.         {
  162.             System.out.println("There is no door!");
  163.             System.out.println();
  164.         }
  165.        
  166.         else
  167.         {
  168.             currentRoom = nextRoom;
  169.             System.out.println("You are " + currentRoom.getDescription());
  170.             System.out.print("Exits: ");
  171.            
  172.             if (currentRoom.northExit != null)
  173.             {
  174.                 System.out.print("north ");
  175.             }
  176.            
  177.             if (currentRoom.eastExit != null)
  178.             {
  179.                 System.out.print("east ");
  180.             }
  181.            
  182.             if (currentRoom.southExit != null)
  183.             {
  184.                 System.out.print("south ");
  185.             }
  186.            
  187.             if (currentRoom.westExit != null)
  188.             {
  189.                 System.out.print("west ");
  190.             }
  191.            
  192.             System.out.println();
  193.             System.out.println();
  194.         }
  195.     }
  196.    
  197.     private boolean quit(Command command)
  198.     {
  199.         if (command.hasSecondWord())
  200.         {
  201.             System.out.println("Quit what?");
  202.             System.out.println();
  203.             return false;
  204.         }
  205.        
  206.         else
  207.         {
  208.             return true;
  209.         }
  210.     }
  211. }

4. Parse.java
DATA HOSTED WITH ♥ BY PASTEBIN.COM - DOWNLOAD RAW - SEE ORIGINAL
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.util.StringTokenizer;
  4.  
  5. /**
  6.  * Class ini untuk parsing input user dan menginterpretasikan menjadi perintah
  7.  * "Adventure".
  8.  * Tiap kali dipanggil, class akan membaca line terminal dan interpretasi
  9.  * menjadi 2 kata perintah.
  10.  *
  11.  * @author Kevin Ashil
  12.  * @version 24 November 2020
  13.  */
  14.  
  15. public class Parser
  16. {
  17.     private CommandWords commands;
  18.    
  19.     public Parser()
  20.     {
  21.         commands = new CommandWords();
  22.     }
  23.    
  24.     public Command getCommand()
  25.     {
  26.         String inputLine = "";
  27.         String word1;
  28.         String word2;
  29.        
  30.         System.out.print("> ");
  31.        
  32.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  33.        
  34.         try
  35.         {
  36.             inputLine = reader.readLine();
  37.         }
  38.        
  39.         catch (java.io.IOException exc)
  40.         {
  41.             System.out.println("There was an error during reading: " + exc.getMessage());
  42.         }
  43.        
  44.         StringTokenizer tokenizer = new StringTokenizer(inputLine);
  45.        
  46.         if (tokenizer.hasMoreTokens())
  47.         {
  48.             word1 = tokenizer.nextToken();
  49.         }
  50.        
  51.         else
  52.         {
  53.             word1 = null;
  54.         }
  55.        
  56.         if (tokenizer.hasMoreTokens())
  57.         {
  58.             word2 = tokenizer.nextToken();
  59.         }
  60.        
  61.         else
  62.         {
  63.             word2 = null;
  64.         }
  65.        
  66.         if (commands.isCommand(word1))
  67.         {
  68.             return new Command(word1, word2);
  69.         }
  70.        
  71.         else
  72.         {
  73.             return new Command(null, word2);
  74.         }
  75.     }
  76. }

5. Room.java
DATA HOSTED WITH ♥ BY PASTEBIN.COM - DOWNLOAD RAW - SEE ORIGINAL
  1. /**
  2.  * Class ini menampilkan lokasi game yang menghubungkan satu room dengan room
  3.  * lain dengan pintu keluar.
  4.  * Ada pintu keluar north, east, south, dan west.
  5.  * @author Kevin Ashil
  6.  * @version 24 November 2020
  7.  */
  8.  
  9. public class Room
  10. {
  11.     public String description;
  12.     public Room northExit;
  13.     public Room southExit;
  14.     public Room eastExit;
  15.     public Room westExit;
  16.    
  17.     public Room(String description)
  18.     {
  19.         this.description = description;
  20.     }
  21.    
  22.     public void setExits(Room north, Room east, Room south, Room west)
  23.     {
  24.         if (north != null)
  25.         {
  26.             northExit = north;
  27.         }
  28.        
  29.         if (east != null)
  30.         {
  31.             eastExit = east;
  32.         }
  33.        
  34.         if (south != null)
  35.         {
  36.             southExit = south;
  37.         }
  38.        
  39.         if (west != null)
  40.         {
  41.             westExit = west;
  42.         }
  43.     }
  44.    
  45.     public String getDescription()
  46.     {
  47.         return description;
  48.     }
  49. }

Output



Komentar

Postingan populer dari blog ini

FP PBO

TUGAS - 2 - PBKK

Tugas 8 - Applet dan JavaFX