World of Zuul (Take 2)

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!

[2585 byte] By [IndianaxJonesa] at [2007-11-27 3:50:53]
# 1
Can anyone pls help? This is due in tomorrow & I'm really stuck!
IndianaxJonesa at 2007-7-12 8:54:49 > top of Java-index,Java Essentials,Java Programming...
# 2
It expects an object of one class, and you're calling it with an object of another.That is what the error meansUse "code" tags, and your question'd be easier to read.
xiarcela at 2007-7-12 8:54:49 > top of Java-index,Java Essentials,Java Programming...
# 3
Actually, it looks from the working part of your code that findByName takes two arguments...That may be the problem.Post the actual compile errorAlso, use "code" tags
xiarcela at 2007-7-12 8:54:49 > top of Java-index,Java Essentials,Java Programming...
# 4
> I'm currently working on the World of Zuul project which I am sure many of you are familiar with. Never heard of him. It is related to [url= http://www.mozilla.org/projects/xul/]XUL[/url]?
DrLaszloJamfa at 2007-7-12 8:54:49 > top of Java-index,Java Essentials,Java Programming...
# 5
I'm also wondering what this WoZ is, is it this: http://www.cs.kent.ac.uk/people/staff/mik/code/ ?
deAppela at 2007-7-12 8:54:49 > top of Java-index,Java Essentials,Java Programming...
# 6

Here's the code again, I've included the code for FindByName which is the source of the problem.

/**

* findByName: given a string and an ArrayList of Items,

* find the Item with the matching name, or else return null

*/

private Item findByName(String s, ArrayList<Item> L) {

int n=0;

while (n < L.size()) {

Item i = L.get(n);

if (s.equals(i.getDesc()))

return i;

n++;

}

return null;// not found above

}

/**

* totalWeight: given an ArrayList of Items, returns total of weights

*/

private int totalWeight(ArrayList<Item> L) {

int n=0;

int sum = 0;

while (n < L.size()) {

Item i = L.get(n);

sum += i.getWeight();

n++;

}

return sum;// not found above

}

/**

* 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:

* 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);

}

This is due in today! Please help? It's meant to be group work but the person I'm working with is even worse than me! I've had to to it all...

Thankyou

IndianaxJonesa at 2007-7-12 8:54:49 > top of Java-index,Java Essentials,Java Programming...
# 7

> This is due in today! <...>

How interesting. Anyway...

Did you write the findByName method yourself or was that the other person? Note that it expects to be "given a string and an ArrayList of Items".

You call it accordingly in some places, but in the offending line you are passing it only one argument (currentRoom).

Lokoa at 2007-7-12 8:54:49 > top of Java-index,Java Essentials,Java Programming...
# 8

> > I'm currently working on the World of Zuul project

> which I am sure many of you are familiar with.

>

> Never heard of him. It is related to

> [url=http://www.mozilla.org/projects/xul/]XUL[/url]?

I've heard of Zoot, never of Zuul.

She deserves a good spanking...

jwentinga at 2007-7-12 8:54:49 > top of Java-index,Java Essentials,Java Programming...
# 9
You probably meant::Item i = findByName(s, currentRoom);
malcolmmca at 2007-7-12 8:54:49 > top of Java-index,Java Essentials,Java Programming...
# 10
I am the Keymaster...Are you the Gatekeeper?
maple_shafta at 2007-7-12 8:54:49 > top of Java-index,Java Essentials,Java Programming...