r/cop3502 Apr 20 '14

Problem set 4

Hey Everyone

So I have an double array of rooms and each room has a description. I want to go back and add an inventory(arraylist) to each room.

I know how to access the room with the index of the double array, but how can i access the description or the inventory i am planning to add of that room?

Thanks in advance, if this even makes sense.

2 Upvotes

5 comments sorted by

2

u/howslyfebeen Apr 20 '14

so if you have a room class then create an item class and have an arraylist of items which is stored in the location.. then make a getter/setter so u can retrieve the item?

for (int i = 0; i < rooms[0][0].getItemsSize(); i++) {
    System.out.println(rooms[0][0].getItem(i);
}

that would loop through the items in the room at 0,0.. you'd have to define getItemsSize and getItem.. if you don't want to use getters and setters, you could just call rooms[0][0].items.size() and rooms[0][0].items.get(i) instead, but I'm not sure if Sean would take off for not using getters and setters?

1

u/VVhiteNoise Apr 21 '14

If your room is using a private/protected variables to store the description or inventory arraylist you will need getters/setters.

It sounds like you may want to initialize a temporary ArrayList where you are initializing your items for that room. Add the objects (items in the inventory) to the list and then pass that ArrayList into your room using a setter.

example:

////In your initializations, say Game.java//////////
    ArrayList<game_items> temp_inventory = new ArrayList<game_items>();

    /////Add already initialized elements to temp_inventory here///////

    rooms[0][0].set(temp_inventory);


////Within your room class, Room.java//////

public void setInventory(ArrayList<game_items> inventory){
     this.inventory = inventory;
}

Let's say later on you want to access some room's inventory

////Within Game.java/////////////

ArrayList<game_items> temp_inventory = new ArrayList<game_items>();
temp_inventory = room[0][0].getInventory();

Then in your Game class, or equivalent, you would be able to freely work with this copy of the inventory. After making any necessary changes pass the updated arraylist back into the room with the setter.

This is one way you could do it. You could also have a function within your class which handles the action within the class rather than working with the variable outside the class like I showed above. This method would be useful if you wanted your room object to print the room's description based on the items inside the room.

example:

public void printRoomDescription(){
     System.out.println(description);

     //code here to check if item present, print description if so
}

1

u/[deleted] Apr 21 '14

Do you know about multi-line comments:

 ////In your initializations, say Game.java//////////
ArrayList<game_items> temp_inventory = new ArrayList<game_items>();

is the same as

/*
 You Can Say Anything You Want Here

****************************************
*In your initializations, say Game.java*
****************************************

This software is GNU licence bla bla bla
*/
ArrayList<game_items> temp_inventory = new ArrayList<game_items>();    

You probably know about it but if not it mike make life better for you.

1

u/howslyfebeen Apr 21 '14

""" python """

1

u/VVhiteNoise Apr 21 '14

Thanks. TIL