Room.rb
Download
#!/usr/bin/env ruby
#### Incomplete room parser for the English mode of howie@umix:/home/howie/adventure
#### written by Anthony Martinez during ICFP 2006 - Team Abstraction Anonymous
#### License TBD
#### Contact: pi % pihost ! us
### Designed to be pasted/loaded into irb and manually abused. Autosolve functionality was planned
### but work never began on it.
class Room
attr_reader :name, :desc, :items, :exits
def initialize
@name = ""
@desc = ""
@items = []
@exits = {}
@seenit = Hash.new(0)
end
def seen(it)
if @seenit[it] > 0 then return true
else @seenit[it] += 1 ; return false
end
end
def to_s
ev = ""
ev << "Room: #{@name}\n"
ev << "Items: " + @items.join(", ") + "\n"
ev << "Exits: " + @exits.keys.join(", ")
ev << "There are "
@seenit.each do |k, v|
next if v == 1
ev << "#{v} #{k}'s, "
end
ev
end
def parse(str)
@desc = str
arr = @desc.split.join(" ").split(".")
@parsed = arr
arr.each { |t| t.chomp! }
arr.delete_if { |t| t.nil? || t.empty? }
@name = arr.shift
last_item = arr.pop
warn "The last item is #{last_item}"
arr.each do |ln|
case ln
when /(?:leads|you can go) (.*)/
@exits = $1.split(/, ?/)
@exits.each {|e| e.gsub!(" ?or ?", "")}
when /There is an? (.*) here/
warn "Got what seems to be a new room description in the middle of other items" unless @items.empty?
warn "There is a: I got a #{$1}"
@items << Item.new($1) unless seen($1)
when /Underneath the ([^,]+)/i
warn "Underneath: I got a #{$1}"
@items << Item.new($1) unless seen($1)
end
end
last_item =~ /Underneath the [^,]+, there is a (?:\(broken\))? (.*)\Z/
@items << Item.new($1) unless seen($1)
end
end
class Item
def initialize
@name = ""
@missing = []
@takes = []
@description = ""
@attribute = ""
end
def broken?
@missing.empty?
end
def initialize(name,attribute="")
@name = name
@attribute = attribute
end
end
Hell is other programming languages. -- Sartran
Hell is that programming language! -- Dan
Ordinarily, one would enrich this language with more powerful means of
computation. Instead I take a different tack... -- Harmonious Monk