Question
Now that there is a class for representing Items we need a way to allow the rooms to contain an item. Modify the Room class
Now that there is a class for representing Items we need a way to allow the rooms to contain an item. Modify the Room class so that one item can be added to or removed from the room. You will need to think about what fields and methods to add to the Room class. Also think about what the methods that you add should do when an attempt is made to add an item to a room that already contains an item, or an attempt is made to remove an item from a room that does not contain an item.
Now that a room can contain an item, when the player enters a room he/she should be told about the item in that room (if there is one). Modify the appropriate code so that if the player enters a room containing an item, the name and description of the item are displayed along with the description of the room and the list of exits.
Modify the Game class so that it will recognize the command take. When the user enters the "take" command, the item in the current room, if there is one, should be added to the items that the player is carrying and a message should be printed indicating that the player has taken the item. If there is no item in the current room the take command should print an error message. Be sure to test any methods that you add or modify. (Hint: Remember that one task of the Game constructor is to "teach" the CommandReader what words are valid commands. Thus, you will need to make a change in Game's constructor if you want to introduce a new command.
Modify the Game class so that it will recognize the command inventory. When the user types "inventory" the game prints the names of the items that the player is currently carrying. You should think carefully about where the list of item names should be generated. (Consider the fact that the player is carrying the items and think about how the list of exits for a room is generated and displayed.)
Add support to the game for a drop command so that the player can drop an item by name (e.g. "drop book"). The dropped item should appear in the current room. If the current room already contains an item, the drop command should print an error message indicating that the room is full and the player should continue to carry the item.
public class Game { private Parser parser; private Room currentRoom; /** * Create the game and initialise its internal map. */ public Game() { createRooms(); parser = new Parser(); }
/** * Create all the rooms and link their exits together. */ private void createRooms() { Room foyer,battlezone,art_gallery,mummy_tomb,bone_house,passage, chamber,crypt,vault; // create the rooms foyer = new Room("in the Museum Foyer"); battlezone = new Room("in the Battlezone"); art_gallery = new Room("in the Art Gallery"); mummy_tomb = new Room("in a ancient Egyptian Mummy Tomb"); bone_house = new Room("in a Bone House"); passage = new Room("in a dark passage"); chamber = new Room("in a chamber"); crypt = new Room("in a crypt"); vault = new Room("in the vault"); // initialise room exits foyer.setExit("north", battlezone); foyer.setExit("east", art_gallery); foyer.setExit("west", mummy_tomb); battlezone.setExit("south", foyer); battlezone.setExit("east", passage); mummy_tomb.setExit("east", foyer); mummy_tomb.setExit("west", crypt); mummy_tomb.setExit("down", vault); art_gallery.setExit("west", foyer); art_gallery.setExit("east", bone_house); bone_house.setExit("west", art_gallery); bone_house.setExit("south", chamber); bone_house.setExit("up", passage); passage.setExit("down", bone_house); passage.setExit("west", battlezone); chamber.setExit("north", bone_house); crypt.setExit("east", mummy_tomb); vault.setExit("up", mummy_tomb); currentRoom = foyer; // game starts from foyer } /** * 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("You gave in so easily :-( Good bye!"); }
/** * Print out the opening message for the player. */ private void printWelcome() { System.out.println(); System.out.println("Welcome!! to the game 'Lost in the Cursed Museum'"); System.out.println("Lost in the Cursed Museum is a new, flimsy lame adventure game."); System.out.println("Type 'help' if you need help."); System.out.println(); System.out.println(currentRoom.getLongDescription()); }
/** * Given a command, process (that is: execute) the command. * @param command The command to be processed. * @return true If the command ends the game, false otherwise. */ 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")) { goRoom(command); } else if (commandWord.equals("quit")) { wantToQuit = quit(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("in the Museum :-?."); System.out.println(); System.out.println("Your command words are:"); System.out.println(" go quit help"); }
/** * Try to go in 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 = currentRoom.getExit(direction);
if (nextRoom == null) { System.out.println("There is no door!"); } else { currentRoom = nextRoom; System.out.println(currentRoom.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 } } }
public class Room { private String description; private HashMap
/** * Creates a room's "description". Initially, it has * no exits. "description" is something like "a kitchen" or * "an open court yard". * @param description The room's description. */ public Room(String description) { this.description = description; exits = new HashMap<>(); }
/** * Defines the exits of the room. * @param direction The direction of the exit. * @param neighbor The room exit leading to next room. */ public void setExit(String direction, Room neighbor) { exits.put(direction, neighbor); }
/** * @return The description of the room. */ public String getDescription() { return description; } /** * Return a long description of the current room * and a list of exits available */ public String getLongDescription() { return "You are " + description + " " + getExitString(); } /** * Returns list of exits for a room * for example, "Exits: north west east". */ private String getExitString() { String returnString = "Exits:"; Set
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started