新手问题 谁能帮忙分析这段代码问题在哪?

wangyzyoga · 2013年08月20日 · 最后由 wangyzyoga 回复于 2013年08月21日 · 2019 次阅读

按照《Ruby 从入门到精通》敲了一段代码,执行提示:

核对代码,没有敲错,谁能帮忙指出问题在哪,如何解决?具体代码如下:

class Dungeon
  attr_accessor :player
  def initialize(player_name)
    @player = Player.new(player_name)
    @rooms = [ ]
  end
    def add_room(reference,name,description,connections)
      @rooms << 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\nYou are in "+@description
    end
  end
end
my_dungeon = Dungeon.new("Fred Bloggs")
my_dungeon.add_room(:largecave,"Large Cave","a large cavernous cave",{ :west =>:smallcave })
my_dungeon.add_room(:smallcave,"Small Cave","a small,claustrophobic cave",{ :east =>:largecave })
my_dungeon.start(:smallcave)
my_dungeon.go(:west)

请把代码格式修整一下。编辑器右上角应该有个插入代码 的功能

#1 楼 @ichord 刚才失手发出,已修改

@wangyzyoga 你从 :smallcave 房间开始,向 :west 走。但是 :smallcave 只有 :east 一个通向 :largecave 的门啊!!! 你向 :west 干嘛?!

#3 楼 @ichord 把 west 改为 east,执行成功,谢谢!

需要 登录 后方可回复, 如果你还没有账号请 注册新账号