13 Replies - 4635 Views - Last Post: 15 April 2012 - 12:46 PM Rate Topic: -----

#1 labrower17  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 29-March 12

How to create items in "zork"-like game? (text based game)

Posted 12 April 2012 - 04:05 PM

Hey everyone,
I am writing a text-based video game for my highschool pre-AP Computer class and i need help with the items. I am thinking about using a multidimentional array to assign the items to certain rooms. So far i have met with minimal success as i am not super familiar with arrays. How would you go about creating usable items in a text-based game?

Thanks,
labrower17
Is This A Good Question/Topic? 0
  • +

Replies To: How to create items in "zork"-like game? (text based game)

#2 oha055  Icon User is offline

  • D.I.C Head

Reputation: 46
  • View blog
  • Posts: 249
  • Joined: 02-February 09

Re: How to create items in "zork"-like game? (text based game)

Posted 12 April 2012 - 04:26 PM

Item item1 = new Item("Sword", 10);
/*
 * ...
 */

Item[][] items = {{item1,item2,item3},{item4,item5,item6},{item7,item8,item9}};



And you will ofcourse need to create an Item class:

public class Item {

	private String name;
	private double weight;

	public Item(String n, double w) {
		this.name = n;
		this.weight = w;
	}
}


This post has been edited by oha055: 12 April 2012 - 04:28 PM

Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: How to create items in "zork"-like game? (text based game)

Posted 12 April 2012 - 09:20 PM

Item[][] items = {{item1,item2,item3},{item4,item5,item6},{item7,item8,item9}};


or all of them directly
Item[] items = {
                 {new Item("Sword", 10), new Item("Knife", 7}, new Item("Gun", 30},
                 {new Item("Excalibur", 100), new Item("CorkScrew", 7}, new Item("xxx", 10},
                 {new Item("yyyy", 10), new Item("Bullit", 7}, new Item("xxx", 10}
               };


This post has been edited by pbl: 12 April 2012 - 09:21 PM

Was This Post Helpful? 0
  • +
  • -

#4 labrower17  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 29-March 12

Re: How to create items in "zork"-like game? (text based game)

Posted 13 April 2012 - 08:53 AM

Thanks guys that really helped. Now how can i create a chest to put an item in and assign that chest to a room?
Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7293
  • View blog
  • Posts: 12,103
  • Joined: 19-March 11

Re: How to create items in "zork"-like game? (text based game)

Posted 13 April 2012 - 08:59 AM

I don't think multidimensional array is your best bet for this. What happens, for example if you're in a room that starts off with three items, and you pick up one item... and then drop two?

Arrays are nice for some things, but you probably want something more flexible and objecty for this.

I'd suggest you create a Room class with an ArrayList of Items.


EDIT: Speaking of a chest, this allows you to extend Item to create a Container class. A Container is an Item that can contain other Items. There's your Chest, and also a Knapsack and maybe a Lunchbox (which is where you find the note from your mother which has the clue to some puzzle) and so forth.
The Container can even serve as the basis your character's inventory, if you like.

This post has been edited by jon.kiparsky: 13 April 2012 - 09:02 AM

Was This Post Helpful? 1
  • +
  • -

#6 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2013
  • View blog
  • Posts: 3,037
  • Joined: 21-June 11

Re: How to create items in "zork"-like game? (text based game)

Posted 13 April 2012 - 09:17 AM

View Postjon.kiparsky, on 13 April 2012 - 05:59 PM, said:

I'd suggest you create a Room class with an ArrayList of Items.


Yes, I second that. And instead of having an array (or ArrayList) containing all the rooms, I'd have each room have a list of its neighbors. So instead of having an array of rooms, you'd just have a reference to the starting room and the other rooms would be accessed from there. So basically you'd have a room graph.
Was This Post Helpful? 1
  • +
  • -

#7 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7293
  • View blog
  • Posts: 12,103
  • Joined: 19-March 11

Re: How to create items in "zork"-like game? (text based game)

Posted 13 April 2012 - 09:25 AM

Yes. Except I'd have a map instead of a list, with directions as the keys to the map (however you choose to represent directions). So you can do

public Room getNeighbor(direction) 
{
  return neighboringRooms.get(direction);
}



So moving is now basically

current=current.getNeighbor(direction); 


But I'm leaving out the case of no room in that direction, because you've got some decisions to make there.
Was This Post Helpful? 0
  • +
  • -

#8 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 797
  • View blog
  • Posts: 2,422
  • Joined: 29-July 11

Re: How to create items in "zork"-like game? (text based game)

Posted 13 April 2012 - 10:00 AM

This is a good read.
Was This Post Helpful? 0
  • +
  • -

#9 mladnier  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 13-April 12

Re: How to create items in "zork"-like game? (text based game)

Posted 13 April 2012 - 10:09 AM

View Postoha055, on 12 April 2012 - 04:26 PM, said:

Item item1 = new Item("Sword", 10);
/*
 * ...
 */

Item[][] items = {{item1,item2,item3},{item4,item5,item6},{item7,item8,item9}};



And you will ofcourse need to create an Item class:

public class Item {

	private String name;
	private double weight;

	public Item(String n, double w) {
		this.name = n;
		this.weight = w;
	}
}



Where does the first section of code go under? Like which class would it be in?
Was This Post Helpful? 0
  • +
  • -

#10 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7293
  • View blog
  • Posts: 12,103
  • Joined: 19-March 11

Re: How to create items in "zork"-like game? (text based game)

Posted 13 April 2012 - 10:37 AM

That would depend on how you'd set up your code. It's just an example - it sets up a structure. Not a great structure, I think, but the one that the OP asked for.

If you're using a naive MVC, it would be in the Model portion.
Was This Post Helpful? 0
  • +
  • -

#11 labrower17  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 29-March 12

Re: How to create items in "zork"-like game? (text based game)

Posted 13 April 2012 - 12:28 PM

I have the Rooms class already and i have the rooms mapped out. Here is a diagram i have made of the rooms. Also i am interested in the Container Class. How would you go about creating one?

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#12 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7293
  • View blog
  • Posts: 12,103
  • Joined: 19-March 11

Re: How to create items in "zork"-like game? (text based game)

Posted 13 April 2012 - 03:26 PM

Well, it's just like an Item, except it has an ArrayList of Items in it, and you can take items out of it and put them in again.
You can put all sorts of restrictions on it if you want - this scabbard only takes a Sword, that chest can fit anything you put into it, that knapsack will break if you put too much weight in it, and so forth. It's really up to you on that one.
Was This Post Helpful? 1
  • +
  • -

#13 labrower17  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 29-March 12

Re: How to create items in "zork"-like game? (text based game)

Posted 15 April 2012 - 12:39 PM

Ok now that i understand what it can be used for how do i make one? i have never used a container class. do u have an example you could show me?
Was This Post Helpful? 0
  • +
  • -

#14 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7293
  • View blog
  • Posts: 12,103
  • Joined: 19-March 11

Re: How to create items in "zork"-like game? (text based game)

Posted 15 April 2012 - 12:46 PM

No, really - just extend an Item (so it has all of the functionality of Item) and add an ArrayList<Item> and some methods to allow you to take things out of it and put things in it.

How you do that depends on what the rest of your code looks like, but you might have a remove(Item i) method which would return the Item requested or null if it's not in the container, and a put(Item i) which would add the Item to the Container. For specialized items you can put restrictions on what can be added (it might be good to think about how to report failure of this method: for example, if you try to do scabbard.put(side_of_beef) you'd probably like it to not work: your side of beef remains in your inventory and your scabbard doesn't have a hunk of a cow in it.

I'd probably do that by returning a boolean to indicate success. Failure to retrieve an Item can be signalled by returning null.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1