Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

aceGameWithPlayer

By: ace on Aug 4th, 2010  |  syntax: None  |  size: 8.24 KB  |  hits: 8  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import java.util.HashMap;
  2.  
  3.  
  4. /**
  5.  *  This class is the main class of the "World of Zuul" application.
  6.  *  "World of Zuul" is a very simple, text based adventure game.  Users
  7.  *  can walk around some scenery. That's all. It should really be extended
  8.  *  to make it more interesting!
  9.  *
  10.  *  To play this game, create an instance of this class and call the "play"
  11.  *  method.
  12.  *
  13.  *  This main class creates and initialises all the others: it creates all
  14.  *  rooms, creates the parser and starts the game.  It also evaluates and
  15.  *  executes the commands that the parser returns.
  16.  *
  17.  * @author  Michael Kolling and David J. Barnes
  18.  * @version 1.0 (February 2002)
  19.  */
  20.  
  21. public class Game
  22. {
  23.     private Parser parser;
  24.     private String lastDirection;
  25.     private HashMap<String,Room> rooms;
  26.     private Player player;
  27.        
  28.     /**
  29.      * Create the game and initialise its internal map.
  30.      */
  31.     public Game()
  32.     {
  33.        
  34.         parser = new Parser();
  35.         rooms = new HashMap<String, Room>();
  36.         createRooms();
  37.        
  38.     }
  39.  
  40.     /**
  41.      * Create all the rooms and link their exits together.
  42.      */
  43.     private void createRooms()
  44.     {
  45.         Room outside, galleria, theatre, pub, store, office;
  46.      
  47.         // create the rooms
  48.         outside = new Room("outside the main entrance of the mall");
  49.         galleria = new Room("in the mall galleria");
  50.         theatre = new Room("in a movie theatre");
  51.         pub = new Room("in the pub");
  52.         store = new Room("in a store");
  53.         office = new Room("in the mall office");
  54.        
  55.         //initialise roommap
  56.         rooms.put("outside", outside);
  57.         rooms.put("theatre", theatre);
  58.         rooms.put("pub", pub);
  59.         rooms.put("office", office);
  60.         rooms.put("galleria", galleria);
  61.         rooms.put ("store", store);
  62.        
  63.         // initialise room exits
  64.         outside.setExit("east", pub);
  65.         outside.setExit("south", galleria);
  66.         outside.setExit ("west", theatre);
  67.         theatre.setExit ("east", outside);
  68.         theatre.setExit ("south", office);
  69.         pub.setExit("west", outside);
  70.         pub.setExit("south", store);
  71.         office.setExit("north", theatre);
  72.         office.setExit("east", galleria);
  73.         galleria.setExit("north", outside);
  74.         galleria.setExit("east", store);
  75.         galleria.setExit("west", office);
  76.         store.setExit ("north", pub);
  77.         store.setExit("west", galleria);
  78.        
  79.         createItems();//added createItems to createRooms
  80.        
  81.         player = new Player(outside);  // start game outside
  82.         lastDirection = null;
  83.     }
  84.    
  85.     private void createItems()//changed this so it does not take a Room parameter
  86.     {
  87.        Item tickingBomb, popcornMachine, beerTap;
  88.        
  89.         // create the items
  90.         tickingBomb = new Item(1, "a ticking bomb");
  91.         popcornMachine = new Item(2, "a popcorn machine");
  92.         beerTap  = new Item (3, "a beer tap");
  93.    
  94.         rooms.get("pub").setItems(tickingBomb);
  95.         rooms.get("pub").setItems(popcornMachine);
  96.         rooms.get("pub").setItems(beerTap);
  97.        
  98.         // initialize item locations
  99.        
  100. }
  101.  
  102. private void printLocationInfo()
  103. {
  104. System.out.println("You are " + player.getCurrentRoom().getLongDescription());
  105. System.out.print("Exits: ");
  106. System.out.println(player.getCurrentRoom().getExitString());
  107. }
  108.    
  109.    
  110.    
  111.     /**
  112.      *  Main play routine.  Loops until end of play.
  113.      */
  114.     public void play()
  115.     {            
  116.         printWelcome();
  117.  
  118.         // Enter the main command loop.  Here we repeatedly read commands and
  119.         // execute them until the game is over.
  120.                
  121.         boolean finished = false;
  122.         while (! finished) {
  123.             Command command = parser.getCommand();
  124.             finished = processCommand(command);
  125.         }
  126.         System.out.println("Thank you for playing.  Good bye.");
  127.     }
  128.  
  129.     /**
  130.      * Print out the opening message for the player.
  131.      */
  132.     private void printWelcome()
  133.     {
  134.         System.out.println();
  135.         System.out.println("Welcome to the World of Zuul!");
  136.         System.out.println("World of Zuul is a new, incredibly boring adventure game.");
  137.         System.out.println("Type 'help' if you need help.");
  138.         System.out.println();
  139.         printLocationInfo();
  140.        
  141.     }
  142.  
  143.     /**
  144.      * Given a command, process (that is: execute) the command.
  145.      * If this command ends the game, true is returned, otherwise false is
  146.      * returned.
  147.      */
  148.     private boolean processCommand(Command command)
  149.     {
  150.         boolean wantToQuit = false;
  151.  
  152.         if(command.isUnknown()) {
  153.             System.out.println("I don't know what you mean...");
  154.             return false;
  155.         }
  156.  
  157.         String commandWord = command.getCommandWord();
  158.         if (commandWord.equals("help"))
  159.             printHelp();
  160.         else if (commandWord.equals("go"))
  161.             goRoom(command);
  162.             else if (commandWord.equals("look"))
  163.             look();
  164.         else if (commandWord.equals("quit"))
  165.             wantToQuit = quit(command);
  166.         else if (commandWord.equals("defuse"))
  167.             defuse();
  168.         else if(commandWord.equals ("back"))
  169.             back();
  170.             return wantToQuit;
  171.  
  172.        
  173.     }
  174.  
  175.     // implementations of user commands:
  176.  
  177.     /**
  178.      * Print out some help information.
  179.      * Here we print some stupid, cryptic message and a list of the
  180.      * command words.
  181.      */
  182.     private void printHelp()
  183.     {
  184.         System.out.println("You are lost. You are alone. You wander");
  185.         System.out.println("around at the university.");
  186.         System.out.println();
  187.         System.out.println("Your command words are:");
  188.         System.out.println ("" + parser.showCommands());
  189.  
  190.     }
  191.  
  192.     /**
  193.      * Try to go to one direction. If there is an exit, enter
  194.      * the new room, otherwise print an error message.
  195.      */
  196.     private void goRoom(Command command)
  197.     {
  198.         if(!command.hasSecondWord()) {
  199.             // if there is no second word, we don't know where to go...
  200.             System.out.println("Go where?");
  201.             return;
  202.         }
  203.  
  204.         String direction = command.getSecondWord();
  205.         // Try to leave current room.
  206.         Room nextRoom = player.getCurrentRoom().getExit(direction);
  207.        
  208.    
  209.         if (nextRoom == null)
  210.             System.out.println("There is no door!");
  211.         else {
  212.             lastDirection = direction;
  213.             player.setCurrentRoom(nextRoom);
  214.             printLocationInfo();
  215.            
  216.         }
  217.     }
  218.    
  219.     private void defuse()
  220.         {
  221.          System.out.println ("you have defused the bomb");
  222.         }
  223.  
  224.     private void look()
  225.     {
  226.         System.out.println(player.getCurrentRoom().getLongDescription());
  227.     }
  228.  
  229.    
  230.     /**
  231.      * "Quit" was entered. Check the rest of the command to see
  232.      * whether we really quit the game. Return true, if this command
  233.      * quits the game, false otherwise.
  234.      */
  235.     private boolean quit(Command command)
  236.     {
  237.         if(command.hasSecondWord()) {
  238.             System.out.println("Quit what?");
  239.             return false;
  240.         }
  241.         else
  242.             return true;  // signal that we want to quit
  243.     }
  244.    
  245.     private void back()
  246.        {
  247.            String reverseLastDirection = "";
  248.            if(lastDirection.equals("north"))
  249.            
  250.            {
  251.            reverseLastDirection = "south";
  252.         }
  253.            if(lastDirection.equals("east"))
  254.            {
  255.                reverseLastDirection = "west";
  256.              
  257.             }
  258.             if(lastDirection.equals("south"))
  259.             {
  260.                 reverseLastDirection = "north";
  261.                
  262.             }
  263.             if (lastDirection.equals("west"))
  264.             {
  265.                 reverseLastDirection = "east";
  266.                
  267.             }
  268.             Command backCommand = new Command ("go", reverseLastDirection);
  269.             goRoom(backCommand);
  270.            }
  271.  
  272.  
  273.        
  274.            
  275.  
  276.  
  277. /*private void getItemDescription(Item item)
  278.     {
  279.         item.getItemDescription(item);
  280.     }
  281.    
  282.     private void getItemWeight(Item item)
  283.     {
  284.         item.getItemWeight(item);
  285.     }*/
  286. }