document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. public class Game
  2. {
  3.     private Parser parser;
  4.     private Room currentRoom;
  5.     /**
  6.       * Create the game and initialise its internal map.
  7.       */
  8.     public Game()
  9.     {
  10.         createRooms();
  11.         parser = new Parser();
  12.     }
  13.     /**
  14.       * Create all the rooms and link their exits together.
  15.       */
  16.     private void createRooms()
  17.     {
  18.         Room outside, theatre, pub, lab, office;
  19.        
  20.         // create the rooms
  21.        
  22.         outside = new Room("outside the main entrance of the university");
  23.         theatre = new Room("in a lecture theatre");
  24.         pub = new Room("in the campus pub");
  25.         lab = new Room("in a computing lab");
  26.         office = new Room("in the computing admin office");
  27.        
  28.         // initialise room exits
  29.        
  30.         outside.setExits(null, theatre, lab, pub);
  31.         theatre.setExits(null, null, null, outside);
  32.         pub.setExits(null, outside, null, null);
  33.         lab.setExits(outside, office, null, null);
  34.         office.setExits(null, null, null, lab);
  35.         currentRoom = outside; // start game outside
  36.     }
  37.     /**
  38.       * Main play routine. Loops until end of play.
  39.       */
  40.     public void play()
  41.     {
  42.         printWelcome();
  43.         // Masuk ke main command loop. Kita akan berulang kali membaca perintah dan
  44.         // menjalankannya sampai game selesai
  45.         boolean finished = false;
  46.         while (! finished) {
  47.             Command command = parser.getCommand();
  48.             finished = processCommand(command);
  49.         }
  50.         System.out.println("Thank you for playing. Good bye.");
  51.     }
  52.     /**
  53.       * Print out the opening message for the player
  54.       */
  55.     private void printWelcome()
  56.     {
  57.         System.out.println();
  58.         System.out.println("Welcome to Adventure!");
  59.         System.out.println("Adventure is a new, incredibly boring adventure game.");
  60.         System.out.println("Type \'help\' if you need help.");
  61.         System.out.println();
  62.         System.out.println("You are " + currentRoom.getDescription());
  63.         System.out.print("Exits: ");
  64.         if(currentRoom.northExit != null)
  65.             System.out.print("north ");
  66.         if(currentRoom.eastExit != null)
  67.             System.out.print("east ");
  68.         if(currentRoom.southExit != null)
  69.             System.out.print("south ");
  70.         if(currentRoom.westExit != null)
  71.             System.out.print("west ");
  72.         System.out.println();
  73.     }
  74.     /**
  75.       * Given a command, process (that is: execute) the command.
  76.       * If this command ends the game, true is returned,
  77.       * otherwise false is returned.
  78.       */
  79.     private boolean processCommand(Command command)
  80.     {
  81.         boolean wantToQuit = false;
  82.         if(command.isUnknown()) {
  83.             System.out.println("I don\'t know what you mean...");
  84.             return false;
  85.         }
  86.         String commandWord = command.getCommandWord();
  87.         if (commandWord.equals("help"))
  88.             printHelp();
  89.         else if (commandWord.equals("go"))
  90.             goRoom(command);
  91.         else if (commandWord.equals("quit"))
  92.             wantToQuit = quit(command);
  93.         return wantToQuit;
  94.     }
  95.     // implementations of user commands:
  96.     /**
  97.       * Print out some help information.
  98.       * Here we print some stupid, cryptic
  99.       * message and a list of the command words
  100.       */
  101.     private void printHelp()
  102.     {
  103.         System.out.println("You are lost. You are alone. You wander");
  104.         System.out.println("around at the university.");
  105.         System.out.println();
  106.         System.out.println("Your command words are:");
  107.         System.out.println(" go quit help");
  108.     }
  109.     /**
  110.       * Try to go to one direction. If there is an exit, enter
  111.       * the new room, otherwise print an error message
  112.       */
  113.     private void goRoom(Command command)
  114.     {
  115.         if(!command.hasSecondWord()) {
  116.             // if there is no second word, we don\'t know where to go...
  117.             System.out.println("Go where?");
  118.             return;
  119.         }
  120.         String direction = command.getSecondWord();
  121.         // Try to leave current room.
  122.         Room nextRoom = null;
  123.         if(direction.equals("north"))
  124.             nextRoom = currentRoom.northExit;
  125.         if(direction.equals("east"))
  126.             nextRoom = currentRoom.eastExit;
  127.         if(direction.equals("south"))
  128.             nextRoom = currentRoom.southExit;
  129.         if(direction.equals("west"))
  130.             nextRoom = currentRoom.westExit;
  131.         if (nextRoom == null)
  132.             System.out.println("There is no door!");
  133.         else {
  134.             currentRoom = nextRoom;
  135.             System.out.println("You are " +
  136.             currentRoom.getDescription());
  137.             System.out.print("Exits: ");
  138.             if(currentRoom.northExit != null)
  139.                 System.out.print("north ");
  140.             if(currentRoom.eastExit != null)
  141.                 System.out.print("east ");
  142.             if(currentRoom.southExit != null)
  143.             System.out.print("south ");
  144.             if(currentRoom.westExit != null)
  145.             System.out.print("west ");
  146.             System.out.println();
  147.         }
  148.     }
  149.     /**
  150.       * "Quit" was entered. Check the rest of the command to see
  151.       * whether we really quir the game. Return true if this command
  152.       * quits the game, false otherwise.
  153.       */
  154.     private boolean quit(Command command)
  155.     {
  156.         if(command.hasSecondWord()) {
  157.             System.out.println("Quit what?");
  158.             return false;
  159.         }
  160.         else
  161.             return true; // signal that we want to quit
  162.     }
  163. }
');