- import java.util.HashMap;
- import java.util.Random;
- import java.util.Collection;
- import java.util.Set;
- /**
- * This class is the main class of the "World of Zuul" application.
- * "World of Zuul" is a very simple, text based adventure game. Users
- * can walk around some scenery. That's all. It should really be extended
- * to make it more interesting!
- *
- * To play this game, create an instance of this class and call the "play"
- * method.
- *
- * This main class creates and initialises all the others: it creates all
- * rooms, creates the parser and starts the game. It also evaluates and
- * executes the commands that the parser returns.
- *
- * @author Michael Kolling and David J. Barnes
- * @version 1.0 (February 2002)
- */
- public class Game
- {
- private Parser parser;
- private String lastDirection;
- private HashMap<String,Room> rooms;
- private Player player;
- private int numberOfMoves;
- private Room lastRoom;
- private Random tPort;
- private HashMap<String,Character> characters;
- /**
- * Create the game and initialise its internal map.
- */
- public static void main(String[] args)
- {
- Game game = new Game();
- }
- public Game()
- {
- parser = new Parser();
- rooms = new HashMap<String, Room>();
- characters = new HashMap<String, Character>();
- player = new Player();
- createRooms();
- numberOfMoves = 0;
- tPort = new Random();
- }
- /**
- * Create all the rooms and link their exits together.
- */
- private void createRooms()
- {
- Room outside, galleria, theatre, pub, store, office, closet;
- // create the rooms
- outside = new Room("outside the main entrance of the mall");
- galleria = new Room("in the mall galleria");
- theatre = new Room("in a movie theatre");
- pub = new Room("in the pub");
- store = new Room("in a store");
- office = new Room("in the mall office");
- closet = new Room ("in a closet");
- //initialise roommap
- rooms.put("outside", outside);
- rooms.put("theatre", theatre);
- rooms.put("pub", pub);
- rooms.put("office", office);
- rooms.put("galleria", galleria);
- rooms.put ("store", store);
- rooms.put("closet", closet);
- // initialise room exits
- outside.setExit("east", pub);
- outside.setExit("south", galleria);
- outside.setExit ("west", theatre);
- theatre.setExit ("east", outside);
- theatre.setExit ("south", office);
- pub.setExit("west", outside);
- pub.setExit("south", store);
- office.setExit("north", theatre);
- office.setExit("east", galleria);
- galleria.setExit("north", outside);
- galleria.setExit("east", store);
- galleria.setExit("west", office);
- galleria.setExit("south", closet);
- store.setExit ("north", pub);
- store.setExit("west", galleria);
- closet.setExit("north", galleria);
- createItems();
- player.setCurrentRoom(outside); // start game outside
- lastDirection = null;
- createCharacters();
- }
- private void createItems()
- {
- Item tickingBomb, popcornMachine, beerTap, beer, key;
- // create the items
- tickingBomb = new Item(1, "a ticking bomb", "bomb");
- popcornMachine = new Item(4, "a popcorn machine", "popcornmachine");
- key = new Item(0, "a key", "key");
- beerTap = new Item (3, "a beer tap", "beertap");
- rooms.get("pub").setItems(tickingBomb);
- rooms.get("pub").setItems(popcornMachine);
- rooms.get("pub").setItems(beerTap);
- rooms.get("office").setItems(key);
- }
- /* create charcters and populater hashmap characters*/
- private void createCharacters()
- {
- Character littleBoy, bartender, shopper, distraughtMother, partyGuy;
- // create the items
- littleBoy = new Character("littleBoy", "tommy");
- bartender = new Character("bartender", "michael");
- shopper = new Character("shopper", "julie");
- distraughtMother = new Character("distraughtmother", "emily");
- partyGuy = new MobileCharacter("partyGuy", "bob", "west");
- littleBoy.setCurrentRoom(rooms.get("store"));
- bartender.setCurrentRoom(rooms.get("pub"));
- shopper.setCurrentRoom(rooms.get("store"));
- distraughtMother.setCurrentRoom(rooms.get("theatre"));
- partyGuy.setCurrentRoom(rooms.get("pub"));
- characters.put("littleboy", littleBoy);
- characters.put("bartender", bartender);
- characters.put("shopper", shopper);
- characters.put("distraughtmother", distraughtMother);
- characters.put("partyGuy", partyGuy);
- }
- private void printLocationInfo()
- {
- System.out.println ("You are " + player.getCurrentRoom().getLongDescription() + characterInfo());
- }
- /**
- * Main play routine. Loops until end of play.
- */
- public void play()
- {
- printWelcome();
- // Enter the main command loop. Here we repeatedly read commands and
- // execute them until the game is over.
- boolean finished = false;
- while (! finished) {
- Command command = parser.getCommand();
- finished = processCommand(command);
- }
- System.out.println("Thank you for playing. Good bye.");
- }
- /**
- * Print out the opening message for the player.
- */
- private void printWelcome()
- {
- System.out.println();
- System.out.println("Welcome to the World of Zuul!");
- System.out.println("World of Zuul is a new, incredibly boring adventure game.");
- System.out.println("Type 'help' if you need help.");
- System.out.println();
- printLocationInfo();
- }
- /**
- * Given a command, process (that is: execute) the command.
- * If this command ends the game, true is returned, otherwise false is
- * returned.
- */
- private boolean processCommand(Command command)
- {
- boolean wantToQuit = false;
- if(numberOfMoves == 20)
- {
- System.out.println("the bomb has detonated destoying the mall! You have failed.");
- return true;
- }
- if(command.isUnknown()) {
- System.out.println("I don't know what you mean...");
- return false;
- }
- String commandWord = command.getCommandWord();
- if (commandWord.equals("help"))
- printHelp();
- else if (commandWord.equals("go"))
- goRoom(command);
- else if (commandWord.equals("look"))
- look();
- else if (commandWord.equals("quit"))
- wantToQuit = quit(command);
- else if (commandWord.equals("defuse"))
- wantToQuit = defuse(command);
- else if(commandWord.equals ("back"))
- back();
- else if (commandWord.equals ("take"))
- take(command);
- else if (commandWord.equals("drop"))
- drop (command);
- else if (commandWord.equals("inventory"))
- inventory();
- else if (commandWord.equals("pour"))
- pour(command);
- else if (commandWord.equals("drink"))
- drink(command);
- else if (commandWord.equals("rescue"))
- rescue(command);
- return wantToQuit;
- }
- // implementations of user commands:
- /**
- * Print out some help information.
- * Here we print some stupid, cryptic message and a list of the
- * command words.
- */
- private void printHelp()
- {
- System.out.println("You are lost. You are alone. ");
- System.out.println("You wander around at the mall.");
- System.out.println();
- System.out.println("Your command words are:");
- System.out.println ("" + parser.showCommands());
- }
- /**
- * Try to go to one direction. If there is an exit, enter
- * the new room, otherwise print an error message.
- */
- private void goRoom(Command command)
- {
- if(!command.hasSecondWord()) {
- // if there is no second word, we don't know where to go...
- System.out.println("Go where?");
- return;
- }
- String direction = command.getSecondWord();
- // Try to leave current room.
- Room nextRoom = player.getCurrentRoom().getExit(direction);
- if (nextRoom == null)
- {
- System.out.println("There is no door!");
- }
- else if ((nextRoom == rooms.get("office")) && (player.getCurrentRoom() == rooms.get("galleria")))
- {
- System.out.println ("That door is locked");
- printLocationInfo();
- }
- else if (nextRoom == rooms.get("closet"))
- {
- tPort();
- printLocationInfo();
- incrementMoves();
- moveCharacter();
- }
- else
- {
- incrementMoves();
- lastDirection = direction;
- player.setLastRoom(player.getCurrentRoom());
- player.setCurrentRoom(nextRoom);
- System.out.println (" end of line");
- printLocationInfo();
- moveCharacter();
- }
- }
- private boolean defuse(Command command)
- {
- if(!command.hasSecondWord())
- {
- System.out.println("defuse what?");
- return false;
- }
- if (player.checkInventory().equals ("a ticking bomb"))
- {
- System.out.println ("you have defused the bomb");
- System.out.println ("You have saved the mall! You are commended for bravery.");
- return true;
- }
- else
- {
- System.out.println("You cannot defuse that!");
- return false;
- }
- }
- private void look()
- {
- System.out.println ("" + player.getCurrentRoom().getLongDescription());
- }
- /**
- * "Quit" was entered. Check the rest of the command to see
- * whether we really quit the game. Return true, if this command
- * quits the game, false otherwise.
- */
- private boolean quit(Command command)
- {
- if(command.hasSecondWord()) {
- System.out.println("Quit what?");
- return false;
- }
- else
- return true; // signal that we want to quit
- }
- private void back()
- {
- String reverseLastDirection = "";
- System.out.println("" + lastDirection);
- if(lastDirection.equals ("north"))
- {
- reverseLastDirection = "south";
- }
- if(lastDirection.equals("east"))
- {
- reverseLastDirection = "west";
- }
- if (lastDirection.equals("south"))
- {
- reverseLastDirection = "north";
- }
- if (lastDirection.equals("west"))
- {
- reverseLastDirection = "east";
- }
- System.out.println("" + reverseLastDirection);
- Command backCommand = new Command ("go", reverseLastDirection);
- goRoom(backCommand);
- }
- private void take(Command command)
- {
- if(!command.hasSecondWord()) {
- // if there is no second word, we don't know what to pick up...
- System.out.println("Take what?");
- return;
- }
- String itemName = command.getSecondWord();
- // take item
- String result = player.takeItem(itemName);
- System.out.println(result);
- }
- private void drop(Command command)
- {
- if(!command.hasSecondWord())
- {
- System.out.println ("Drop what?");
- }
- String itemName = command.getSecondWord();
- player.dropItem(itemName);
- System.out.println ("you have dropped " + itemName);
- }
- private void inventory()
- {
- System.out.println(player.checkInventory());
- }
- private void pour(Command command)
- {
- if(!command.hasSecondWord())
- {
- System.out.println ("Pour what?");
- }
- else
- {
- if (command.getSecondWord().equals("beer") &&
- player.getCurrentRoom().itemTest("beertap") == true)
- {
- System.out.println ("you pull the beer tap");
- Item beer = new Item (1, "a beer", "beer");
- player.getCurrentRoom().setItems(beer);
- }
- else
- {
- System.out.println("You cannot pour that.");
- }
- }
- }
- private void drink(Command command)
- {
- if(!command.hasSecondWord())
- {
- System.out.println("Drink what?");
- }
- else
- {
- if (command.getSecondWord().equals("beer") &&
- player.getCurrentRoom().itemTest("beer") == true)
- {
- System.out.println("you drink a beer.");
- player.increaseStrength(1);
- }
- }
- }
- private void incrementMoves()
- {
- numberOfMoves = numberOfMoves +1;
- }
- private void tPort()
- {
- int i = tPort.nextInt(6);
- System.out.println ("you have been teleported.");
- System.out.println ("" + i);
- if(i == 0)
- {
- System.out.println("you have been teleported to the outside.");
- player.setCurrentRoom(rooms.get("outside"));
- }
- else if(i == 1)
- {
- System.out.println("you have been teleported to the pub.");
- player.setCurrentRoom(rooms.get("pub"));
- }
- else if(i == 2)
- {
- System.out.println("you have been teleported to the theatre");
- player.setCurrentRoom(rooms.get("theatre"));
- }
- else if(i == 3)
- {
- System.out.println("you have been teleported to the office.");
- player.setCurrentRoom(rooms.get("office"));
- }
- else if(i == 4)
- {
- System.out.println("you have been teleported to the store.");
- player.setCurrentRoom(rooms.get("store"));
- }
- else if(i == 5)
- {
- System.out.println("you have been teleported to the galleria.");
- player.setCurrentRoom(rooms.get("galleria"));
- }
- else
- {
- System.out.println("you have reached the else statement.");
- printLocationInfo();
- player.setCurrentRoom(rooms.get("closet"));
- }
- }
- private void rescue(Command command)
- {
- if(!command.hasSecondWord())
- {
- // if there is no second word, we don't know where to go...
- System.out.println("Rescue who?");
- return;
- }
- else
- {
- String characterID = command.getSecondWord();
- if(characters.get(characterID).getCurrentRoom() == null)
- {
- System.out.println("They are not here!");
- }
- else
- {
- if(characters.get(characterID).getCurrentRoom() == player.getCurrentRoom())
- {
- characters.get(characterID).setCurrentRoom(rooms.get("outside"));
- System.out.println (("you have saved ") + characterID + (" !"));
- }
- else
- {
- System.out.println("They are not here!");
- }
- }
- }
- }
- //returns the values - character objects - from the hashmap of characters
- private Collection <Character> characterCollection()
- {
- Collection <Character> characterValues = characters.values();
- return characterValues;
- //return characters.values();
- }
- private String characterInfo()
- {
- String s = ("There is ");
- for (Character character : characterCollection())
- {
- if (character.getCurrentRoom() == player.getCurrentRoom())
- {
- s = s + character.getCharacterDescription() + ("\n");
- }
- }
- return s;
- }
- private void moveCharacter ()
- {
- // MobileCharacter mobileCharacter, Room room, String direction
- MobileCharacter bob = (MobileCharacter) characters.get("partyGuy");
- Room room = bob.getCurrentRoom();
- String direction = bob.getDirection();
- Room nextRoom = bob.getCurrentRoom().getExit(direction);
- Set<String> exitSet = room.exitSet();
- for(String s : exitSet){
- if(s.equals(direction))
- {
- bob.setCurrentRoom(nextRoom);
- System.out.println("bob is in " + bob.getCurrentRoom().getDescription());
- }
- else
- {
- bob.changeDirection();
- System.out.println("bob's direction is " + bob.getDirection());
- }
- }
- }
- }