Text Based Game, Help With Inventory?

Started by
2 comments, last by Tangletail 8 years, 3 months ago

(Java)

Okay, so I recently started learning java because I honestly love the whole idea of programming. Not for school, but I just practice it in my free time. A while ago I started creating a text-based game, kind of like Zork (or Zorg, can't remember). I was also trying to figure out how I would get an inventory system to work. The player has to be able to access the inventory at any time throughout the game, and if he picks up an item it has to be in his inventory. I don't think I should share the code because there honestly isn't a lot to work with - I just need help with the concept itself.

Advertisement
Aside from Java, how much other programming experience do you have? For a simple text-based game like Zork, a simple array of strings should do. You can use an ArrayList. When picking up an item, you can add that item to the list. You can then use indexOf to query if the player has an item, and then use remove to remove the item with that index:


// populate items
ArrayList<String> items = new ArrayList<String>();
items.add("room key");
items.add("lemonade");
items.add("clothes");

// display initial contents of inventory
System.out.println("Inventory:");
for ( String item : items )
	System.out.println(item);

// query and remove an item
int idx = items.indexOf("room key");
if ( idx != -1 )
	items.remove(idx);
else
	System.out.println("not found");


// display new contents of inventory
System.out.println("Inventory:");
for ( String item : items )
	System.out.println(item);

Aside from Java, how much other programming experience do you have? For a simple text-based game like Zork, a simple array of strings should do. You can use an ArrayList. When picking up an item, you can add that item to the list. You can then use indexOf to query if the player has an item, and then use remove to remove the item with that index:


// populate items
ArrayList<String> items = new ArrayList<String>();
items.add("room key");
items.add("lemonade");
items.add("clothes");

// display initial contents of inventory
System.out.println("Inventory:");
for ( String item : items )
	System.out.println(item);

// query and remove an item
int idx = items.indexOf("room key");
if ( idx != -1 )
	items.remove(idx);
else
	System.out.println("not found");


// display new contents of inventory
System.out.println("Inventory:");
for ( String item : items )
	System.out.println(item);

Ah, yeah, that'll do just fine. The game won't exactly be as 'straight forward' as Zork. The user would be able to open his inventory at any time, there would be item descriptions, the player would pick up items, etc. It's just that I'm slightly confused at the idea of ArrayList, and how I would go about actually ADDING items if the player picks it up - but I THINK I got it. Thanks for the help!

The beauty of Object Based programming is polymorphism. Which means that a class that inherits from a similar parent may have traces of the same function, just with different techniques.


That being said... you are making a text based game like Zorg. You're going to have a variety of items each with their own description, use case, functions, etc.

It'll help if you make an Item class with data fields like.


class Item {
private:
    string Name;
    string Description;
    int amount;
public:
void Use;
string ShowDescription;
}

This topic is closed to new replies.

Advertisement