-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext_based_dungeon_game.rb
66 lines (52 loc) · 1.48 KB
/
text_based_dungeon_game.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
54
55
56
57
58
59
60
61
62
63
64
65
66
class Dungeon
attr_accessor :player
def initialize(player_name)
@player = Player.new(player_name)
@rooms = []
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
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
end
my_dungeon = Dungeon.new("Adam Berman")
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(:largecave)
my_dungeon.go(:west)
my_dungeon.go(:east)