Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.HashMap;
- /**
- * 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)
- */
- class Game
- {
- private Parser parser;
- private Person player;
- private Person skeleton;
- private Person goblin;
- private Person warrior;
- HashMap<String, Item> items;
- //HashMap<String, Room> rooms;
- int inventorySize;
- boolean encounter;
- /**
- * Create the game and initialise its internal map.
- */
- public Game()
- {
- player = new Person("Player", 100, 10);
- skeleton = new Person("Skeleton", 75, 50);
- goblin = new Person("Goblin", 45, 7);
- warrior = new Person("Warrior", 90, 6);
- createRooms();
- parser = new Parser();
- items = new HashMap<String, Item>();
- //rooms = new HashMap<String, Room>();
- encounter = false;
- }
- /**
- * Create all the rooms and link their exits together.
- * @return
- * @return
- */
- private void createRooms()
- {
- Room dungeon_room1 = new Room("dungeon_room 1");
- Room dungeon_room2 = new Room("dungeon_room 2");
- Room dungeon_room3 = new Room("dungeon_room 3");
- Room dungeon_room4 = new Room("dungeon_room 4");
- Room dungeon_room5 = new Room("dungeon_room 5");
- Room dungeon_room6 = new Room("dungeon_room 6");
- Room dungeon_room7 = new Room("dungeon_room 7");
- dungeon_room1.setExit("north", dungeon_room2);
- dungeon_room2.setExit("east", dungeon_room3);
- dungeon_room2.setExit("west", dungeon_room4);
- dungeon_room3.setExit("west", dungeon_room2);
- dungeon_room3.setExit("south", dungeon_room7);
- dungeon_room4.setExit("east", dungeon_room2);
- dungeon_room4.setExit("north", dungeon_room5);
- dungeon_room5.setExit("west", dungeon_room6);
- dungeon_room5.setExit("south", dungeon_room4);
- dungeon_room6.setExit("east", dungeon_room5);
- dungeon_room7.setExit("north", dungeon_room3);
- //Item sword = new Item("sword", 5, 7);
- //Item axe = new Item("axe", 8, 9);
- //Item healthPotion = new Item("healthpotion", 2, 0);
- Sword s = new Sword(5);
- //dungeon_room2.roomInventory.items.put("sword", sword);
- //dungeon_room3.roomInventory.items.put("axe", axe);
- //dungeon_room4.roomInventory.items.put("healthPotion", healthPotion);
- player.setCurrentRoom(dungeon_room1); // start game outside
- skeleton.setCurrentRoom(dungeon_room7);
- goblin.setCurrentRoom(dungeon_room5);
- warrior.setCurrentRoom(dungeon_room6);
- dungeon_room2.roomInventory.showAllItems();
- return;
- }
- /**
- * 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 Adventure!");
- System.out.println("Adventure is a new, incredibly boring adventure game.");
- System.out.println("Type 'help' if you need help.");
- System.out.println();
- System.out.println(player.getCurrentRoom().getLongDescription());
- }
- /**
- * 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(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"))
- {
- gotoRoom(command);
- }else if (commandWord.equals("quit"))
- {
- wantToQuit = quit(command);
- }else if (commandWord.equals("search"))
- {
- searchRoom(command);
- }else if (commandWord.equals("take"))
- {
- // takeItem(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. You wander");
- System.out.println("around at the university.");
- System.out.println();
- System.out.println("Your command words are:");
- parser.showCommands();
- }
- /**
- * Try to go to one direction. If there is an exit, enter the new
- * room, otherwise print an error message.
- */
- private void gotoRoom(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 {
- player.setCurrentRoom(nextRoom);
- System.out.println(player.getCurrentRoom().getLongDescription());
- // person.removeHealth(25);
- System.out.println("Your current health is: "+ player.getPersonHealth());
- }
- if (player.getPersonHealth() <= 0) {
- System.out.println("You died, thanks for playing.");
- }
- if (player.getCurrentRoom().equals(skeleton.getCurrentRoom()))
- {
- System.out.println("You've encounterd a Skeleton");
- skeleton.attack(player);
- System.out.println("The skeleton attacked and dealth " + skeleton.getDamage() + " damage.");
- System.out.println("Your current health is " + player.getPersonHealth());
- }
- }
- /**
- * "Look" was entered. It will display the room description to the player.
- *
- */
- private void searchRoom(Command command)
- {
- if(command.hasSecondWord()) {
- System.out.println("Simply type 'look', to look around.");
- return;
- }
- System.out.println(player.getCurrentRoom().getLongDescription());
- }
- private void takeItem(Command command, Item i)
- {
- if(command.hasSecondWord()) {
- System.out.println("Simply type 'look', to look around.");
- return;
- }
- if(player.personInventory.inventorySize >= i.weight)
- {
- // player.personInventory.items.put(i);
- // player.getCurrentRoom().remove(i);
- }
- }
- /**
- * "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
- }
- public static void main(String[] args)
- {
- Game game = new Game();
- game.play();
- System.out.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement