File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Game Development and the fly likes Storing location in a text game Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Game Development
Bookmark "Storing location in a text game" Watch "Storing location in a text game" New topic

Storing location in a text game

Flo Powers
Ranch Hand

Joined: May 12, 2004
Posts: 57
I am learning Java (well, do you ever finish learning?), and decided to try and make a simple textbased game to set myself an interesting task. I have some idea of what text games are all about (played "The hobbit" a couple of decades ago, and have since tried MUDs for example, but don't know much about their internal design.

I intend to make a single-player game to begin with, and have made a GUI terminal with a textfield for entering commands and a display for the text to display. I can enter commands, and the world object uses multithreading to allow time to pass (one minute itrl = one hour in my game), and the weather to change. When events happen (e.g. the church bell rings every hour), this is described in the scrolling text.

However, I am unsure how best to organize room objects. I imagine having a room object that can contain item objects, non-playable character objects and even other rooms possibly. But how best to arrange this? Is it an idea to put room objects in a 2D (or even 3D) array, or is there a better way? Maybe I'm going about it the wrong way.

I imagine saving all rooms and items, and the stats for the player object to a file (autosave every five minutes), so the player can continue from the same point next time s/he starts the game. I also imagine having a login, so you can have different games underway. Finally, if you log in as admin, you can also edit rooms, add rooms, etc.

The next step I guess is to create the world as a separate program, and then let people connect - I'm thinking perhaps RMI might be a way to let me run the main program as a sort of "server" program and then let more than one player enter the game at once (on a LAN). Of course, this is not as good as having it run on an Internet server and using a proper MUD library, but hey - I'm only just starting out and like the challenge of making it from scratch...

Thanks for any hints!
Tad Dicks
Ranch Hand

Joined: Nov 16, 2004
Posts: 264
Why not just make a "World" object that holds all the "room/location" objects and then contains a "map" of what rooms are connected and how. Then each room can hold other game objects (npcs/items etc).
Jeremy Tartaglia
Ranch Hand

Joined: Mar 11, 2004
Posts: 62
I'm going to keep this simple, because the most common way for MUDs and other MU*s to do this is a little complicated to explain.

Probably the easiest way to code this would be to use an Interface (or an Abstract Class) called GameObject (or similar). Then to derive from that all the possible subclasses you would need (Room, Creature, Item, Weapon, Exit, etc.) trying to abstract whenever possible. For instance, GameObject -> Creature -> Character -> NPC or PC would be an intelligent design.

Every game object should be allowed to "contain" other objects. Thus, rooms could contain Players, Monsters, Items, and Exits. In that way, you incorporate tree/graph abilities to every GameObject, even for ones which couldn't/wouldn't use it. If you maintain a root GameObject, you could easily traverse through all known game objects in existance for things like searching, or making sure the map is complete (ie: no unenterable or unexitable rooms unless you wanted that).

That's just my opinion, but it's how most MU*s work on a smaller scale.
Flo Powers
Ranch Hand

Joined: May 12, 2004
Posts: 57
Well, potentially, this could grow to be a very large number of rooms - in the online games, we're talking thousands of rooms. Even if I only have say 50 rooms, it seems awkward to use individual room object references. As far as I can tell, there should be some data structure for storing them.

If I make it a very simple and structured world, each room would have max four exits (connections to other rooms), east, west, north and south. And presumably, if from room A we go east, then north and then west, we should be in the room next to where we started (one room north of A). I was wondering if I could use linked structures (LinkNodes with four LinkNode references east, west, north and south), but I could not see a simple way to automate that spatial logic (east, north and west = north). It seemed awkward to have to manually hardcode the links between room objects. An array using just counters would take care of that, but is that the way it should be done or is there a better way?
Ben Cramphorn

Joined: May 13, 2004
Posts: 17
Why not just populate an array of "Rooms" from a text file or something.

Example for each room would have a line in the text file something like...

1, "Some room description here", NO_EXIT, NO_EXIT, 3, 6, Sword, "A big sword", Gun, "A big gun"

Where the first number is the ID of the room, then some room description, next four would be where each of the exits leads to. Then you could have a list of objects and descriptions.

You'd just create an array or room objects and then populate it with the different lines in the text file. E.g. room with ID one would go into index one. And if going west took you to room 6 you'd just look up the room object in room_array [6]

You could alternatively have a 2d array with each item being a room and the index of the item would be it's location. E.g. If your in (3,2) and go north you know you would then be in (3,1).
I agree. Here's the link:
subject: Storing location in a text game
It's not a secret anymore!