/
cave.rb
53 lines (41 loc) · 1.08 KB
/
cave.rb
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
require "json"
module Wumpus
class Cave
def self.dodecahedron
from_json("#{Wumpus::DATA_DIR}/dodecahedron.json")
end
def self.from_json(filename)
new(JSON.parse(File.read(filename)))
end
def initialize(edges)
# FIXME: Can probably be changed to use an arbitrary number of rooms.
# The only reason it's not set up that way now is because of the
# brittleness of test/acceptance.rb
@rooms = (1..20).map.with_object({}) { |i, h| h[i] = Room.new(i) }
edges.each { |a,b| @rooms[a].connect(@rooms[b]) }
end
def add_hazard(thing, count)
count.times do
room = random_room
redo if room.has?(thing)
room.add(thing)
end
end
def random_room
@rooms.values.sample
end
def move(thing, from: raise, to: raise)
from.remove(thing)
to.add(thing)
end
def room_with(thing)
@rooms.values.find { |e| e.has?(thing) }
end
def entrance
@entrance ||= @rooms.values.find(&:safe?)
end
def room(number)
@rooms[number]
end
end
end