Class: DungeonWalker
- Inherits:
-
Object
- Object
- DungeonWalker
- Defined in:
- lib/dungeon/dungeon_walker.rb
Constant Summary collapse
- WALKING_POSITIONS =
[ [ -1, 0 ], [ 1, 0 ], [ 0, -1 ], [ 0, 1 ] ]
Instance Method Summary collapse
- #get_connected_rooms_positions(room_position) ⇒ Object
-
#initialize(rooms, dungeon_size, entry_room) ⇒ DungeonWalker
constructor
A new instance of DungeonWalker.
- #walk_rooms ⇒ Object
Constructor Details
#initialize(rooms, dungeon_size, entry_room) ⇒ DungeonWalker
Returns a new instance of DungeonWalker.
7 8 9 10 11 |
# File 'lib/dungeon/dungeon_walker.rb', line 7 def initialize( rooms, dungeon_size, entry_room ) @rooms = rooms @dungeon_size = dungeon_size @entry_room = entry_room end |
Instance Method Details
#get_connected_rooms_positions(room_position) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/dungeon/dungeon_walker.rb', line 34 def get_connected_rooms_positions( room_position ) connected_positions = [] WALKING_POSITIONS.each do |wp| top = room_position[0] + wp[0] left = room_position[1] + wp[1] if( room_position != [ top, left ] && top >= 1 && left >= 1 && top <= @dungeon_size && left <= @dungeon_size && @rooms.has_key?( [ top, left ] ) ) connected_positions << [ top, left ] end end # p connected_positions connected_positions end |
#walk_rooms ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/dungeon/dungeon_walker.rb', line 13 def walk_rooms() walking_room_queue = [ @entry_room.top_left_array ] walked_rooms_positions = Set.new until walking_room_queue.empty? current_room_position = walking_room_queue.shift walked_rooms_positions << current_room_position connected_rooms_positions = get_connected_rooms_positions( current_room_position ) connected_rooms_positions.each do |room_position| # p room_position next if walked_rooms_positions.include?( room_position ) walked_rooms_positions << room_position walking_room_queue << room_position # p walked_rooms_positions end end walked_rooms_positions end |