13 Replies - 2407 Views - Last Post: 15 April 2012 - 12:46 PM
#1
How to create items in "zork"-like game? (text based game)
Posted 12 April 2012 - 04:05 PM
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
Replies To: How to create items in "zork"-like game? (text based game)
#2
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
#3
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
#4
Re: How to create items in "zork"-like game? (text based game)
Posted 13 April 2012 - 08:53 AM
#5
Re: How to create items in "zork"-like game? (text based game)
Posted 13 April 2012 - 08:59 AM
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
#6
Re: How to create items in "zork"-like game? (text based game)
Posted 13 April 2012 - 09:17 AM
jon.kiparsky, on 13 April 2012 - 05:59 PM, said:
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.
#7
Re: How to create items in "zork"-like game? (text based game)
Posted 13 April 2012 - 09:25 AM
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.
#9
Re: How to create items in "zork"-like game? (text based game)
Posted 13 April 2012 - 10:09 AM
oha055, 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?
#10
Re: How to create items in "zork"-like game? (text based game)
Posted 13 April 2012 - 10:37 AM
If you're using a naive MVC, it would be in the Model portion.
#11
Re: How to create items in "zork"-like game? (text based game)
Posted 13 April 2012 - 12:28 PM
Attached image(s)
#12
Re: How to create items in "zork"-like game? (text based game)
Posted 13 April 2012 - 03:26 PM
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.
#13
Re: How to create items in "zork"-like game? (text based game)
Posted 15 April 2012 - 12:39 PM
#14
Re: How to create items in "zork"-like game? (text based game)
Posted 15 April 2012 - 12:46 PM
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.
|
|

New Topic/Question
Reply



MultiQuote





|