summaryrefslogtreecommitdiff
path: root/game.rb
blob: 0af3bcc559110af2c6d7dd9dae05d2df943780cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
exit 1 if ARGV.empty?

$thingfactories = {}
$roomfactories = {}
$rooms = {}

class ThingFactory
  def initialize(name, block)
    @name = name
    @properties = []
    @energy = 0
    self.instance_eval(&block)
  end

  def property(*strs)
    strs.each do |str|
      @properties << str
    end
  end

  def energy(num)
    @energy = num
  end

  def create
    return Thing.new(@name, @properties, @energy)
  end

  attr_reader :name
end

class Thing
  def initialize(name, properties, energy)
    @name = name
    @properties = properties
    @energy = energy
  end

  def has_property(str)
    return @properties.include?(str)
  end

  def method_missing(name)
    if name.to_s =~ /^is_(.*)\?$/ then
      has_property($1)
    end
  end

  attr_reader :name
  attr_accessor :energy
end

class RoomFactory
  def initialize(block)
    @description = ""
    @properties = []
    @inventory = []
    @north = nil
    @south = nil
    @east = nil
    @west = nil
    self.instance_eval(&block)
  end

  def description(description)
    @description = description
  end

  def property(str)
    @properties << str
  end

  def inventory(*strs)
    strs.each do |str|
      @inventory << str
    end
  end

  def north(str)
    @north = str
  end

  def south(str)
    @south = str
  end

  def east(str)
    @east = str
  end

  def west(str)
    @west = str
  end

  def create
    return Room.new(@description, @properties, @inventory, @north, @south, @east, @west)
  end
end

class Room
  def initialize(description, properties, inventory, north, south, east, west)
    @description = description
    @properties = properties
    @inventory = inventory
    @north = north
    @south = south
    @east = east
    @west = west
  end

  def reify!(roomhash)
    t = []
    @inventory.each do |str|
      t << $thingfactories[str].create
    end
    @inventory = t

    @north = roomhash[@north] unless @north.nil?
    @south = roomhash[@south] unless @south.nil?
    @east  = roomhash[@east ] unless @east.nil?
    @west  = roomhash[@west ] unless @west.nil?
  end

  def take(thing)
    matches = @inventory.select do |thg|
      thg.name == thing
    end

    return @inventory.delete(matches[0])
  end

  def put(thing)
    @inventory << thing
  end

  def has_property(str)
    return @properties.include?(str)
  end

  def method_missing(name)
    if name.to_s =~ /^is_(.*)\?$/ then
      has_property($1)
    end
  end

  attr_reader :description, :inventory, :north, :south, :east, :west
end



def room(name, &block)
  factory = RoomFactory.new(block)
  $roomfactories[name] = factory
end

def thing(name, &block)
  factory = ThingFactory.new(name, block)
  $thingfactories[name] = factory
end



def createRooms
  $roomfactories.each do |name, factory|
    $rooms[name] = factory.create
  end
  $rooms.each do |_, room|
    room.reify!($rooms)
  end
end

load ARGV.shift.strip
createRooms



puts "ERROR 1" unless $rooms["start"].east == $rooms["start"]
puts "ERROR 2" unless $rooms["another"].is_dark?
puts "ERROR 3" unless $rooms["another"].take("lamp").nil?
puts "ERROR 4" if $rooms["another"].take("apple").nil?