2

I am stuck on the part in chapter 6. It is a dungeon text-adventure game with rooms and a player that moves around. I keep getting a noMethod error for the .detect method used in the find_room_in_dungeon method. I assume that I am probably missing something but I just can't figure out what. I would really appreciate if someone could help me out. thanks.

class Dungeon
  attr_accessor :player

  def initialize(player_name)
    @player=Player.new(player_name)
    @room  = []
  end

  def add_room(reference, name, description, connections)
    @room << Room.new(reference, name, description, connections)
  end

  def start(location)
    @player.location=location
    show_current_description
  end

  def show_current_description
    puts find_room_in_dungeon(@player.location).full_description
  end

  ###
  def find_room_in_dungeon(reference)
    @rooms.detect { |room| room.reference == reference }
  end
  ###

  def find_room_in_direction(direction)
    find_room_in_dungeon(@player.location).connections[direction]
  end

  def go(direction)
    puts "you go " + direction.to_s
    @player.location= find_room_in_direction(direction)
    show_current_description
  end

  class Player
    attr_accessor :name, :location

    def initialize(name)
      @name = name
    end

  end

  class Room
    attr_accessor :reference, :name, :description, :connections

    def initialize(reference, name, description, connections)
      @reference  =reference
      @name       =name
      @description=description
      @connections=connections
    end

    def full_description
      @name + "/n/n you are in" + @description
    end

  end
end
my_dungeon= Dungeon.new("adventurer")
my_dungeon.add_room(:largecave, "large cave", "a very large cave", {:west => :smallcave})
my_dungeon.add_room(:smallcave, "smallcave", "a small cave", {:east => :largecave})
my_dungeon.start(:largecave)
Bob Gilmore
  • 12,608
  • 13
  • 46
  • 53
Spencer Cooley
  • 8,471
  • 16
  • 48
  • 63
  • 1
    Try using `ruby -wW2` to turn on the highest level of warnings for your script. The interpreter would have told you "warning: instance variable @rooms not initialized" which would have helped you zoom in on the problem. – the Tin Man Feb 27 '11 at 03:37
  • You may also want to use some of the techniques listed in [How do I debug ruby scripts?](http://stackoverflow.com/questions/3955688/how-do-i-debug-ruby-scripts) – Andrew Grimm Feb 27 '11 at 22:37

1 Answers1

3

You called the variable @room when you assigned it and when you access it in add_room, but @rooms when you try to access it in find_room_in_dungeon. If you rename it to be the same in all three cases, it will work.

I would suggest naming the variable @rooms everywhere rather than @room as it is an array of (possibly) multiple rooms, not a single room.

Dylan Markow
  • 123,080
  • 26
  • 284
  • 201
sepp2k
  • 363,768
  • 54
  • 674
  • 675
  • thanks a lot. I think it was a typo in the book or something, because it says @rooms.detect. I thought that it looked kind of weird like that. – Spencer Cooley Feb 27 '11 at 02:51
  • 5
    Hi Spencer, I'm Peter Cooper, the author of Beginning Ruby. Thanks for reading my book! I just checked up and the code in the book (page 155) uses @rooms throughout so I don't think it's a typo. However, if you could tell me specifically what edition you have and where you copied the code from, I can double check this. Thanks! – Peter Cooper Feb 27 '11 at 05:32
  • Thanks Peter. I looked over it again and realize I was mistaken. No typo (except for mine haha). I have been working through the book on my little netbook. That little screen is messing with my eyes. – – Spencer Cooley Feb 27 '11 at 07:01