Advertisement
thomasfelix

PBO 6

Nov 23rd, 2020 (edited)
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.45 KB | None | 0 0
  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 Thomas Felix
  7.  * @version 23 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(null, null, null, outside);
  33.         pub.setExits(null, outside, null, null);
  34.         lab.setExits(outside, office, null, null);
  35.         office.setExits(null, null, null, 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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement