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.  
  7.     public Game()
  8.     {
  9.         createRooms();
  10.         parser = new Parser();
  11.     }
  12.  
  13.     private void createRooms()
  14.     {
  15.         Room outside, theater, pub, lab, office;
  16.      
  17.  
  18.         outside = new Room("outside the main entrance of the university");
  19.         theater = new Room("in a lecture theater");
  20.         pub = new Room("in the campus pub");
  21.         lab = new Room("in a computing lab");
  22.         office = new Room("in the computing admin office");
  23.  
  24.         outside.setExits(null, theater, lab, pub);
  25.         theater.setExits(null, null, null, outside);
  26.         pub.setExits(null, outside, null, null);
  27.         lab.setExits(outside, office, null, null);
  28.         office.setExits(null, null, null, lab);
  29.  
  30.         currentRoom = outside;  
  31.     }
  32.  
  33.     public void play()
  34.     {            
  35.         printWelcome();
  36.                
  37.         boolean finished = false;
  38.         while (! finished) {
  39.             Command command = parser.getCommand();
  40.             finished = processCommand(command);
  41.         }
  42.         System.out.println("Thank you for playing.  Good bye.");
  43.     }
  44.  
  45.     private void printWelcome()
  46.     {
  47.         System.out.println();
  48.         System.out.println("Welcome to the World of Zuul!");
  49.         System.out.println("World of Zuul is a new, incredibly boring adventure game.");
  50.         System.out.println("Type \'help\' if you need help.");
  51.         System.out.println();
  52.         System.out.println("You are " + currentRoom.getDescription());
  53.         System.out.print("Exits: ");
  54.         if(currentRoom.northExit != null) {
  55.             System.out.print("north ");
  56.         }
  57.         if(currentRoom.eastExit != null) {
  58.             System.out.print("east ");
  59.         }
  60.         if(currentRoom.southExit != null) {
  61.             System.out.print("south ");
  62.         }
  63.         if(currentRoom.westExit != null) {
  64.             System.out.print("west ");
  65.         }
  66.         System.out.println();
  67.     }
  68.  
  69.     private boolean processCommand(Command command)
  70.     {
  71.         boolean wantToQuit = false;
  72.  
  73.         if(command.isUnknown()) {
  74.             System.out.println("I don\'t know what you mean...");
  75.             return false;
  76.         }
  77.  
  78.         String commandWord = command.getCommandWord();
  79.         if (commandWord.equals("help")) {
  80.             printHelp();
  81.         }
  82.         else if (commandWord.equals("go")) {
  83.             goRoom(command);
  84.         }
  85.         else if (commandWord.equals("quit")) {
  86.             wantToQuit = quit(command);
  87.         }
  88.  
  89.         return wantToQuit;
  90.     }
  91.  
  92.     private void printHelp()
  93.     {
  94.         System.out.println("You are lost. You are alone. You wander");
  95.         System.out.println("around at the university.");
  96.         System.out.println();
  97.         System.out.println("Your command words are:");
  98.         System.out.println("   go quit help");
  99.     }
  100.  
  101.     private void goRoom(Command command)
  102.     {
  103.         if(!command.hasSecondWord()) {
  104.             System.out.println("Go where?");
  105.             return;
  106.         }
  107.  
  108.         String direction = command.getSecondWord();
  109.  
  110.         Room nextRoom = null;
  111.         if(direction.equals("north")) {
  112.             nextRoom = currentRoom.northExit;
  113.         }
  114.         if(direction.equals("east")) {
  115.             nextRoom = currentRoom.eastExit;
  116.         }
  117.         if(direction.equals("south")) {
  118.             nextRoom = currentRoom.southExit;
  119.         }
  120.         if(direction.equals("west")) {
  121.             nextRoom = currentRoom.westExit;
  122.         }
  123.  
  124.         if (nextRoom == null) {
  125.             System.out.println("There is no door!");
  126.         }
  127.         else {
  128.             currentRoom = nextRoom;
  129.             System.out.println("You are " + currentRoom.getDescription());
  130.             System.out.print("Exits: ");
  131.             if(currentRoom.northExit != null) {
  132.                 System.out.print("north ");
  133.             }
  134.             if(currentRoom.eastExit != null) {
  135.                 System.out.print("east ");
  136.             }
  137.             if(currentRoom.southExit != null) {
  138.                 System.out.print("south ");
  139.             }
  140.             if(currentRoom.westExit != null) {
  141.                 System.out.print("west ");
  142.             }
  143.             System.out.println();
  144.         }
  145.     }
  146.  
  147.     private boolean quit(Command command)
  148.     {
  149.         if(command.hasSecondWord()) {
  150.             System.out.println("Quit what?");
  151.             return false;
  152.         }
  153.         else {
  154.             return true;
  155.         }
  156.     }
  157. }
');