ICFP Contest 2006
Team: Abstraction Anonymous

Room.rb

Download

  1. #!/usr/bin/env ruby
  2. #### Incomplete room parser for the English mode of howie@umix:/home/howie/adventure
  3. #### written by Anthony Martinez during ICFP 2006 - Team Abstraction Anonymous
  4. #### License TBD
  5. #### Contact: pi % pihost ! us
  6.  
  7. ### Designed to be pasted/loaded into irb and manually abused. Autosolve functionality was planned
  8. ### but work never began on it.
  9. class Room
  10.  
  11. attr_reader :name, :desc, :items, :exits
  12. def initialize
  13. @name = ""
  14. @desc = ""
  15. @items = []
  16. @exits = {}
  17. @seenit = Hash.new(0)
  18. end
  19.  
  20. def seen(it)
  21. if @seenit[it] > 0 then return true
  22. else @seenit[it] += 1 ; return false
  23. end
  24. end
  25.  
  26. def to_s
  27. ev = ""
  28. ev << "Room: #{@name}\n"
  29. ev << "Items: " + @items.join(", ") + "\n"
  30. ev << "Exits: " + @exits.keys.join(", ")
  31. ev << "There are "
  32. @seenit.each do |k, v|
  33. next if v == 1
  34. ev << "#{v} #{k}'s, "
  35. end
  36. ev
  37. end
  38.  
  39. def parse(str)
  40. @desc = str
  41. arr = @desc.split.join(" ").split(".")
  42. @parsed = arr
  43. arr.each { |t| t.chomp! }
  44. arr.delete_if { |t| t.nil? || t.empty? }
  45.  
  46. @name = arr.shift
  47. last_item = arr.pop
  48. warn "The last item is #{last_item}"
  49. arr.each do |ln|
  50. case ln
  51. when /(?:leads|you can go) (.*)/
  52. @exits = $1.split(/, ?/)
  53. @exits.each {|e| e.gsub!(" ?or ?", "")}
  54. when /There is an? (.*) here/
  55. warn "Got what seems to be a new room description in the middle of other items" unless @items.empty?
  56. warn "There is a: I got a #{$1}"
  57. @items << Item.new($1) unless seen($1)
  58. when /Underneath the ([^,]+)/i
  59. warn "Underneath: I got a #{$1}"
  60. @items << Item.new($1) unless seen($1)
  61. end
  62. end
  63.  
  64. last_item =~ /Underneath the [^,]+, there is a (?:\(broken\))? (.*)\Z/
  65. @items << Item.new($1) unless seen($1)
  66.  
  67. end
  68. end
  69.  
  70. class Item
  71. def initialize
  72. @name = ""
  73. @missing = []
  74. @takes = []
  75. @description = ""
  76. @attribute = ""
  77. end
  78.  
  79. def broken?
  80. @missing.empty?
  81. end
  82.  
  83. def initialize(name,attribute="")
  84. @name = name
  85. @attribute = attribute
  86. end
  87. end
  88.  
  89.  

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