mnaufaldillah

Game Tugas 6

Nov 24th, 2020
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.70 KB | None | 0 0
  1. /**
  2.  * This class is the main class of the "World of Zuul"
  3.  * application.
  4.  * "World of Zuul" is a very simple, text based
  5.  * adventure game. Users can walk around some scenery. That's all. It should
  6.  * really be extended to make it more interesting!
  7.  *
  8.  * To play this game, create an instance of this class
  9.  * and call the "play" method.
  10.  *
  11.  * This main class creates and initialises all the
  12.  * others: it creates all rooms, creates the parser and starts the game. It
  13.  * also evaluates and executes the commands that the parser returns.
  14.  *
  15.  * @author (your name)
  16.  * @version (a version number or a date)
  17.  */
  18. public class Game
  19. {
  20.     private Parser parser;
  21.     private Room currentRoom;
  22.    
  23.     /**
  24.      * Create the game and initialise its internal map.
  25.      */
  26.     public Game()
  27.     {
  28.         createRooms();
  29.         parser = new Parser();
  30.     }
  31.    
  32.     /**
  33.      * Create all the rooms and link their exits
  34.      * together.
  35.      */
  36.     private void createRooms()
  37.     {
  38.         Room outside, theatre, pub, lab, office;
  39.        
  40.         // create the rooms
  41.         outside = new Room("outside thee main entrance of the university");
  42.         theatre = new Room("in a lecture theatre");
  43.         pub = new Room("in the campus pub");
  44.         lab = new Room("in a computing lab");
  45.         office = new Room("in the computing admin office");
  46.        
  47.         // initialise room exits
  48.         outside.setExits(null, theatre, lab, pub);
  49.         theatre.setExits(null, null, null, outside);
  50.         pub.setExits(null, outside, null, null);
  51.         lab.setExits(outside, office, null, null);
  52.         office.setExits(null, null, null, lab);
  53.        
  54.         currentRoom = outside; // start game outside
  55.     }
  56.    
  57.     /**
  58.      * Main play routine. Loops until end of play.
  59.      */
  60.     public void play()
  61.     {
  62.         printWelcome();
  63.        
  64.         // Enter the main command loop. Here we repeatedly read commands and
  65.         // execute them until the game is over.
  66.        
  67.         boolean finished = false;
  68.         while (!finished)
  69.         {
  70.             Command command = parser.getCommand();
  71.             finished = processCommand(command);
  72.         }
  73.         System.out.println("Thank you for playing. Good bye.");
  74.     }
  75.    
  76.     /**
  77.      * Print out the opening message for the player.
  78.      */
  79.     private void printWelcome()
  80.     {
  81.         System.out.println();
  82.         System.out.println("Welcome to Adventure!");
  83.         System.out.println("Adventure is a new, incredibly boring adventure game.");
  84.         System.out.println("Type 'help' if you need help.");
  85.         System.out.println();
  86.         System.out.println("You are " + currentRoom.getDescription());
  87.         System.out.print("Exits: ");
  88.        
  89.         if(currentRoom.northExit != null)
  90.         {
  91.             System.out.print("north ");
  92.         }
  93.         if(currentRoom.eastExit != null)
  94.         {
  95.             System.out.print("east ");
  96.         }
  97.         if(currentRoom.southExit != null)
  98.         {
  99.             System.out.print("south ");
  100.         }
  101.         if(currentRoom.westExit != null)
  102.         {
  103.             System.out.print("west ");
  104.         }
  105.        
  106.         System.out.println();
  107.     }
  108.    
  109.     /**
  110.      * Given a command, process (that is: execute) the
  111.      * command.
  112.      * If this command ends the game, true is returned,
  113.      * otherwise false is returned.
  114.      */
  115.     private boolean processCommand(Command command)
  116.     {
  117.         boolean wantToQuit = false;
  118.        
  119.         if(command.isUnknown())
  120.         {
  121.            System.out.println("I don't know what you mean...");
  122.            return false;
  123.         }
  124.        
  125.         String commandWord = command.getCommandWord();
  126.        
  127.         if(commandWord.equals("help"))
  128.         {
  129.             printHelp();
  130.         }
  131.         else if(commandWord.equals("go"))
  132.         {
  133.             goRoom(command);
  134.         }
  135.         else if(commandWord.equals("quit"))
  136.         {
  137.             wantToQuit = quit(command);
  138.         }
  139.        
  140.         return wantToQuit;
  141.     }
  142.    
  143.     // implementation of user commands:
  144.    
  145.     /**
  146.      * Print out some help information.
  147.      *  Here we print some stupid, cryptic message and a
  148.      *  list of the command words.
  149.      */
  150.     private void printHelp()
  151.     {
  152.         System.out.println("You are lost. You are alone. You wander");
  153.         System.out.println("around at the university.");
  154.         System.out.println();
  155.         System.out.println("Your command words are:");
  156.         System.out.println(" go quit help");
  157.     }
  158.    
  159.     /**
  160.      * Try to go to one direction. If there is an exit,
  161.      * enter the new room, otherwise print an error message.
  162.      */
  163.     private void goRoom(Command command)
  164.     {
  165.         if(!command.hasSecondWord())
  166.         {
  167.             // if there is no second word, we don't know where to go...
  168.             System.out.println("Go where?");
  169.             return;
  170.         }
  171.        
  172.         String direction = command.getSecondWord();
  173.        
  174.         // Try to leave current room.
  175.         Room nextRoom = null;
  176.         if(direction.equals("north"))
  177.         {
  178.             nextRoom = currentRoom.northExit;
  179.         }
  180.         if(direction.equals("east"))
  181.         {
  182.             nextRoom = currentRoom.eastExit;
  183.         }
  184.         if(direction.equals("south"))
  185.         {
  186.             nextRoom = currentRoom.southExit;
  187.         }
  188.         if(direction.equals("west"))
  189.         {
  190.             nextRoom = currentRoom.westExit;
  191.         }
  192.        
  193.         if(nextRoom == null)
  194.         {
  195.            System.out.println("There is no door!");  
  196.         }
  197.         else
  198.         {
  199.             currentRoom = nextRoom;
  200.             System.out.println("You are " + currentRoom.getDescription());
  201.             System.out.print("Exits: ");
  202.            
  203.             if(currentRoom.northExit != null)
  204.             {
  205.                 System.out.print("north ");
  206.             }
  207.             if(currentRoom.eastExit != null)
  208.             {
  209.                 System.out.print("east ");
  210.             }
  211.             if(currentRoom.southExit != null)
  212.             {
  213.                 System.out.print("south ");
  214.             }
  215.             if(currentRoom.westExit != null)
  216.             {
  217.                 System.out.print("west ");
  218.             }
  219.            
  220.             System.out.println();
  221.         }
  222.     }
  223.    
  224.     /**
  225.      *  "Quit" was entered. Check the rest of the
  226.      *  command to see whether we really quit the game. Return true, if
  227.      *  this command quits the game, false otherwise.
  228.      */
  229.     private boolean quit(Command command)
  230.     {
  231.         if(command.hasSecondWord())
  232.         {
  233.             System.out.println("Quit what?");
  234.         }
  235.        
  236.         return true;
  237.     }
  238. }
Advertisement
Add Comment
Please, Sign In to add comment