Skip to content

Instantly share code, notes, and snippets.

@glinesbdev
Last active August 29, 2015 14:04
Show Gist options
  • Save glinesbdev/ae6fc8b0d689a967a1d4 to your computer and use it in GitHub Desktop.
Save glinesbdev/ae6fc8b0d689a967a1d4 to your computer and use it in GitHub Desktop.
Dungeon game
# (*) = methods I added
class Dungeon
attr_accessor :player
def initialize(player_name)
@player = Player.new(player_name)
@rooms = []
end
def add_room(reference, name, description, connections, *args)
@rooms << Room.new(reference, name, description, connections, args)
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
has_item?
end
# (*)
def check_room(reference)
@rooms.include?(reference)
end
# (*)
def has_item?
@rooms.include?(:item)
end
class Player
attr_accessor :name, :location, :inventory
def initialize(name)
@name = name
@inventory = []
end
end
class Room
attr_accessor :reference, :name, :description, :connections, :item
def initialize(reference, name, description, connections, *args)
@reference = reference
@name = name
@description = description
@connections = connections
end
def full_description
@name + "\n\nYou are in " + @description
end
end
end
d = Dungeon.new("Bradyn Glines")
d.add_room(:largecave, "Large Cave", "a large cavernous cave", {:west => :smallcave})
d.add_room(:smallcave, "Small Cave", "a small, claustrophobic cave", {:east => :largecave}, {item: :sword})
d.start(:largecave)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment