Advertisement
phamt

Game + Main class

Feb 25th, 2018
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.80 KB | None | 0 0
  1.  
  2.  
  3. package edu.pcc.cis.zuul;
  4.  
  5. import java.util.ArrayList;
  6.  
  7. public class Game
  8. {
  9.     private Parser parser;
  10.     private Room currentRoom;
  11.     private ArrayList<Item> item1;
  12.        
  13.     /**
  14.      * Create the game and initialise its internal map.
  15.      */
  16.     public Game()
  17.     {
  18.         createRooms();
  19.         parser = new Parser();
  20.     }
  21.  
  22.     /**
  23.      * Create all the rooms and link their exits together.
  24.      */
  25.     private void createRooms()
  26.     {
  27.         Room entrance, policestation, club, lab, cityhall, hospital, center, cellar, watchtower;
  28.      
  29.         // create the rooms
  30.         entrance = new Room("at the main entrance of town, a thick blanket fog covers the entirety of town");
  31.         entrance.addItem("and found a dirty Journal", 5);
  32.         cellar = new Room("down into the cellar the smell of decomposing matter is strong");
  33.         cellar.addItem("and found a folder", 2);
  34.         watchtower = new Room("up in the watchtower, it is cold and you can't see anything thanks to the fog");
  35.         watchtower.addItem("and found a pair of binoculars", 4);
  36.         policestation = new Room("in the police station, you find find evidence of a gunfight that left bullet holes scattered throughout the place");
  37.         policestation.addItem("and found a gun", 6);
  38.         club = new Room("in the local club, where you find broken bottles scattered around the premises covered in strange black goo.");
  39.         club.addItem("and found a water flask", 3);
  40.         lab = new Room("in the science lab, you find broken glass and the signs of an explosion");
  41.         lab.addItem("and found a vial of strange liquid", 1);
  42.         cityhall = new Room("in the mayor's office, you find papers strewn all over the place");
  43.         cityhall.addItem("and found a notebook full of notes", 2);
  44.         hospital = new Room("in the hospital, you find signs of a struggle along with black goo splattered all over the place along with the sound of something scratching against one of the doors.");
  45.         hospital.addItem("and found a small first-aid kit alogn with a stash of granolar bars", 7);
  46.         center = new Room("in the center of town, you find signs of a huge accident along with cars that are either burnt out are have been broken into. You find black goo on some of the broken windows.");
  47.         center.addItem("and found a flashlight", 2);
  48.        
  49.        
  50.         // initialise room exits
  51.         entrance.setExit("east", policestation);
  52.         entrance.setExit("south", center);
  53.      
  54.         policestation.setExit("up", watchtower);
  55.         policestation.setExit("west", entrance);
  56.                
  57.         club.setExit("north", center);
  58.        
  59.         lab.setExit("north", hospital);
  60.         lab.setExit("down", cellar);
  61.        
  62.         cityhall.setExit("east", center);
  63.        
  64.         hospital.setExit("west", center);
  65.         hospital.setExit("south", lab);
  66.        
  67.         center.setExit("north", entrance);
  68.         center.setExit("east", hospital);
  69.         center.setExit("south", club);
  70.         center.setExit("north", entrance);
  71.        
  72.         cellar.setExit("up", lab);
  73.        
  74.         watchtower.setExit("down", policestation);
  75.        
  76.         currentRoom = entrance;  // start game outside
  77.     }
  78.  
  79.     /**
  80.      *  edu.pcc.cis.zuul.Main play routine.  Loops until end of play.
  81.      */
  82.     public void play()
  83.     {            
  84.         printWelcome();
  85.  
  86.         // Enter the main command loop.  Here we repeatedly read commands and
  87.         // execute them until the game is over.
  88.                
  89.         boolean finished = false;
  90.         while (! finished) {
  91.             Command command = parser.getCommand();
  92.             finished = processCommand(command);
  93.         }
  94.         System.out.println("Thank you for playing.  Good bye.");
  95.     }
  96.  
  97.     /**
  98.      * Print out the opening message for the player.
  99.      */
  100.     private void printWelcome()
  101.     {
  102.         System.out.println();
  103.         System.out.println("Welcome to the Town of Silent Hill!");
  104.         System.out.println("Town of Silent Hill is a adventure game where You, a detective, must find clues as to happened to the inhabitants of the town of Silent Hill.");
  105.         System.out.println("Type 'help' if you need help.");
  106.         System.out.println();
  107.         printLocationInfo();
  108.     }
  109.  
  110.     /**
  111.      * Given a command, process (that is: execute) the command.
  112.      * @param command The command to be processed.
  113.      * @return true If the command ends the game, false otherwise.
  114.      */
  115.     private boolean processCommand(Command command)
  116.     {
  117.         boolean wantToQuit = false;
  118.  
  119.         if(command.isUnknown()) {
  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.             printHelp();
  127.         }
  128.         else if (commandWord.equals("go")) {
  129.             goRoom(command);
  130.         }
  131.         else if (commandWord.equals("look")) {
  132.             look();
  133.         }
  134.         else if (commandWord.equals("eat")) {
  135.             eat();
  136.         }
  137.         else if (commandWord.equals("quit")) {
  138.             wantToQuit = quit(command);
  139.         }
  140.        
  141.  
  142.         return wantToQuit;
  143.     }
  144.  
  145.   /**
  146.    * Look command
  147.    * @return output of Look()
  148.    */
  149.   private void look()
  150.   {
  151.       System.out.println(currentRoom.getLongDescription());
  152.   }  
  153.  
  154.   /**
  155.   * Eat command
  156.   * @return description if you've eaten food.
  157.   */
  158.   private void eat()
  159.   {
  160.     System.out.println("You have eaten now and you are not hungry any more");
  161.   }
  162.     // implementations of user commands:
  163.  
  164.   /**
  165.      * Print out some help information.
  166.      * Here we print some cryptic message and a list of the
  167.      * command words.
  168.      */
  169.     private void printHelp()
  170.     {
  171.         System.out.println("You are lost. You are alone. You wander");
  172.         System.out.println("around the abandoned town.");
  173.         System.out.println();
  174.         System.out.println("Your command words are:");
  175.         parser.showCommands();
  176.     }
  177.  
  178.   /**
  179.      * Try to go in one direction. If there is an exit, enter
  180.      * the new room, otherwise print an error message.
  181.      */
  182.     private void goRoom(Command command)
  183.     {
  184.     if(!command.hasSecondWord()) {
  185.             // if there is no second word, we don't know where to go...
  186.             System.out.println("Go where?");
  187.             return;
  188.     }
  189.  
  190.     String direction = command.getSecondWord();
  191.    
  192.     // Try to leave current room.
  193.    
  194.     Room nextRoom = currentRoom.getExit(direction);
  195.  
  196.     if (nextRoom == null) {
  197.             System.out.println("That is not a valid direction!");
  198.     }
  199.     else {
  200.             currentRoom = nextRoom;
  201.             printLocationInfo();
  202.     }
  203.   }  
  204.  
  205.   /**
  206.      * Print Location Info.
  207.      * @return directions available and the description of the current room.
  208.      */
  209.     private void printLocationInfo()
  210.     {
  211.    
  212.     System.out.print("Directions: ");
  213.     System.out.println();
  214.     System.out.println(currentRoom.getLongDescription());
  215.  
  216.     }
  217.    
  218.     /**
  219.      * "Quit" was entered. Check the rest of the command to see
  220.      * whether we really quit the game.
  221.      * @return true, if this command quits the game, false otherwise.
  222.      */
  223.     private boolean quit(Command command)
  224.     {
  225.         if(command.hasSecondWord()) {
  226.             System.out.println("Quit what?");
  227.             return false;
  228.         }
  229.         else {
  230.             return true;  // signal that we want to quit
  231.         }
  232.     }
  233. }
  234. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  235. Main class:
  236. package edu.pcc.cis.zuul;
  237. //Zuul transporter project
  238. public class Main {
  239.     public static void main(String[] args) {
  240.     new Game().start();
  241.     }
  242.  
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement