From: "Jesús Gabriel y Galán" Date: 2012-09-25T05:05:19+09:00 Subject: Re: still learning by doing - connecting rooms in a game On Mon, Sep 24, 2012 at 8:46 PM, Sebastjan H. wrote: > Thank you all for recommending the reading and all the suggestions and > advices. According to those I'll try to re-factor the game like so: > > 1. Have one class for Room and then have subclasses for specific rooms. > 2. Make a map of the rooms in one of the forms suggested above. > > I still have difficulties imagining hot to resolve the interaction > between certain objects. > > For example, I wanted to have the kitchen and the dinning room play out > like so: > > a) room description > b) have the player decide whether or not they take some stuff > c) second description, prologue to fight > d) fight with a zombie > e) exit the room It looks that there is a common algorithm for handling all rooms, so this algorithm should be implemented only once, maybe in a parent class of all rooms. It can delegate to the actual implementation of a room the things related to data: does this room have items to pick?, the descriptions, the monster and the connections to other rooms. Then you run this algorithm from a specific room against the player that enters the room. An example: class Room def player_enter player puts first_description if has_items? #maybe you could check the return of items instead of having this method puts "This is what you can find in the room: #{items}" # perform the logic for the player to choose or not to pick end puts second_description if has_monster? fight player, monster end puts "Choose a door:" connections.each {|c| print the connection } .. end And then a specific room just implements the required methods: class DiningRoom < Room def first_description "a typical dining room" end def has_items? true end def items [:gun, :bullets] end def second_description "you hear a strange sound coming from the dark corner" end def has_monster? true end def monster Zombie.new :hp => 100, :power => 5 #or however you instantiate zombies end end BTW, this pattern is called Template Method: http://en.wikipedia.org/wiki/Template_method_pattern > If I have all this in the room.state() I can't reference the player > object since I get the undefined local variable or method for player. I think the method that runs the algorithm could receive the player as an argument. > So I assume I should put any decision making or fighting in the "runner" > file since the runner knows about all the objects and have the > room.state contain only the description? Well, the algorithm to go through a room can very well be in other object other than a parent of all rooms, but still the above applies: implement in each room the specifics of the room, putting the algorithm in a single place. > If there is another way I can reference these objects? If I initialize > an object like player in the room itself it is again limited to the room > in question, right? I don't think the Room is the right place to initialize the player object. The player object probably belongs to a Game object or such. What you can do is pass the appropiate objects where they need to be used. Jesus.