Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

World of Zuul (Take 2)

807606May 9 2007 — edited May 11 2007
I'm currently working on the World of Zuul project which I am sure many of you are familiar with. I have managed to make all my rooms, added appropriate exits and items that I'd like.

I've added an inventory which begins the game with the player carrying some items, but here's where the problem arises.

I have successfully managed to create a DROP command, which adds the item that is dropped to the current room, and deletes it from the Inventory. However, I am struggling with the TAKE command that'll allow me to pick an item up.

I thought it'd be easy, but I simply cannot get it to work! I am very new to Java and only know the basics so any help at all would be appreciated!

This is the DROP command and then my attempt at the TAKE:

/**
* doDrop:
* 1. checks if user specified a thing to be dropped
* 2. checks that it's there in inventory
* 3. deletes that item from inventory, and adds to currentRoom
* 4. prints a message about it.
*/
public void doDrop(Command c) {
if (! c.hasSecondWord()) { // "DROP",but no object named
System.out.println("Drop what?");
return;
}
String s = c.getSecondWord();
Item i = findByName(s, inventory);
if (i == null) {
System.out.println("You don't have a " + s);
return;
}
inventory.remove(i);
currentRoom.addItem(i);
System.out.println("You have dropped the " + s);
}

/**
* doTake: doesn't do anything yet
* 1. checks if user specified a thing to be taken
* 2. checks that it's there in the room
* 3. deletes that item from currentRoom, and adds to inventory

public void doTake(Command c) {
if(! c.hasSecondWord()) {
// if there is no second word, we don't know what to take
System.out.println("Take what?");
return;
}

String s = c.getSecondWord();
Item i = findByName(currentRoom);
if (i == null) {
System.out.println("There is no " + s + " here.");
return;
}
inventory.addItem(i);
currentRoom().remove(i);
}

The error lies in the line: "Item i = findByName(currentRoom);"

It says findByName in Game cannot be applied to (Room)??

(This is the Game class)
Please help!



Also, could anyone help me get started with how I'd use items to access rooms? This is what I shall make the object of my game (to find a 'key' to access the exit, which completes the game) A few pointers would be awesome.

Thanks for your time!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 8 2007
Added on May 9 2007
10 comments
586 views