File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Game Development and the fly likes Objects inside rooms Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Game Development
Bookmark "Objects inside rooms" Watch "Objects inside rooms" New topic
Author

Objects inside rooms

Eric Daly
Ranch Hand

Joined: Jul 11, 2006
Posts: 143
Hi, I have been working at this problem all day now, I'm trying to write a somewhat simple text adventure "engine", although right now I'm hard coding everything just to get the concept down... I'm having trouble particularly with objects inside of rooms: I can't easily change the state of a given object without writing TONS of code. I'm looking for maybe some advice as to a better approach (object oriented) not code, just a more clear idea of how to approach the design of the problem. Here's my setup:

I have a class for the game, which I'm calling Gnome Mansion. Then there's a class for Room, which is not used, only extended. Also there's a class for RoomObject, also never used, only extended. For each room I create a new class and fill in the description and list of objects. Example:

Room by the way holds an array of RoomObjects, and there is a preset number.

So basically from my GnomeMansion class I make an array of Rooms, then populate each location with the actual rooms. I have a player class but don't seem to ever really need it... Then my parser calls an appropriate function based on the input. Here's the problem: If there's a lamp in the room, I can't just say room[x][y].lamp.turnOn(). However, that's the simplicity I'm looking to achieve. Currently I have to pretty much re-parse (if that's a word) some of the input. Let's say I want to turn the lamp on, I say "turn on the lamp" and the parser grabs tokens, finds "lamp" and "on" so it then attempts to turn the lamp on. Now I know this is a horrible way to do this, it seems, but I am calling a performActionOn() method that takes an object and a string that says what kind of action to perform.

As you can see, I'm obviously not making good use of polymorphism because I'm writing all this treacherous code just to perform small, simple functions. And imagine if my lamp had one more property... More and more nested if statements, that's what.
Does anyone have any advice? I really appreciate any time spent looking at this. And sorry for the long post, I am just trying to be as clear and concise as possible... it's just turning into somewhat of a large project (at least for me).


Studying for SCJP 6
Suman Sharma
Ranch Hand

Joined: May 16, 2005
Posts: 72
Could you please write the purpose of this whole project briefly, so we know what it is all about and it can be solved?
Thanks.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Originally posted by Suman Sharma:
Could you please write the purpose of this whole project briefly, so we know what it is all about and it can be solved?
Thanks.


I don't think that is necessary. Eric provided a great deal of information for someone to help him solve the problem.


GenRocket - Experts at Building Test Data
Eric Daly
Ranch Hand

Joined: Jul 11, 2006
Posts: 143
What else is there to explain? Read about text adventure games: READ
Or study the Java language on the Java tutorial. Google it.
[ April 14, 2007: Message edited by: Eric Daly ]
Suman Sharma
Ranch Hand

Joined: May 16, 2005
Posts: 72
Actually I am new in this area. I did get some Sun certificates. I learnt about Java Design Patterns. I think if you try Command Pattern for your example, the code might be simpler. Here is the website for this pattern:

http://www.javaworld.com/javatips/jw-javatip68_p.html

-Suman
Eric Daly
Ranch Hand

Joined: Jul 11, 2006
Posts: 143
Hey, I haven't had much time to test out that command pattern design, but it looks like that could really help me out with the problem I'm having. Thank you for mentioning it.
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
Hiya,
I have been thinking about text adventures of late.
What I was thinking was that mostly an object only has one function, for example a lamp is either on or off, a chest is opened or closed etc.

So perhaps if you added a command function to your highlevel "object", say an activate and deactivate, you could just iterate though a list of objects doing activate/deactivate.

If you also gave each object an ID you could say activate object X without having to know what type it is.

G
[ April 16, 2007: Message edited by: Gavin Tranter ]
Roger F. Gay
Ranch Hand

Joined: Feb 16, 2007
Posts: 349
Originally posted by Eric Daly:
I can't just say room[x][y].lamp.turnOn(). However, that's the simplicity I'm looking to achieve.


It seems to me that naming your instance of lamp1, object[0], kind-of runs against the grain of what you want to do. I can see reason to have an array of objects for the room; but ... it's just a thought really.

Following the train of thought in this thread, I think I can agree that it might take a little more thought about what you're going to do with objects and what the circumstances are when you do them. When would you not have a handle on a lamp when you're turning it on or off? Power failure? I guess then the effect of the lamp could simply be made not available.


Correlation does not prove causality.
Eric Daly
Ranch Hand

Joined: Jul 11, 2006
Posts: 143
@Roger F. Gay: I don't mean to offend you, but I don't understand a word you said...
I tried out the command pattern (I think), but it doesn't seem to help simplify things much. It's more of a slight change it seems.
I guess I need general help thinking through the design of this program rather than a specific area of my code. Or maybe I haven't come to realize that this is a much bigger project than I imagined it would be. I mean, code has to be written to take into account every little thing that happens, and the more interactive the environment is, the more code that has to be written. Although, if anybody is interested to see what I have so far, I'll post it soon (just as soon as I get back into my Windows environment). It is functional, for the most part, but either I'm gonna have to find a way to simplify my code so that it's easier to add rooms and objects, or this is going to be one insanely short game.
Mark Hedge
Greenhorn

Joined: May 06, 2007
Posts: 6
I do a lot of code on MUSHes and MUDs, which are obviously text based games, they are essentially object oriented databases. I imagine an explanation of the way they work might help you?

Every object on a MUSH is given a database number by the server, which looks something like #20, or #52, or #1005.

Every object on a MUSH is one of four types - THING, ROOM, PLAYER, or EXIT.

Every object bar a room has a location (an attribute).

Every object bar a room as a 'home'. this is usually ignored on a MUSH for things and players, but for exits the home is the far side of the exit.

So there you go, you have your rooms and your things in the rooms. There are a few functions provided like lexits() (return the numbers of all exits in a given room), lcon() (return the numbers of all things or players in a given room). With those two you can shape what the player sees in the room, so a description of the room itself, followed by the names of all players and things, followed by the names of all the exits.

Its quite a nice system because rooms/exits/players/things are all fundamentally exactly the same thing. Its possible in a MUSH to be inside a thing (for example, a car, or a backpack) as well as a room, for example.

...as MUSHes are object oriented every object can be parented to another object, and you can store commands/attributes on them, but I guess thats probably more than you need. In terms of the real basics I figure a system like that should work, right?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60780
    
  65

"Someone Else",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
JavaRanch Sheriff


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Objects inside rooms
 
Similar Threads
sample java code needed for UML class associations
[URLyBird] Should I use a business object or String[] in my Data class?
Adding JLabels to a vector
Object trouble.
Room Class