• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Objects inside rooms

 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Eric Daly
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 409
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Eric Daly
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic