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

playerGame1

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