I have all figured out I think except exercise 7.20 Extend zuul by adding an Class called item. Each item is created with a description such as rock or stick and a weight. When creating rooms and setting their exits, items for this game should also be created. When player enters a room information about an item present in this room should be displayed.
import java.util.Set;
import java.util.HashMap;
import java.util.Iterator;
import java.util.ArrayList;
/**
* Item class creates an item to go into a room
*
* @author (Carolyn Leithner)
* @version (2/10/2011)
*/
// Exercise 7.20
public class Item
{
// instance variables - replace the example below with your own
private String description;
private int weight;
//private HashMap items;
// a constant array that holds all items
// exercise 7.20
private ArrayList<Item> items;
/**
* Constructor for objects of class Item
* Create an item described "description", and Weight
*/
public Item(String description, int weight)
{
// initialise instance variables
description = description;
weight = weight;
this.items = new ArrayList<Item>();
}
/**
* Return the description of the item
*/
public String getShortDescription()
{
return description;
}
/**
* Return the weight of the item
*/
public int weight()
{
return weight;
}
/**
* Return a long description of the item that includes the description
* and weight.
*/
public String getLongDescription()
{
return "Item " + description + " Weight " + weight;
}
// exercise 7.20
// /**
// * add an item to the room when it is created
// */
// public void addItem(String description,int weight)
// {
// items.put(new Item (description, weight));
// }
/**
* create a variety of items
*/
public void add(Item aItem)
{
items.add(aItem);
}
/**
* get the item
*/
public ArrayList<Item> getItems()
{
return items;
}
/**
*
*/
public String getItemString()
{
String returnString = "Item:";
{
returnString += " " + items;
}
return returnString;
}
}
import java.util.Set;
import java.util.HashMap;
import java.util.Iterator;
import java.util.ArrayList;
/**
* This is the Zuul-Starter file used for the Homework. Please add the
* appropriate class comments below
*
* @author: Carolyn Leithner
* @version: Feb 10,2011
*/
public class Room
{
public String description;
private HashMap<String, Room> exits;
/*
private Room northExit;
private Room southExit;
private Room eastExit;
private Room westExit;
*/
// exercise 7.20
private ArrayList<Item> items;
//private HashMap<String, Item> items;
private String item;
/**
* Create a room described "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<String, Room>();
//addItem(items);
//items = new ArrayList<String, Room>();
}
// starter file
/**
* Define an exit from this room.
* @param direction The direction of the exit.
* @param neighbor The room to which the exit leads.
*/
public void setExit(String direction, Room neighbor)
{
exits.put(direction, neighbor);
}
/**
* @return The description of the room.
*/
public String getDescription()
{
return description;
}
public Room getExit(String direction) {
return exits.get(direction);
/*
if(direction.equals("north")) {
return northExit;
}
if(direction.equals("east")) {
return eastExit;
}
if(direction.equals("south")) {
return southExit;
}
if(direction.equals("west")) {
return westExit;
}
return null;
*/
}
/**
* Return a string describing the room's exits, for example
* "Exits: north west".
* @return Details of the room's exits.
*/
public String getExitString()
{
/*
String exitString = "Exits: ";
if(northExit != null)
exitString += "north ";
if(eastExit != null)
exitString += "east ";
if(southExit != null)
exitString += "south ";
if(westExit != null)
exitString += "west ";
return exitString;
*/
String returnString = "Exits:";
Set<String> keys = exits.keySet();
for(String exit : keys) {
returnString += " " + exit;
}
return returnString;
}
//exercise 7.11
/**
* Return a long description of this room, of the form:
* You are in the kitchen
* Exits: north west
* @return A description of the room, including exits.
*/
public String getLongDescription()
{
return "You are " + description +".\n" + getExitString() + ".\n";
// + "Items in Room" + getItemString();
}
// // exercise 7.20
/**
* add an item to the room when it is created
*/
public void addItem(Item item)
{
addItem (item);
}
}
/**
* This is the Zuul-Starter file used for the Homework. Please add the
* appropriate class comments below
*
* @author:
* @version:
*/
public class Game
{
private Parser parser;
private Room currentRoom;
private Item item;
/**
* 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 outside, theatre, pub, lab, office, cellar;
// create the rooms
outside = new Room("outside the main entrance of the university");
theatre = new Room("in a lecture theatre");
pub = new Room("in the campus pub");
lab = new Room("in a computing lab");
office = new Room("in the computing admin office");
cellar = new Room("in the cellar");
// initialise room exits
//outside.setExits(null, theatre, lab, pub);
outside.setExit("east", theatre);
outside.setExit("south", lab);
outside.setExit("west", pub);
//theatre.setExits(null, null, null, outside);
theatre.setExit("west", outside);
//pub.setExits(null, outside, null, null);
pub.setExit("east", outside);
//lab.setExits(outside, office, null, null);
lab.setExit("north", outside);
lab.setExit("east", office);
//office.setExits(null, null, null, lab);
office.setExit("west", lab);
office.setExit("down", cellar);
cellar.setExit("up", office);
currentRoom = outside; // start game outside
}
/**
* 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();
/*
System.out.println("You are " + currentRoom.getDescription());
System.out.print("Exits: ");
if(currentRoom.northExit != null) {
System.out.print("north ");
}
if(currentRoom.eastExit != null) {
System.out.print("east ");
}
if(currentRoom.southExit != null) {
System.out.print("south ");
}
if(currentRoom.westExit != null) {
System.out.print("west ");
}
System.out.println();
*/
}
/**
* 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);
// exercise 7.14
else if (commandWord.equals("look"))
look();
return wantToQuit;
}
// starter file
/**
* 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:");
// exercise 7.16
parser.showCommands();
System.out.println(" go quit help");
}
/**
* 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 = currentRoom.getExit(direction);
/*
if(direction.equals("north")) {
nextRoom = currentRoom.northExit;
}
if(direction.equals("east")) {
nextRoom = currentRoom.eastExit;
}
if(direction.equals("south")) {
nextRoom = currentRoom.southExit;
}
if(direction.equals("west")) {
nextRoom = currentRoom.westExit;
}
*/
if (nextRoom == null) {
System.out.println("There is no door!");
}
else {
currentRoom = nextRoom;
printLocationInfo();
/*
System.out.println("You are " + currentRoom.getDescription());
System.out.print("Exits: ");
if(currentRoom.northExit != null) {
System.out.print("north ");
}
if(currentRoom.eastExit != null) {
System.out.print("east ");
}
if(currentRoom.southExit != null) {
System.out.print("south ");
}
if(currentRoom.westExit != null) {
System.out.print("west ");
}
System.out.println();
*/
}
}
/**
* "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
}
}
// exercise 7.14
private void look()
{
System.out.println(currentRoom.getLongDescription());
}
public void printLocationInfo() {
//System.out.println("You are " + currentRoom.getDescription());
//System.out.println(currentRoom.getExitString());
//exercise 7.11
System.out.println(currentRoom.getLongDescription());
/*
System.out.print("Exits: ");
if(currentRoom.getExit("north") != null) {
System.out.print("north ");
}
if(currentRoom.getExit("east") != null) {
System.out.print("east ");
}
if(currentRoom.getExit("south") != null) {
System.out.print("south ");
}
if(currentRoom.getExit("west") != null) {
System.out.print("west ");
}
*/
System.out.println();
}
}