hi people...a question all the way from new zealand. i have an assignment, of which i can choose, the only rule is that it must be a short text based adventure game. simple?. well my game is a jewel game. heres my problem. it has nine rooms (3x3) of which you start in a room and move through the rooms by the 'go' command. each movement is by way of north,south, east,west. ie go north. this movement will return the room, in my case the rooms are labelled as colours. this is ok for me to code. BUT, in each room is a coloured jewel, the same colours as the room colours, but a different coloured jewel in each room, the goal of my game(to win)is to put a specific jewel in a specific room by way of a 'pick up' and 'put down' method, using clues(im not sure yet), the problem?? how do i assign the coloured jewel to the room. (and i suppose i should ask also how can i move jewel, from room to room). below is my game class...(1 class from 6)
////////////////// as you can see here i have no clue to what i should do next. no idea as to what i should do to allocate a jewel to a room. please any help would be marvellous! thankyou
(edited by Cindy to format code using [ code] and [ /code] tags without the spaces) [ March 18, 2003: Message edited by: Cindy Glass ]
Instead of having every jewel know what room it's in, why not let the rooms keep track of what jewel is in them. You could do something like this:
Then, when you initialize your game, you'll want to create a jewel and then "give" that jewel to a room. You can always "ask" the room later to give it back so that you can do things with it. I have a feeling that an approach like this might be a bit easier for you. What do you think? Corey
awesome corey..thanks for the help..it did work... my problem now is how do i access a method in a class from inside another class. ie access the method jewelDescription in the Jewel class from the Room class (where i print out the jewel description as i move from room to room)
here ??? represents what i need to add to display the jewel description in that particular room.
posted 17 years ago
Originally posted by tom brownlee: awesome corey..thanks for the help..it did work... my problem now is how do i access a method in a class from inside another class.
Don't make the Room class invoke everything on the Jewel class for you. The Room class contains the Jewel class, it doesn't "wrap" it. Generally, when we talk about an object that "wraps" another, we think of a case in which the wrapping object entirely obscures the fact that it contains the object. In this case, however, it's no secret that a Room contains a Jewel. With the getContainedJewel() method I defined above, you already have a mechanism in the Room class in which to obtain the Jewel that it contains. Then, if your Jewel class has a method to retrieve it's description, you're all set, like this:
You see, you can actually retrieve the Jewel object from the Room and then invoke the methods of the Jewel class on that object - you don't have to make the Room class invoke the methods for you. Also, you can do that in one line by chaining method invocations, like this:
You see, the first method, getContainedJewel(), is resolved first, which returns a Jewel object. Then, we invoke the getDescription() method on that object and, nicely enough, we're left with the description of the Jewel that is contained within that room. I hope that helps, Corey
For debugging purposes, I like to have a string that prints out the "contents" of a class, similar to the longDescription() method. (I normally use the toString() method instead, but that's just my own personal preference). You can easily call methods on the jewel contained in a room. For example:
Remember that each Room object contains a Jewel object named containedJewel. You can use this reference to call any methods you wish. HTH Layne
This only works if you have access to the containedJewel variable from where this code is executing. In the example I posted, the above code would cause a compile-time error because from the main method in class Game, where you'd be getting the description of the Jewel, you don't have access to the containedJewel variable within a Room object because that variable is declared as private. Therefore, you must first "get" the object through the getContainedJewel() method. Of course, you could change the accessibility modifier on the containedJewel variable to protected or public, but it's often a bad idea to do so and almost certainly deviates from good OO practice. Corey