Advertisement
raffi_pratama

Untitled

Nov 16th, 2020
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.58 KB | None | 0 0
  1.  
  2. /**
  3.  *  Untuk memainkan gamenya, buat contoh metode ini, dan panggil
  4.  *  metode "play"
  5.  *  Kelas ini menginisialisasi yang lain : ia membuat ruangan, parser, dan
  6.  *  memulai permainan. Juga mengevaluasi dan menjalankan perintah yang
  7.  *  dikembalikan parser.
  8.  
  9.  
  10.  * @author  M. Rayhan Raffi P.
  11.  
  12.  * @version 16-11-2020
  13.  
  14.  */
  15.  
  16.  
  17.  
  18. public class Game
  19.  
  20. {
  21.  
  22.     private Parser parser;
  23.  
  24.     private Room currentRoom;
  25.  
  26.        
  27.  
  28.     /**
  29.  
  30.      * Create the game and initialise its internal map.
  31.  
  32.      */
  33.  
  34.     public Game()
  35.  
  36.     {
  37.  
  38.         createRooms();
  39.  
  40.         parser = new Parser();
  41.  
  42.     }
  43.  
  44.  
  45.  
  46.     /**
  47.  
  48.      * Create all the rooms and link their exits together.
  49.  
  50.      */
  51.  
  52.     private void createRooms()
  53.  
  54.     {
  55.  
  56.         Room outside, theater, pub, lab, office;
  57.  
  58.      
  59.  
  60.         // create the rooms
  61.  
  62.         outside = new Room("outside the main entrance of the university");
  63.  
  64.         theater = new Room("in a lecture theater");
  65.  
  66.         pub = new Room("in the campus pub");
  67.  
  68.         lab = new Room("in a computing lab");
  69.  
  70.         office = new Room("in the computing admin office");
  71.  
  72.        
  73.  
  74.         // initialise room exits
  75.  
  76.         outside.setExits(null, theater, lab, pub);
  77.  
  78.         theater.setExits(null, null, null, outside);
  79.  
  80.         pub.setExits(null, outside, null, null);
  81.  
  82.         lab.setExits(outside, office, null, null);
  83.  
  84.         office.setExits(null, null, null, lab);
  85.  
  86.  
  87.  
  88.         currentRoom = outside;  // start game outside
  89.  
  90.     }
  91.  
  92.  
  93.  
  94.     /**
  95.  
  96.      *  Main play routine.  Loops until end of play.
  97.  
  98.      */
  99.  
  100.     public void play()
  101.  
  102.     {            
  103.  
  104.         printWelcome();
  105.  
  106.  
  107.  
  108.         // Enter the main command loop.  Here we repeatedly read commands and
  109.  
  110.         // execute them until the game is over.
  111.  
  112.                
  113.  
  114.         boolean finished = false;
  115.  
  116.         while (! finished) {
  117.  
  118.             Command command = parser.getCommand();
  119.  
  120.             finished = processCommand(command);
  121.  
  122.         }
  123.  
  124.         System.out.println("Thank you for playing.  Good bye.");
  125.  
  126.     }
  127.  
  128.  
  129.  
  130.     /**
  131.  
  132.      * Print out the opening message for the player.
  133.  
  134.      */
  135.  
  136.     private void printWelcome()
  137.  
  138.     {
  139.  
  140.         System.out.println();
  141.  
  142.         System.out.println("Welcome to the World of Zuul!");
  143.  
  144.         System.out.println("World of Zuul is a new, incredibly boring adventure game.");
  145.  
  146.         System.out.println("Type 'help' if you need help.");
  147.  
  148.         System.out.println();
  149.  
  150.         System.out.println("You are " + currentRoom.getDescription());
  151.  
  152.         System.out.print("Exits: ");
  153.  
  154.         if(currentRoom.northExit != null) {
  155.  
  156.             System.out.print("north ");
  157.  
  158.         }
  159.  
  160.         if(currentRoom.eastExit != null) {
  161.  
  162.             System.out.print("east ");
  163.  
  164.         }
  165.  
  166.         if(currentRoom.southExit != null) {
  167.  
  168.             System.out.print("south ");
  169.  
  170.         }
  171.  
  172.         if(currentRoom.westExit != null) {
  173.  
  174.             System.out.print("west ");
  175.  
  176.         }
  177.  
  178.         System.out.println();
  179.  
  180.     }
  181.  
  182.  
  183.  
  184.     /**
  185.  
  186.      * Given a command, process (that is: execute) the command.
  187.  
  188.      * @param command The command to be processed.
  189.  
  190.      * @return true If the command ends the game, false otherwise.
  191.  
  192.      */
  193.  
  194.     private boolean processCommand(Command command)
  195.  
  196.     {
  197.  
  198.         boolean wantToQuit = false;
  199.  
  200.  
  201.  
  202.         if(command.isUnknown()) {
  203.  
  204.             System.out.println("I don't know what you mean...");
  205.  
  206.             return false;
  207.  
  208.         }
  209.  
  210.  
  211.  
  212.         String commandWord = command.getCommandWord();
  213.  
  214.         if (commandWord.equals("help")) {
  215.  
  216.             printHelp();
  217.  
  218.         }
  219.  
  220.         else if (commandWord.equals("go")) {
  221.  
  222.             goRoom(command);
  223.  
  224.         }
  225.  
  226.         else if (commandWord.equals("quit")) {
  227.  
  228.             wantToQuit = quit(command);
  229.  
  230.         }
  231.  
  232.  
  233.  
  234.         return wantToQuit;
  235.  
  236.     }
  237.  
  238.  
  239.  
  240.     // implementations of user commands:
  241.  
  242.  
  243.  
  244.     /**
  245.  
  246.      * Print out some help information.
  247.  
  248.      * Here we print some stupid, cryptic message and a list of the
  249.  
  250.      * command words.
  251.  
  252.      */
  253.  
  254.     private void printHelp()
  255.  
  256.     {
  257.  
  258.         System.out.println("You are lost. You are alone. You wander");
  259.  
  260.         System.out.println("around at the university.");
  261.  
  262.         System.out.println();
  263.  
  264.         System.out.println("Your command words are:");
  265.  
  266.         System.out.println("   go quit help");
  267.  
  268.     }
  269.  
  270.  
  271.  
  272.     /**
  273.  
  274.      * Try to go in one direction. If there is an exit, enter
  275.  
  276.      * the new room, otherwise print an error message.
  277.  
  278.      */
  279.  
  280.     private void goRoom(Command command)
  281.  
  282.     {
  283.  
  284.         if(!command.hasSecondWord()) {
  285.  
  286.             // if there is no second word, we don't know where to go...
  287.  
  288.             System.out.println("Go where?");
  289.  
  290.             return;
  291.  
  292.         }
  293.  
  294.  
  295.  
  296.         String direction = command.getSecondWord();
  297.  
  298.  
  299.  
  300.         // Try to leave current room.
  301.  
  302.         Room nextRoom = null;
  303.  
  304.         if(direction.equals("north")) {
  305.  
  306.             nextRoom = currentRoom.northExit;
  307.  
  308.         }
  309.  
  310.         if(direction.equals("east")) {
  311.  
  312.             nextRoom = currentRoom.eastExit;
  313.  
  314.         }
  315.  
  316.         if(direction.equals("south")) {
  317.  
  318.             nextRoom = currentRoom.southExit;
  319.  
  320.         }
  321.  
  322.         if(direction.equals("west")) {
  323.  
  324.             nextRoom = currentRoom.westExit;
  325.  
  326.         }
  327.  
  328.  
  329.  
  330.         if (nextRoom == null) {
  331.  
  332.             System.out.println("There is no door!");
  333.  
  334.         }
  335.  
  336.         else {
  337.  
  338.             currentRoom = nextRoom;
  339.  
  340.             System.out.println("You are " + currentRoom.getDescription());
  341.  
  342.             System.out.print("Exits: ");
  343.  
  344.             if(currentRoom.northExit != null) {
  345.  
  346.                 System.out.print("north ");
  347.  
  348.             }
  349.  
  350.             if(currentRoom.eastExit != null) {
  351.  
  352.                 System.out.print("east ");
  353.  
  354.             }
  355.  
  356.             if(currentRoom.southExit != null) {
  357.  
  358.                 System.out.print("south ");
  359.  
  360.             }
  361.  
  362.             if(currentRoom.westExit != null) {
  363.  
  364.                 System.out.print("west ");
  365.  
  366.             }
  367.  
  368.             System.out.println();
  369.  
  370.         }
  371.  
  372.     }
  373.  
  374.  
  375.  
  376.     /**
  377.  
  378.      * "Quit" was entered. Check the rest of the command to see
  379.  
  380.      * whether we really quit the game.
  381.  
  382.      * @return true, if this command quits the game, false otherwise.
  383.  
  384.      */
  385.  
  386.     private boolean quit(Command command)
  387.  
  388.     {
  389.  
  390.         if(command.hasSecondWord()) {
  391.  
  392.             System.out.println("Quit what?");
  393.  
  394.             return false;
  395.  
  396.         }
  397.  
  398.         else {
  399.  
  400.             return true;  // signal that we want to quit
  401.  
  402.         }
  403.  
  404.     }
  405.  
  406. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement