Advertisement
lamaulfarid

Game

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